Commit Graph

38 Commits

Author SHA1 Message Date
Meghana Gupta
5d2454f3e3 Merge pull request #68180 from meg-gupta/lowerb4inline
Lower OwnershipModelEliminator to just before inlining
2024-01-10 13:53:34 -08:00
Meghana Gupta
9dbf7ccec2 Address review comments from #68150 2024-01-05 13:20:52 -08:00
Andrew Trick
2128c21106 Migrate SwiftCompilerSources to FunctionConvention.
Layers:
- FunctionConvention: AST FunctionType: results, parameters
- ArgumentConventions: SIL function arguments
- ApplyOperandConventions: applied operands

The meaning of an integer index is determined by the collection
type. All the mapping between the various indices (results,
parameters, SIL argument, applied arguments) is restricted to the
collection type that owns that mapping. Remove the concept of a
"caller argument index".
2024-01-03 12:24:50 -08:00
Erik Eckstein
96e57d62f6 Optimizer: de-virtualize deinits of non-copyable types
In regular swift this is a nice optimization. In embedded swift it's a requirement, because the compiler needs to be able to specialize generic deinits of non-copyable types.
The new de-virtualization utilities are called from two places:

* from the new DeinitDevirtualizer pass. It replaces the old MoveOnlyDeinitDevirtualization, which is very basic and does not fulfill the needs for embedded swift.

* from MandatoryPerformanceOptimizations for embedded swift
2023-11-27 09:21:34 +01:00
Erik Eckstein
0509a056fb SwiftCompilerSources: improve APIs for UseList
Make filter APIs for UseList chainable by adding them to Sequence where Element == Operand

For example, it allows to write:
```
let singleUse = value.uses.ignoreDebugUses.ignoreUsers(ofType: EndAccessInst.self).singleUse
```

Also, add `UseList.getSingleUser(notOfType:)`
2023-11-13 20:18:07 +01:00
Kuba Mracek
a3d15b12bd [embedded] No need to pass isEmbeddedSwift as an explicit argument, use context directly 2023-09-20 11:14:36 -07:00
Kuba Mracek
f67c660eb6 [embedded] Move isEmbedded check to MandatoryPerformanceOptimizations from VTableSpecializer 2023-09-20 11:03:48 -07:00
Kuba Mracek
89df0426e0 [embedded] Specialize vtables when processing instructions, not bulk 2023-09-20 09:41:09 -07:00
Kuba Mracek
03f927eca1 [embedded] Perform VTable specialization iteratively as part of MandatoryPerformanceOptimizations 2023-09-20 09:41:09 -07:00
Kuba Mracek
c0750cdd33 [embedded] Fix accidentally reversed logic in isGenericFunction + addAllNonGenericFunctions 2023-09-12 21:27:29 -07:00
zoecarver
520305332a [embedded] Don't add functions twice; fix a typo. 2023-09-06 10:59:49 -07:00
zoecarver
48513f3c5b [embedded] Introduce isGenericFunction and addAllNonGenericFunctions. 2023-09-06 10:48:17 -07:00
zoecarver
52cea4250e [embedded] 🚀 add Embedded expiremental feature.
Optimize all functions in embedded mode. Diagnose any uses of existentials or witness tables.
2023-09-06 10:48:17 -07:00
Manu
02b5fa2c8e Fix some typos in the codebase 2023-08-31 18:50:10 -03:00
Meghana Gupta
9f93ba003e Don't inline non-ossa function into an ossa function in mandatory inlining
Update the inliner with this check.
Inlining non-ossa into ossa can causes unexpected consequences.

Fixes rdar://114466605
2023-08-25 21:46:50 -07:00
Erik Eckstein
538d7306bd MandatoryPerformanceOptimizations: better handling of statically initialized globals which contain a pointer to another global
E.g.
```
  var p = Point(x: 10, y: 20)
  let o = UnsafePointer(&p)
```

MandatoryPerformanceOptimization must inline the accessor to the referenced global.
2023-08-10 20:49:20 +02:00
Erik Eckstein
8223b3e210 MandatoryPerformanceOptimizations: fix some problems with inilning
* don't inline functions if it's not possible - by checking `SILInliner::canInlineApplySite`
* fix stack nesting after inlining a `begin_apply`
2023-08-10 15:01:47 +02:00
Erik Eckstein
799bd0a5f5 Swift Optimizer: some reformatting in the optimization passes
Use tail closures for all optimization passes.
NFC.
2023-08-04 10:33:52 +02:00
Erik Eckstein
64255651f5 MandatoryPerformanceOptimizations: refine the inlining decisions in global initializers
We inline a function (e.g. a struct initializer) into a global init function if the result is part of the initialized global.
Now, also handle functions with indirect return values. Such function can result from not-reabstracted generic specializations.
Handle cases where the result is stored into a temporary alloc_stack or directly stored to (a part) of the global variable.
2023-07-26 11:06:50 +02:00
Kuba Mracek
2bf728d6cb MandatoryPerformanceOptimizations: Use Set<PathFunctionTuple> to track inlined functions 2023-07-10 08:58:13 -07:00
Kuba Mracek
145f12f6a3 Allow using structs with trivial initializers in globals that require static initialization (e.g. @_section attribute)
Before this change, if a global variable is required to be statically initialized (e.g. due to @_section attribute), we don't allow its type to be a struct, only a scalar type works. This change improves on that by teaching MandatoryPerformanceOptimizations pass to inline struct initializer calls into initializer of globals, as long as they are simple enough so that we can be sure that we don't trigger recursive/infinite inlining.
2023-07-08 19:26:59 -07:00
Erik Eckstein
86f2ced581 StackProtection: treat source-operands of memcpy and memmove intrinsics as read-only
This makes `UnsafeMutableRawPointer.storeBytes` not triggering stack protection

rdar://110738333
2023-06-19 13:56:44 +02:00
Erik Eckstein
fccecbe15e MandatoryPerformanceOptimizations: remove dead metatype instructions after specialization
Generic specialization drops metatype arguments and therefore exposes opportunities to remove dead metatype instructions.
Instead of removing dead metatype instructions before specialization, try to remove them after specialization.
2023-06-15 21:42:01 +02:00
Erik Eckstein
7bbf66ab5b ReadOnlyGlobalVariables: don't convert external global vars to lets
Because we don't know if external variables will be written

Fixes a miscompile
rdar://109476745
2023-05-19 18:28:22 +02:00
Erik Eckstein
dc3cb18029 Swift Optimizer: add the MandatoryPerformanceOptimizations pass
As a replacement for the old MandatoryGenericSpecializer

The pass it not enabled yet in the pass pipeline
2023-05-11 08:11:44 +02:00
Erik Eckstein
3d555a412a refactor two swift passes
NFC
2023-05-10 16:04:57 +02:00
Erik Eckstein
960ca70def Swift Optimizer: add the module pass ReadOnlyGlobalVariables
It marks global `var` variables as `let` if they are never written.
2023-05-08 21:23:36 +02:00
Erik Eckstein
f1095556c9 Swift SIL: let var UnaryInstruction.operand return an Operand and not a Value
To avoid confusion. Instead add specific getters for unary instructions with dedicated names.

NFC
2023-02-21 17:57:29 +01:00
Erik Eckstein
49e66c57b8 StackProtection: ignore pointers with no stores
Stack protection only protects against overflows, but not against out of bounds reads.

rdar://105231457
2023-02-15 08:20:48 +01:00
Erik Eckstein
393d1a1488 SIL Builder: rename insert(at:) -> insert(before:)
It matches with `insert(after:)` and the intent should be clearer now
2023-01-16 15:11:34 +01:00
Erik Eckstein
cc68bd98c9 Swift Optimizer: rework pass context types and instruction passes
* split the `PassContext` into multiple protocols and structs: `Context`, `MutatingContext`, `FunctionPassContext` and `SimplifyContext`
* change how instruction passes work: implement the `simplify` function in conformance to `SILCombineSimplifyable`
* add a mechanism to add a callback for inserted instructions
2023-01-16 15:11:34 +01:00
Erik Eckstein
6c35258f83 Swift SIL: rename parent accessors to parentX, e.g. Instruction.parentBlock
It makes it easier to read
2023-01-16 15:11:34 +01:00
Erik Eckstein
ef302ce4ac SILOptimizer: enable stack protection by default
The pass to decide which functions should get stack protection was added in https://github.com/apple/swift/pull/60933, but was disabled by default.

This PR enables stack protection by default, but not the possibility to move arguments into temporaries - to keep the risk low.
Moving to temporaries can be enabled with the new frontend option `-enable-move-inout-stack-protector`.

rdar://93677524
2022-11-11 17:14:08 +01:00
Egor Zhdan
94ec683ba5 Revert "Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift""
This reverts commit 69431f00
2022-10-18 18:55:57 +01:00
eeckstein
69431f00e8 Revert "[cxx-interop][SwiftCompilerSources] Use C++ enums directly from Swift" 2022-09-22 11:45:41 +02:00
Egor Zhdan
3f1a30c52a [cxx-interop][SwiftCompilerSources] Use swift::SILAccessKind instead of BridgedAccessKind
rdar://83361087
2022-09-20 12:03:48 +01:00
Egor Zhdan
78cdf5eca6 [cxx-interop][SwiftCompilerSources] Use swift::BuiltinValueKind instead of BridgedBuiltinID
rdar://83361087
2022-09-20 12:03:48 +01:00
Erik Eckstein
b2b44c0d83 Swift Optimizer: add the StackProtection optimization
It decides which functions need stack protection.

It sets the `needStackProtection` flags on all function which contain stack-allocated values for which an buffer overflow could occur.

Within safe swift code there shouldn't be any buffer overflows.
But if the address of a stack variable is converted to an unsafe pointer, it's not in the control of the compiler anymore.
This means, if there is any `address_to_pointer` instruction for an `alloc_stack`, such a function is marked for stack protection.
Another case is `index_addr` for non-tail allocated memory.
This pattern appears if pointer arithmetic is done with unsafe pointers in swift code.

If the origin of an unsafe pointer can only be tracked to a function argument, the pass tries to find the root stack allocation for such an argument by doing an inter-procedural analysis.
If this is not possible, the fallback is to move the argument into a temporary `alloc_stack` and do the unsafe pointer operations on the temporary.

rdar://93677524
2022-09-08 08:42:25 +02:00