Commit Graph

230 Commits

Author SHA1 Message Date
Erik Eckstein
a30a4adbf4 PassManager: fix a small bug with performance measurments
The check for invalidation was wrong, leading to not restore from the snapshot, which can result in wrong measurements.
2022-07-29 16:05:03 +02:00
Erik Eckstein
c9c4e10e0d SILOptimizer: add an option to profile and measure the runtime of optimization passes.
When enabling the option `-sil-opt-profile-repeat=<n>`, the optimizer runs passes n times and reports the total runtime at the end of the pass pipeline.
This is useful to profile a specific optimization pass with `sil-opt`.
For example, to profile the stack promotion pass:
```
  sil-opt -stack-promotion -sil-opt-profile-repeat=10000 -o /dev/null test.sil
```
2022-07-27 17:50:03 +02:00
Egor Zhdan
0e2d438c5b [cxx-interop][SwiftCompilerSources] Use llvm::StringRef instead of BridgedStringRef
rdar://83361000
2022-07-21 16:32:16 +01:00
Erik Eckstein
49a5b3ebdc Swift SIL: add ValueSet and InstructionSet utilities.
These sets are _much_ more efficient than `Set<Value>` and `Set<Instruction>` because they bridge to the efficient `NodeSet`.
Insertions/deletions are just bit operations.
2022-07-13 14:27:50 +02:00
Erik Eckstein
3b43da9637 Swift Optimizer: improve ergonomics of Builder and PassContext
* split the PassUtils.swift file into PassContext.swift and Passes.swift
* rework `Builder` bridging allowing more insertion point variations, e.g. inserting at the end of a block.
* add Builder.create functions for more instructions
* add `PassContext.splitBlock`
* move SIL modification functions from PassContext to extensions of the relevant types (e.g. instructions).
* rename `Location.bridgedLocation` -> `Location.bridged`
2022-05-12 21:48:37 +02:00
Erik Eckstein
5d3339e9a3 Swift SIL: support for sub-pass bisecting: add PassContext.continueWithNextSubpassRun 2022-04-25 11:57:02 +02:00
Erik Eckstein
d8bf949582 BasicCalleeAnalysis: improve finding the actual called deinits of a destroy/release instruction
* C++: add a function `getDestructors(SILType type, bool isExactType)’: if the type is a final class or `isExactType` is true, then return the one and only destructor of that class.
* swift: add `getDestructor(ofExactType type: Type)` and `getIncompleteCallees`
* swift: remove `getDestructor` from the PassContext. The API of the `calleeAnalysis` can be used instead.
2022-04-05 20:33:23 +02:00
Erik Eckstein
eaeaa02415 PassManager: disable verification of analysis with -sil-verify-none 2022-03-31 10:12:32 +02:00
Nate Chandler
8b99b9f77b [NFC] Exported printing query function.
Allow other compilation units to refer to isFunctionSelectedForPrinting
by declaring it extern.  Maybe at some point it might be nice to put
this into a header related to printing.
2022-03-15 10:51:37 -07:00
Erik Eckstein
8fe36eedd8 SILPassManager: improve bisecting for debugging the optimizer
* Add the possibility to bisect the individual transforms of SILCombine and SimplifyCFG.
   To do so, the `-sil-opt-pass-count` option now accepts the format `<n>.<m>`, where `m` is the sub-pass number.
   The sub-pass number limits the number of individual transforms in SILCombine or SimplifyCFG.

* Add an option `-sil-print-last` to print the SIL of the currently optimized function before and after the last pass, which is specified with `-sil-opt-pass-count`.
2022-02-09 13:25:30 +01:00
Erik Eckstein
79e9b9a088 SIL optimizer: add the ability to disable swift instruction passes with the -sil-disable-pass option. 2022-02-01 18:30:05 +01:00
Erik Eckstein
603e837a8f Swift optimizations: make isSwift51RuntimeAvailable sensitive to the resilience domain of the function. 2022-01-27 13:20:38 +01:00
Max Desiatov
b964dba177 libswift: implement ReleaseDevirtualizer in Swift 2022-01-19 18:51:19 +00:00
Max Desiatov
7d961001d3 libswift: bridge more functions from SILBuilder 2022-01-19 18:51:18 +00:00
Max Desiatov
0b34baa80f libswift: bridge RCIdentityAnalysis code 2022-01-19 18:51:17 +00:00
Erik Eckstein
e028239cc2 Swift Optimizer: add AllocRefInstBase.setIsStackAllocatable 2022-01-12 15:47:16 +01:00
Erik Eckstein
e152b2cb4a Swift Optimizer: add the fixStackNesting utility in PassContext
This bridges to the StackNesting utility in C++
2022-01-12 15:47:16 +01:00
Erik Eckstein
a24b17a333 Swift Optimizer: add the dominator and post-dominator tree analysis 2022-01-12 15:47:16 +01:00
Erik Eckstein
4440beb555 Swift Optimizer: add bridging to the DeadEndBlocksAnalysis 2022-01-12 15:47:16 +01:00
Erik Eckstein
40200d6544 Swift Optimizer: add BasicBlock utility data structures and rename StackList
* add `BasicBlockSet`
* add `BasicBlockWorklist`
* add `BasicBlockRange`, which defines a range of blocks from a common dominating “begin” block to a set of “end” blocks.
* add  `InstructionRange`, which is similar to `BasicBlockRange`, just on instruction level. It can be used for value lifetime analysis.
* rename `StackList` -> `Stack` and move it to `Optimizer/DataStructures`
* rename `PassContext.passContext` to `PassContext._bridged`
* add notify-functions to PassContext
2022-01-12 15:47:16 +01:00
Erik Eckstein
3522ba1521 SILOptimizer: rename LibswiftPassInvocation -> SwiftPassInvocation
And a few other small related changes:
* remove libswiftPassInvocation from SILInstructionWorklist (because it's not needed)
* replace start/finishPassRun with start/finishFunction/InstructionPassRun

NFC
2022-01-05 10:15:56 +01:00
Meghana Gupta
2d7e20e599 Remove unused mode in OME 2021-10-05 14:19:58 -07:00
Erik Eckstein
50195e7e7d libswift: fix a few bugs in StackList
Use MemoryLayout.stride instead pf MemoryLayout.size. This fixes a buffer overflow bug in case of unaligned elements.
Plus some other bug fixes for stacklists which get larger than a single slab.
Also, add the `append(contentsOf:)` method.
2021-09-20 07:56:46 +02:00
Erik Eckstein
90c71ad002 libswift: improve and simplify pass invocation
* unify FunctionPassContext and InstructionPassContext
* add a modification API: PassContext.setOperand
* automatic invalidation notifications when the SIL is modified
2021-08-23 10:21:12 +02:00
Erik Eckstein
7ed04c3ba3 libswift: add CalleeAnalysis
Bridging to BasicCalleeAnalysis
2021-07-01 17:30:56 +02:00
Erik Eckstein
20c63cc3f5 libswift: add AliasAnalysis 2021-07-01 17:16:47 +02:00
Erik Eckstein
1010f1e2ae libswift: infrastructure to define "instruction" passes.
Instruction passes are basically visit functions in SILCombine for a specific instruction type.
With the macro SWIFT_INSTRUCTION_PASS such a pass can be declared in Passes.def.
SILCombine then calls the run function of the pass in libswift.
2021-06-09 11:33:41 +02:00
Erik Eckstein
14422cdb50 libswift: Add the StackList data structure
StackList is a very efficient data structure for worklist type things.
This is a port of the C++ utility with the same name.

Compared to Array, it does not require any memory allocations.
2021-06-09 11:33:41 +02:00
Erik Eckstein
81f5e2f467 libswift: Infrastructure to call libswift function passes from the SILOptimizer's PassManager
With the macro SWIFT_FUNCTION_PASS a new libswift function pass can be defined in Passes.def.
The SWIFT_FUNCTION_PASS_WITH_LEGACY is similar, but it allows to keep an original C++ “legacy” implementation of the pass, which is used if the compiler is not built with libswift.
2021-06-09 11:30:59 +02:00
Erik Eckstein
4977850092 SIL: remove the notifyDeleteHandlers mechanism
It's not needed anymore with delayed instruction deletion.
It was used for two purposes:
1. For analysis, which cache instructions, to avoid dangling instruction pointers
2. For passes, which maintain worklists of instructions, to remove a deleted instructions from the worklist. This is now done by checking SILInstruction::isDeleted().
2021-05-26 21:57:54 +02:00
Erik Eckstein
24799e1526 SIL: defer instruction deletion to the end of a pass run.
When an instruction is "deleted" from the SIL, it is put into the SILModule::scheduledForDeletion list.
The instructions in this list are eventually deleted for real in SILModule::flushDeletedInsts(), which is called by the pass manager after each pass run.
In other words: instruction deletion is deferred to the end of a pass.

This avoids dangling instruction pointers within the run of a pass and in analysis caches.
Note that the analysis invalidation mechanism ensures that analysis caches are invalidated before flushDeletedInsts().
2021-05-26 21:57:54 +02:00
Meghana Gupta
83b4aeb11c Print the name of the generic function for a specialized function
When debug printing for a function is turned on with
-sil-print-function, print the generic function is was derived from.
2021-02-25 11:30:27 -08:00
Erik Eckstein
ee5491a7c9 PassManager: simplify the options to print intermediate SIL.
* rename -sil-print-only-function to -sil-print-function and -sil-print-only-functions to -sil-print-functions
* to print single functions, don't require -Xllvm -sil-print-all. It's now sufficient to use e.g. -Xllvm -sil-print-function=<f>
But it's still possible to select functions with -sil-print-function(s) for other print options, -sil-print-after.
2021-02-09 09:12:06 +01:00
Minhyuk Kim
e924cf6104 Replace usages of StringRef.find(Key) != StringRef::npos to StringRef.contains(Key) 2021-02-04 00:42:04 +09:00
Erik Eckstein
011358edd6 SIL: let SingleValueInstruction only inherit from a single SILNode.
This removes the ambiguity when casting from a SingleValueInstruction to SILNode, which makes the code simpler. E.g. the "isRepresentativeSILNode" logic is not needed anymore.
Also, it reduces the size of the most used instruction class - SingleValueInstruction - by one pointer.

Conceptually, SILInstruction is still a SILNode. But implementation-wise SILNode is not a base class of SILInstruction anymore.
Only the two sub-classes of SILInstruction - SingleValueInstruction and NonSingleValueInstruction - inherit from SILNode. SingleValueInstruction's SILNode is embedded into a ValueBase and its relative offset in the class is the same as in NonSingleValueInstruction (see SILNodeOffsetChecker).
This makes it possible to cast from a SILInstruction to a SILNode without knowing which SILInstruction sub-class it is.
Casting to SILNode cannot be done implicitly, but only with an LLVM `cast` or with SILInstruction::asSILNode(). But this is a rare case anyway.
2021-01-27 16:40:15 +01:00
Eric Miotto
8e7f9c9cbd Revert "SIL: let SingleValueInstruction only inherit from a single SILNode." 2021-01-26 10:02:24 -08:00
Erik Eckstein
ff1991740a SIL: let SingleValueInstruction only inherit from a single SILNode.
This removes the ambiguity when casting from a SingleValueInstruction to SILNode, which makes the code simpler. E.g. the "isRepresentativeSILNode" logic is not needed anymore.
Also, it reduces the size of the most used instruction class - SingleValueInstruction - by one pointer.

Conceptually, SILInstruction is still a SILNode. But implementation-wise SILNode is not a base class of SILInstruction anymore.
Only the two sub-classes of SILInstruction - SingleValueInstruction and NonSingleValueInstruction - inherit from SILNode. SingleValueInstruction's SILNode is embedded into a ValueBase and its relative offset in the class is the same as in NonSingleValueInstruction (see SILNodeOffsetChecker).
This makes it possible to cast from a SILInstruction to a SILNode without knowing which SILInstruction sub-class it is.
Casting to SILNode cannot be done implicitly, but only with an LLVM `cast` or with SILInstruction::asSILNode(). But this is a rare case anyway.
2021-01-25 09:30:04 +01:00
Meghana Gupta
f4bbafb392 Revert "[PassManager] Update PassManager's function worklist for newly added SILFunctions" 2020-10-05 10:23:31 -07:00
Meghana Gupta
163d47ec90 Revert "Revert #33106 and #33205" (#34106) 2020-09-28 23:08:14 -07:00
Meghana Gupta
77a76a8422 Revert "Merge pull request #33205 from meg-gupta/ometofunctionpass"
This reverts commit 8dbac48c18, reversing
changes made to c22ba90700.
2020-09-25 11:49:52 -07:00
Meghana Gupta
9c9a8ef224 Allow OME to run mandatorily 2020-09-22 18:02:04 -07:00
Meghana Gupta
337c4e88d7 Add a new -sil-disable-only-function flag (#32622)
This will enable us apply -sil-disable-pass only on certain functions
2020-07-01 10:18:36 -07:00
Meghana Gupta
b5a8b518ea Merge pull request #32141 from meg-gupta/commaflags
[NFC] Make some PassManager options to accept comma separated values
2020-06-11 15:28:32 -07:00
Meghana Gupta
a1e281d926 Merge pull request #30710 from meg-gupta/bottomupfunction
[PassManager] Update PassManager's function worklist for newly added SILFunctions
2020-06-03 08:20:39 -07:00
Meghana Gupta
15583ac269 Make some PassManager options to accept comma separated values 2020-06-01 23:29:13 -07:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Meghana Gupta
fd98ce10c7 Update PassManager's function worklist for newly added SILFunctions
The PassManager should transform all functions in bottom up order.
This is necessary because when optimizations like inlining looks at the
callee function bodies to compute profitability, the callee functions
should have already undergone optimizations to get better profitability
estimates.

The PassManager builds its function worklist based on bottom up order
on initialization. However, newly created SILFunctions due to
specialization etc, are simply appended to the function worklist. This
can cause us to make bad inlining decisions due to inaccurate
profitability estimates. This change now updates the function worklist such
that, all the callees of the newly added SILFunction are proccessed
before it by the PassManager.

Fixes rdar://52202680
2020-05-11 19:43:22 -07:00
Meghana Gupta
47fe49a2a9 Fix the mid-level function-pass pipeline (#31424)
* Fix the mid-level pass pipeline.

Module passes need to be in a separate pipeline, otherwise the
pipeline restart mechanism will be broken.

This makes GlobalOpt and serialization run earlier in the
pipeline. There's no explicit reason for them to be run later, in the
middle of a function pass pipeline.

Also, pipeline boundaries, like serialization and module passes should
be explicit at the the top level function that creates the pass
pipelines.

* SILOptimizer: Add enforcement of function-pass pipelines.

Don't allow module passes to be inserted within a function pass
pipeline. This silently breaks the function pipeline both interfering
with analysis and the normal pipeline restart mechanism.

* Add misssing pass in addFunctionPasses

Co-authored-by: Andrew Trick <atrick@apple.com>
2020-05-03 18:23:40 -07:00
Meghana Gupta
6c3857b6d6 Add forced precomputation and verification of analysis (#31251)
-sil-verify-all flag will verify analyses before and after a pass to
confirm correct invalidations. But if an analysis was never
constructed or invalidated as per current pass order,
it may never detect insufficient invalidations.

-sil-verify-force-analysis will force construct an analysis so that we
can better check for insufficient invalidations.
It is also terribly slow compared to -sil-verify-all.
2020-04-24 10:55:45 -07:00
Robert Widmann
987cd55f50 [NFC] Drop llvm::Expected from Evaluation Points
A request is intended to be a pure function of its inputs. That function could, in theory, fail. In practice, there were basically no requests taking advantage of this ability - the few that were using it to explicitly detect cycles can just return reasonable defaults instead of forwarding the error on up the stack.

This is because cycles are checked by *the Evaluator*, and are unwound by the Evaluator.

Therefore, restore the idea that the evaluate functions are themselves pure, but keep the idea that *evaluation* of those requests may fail. This model enables the best of both worlds: we not only keep the evaluator flexible enough to handle future use cases like cancellation and diagnostic invalidation, but also request-based dependencies using the values computed at the evaluation points. These aforementioned use cases would use the llvm::Expected interface and the regular evaluation-point interface respectively.
2020-03-26 23:08:02 -07:00