-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathOptimizeInputs.fs
More file actions
612 lines (512 loc) · 22.2 KB
/
OptimizeInputs.fs
File metadata and controls
612 lines (512 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
module internal FSharp.Compiler.OptimizeInputs
open System.Collections.Generic
open System.IO
open System.Threading.Tasks
open Internal.Utilities.Library
open FSharp.Compiler
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.Diagnostics
open FSharp.Compiler.CheckDeclarations
open FSharp.Compiler.CompilerConfig
open FSharp.Compiler.CompilerImports
open FSharp.Compiler.CompilerOptions
open FSharp.Compiler.IlxGen
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.Text
open FSharp.Compiler.IO
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeOps
let mutable showTermFileCount = 0
let PrintWholeAssemblyImplementation (tcConfig: TcConfig) outfile header expr =
if tcConfig.showTerms then
if tcConfig.writeTermsToFiles then
let fileName = outfile + ".terms"
use f =
FileSystem.OpenFileForWriteShim(fileName + "-" + string showTermFileCount + "-" + header, FileMode.Create).GetWriter()
showTermFileCount <- showTermFileCount + 1
LayoutRender.outL f (Display.squashTo 192 (DebugPrint.implFilesL expr))
else
dprintf "\n------------------\nshowTerm: %s:\n" header
LayoutRender.outL stderr (Display.squashTo 192 (DebugPrint.implFilesL expr))
dprintf "\n------------------\n"
let AddExternalCcuToOptimizationEnv tcGlobals optEnv (ccuinfo: ImportedAssembly) =
match ccuinfo.FSharpOptimizationData.Force() with
| None -> optEnv
| Some data -> Optimizer.BindCcu ccuinfo.FSharpViewOfMetadata data optEnv tcGlobals
let GetInitialOptimizationEnv (tcImports: TcImports, tcGlobals: TcGlobals) =
let ccuinfos = tcImports.GetImportedAssemblies()
let optEnv = Optimizer.IncrementalOptimizationEnv.Empty
let optEnv = List.fold (AddExternalCcuToOptimizationEnv tcGlobals) optEnv ccuinfos
optEnv
/// Result of the 'FirstLoop' phase
type FirstLoopRes =
{
OptEnv: Optimizer.IncrementalOptimizationEnv
OptInfo: Optimizer.ImplFileOptimizationInfo
HidingInfo: SignatureHidingInfo
OptDuringCodeGen: bool -> Expr -> Expr
}
/// All the state optimization phases can produce (except 'CheckedImplFile') and most of what they require as input.
type PhaseContext =
{
FirstLoopRes: FirstLoopRes
OptEnvExtraLoop: Optimizer.IncrementalOptimizationEnv
OptEnvFinalSimplify: Optimizer.IncrementalOptimizationEnv
}
/// The result of running a single Optimization Phase.
type PhaseRes = CheckedImplFile * PhaseContext
/// 0-based index of an Optimization Phase.
type PhaseIdx = int
/// All inputs required to evaluate each Optimization Phase.
type PhaseInputs =
{
File: CheckedImplFile
FileIdx: int
/// State returned by processing the previous phase for the current file, or initial state.
PrevPhase: PhaseContext
/// State returned by processing the current phase for the previous file, or initial state.
PrevFile: PhaseContext
}
/// Signature of each optimization phase.
type PhaseFunc = PhaseInputs -> CheckedImplFile * PhaseContext
/// Each file's optimization can be split into up to seven different phases, executed one after another.
type Phase =
{
Idx: PhaseIdx
Name: string
}
override this.ToString() = $"{this.Idx}-{this.Name}"
type PhaseInfo = { Phase: Phase; Func: PhaseFunc }
[<RequireQualifiedAccess>]
module private ParallelOptimization =
open Optimizer
/// Identifies a work item scheduled independently of others - consists of a (file) index and OptimizationPhase.
/// There are (NumberOfFiles * NumberOfPhases) nodes in the whole optimization process.
type private Node =
{
FileIdx: int
Phase: PhaseIdx
}
override this.ToString() = $"[{this.FileIdx}-{this.Phase}]"
/// Final processing of file results to produce output needed for further compilation steps.
let private collectFinalResults
(fileResults: PhaseRes[])
: (CheckedImplFileAfterOptimization * ImplFileOptimizationInfo)[] * IncrementalOptimizationEnv =
let finalFileResults =
fileResults
|> Array.map (fun (file, res) ->
let implFile =
{
ImplFile = file
OptimizeDuringCodeGen = res.FirstLoopRes.OptDuringCodeGen
}
implFile, res.FirstLoopRes.OptInfo)
let lastFileFirstLoopEnv =
fileResults |> Array.last |> (fun (_file, res) -> res.FirstLoopRes.OptEnv)
finalFileResults, lastFileFirstLoopEnv
let optimizeFilesInParallel
(env0: IncrementalOptimizationEnv)
(phases: PhaseInfo[])
(files: CheckedImplFile list)
: (CheckedImplFileAfterOptimization * ImplFileOptimizationInfo)[] * IncrementalOptimizationEnv =
let files = files |> List.toArray
/// Initial state for processing the current file.
let initialState =
{
FirstLoopRes =
{
OptEnv = env0
OptInfo =
InterruptibleLazy(fun _ ->
failwith "This dummy value wrapped in a Lazy was not expected to be evaluated before being replaced.")
HidingInfo = SignatureHidingInfo.Empty
// A no-op optimizer
OptDuringCodeGen = fun _ expr -> expr
}
OptEnvExtraLoop = env0
OptEnvFinalSimplify = env0
}
// Functions for accessing a set of node jobs and their results
let getTask, setTask =
let tasks: Task<PhaseRes>[,] = Array2D.zeroCreate files.Length phases.Length
let getTask (node: Node) = tasks[node.FileIdx, node.Phase]
let setTask (node: Node) (task: Task<PhaseRes>) = tasks[node.FileIdx, node.Phase] <- task
getTask, setTask
/// Asynchronously wait for dependencies of a node
let getNodeInputs (node: Node) =
backgroundTask {
let prevPhaseNode = { node with Phase = node.Phase - 1 }
let prevFileNode = { node with FileIdx = node.FileIdx - 1 }
let prevPhaseTask =
if node.Phase > 0 then
backgroundTask {
// Make sure that creating the task does not block before other node tasks finish
do! Task.Yield()
return! getTask prevPhaseNode
}
else
// First phase uses input file without modifications
(files[node.FileIdx], initialState) |> Task.FromResult
let prevFileTask =
if node.FileIdx > 0 then
backgroundTask {
// Make sure that creating the task does not block before other node tasks finish
do! Task.Yield()
return! getTask prevFileNode
}
else
// We don't use the file result in this case, but we need something, so just take the first input file as a placeholder.
(files[0], initialState) |> Task.FromResult
let! results = [| prevPhaseTask; prevFileTask |] |> Task.WhenAll
let prevPhaseFile, prevPhaseRes = results[0]
let _prevFileFile, prevFileRes = results[1]
let inputs =
{
File = prevPhaseFile
FileIdx = node.FileIdx
PrevPhase = prevPhaseRes
PrevFile = prevFileRes
}
return inputs
}
let startNodeTask (phase: PhaseInfo) (node: Node) =
backgroundTask {
// Make sure that creating the task does not block before other node tasks finish
do! Task.Yield()
let! inputs = getNodeInputs node
let res = phase.Func inputs
return res
}
let fileIndices = [| 0 .. files.Length - 1 |]
for fileIdx in fileIndices do
for phase in phases do
let node =
{
Node.Phase = phase.Phase.Idx
FileIdx = fileIdx
}
let task = startNodeTask phase node
setTask node task
let lastPhaseResultsTask =
let lastPhaseIndex = phases[phases.Length - 1].Phase.Idx
fileIndices
|> Array.map (fun fileIdx ->
getTask
{
Node.FileIdx = fileIdx
Phase = lastPhaseIndex
})
|> Task.WhenAll
let lastPhaseResults =
try
lastPhaseResultsTask.GetAwaiter().GetResult()
// If multiple exceptions returned by multiple tasks, ignore all but the first one.
with :? System.AggregateException as ex when ex.InnerExceptions.Count > 0 ->
raise ex.InnerExceptions[0]
collectFinalResults lastPhaseResults
let optimizeFilesSequentially optEnv (phases: PhaseInfo[]) implFiles =
let results, (optEnvFirstLoop, _, _, _) =
let implFiles = implFiles |> List.mapi (fun i file -> i, file)
((optEnv, optEnv, optEnv, SignatureHidingInfo.Empty), implFiles)
||> List.mapFold
(fun (optEnvFirstLoop: Optimizer.IncrementalOptimizationEnv, optEnvExtraLoop, optEnvFinalSimplify, hidden) (fileIdx, implFile) ->
/// Initial state for processing the current file.
let state =
implFile,
{
FirstLoopRes =
{
OptEnv = optEnvFirstLoop
OptInfo =
InterruptibleLazy(fun _ ->
failwith
"This dummy value wrapped in a Lazy was not expected to be evaluated before being replaced.")
HidingInfo = hidden
// A no-op optimizer
OptDuringCodeGen = fun _ expr -> expr
}
OptEnvExtraLoop = optEnvExtraLoop
OptEnvFinalSimplify = optEnvFinalSimplify
}
let runPhase (file: CheckedImplFile, state: PhaseContext) (phase: PhaseInfo) =
// In the sequential mode we always process all phases of the previous file before processing the current file.
// This is why the state returned by the previous phase of the current file contains all changes made in the previous file,
// and we can use it in both places.
let input =
{
File = file
FileIdx = fileIdx
PrevPhase = state
PrevFile = state
}
phase.Func input
let implFile, state = Array.fold runPhase state phases
let file =
{
ImplFile = implFile
OptimizeDuringCodeGen = state.FirstLoopRes.OptDuringCodeGen
}
(file, state.FirstLoopRes.OptInfo),
(state.FirstLoopRes.OptEnv, state.OptEnvExtraLoop, state.OptEnvFinalSimplify, state.FirstLoopRes.HidingInfo))
results, optEnvFirstLoop
let ApplyAllOptimizations
(tcConfig: TcConfig, tcGlobals, tcVal, outfile, importMap, isIncrementalFragment, optEnv, ccu: CcuThunk, implFiles)
=
// NOTE: optEnv - threads through
//
// Always optimize once - the results of this step give the x-module optimization
// info. Subsequent optimization steps choose representations etc. which we don't
// want to save in the x-module info (i.e. x-module info is currently "high level").
PrintWholeAssemblyImplementation tcConfig outfile "pass-start" implFiles
#if DEBUG
if tcConfig.showOptimizationData then
dprintf "Expression prior to optimization:\n%s\n" (LayoutRender.showL (Display.squashTo 192 (DebugPrint.implFilesL implFiles)))
if tcConfig.showOptimizationData then
dprintf "CCU prior to optimization:\n%s\n" (LayoutRender.showL (Display.squashTo 192 (DebugPrint.entityL ccu.Contents)))
#endif
ReportTime tcConfig "Optimizations"
let firstLoopSettings =
{ tcConfig.optSettings with
// Only do abstractBigTargets in the first phase, and only when TLR is on.
abstractBigTargets = tcConfig.doTLR
reportingPhase = true
inlineNamedFunctions = tcConfig.inlineNamedFunctions
}
// Only do these two steps in the first phase.
let extraAndFinalLoopSettings =
{ firstLoopSettings with
abstractBigTargets = false
reportingPhase = false
}
let addPhaseDiagnostics (f: PhaseFunc) (info: Phase) =
fun (inputs: PhaseInputs) ->
use _ =
let tags =
[|
"QualifiedNameOfFile", inputs.File.QualifiedNameOfFile.Text
"OptimisationPhase", info.Name
|]
FSharp.Compiler.Diagnostics.Activity.start $"file-{inputs.FileIdx}_phase-{info.Name}" tags
f inputs
let phases = List<PhaseInfo>()
let addPhase (name: string) (phaseFunc: PhaseFunc) =
let phase = { Idx = phases.Count; Name = name }
let phaseInfo =
{
Phase = phase
Func = addPhaseDiagnostics phaseFunc phase
}
phases.Add(phaseInfo)
let firstLoop
({
File = file
PrevPhase = prevPhase
PrevFile = prevFile
}: PhaseInputs)
: PhaseRes =
let (env, file, optInfo, hidingInfo), optDuringCodeGen =
Optimizer.OptimizeImplFile(
firstLoopSettings,
ccu,
tcGlobals,
tcVal,
importMap,
prevFile.FirstLoopRes.OptEnv,
isIncrementalFragment,
tcConfig.emitTailcalls,
prevFile.FirstLoopRes.HidingInfo,
file
)
file,
{ prevPhase with
FirstLoopRes =
{
OptEnv = env
OptInfo = optInfo
HidingInfo = hidingInfo
OptDuringCodeGen = optDuringCodeGen
}
}
addPhase "firstLoop" firstLoop
let lowerLocalMutables
({
File = file
PrevPhase = prevPhase
PrevFile = _prevFile
}: PhaseInputs)
: PhaseRes =
let file = LowerLocalMutables.TransformImplFile tcGlobals importMap file
file, prevPhase
addPhase "lowerLocalMutables" lowerLocalMutables
let extraLoop
({
File = file
PrevPhase = prevPhase
PrevFile = prevFile
}: PhaseInputs)
: PhaseRes =
let (optEnvExtraLoop, file, _, _), _ =
Optimizer.OptimizeImplFile(
extraAndFinalLoopSettings,
ccu,
tcGlobals,
tcVal,
importMap,
prevFile.OptEnvExtraLoop,
isIncrementalFragment,
tcConfig.emitTailcalls,
prevPhase.FirstLoopRes.HidingInfo,
file
)
file,
{ prevPhase with
OptEnvExtraLoop = optEnvExtraLoop
}
if tcConfig.extraOptimizationIterations > 0 then
addPhase "ExtraLoop" extraLoop
let detuple
({
File = file
PrevPhase = prevPhase
PrevFile = _prevFile
}: PhaseInputs)
: PhaseRes =
let file = file |> Detuple.DetupleImplFile ccu tcGlobals
file, prevPhase
if tcConfig.doDetuple then
addPhase "Detuple" detuple
let innerLambdasToToplevelFuncs
({
File = file
PrevPhase = prevPhase
PrevFile = _prevFile
}: PhaseInputs)
: PhaseRes =
let file =
file
|> InnerLambdasToTopLevelFuncs.MakeTopLevelRepresentationDecisions ccu tcGlobals
file, prevPhase
if tcConfig.doTLR then
addPhase "InnerLambdasToToplevelFuncs" innerLambdasToToplevelFuncs
let lowerCalls
({
File = file
PrevPhase = prevPhase
PrevFile = _prevFile
}: PhaseInputs)
: PhaseRes =
let file = LowerCalls.LowerImplFile tcGlobals file
file, prevPhase
addPhase "LowerCalls" lowerCalls
let finalSimplify
({
File = file
PrevPhase = prevPhase
PrevFile = prevFile
}: PhaseInputs)
: PhaseRes =
let (optEnvFinalSimplify, file, _, _), _ =
Optimizer.OptimizeImplFile(
extraAndFinalLoopSettings,
ccu,
tcGlobals,
tcVal,
importMap,
prevFile.OptEnvFinalSimplify,
isIncrementalFragment,
tcConfig.emitTailcalls,
prevPhase.FirstLoopRes.HidingInfo,
file
)
file,
{ prevPhase with
OptEnvFinalSimplify = optEnvFinalSimplify
}
if tcConfig.doFinalSimplify then
addPhase "FinalSimplify" finalSimplify
let phases = phases.ToArray()
let results, optEnvFirstLoop =
match tcConfig.optSettings.processingMode with
// Parallel optimization breaks determinism - turn it off in deterministic builds.
| Optimizer.OptimizationProcessingMode.Parallel ->
let results, optEnvFirstPhase =
ParallelOptimization.optimizeFilesInParallel optEnv phases implFiles
results |> Array.toList, optEnvFirstPhase
| Optimizer.OptimizationProcessingMode.Sequential -> optimizeFilesSequentially optEnv phases implFiles
#if DEBUG
if tcConfig.showOptimizationData then
results
|> List.map snd
|> List.iter (fun implFileOptData ->
let str =
(LayoutRender.showL (Display.squashTo 192 (Optimizer.moduleInfoL tcGlobals implFileOptData)))
dprintf $"Optimization implFileOptData:\n{str}\n")
#endif
let implFiles, implFileOptDatas = List.unzip results
let assemblyOptData = Optimizer.UnionOptimizationInfos implFileOptDatas
let tassembly = CheckedAssemblyAfterOptimization implFiles
PrintWholeAssemblyImplementation tcConfig outfile "pass-end" (implFiles |> List.map (fun implFile -> implFile.ImplFile))
ReportTime tcConfig "Ending Optimizations"
tassembly, assemblyOptData, optEnvFirstLoop
//----------------------------------------------------------------------------
// ILX generation
//----------------------------------------------------------------------------
let CreateIlxAssemblyGenerator (_tcConfig: TcConfig, tcImports: TcImports, tcGlobals, tcVal, generatedCcu) =
let ilxGenerator =
IlxAssemblyGenerator(tcImports.GetImportMap(), tcGlobals, tcVal, generatedCcu)
let ccus = tcImports.GetCcusInDeclOrder()
ilxGenerator.AddExternalCcus ccus
ilxGenerator
let GenerateIlxCode
(ilxBackend, isInteractiveItExpr, tcConfig: TcConfig, topAttrs: TopAttribs, optimizedImpls, fragName, ilxGenerator: IlxAssemblyGenerator) =
let mainMethodInfo =
if
(tcConfig.target = CompilerTarget.Dll)
|| (tcConfig.target = CompilerTarget.Module)
then
None
else
Some topAttrs.mainMethodAttrs
let ilxGenOpts: IlxGenOptions =
{
generateFilterBlocks = tcConfig.generateFilterBlocks
emitConstantArraysUsingStaticDataBlobs = true
workAroundReflectionEmitBugs = tcConfig.isInteractive
generateDebugSymbols = tcConfig.debuginfo // REVIEW: is this still required?
fragName = fragName
localOptimizationsEnabled = tcConfig.optSettings.LocalOptimizationsEnabled
testFlagEmitFeeFeeAs100001 = tcConfig.testFlagEmitFeeFeeAs100001
mainMethodInfo = mainMethodInfo
ilxBackend = ilxBackend
fsiMultiAssemblyEmit = tcConfig.fsiMultiAssemblyEmit
useReflectionFreeCodeGen = tcConfig.useReflectionFreeCodeGen
isInteractive = tcConfig.isInteractive
isInteractiveItExpr = isInteractiveItExpr
alwaysCallVirt = tcConfig.alwaysCallVirt
parallelIlxGenEnabled = tcConfig.parallelIlxGen
inlineNamedFunctions = tcConfig.inlineNamedFunctions
}
ilxGenerator.GenerateCode(ilxGenOpts, optimizedImpls, topAttrs.assemblyAttrs, topAttrs.netModuleAttrs)
//----------------------------------------------------------------------------
// Assembly ref normalization: make sure all assemblies are referred to
// by the same references. Only used for static linking.
//----------------------------------------------------------------------------
let NormalizeAssemblyRefs (ctok, ilGlobals: ILGlobals, tcImports: TcImports) scoref =
let normalizeAssemblyRefByName nm =
match tcImports.TryFindDllInfo(ctok, Range.rangeStartup, nm, lookupOnly = false) with
| Some dllInfo -> dllInfo.ILScopeRef
| None -> scoref
match scoref with
| ILScopeRef.Local
| ILScopeRef.Module _ -> scoref
| ILScopeRef.PrimaryAssembly -> normalizeAssemblyRefByName ilGlobals.primaryAssemblyName
| ILScopeRef.Assembly aref -> normalizeAssemblyRefByName aref.Name
let GetGeneratedILModuleName (t: CompilerTarget) (s: string) =
// return the name of the file as a module name
let ext =
match t with
| CompilerTarget.Dll -> "dll"
| CompilerTarget.Module -> "netmodule"
| _ -> "exe"
s + "." + ext