Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Parallel ILX Codegen #11531

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d238d25
Enabling parallel parsing for compiling
TIHan Feb 23, 2021
9724f27
Using a delayed error logger per parsing file
TIHan Feb 23, 2021
53f234a
Added -parallel option
TIHan Feb 24, 2021
bf07e86
Fixing error logger
TIHan Feb 24, 2021
4b7c652
Moved parallel compiler option to be a test option
TIHan Feb 25, 2021
d2198df
Trying to get tests to pass
TIHan Feb 25, 2021
d4ad54c
Remove switch
TIHan Feb 25, 2021
23d81e3
Minor refactor
TIHan Feb 25, 2021
3f32f7d
More refactoring
TIHan Feb 25, 2021
03c8b8a
Add comment
TIHan Feb 25, 2021
51c897b
Initial work for parallel type checking
TIHan Feb 25, 2021
d3c674d
Minor refactor
TIHan Feb 26, 2021
20f285e
Add max
TIHan Feb 26, 2021
f40de5b
Merge branch 'parallel-parsing-2' into parallel-type-checking
TIHan Feb 26, 2021
6b06c3a
Some cleanup
TIHan Feb 26, 2021
c6c54b9
do not use SkipImpl
TIHan Feb 26, 2021
0f39ad4
minor refactor
TIHan Feb 26, 2021
fa5d394
Merged main
TIHan Feb 26, 2021
1236cbf
Merge branch 'parallel-parsing-2' into parallel-type-checking
TIHan Feb 26, 2021
5c5a466
Handling aggregate exceptions from ArrayParallel. Using try/finally t…
TIHan Mar 2, 2021
20e5263
Merge branch 'parallel-parsing-2' into parallel-type-checking
TIHan Mar 2, 2021
e5fa18b
Merged main
TIHan Mar 3, 2021
f408f04
Initial ilx-code-gen parallel work
TIHan Mar 3, 2021
b36f45a
compiles
TIHan Mar 3, 2021
0db7045
Does not work but it tries to use parallelism
TIHan Mar 3, 2021
17f3d9b
Compiles again
TIHan Mar 3, 2021
f4a298c
parallel ilx gen works
TIHan Mar 3, 2021
8b419b3
Merging main
TIHan Mar 4, 2021
bdfcb4b
Merge branch 'parallel-type-checking' into parallel-ilx-codegen
TIHan Mar 4, 2021
c1e89ae
Refactoring a bit
TIHan Mar 5, 2021
311d436
Fix build
TIHan Mar 5, 2021
898f894
Merge branch 'parallel-type-checking' into parallel-ilx-codegen
TIHan Mar 5, 2021
e056a1a
Removing a few LOH allocs
TIHan Mar 5, 2021
d3a1c78
Merge remote-tracking branch 'remote/main' into parallel-ilx-codegen
TIHan May 6, 2021
4adf375
merging
TIHan May 10, 2021
db1b986
Merging main
TIHan Nov 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 44 additions & 21 deletions src/fsharp/IlxGen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module internal FSharp.Compiler.IlxGen
open System.IO
open System.Reflection
open System.Collections.Generic
open System.Collections.Immutable

open Internal.Utilities
open Internal.Utilities.Collections
Expand Down Expand Up @@ -966,6 +967,10 @@ and IlxGenEnv =

/// Are we inside of a recursive let binding, while loop, or a for loop?
isInLoop: bool

delayCodeGen: bool

delayedFileGen: ImmutableArray<(cenv -> unit) []>
}

override _.ToString() = "<IlxGenEnv>"
Expand Down Expand Up @@ -2321,7 +2326,7 @@ let rec GenExpr cenv cgbuf eenv sp (expr: Expr) sequel =

cenv.exprRecursionDepth <- cenv.exprRecursionDepth - 1

if cenv.exprRecursionDepth = 0 then
if cenv.exprRecursionDepth = 0 && not eenv.delayCodeGen then
ProcessDelayedGenMethods cenv

and ProcessDelayedGenMethods cenv =
Expand Down Expand Up @@ -2515,7 +2520,20 @@ and CodeGenMethodForExpr cenv mgbuf (spReq, entryPointInfo, methodName, eenv, al
CodeGenMethod cenv mgbuf (entryPointInfo, methodName, eenv, alreadyUsedArgs,
(fun cgbuf eenv -> GenExpr cenv cgbuf eenv spReq expr0 sequel0),
expr0.Range)
code
code

and DelayCodeGenMethodForExpr cenv mgbuf (spReq, entryPointInfo, methodName, eenv, alreadyUsedArgs, expr0, sequel0) =
let ilLazyCode =
lazy
CodeGenMethodForExpr { cenv with exprRecursionDepth = 0; delayedGenMethods = Queue() } mgbuf (spReq, entryPointInfo, methodName, { eenv with delayCodeGen = false }, alreadyUsedArgs, expr0, sequel0)

if cenv.exprRecursionDepth > 0 || eenv.delayCodeGen then
cenv.delayedGenMethods.Enqueue(fun _ -> ilLazyCode.Force() |> ignore)
else
// Eagerly codegen if we are not in an expression depth.
ilLazyCode.Force() |> ignore

ilLazyCode

//--------------------------------------------------------------------------
// Generate sequels
Expand Down Expand Up @@ -5668,8 +5686,14 @@ and GenBindingAfterDebugPoint cenv cgbuf eenv sp (TBind(vspec, rhsExpr, _)) star
cgbuf.mgbuf.AddOrMergePropertyDef(ilGetterMethSpec.MethodRef.DeclaringTypeRef, ilPropDef, m)

let ilMethodDef =
let ilCode = CodeGenMethodForExpr cenv cgbuf.mgbuf (SPSuppress, [], ilGetterMethSpec.Name, eenv, 0, rhsExpr, Return)
let ilMethodBody = MethodBody.IL(lazy ilCode)
let ilLazyCode =
if eenv.delayCodeGen then
DelayCodeGenMethodForExpr cenv cgbuf.mgbuf (SPSuppress, [], ilGetterMethSpec.Name, eenv, 0, rhsExpr, Return)
else
let ilCode = CodeGenMethodForExpr cenv cgbuf.mgbuf (SPSuppress, [], ilGetterMethSpec.Name, eenv, 0, rhsExpr, Return)
lazy ilCode

let ilMethodBody = MethodBody.IL(ilLazyCode)
(mkILStaticMethod ([], ilGetterMethSpec.Name, access, [], mkILReturn ilTy, ilMethodBody)).WithSpecialName
|> AddNonUserCompilerGeneratedAttribs g

Expand Down Expand Up @@ -6121,9 +6145,6 @@ and ComputeMethodImplAttribs cenv (_v: Val) attrs =
let hasAggressiveInliningImplFlag = (implflags &&& 0x0100) <> 0x0
hasPreserveSigImplFlag, hasSynchronizedImplFlag, hasNoInliningImplFlag, hasAggressiveInliningImplFlag, attrs

and DelayGenMethodForBinding cenv mgbuf eenv ilxMethInfoArgs =
cenv.delayedGenMethods.Enqueue (fun cenv -> GenMethodForBinding cenv mgbuf eenv ilxMethInfoArgs)

and GenMethodForBinding
cenv mgbuf eenv
(v: Val, mspec, hasWitnessEntry, generateWitnessArgs, access, ctps, mtps, witnessInfos, curriedArgInfos, paramInfos, argTys, retInfo, topValInfo,
Expand Down Expand Up @@ -6212,20 +6233,10 @@ and GenMethodForBinding
else
body

let ilCodeLazy = lazy CodeGenMethodForExpr cenv mgbuf (SPAlways, tailCallInfo, mspec.Name, eenvForMeth, 0, bodyExpr, sequel)
let ilLazyCode = DelayCodeGenMethodForExpr cenv mgbuf (SPAlways, tailCallInfo, mspec.Name, eenvForMeth, 0, bodyExpr, sequel)

// This is the main code generation for most methods
false, MethodBody.IL(ilCodeLazy), false

match ilMethodBody with
| MethodBody.IL(ilCodeLazy) ->
if cenv.exprRecursionDepth > 0 then
cenv.delayedGenMethods.Enqueue(fun _ -> ilCodeLazy.Force() |> ignore)
else
// Eagerly codegen if we are not in an expression depth.
ilCodeLazy.Force() |> ignore
| _ ->
()
false, MethodBody.IL(ilLazyCode), false

// Do not generate DllImport attributes into the code - they are implicit from the P/Invoke
let attrs =
Expand Down Expand Up @@ -7122,7 +7133,9 @@ and GenImplFile cenv (mgbuf: AssemblyBuilder) mainInfoOpt eenv (implFile: TypedI
let allocVal = ComputeAndAddStorageForLocalTopVal (cenv.amap, g, cenv.intraAssemblyInfo, cenv.opts.isInteractive, NoShadowLocal)
AddBindingsForLocalModuleType allocVal clocCcu eenv mexpr.Type

eenvafter
let eenvfinal = { eenvafter with delayedFileGen = eenvafter.delayedFileGen.Add(cenv.delayedGenMethods |> Array.ofSeq) }
cenv.delayedGenMethods.Clear()
eenvfinal

and GenForceWholeFileInitializationAsPartOfCCtor cenv (mgbuf: AssemblyBuilder) (lazyInitInfo: ResizeArray<_>) tref m =
// Authoring a .cctor with effects forces the cctor for the 'initialization' module by doing a dummy store & load of a field
Expand Down Expand Up @@ -8002,6 +8015,14 @@ let CodegenAssembly cenv eenv mgbuf implFiles =
let eenv = List.fold (GenImplFile cenv mgbuf None) eenv a
let eenv = GenImplFile cenv mgbuf cenv.opts.mainMethodInfo eenv b

let genMeths = eenv.delayedFileGen |> Array.ofSeq

genMeths
|> ArrayParallel.iter (fun genMeths ->
genMeths
|> Array.iter (fun gen -> gen cenv)
)

// Some constructs generate residue types and bindings. Generate these now. They don't result in any
// top-level initialization code.
let extraBindings = mgbuf.GrabExtraBindingsToGenerate()
Expand Down Expand Up @@ -8039,7 +8060,9 @@ let GetEmptyIlxGenEnv (g: TcGlobals) ccu =
innerVals = []
sigToImplRemapInfo = [] (* "module remap info" *)
withinSEH = false
isInLoop = false }
isInLoop = false
delayCodeGen = true
delayedFileGen = ImmutableArray.Empty }

type IlxGenResults =
{ ilTypeDefs: ILTypeDef list
Expand Down
50 changes: 45 additions & 5 deletions src/fsharp/ParseAndCheckInputs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ type TcState =
{ x with tcsTcSigEnv = tcEnvAtEndOfLastInput
tcsTcImplEnv = tcEnvAtEndOfLastInput }

member x.RemoveImpl qualifiedNameOfFile =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes made in ParseAndCheckInputs is from #11152 and needs to be removed in this PR.

{ x with tcsRootImpls = x.tcsRootImpls.Remove(qualifiedNameOfFile) }


/// Create the initial type checking state for compiling an assembly
let GetInitialTcState(m, ccuName, tcConfig: TcConfig, tcGlobals, tcImports: TcImports, niceNameGen, tcEnv0) =
Expand Down Expand Up @@ -876,19 +879,26 @@ let TypeCheckOneInputEventually (checkForErrors, tcConfig: TcConfig, tcImports:
return (tcState.TcEnvFromSignatures, EmptyTopAttrs, None, tcState.tcsCcuSig), tcState
}

/// Typecheck a single file (or interactive entry into F# Interactive)
let TypeCheckOneInput (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp =
let TypeCheckOneInputAux (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp skipImplIfSigExists =
// 'use' ensures that the warning handler is restored at the end
use unwindEL = PushErrorLoggerPhaseUntilUnwind(fun oldLogger -> GetErrorLoggerFilteringByScopedPragmas(false, GetScopedPragmasForInput inp, oldLogger) )
use unwindBP = PushThreadBuildPhaseUntilUnwind BuildPhase.TypeCheck

RequireCompilationThread ctok
TypeCheckOneInputEventually (checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt, TcResultsSink.NoSink, tcState, inp, false)
TypeCheckOneInputEventually (checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt, TcResultsSink.NoSink, tcState, inp, skipImplIfSigExists)
|> Eventually.force CancellationToken.None
|> function
| ValueOrCancelled.Value v -> v
| ValueOrCancelled.Cancelled ce -> raise ce // this condition is unexpected, since CancellationToken.None was passed

/// Typecheck a single file (or interactive entry into F# Interactive)
let TypeCheckOneInput (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp =
TypeCheckOneInputAux(ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp false

/// Typecheck a single file but skip it if the file is an impl and has a backing sig
let TypeCheckOneInputSkipImpl (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp =
TypeCheckOneInputAux(ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt) tcState inp true

/// Finish checking multiple files (or one interactive entry into F# Interactive)
let TypeCheckMultipleInputsFinish(results, tcState: TcState) =
let tcEnvsAtEndFile, topAttrs, implFiles, ccuSigsForFiles = List.unzip4 results
Expand Down Expand Up @@ -918,9 +928,39 @@ let TypeCheckClosedInputSetFinish (declaredImpls: TypedImplFile list, tcState) =

tcState, declaredImpls

let TypeCheckClosedInputSet (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt, tcState, inputs) =
let TypeCheckClosedInputSet (ctok, checkForErrors, tcConfig: TcConfig, tcImports, tcGlobals, prefixPathOpt, tcState, inputs) =
// tcEnvAtEndOfLastFile is the environment required by fsi.exe when incrementally adding definitions
let results, tcState = (tcState, inputs) ||> List.mapFold (TypeCheckOneInput (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt))
let results, tcState =
if tcConfig.concurrentBuild then
let results, tcState = (tcState, inputs) ||> List.mapFold (TypeCheckOneInputSkipImpl (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt))

let inputs = Array.ofList inputs
let newResults = Array.ofList results
let results = Array.ofList results

(inputs, results)
||> Array.zip
|> Array.mapi (fun i (input, (_, _, implOpt, _)) ->
match implOpt with
| None -> None
| Some impl ->
match impl with
| TypedImplFile.TImplFile(qualifiedNameOfFile=qualifiedNameOfFile;implementationExpressionWithSignature=ModuleOrNamespaceExprWithSig.ModuleOrNamespaceExprWithSig(contents=ModuleOrNamespaceExpr.TMDefs [])) ->
Some(i, input, qualifiedNameOfFile)
| _ ->
None
)
|> Array.choose id
|> ArrayParallel.iter (fun (i, input, qualifiedNameOfFile) ->
let tcState = tcState.RemoveImpl(qualifiedNameOfFile)
let result, _ = TypeCheckOneInput (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt) tcState input
newResults.[i] <- result
)

newResults |> List.ofArray, tcState
else
(tcState, inputs) ||> List.mapFold (TypeCheckOneInput (ctok, checkForErrors, tcConfig, tcImports, tcGlobals, prefixPathOpt))

let (tcEnvAtEndOfLastFile, topAttrs, implFiles, _), tcState = TypeCheckMultipleInputsFinish(results, tcState)
let tcState, declaredImpls = TypeCheckClosedInputSetFinish (implFiles, tcState)
tcState, topAttrs, declaredImpls, tcEnvAtEndOfLastFile
2 changes: 2 additions & 0 deletions src/fsharp/ParseAndCheckInputs.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type TcState =

member CreatesGeneratedProvidedTypes: bool

member RemoveImpl: QualifiedNameOfFile -> TcState

/// Get the initial type checking state for a set of inputs
val GetInitialTcState:
range * string * TcConfig * TcGlobals * TcImports * NiceNameGenerator * TcEnv -> TcState
Expand Down
13 changes: 10 additions & 3 deletions src/fsharp/absil/bytes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Internal.Utilities

open System
open System.IO
open System.Buffers
open System.IO.MemoryMappedFiles
open System.Runtime.InteropServices
open FSharp.NativeInterop
Expand Down Expand Up @@ -439,10 +440,16 @@ type internal ByteBuffer =
let oldBufSize = buf.bbArray.Length
if newSize > oldBufSize then
let old = buf.bbArray
buf.bbArray <- Bytes.zeroCreate (max newSize (oldBufSize * 2))
buf.bbArray <- ArrayPool.Shared.Rent(max newSize (oldBufSize * 2))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be in this PR.

Bytes.blit old 0 buf.bbArray 0 buf.bbCurrent
ArrayPool.Shared.Return(old)

member buf.Close () = Bytes.sub buf.bbArray 0 buf.bbCurrent
member buf.Close () =
let result = Bytes.sub buf.bbArray 0 buf.bbCurrent
ArrayPool.Shared.Return(buf.bbArray)
buf.bbArray <- [||]
buf.bbCurrent <- 0
result

member buf.EmitIntAsByte (i:int) =
let newSize = buf.bbCurrent + 1
Expand Down Expand Up @@ -506,7 +513,7 @@ type internal ByteBuffer =
member buf.Position = buf.bbCurrent

static member Create sz =
{ bbArray=Bytes.zeroCreate sz
{ bbArray = ArrayPool.Shared.Rent(sz)
bbCurrent = 0 }

[<Sealed>]
Expand Down
4 changes: 4 additions & 0 deletions src/fsharp/lib.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ type DisposablesTracker =
[<RequireQualifiedAccess>]
module ArrayParallel =

val inline iter : ('T -> unit) -> 'T [] -> unit

val inline iteri : (int -> 'T -> unit) -> 'T [] -> unit

val inline map : ('T -> 'U) -> 'T [] -> 'U []

val inline mapi : (int -> 'T -> 'U) -> 'T [] -> 'U []