Commit Graph

86 Commits

Author SHA1 Message Date
Arnold Schwaighofer
25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
Erik Eckstein
49d3960c68 MandatoryPerformanceOptimization: de-virtualize deinits of builtin "destroyArray"
This is required for embedded swift.
rdar://157131184
2025-08-28 08:05:27 +02:00
Andrew Trick
eb1d5f484c [NFC] SwiftCompilerSources: add a correctly named filterUsers API
Rename existing filterUsers to filterUses.
2025-08-14 09:08:11 -07:00
Erik Eckstein
3a7ed92753 MandatoryPerformanceOptimizations: fix adding witness methods to the function worklist
We were missing adding witness methods of not-specialized witness tables.
This resulted in functions not being processed which led to crashes in IRGen.

rdar://158224693
2025-08-14 11:00:30 +02:00
Erik Eckstein
41a6b8e257 SwiftCompilerSources: move SIL-related Context APIs from Optimizer to the SIL module 2025-07-28 14:19:11 +02:00
Erik Eckstein
2b8c63dcd3 MandatoryPerformanceOptimizations: don't specialize vtables for thin class metatype instructions
Fixes a wrong compiler error for imported C++ classes with custom reference counting.
rdar://154947835
2025-07-03 16:21:21 +02:00
Erik Eckstein
da484f3146 MandatoryPerformanceOptimization: don't recursive into referenced functions if they are not called
Fixes a false compiler error when referencing a function from a global with a section attribute.

rdar://154332540
2025-06-27 10:01:42 +02:00
Erik Eckstein
63cb683cb7 SIL: improve some Location APIs
* rename `var autoGenerated` -> `var asAutoGenerated`
* add `var asCleanup`
* add `func withScope`
2025-06-20 08:15:00 +02:00
Erik Eckstein
f83fb1b14a Optimizer: add FunctionWorklist and CrossFunctionValueWorklist
Originally, `FunctionWorklist` was a private utility in MandatoryPerformanceOptimizations.
Moved this to `Worklist.swift` to make it generally available.
Also, simplify the `pop()` function which changes the popping order - therefore some test changes were necessary.
2025-06-20 08:14:59 +02:00
Erik Eckstein
f0b3ec05b4 MandatoryPerformanceOptimizations: some refactoring
NFC
2025-06-10 08:10:42 +02:00
Erik Eckstein
d6cbaf6ebd MandatoryPerformanceOptimizations: only set the [perf_constraint] flag for callees of performance constraint functions
It used to also set it for functions which are referenced from a global with a const/section attribute - even if not performance attribute was present in the whole module. This is unnecessary and can lead to worse code generation.

rdar://152665294
2025-06-06 20:08:16 +02:00
Erik Eckstein
cf55b9bede MandatoryPerformanceOptimizations: make sure to handle de-serialized vtable methods
When de-serializing a function and this function allocates a class, the methods of the de-serialized vtable must be handled, too.

Fixes an IRGen crash
rdar://152311945
2025-06-02 11:43:34 +02:00
Erik Eckstein
5dc71aa0a5 AST/SIL: support source location in diagnostics for de-serialized debug info
Diagnostics only work with `SourceLoc` which is basically a pointer into a buffer of the loaded source file.
But when debug info is de-serialized, the SIL `Location` consists of a filename+line+column.
To "convert" this to a `SourceLoc`, the file must be loaded.
This change adds `DiagnosticEngine.getLocationFromExternalSource` for this purpose.
Also, the new protocol `ProvidingSourceLocation` - to which `SourceLoc` and `Location` conform - help to generalize the helper struct `Diagnostic` and make this "conversion" happen automatically.
2025-05-14 11:43:47 +02:00
Erik Eckstein
be322877b1 EmbeddedSwiftDiagnostics: improve error message for non-specialized generic function calls
Tell the user if the specialization isn't done because of a dynamic Self type.

rdar://150865684
2025-05-08 19:21:08 +02:00
Erik Eckstein
f52f491936 EmbeddedSwiftDiagnostics: check apply diagnostics also for begin_apply and partial_apply 2025-05-08 19:21:08 +02:00
Erik Eckstein
d9c7a68249 EmbeddedSwiftDiagnostics: fix a wrong "cannot use co-routines (like accessors) in -no-allocations mode" error
rdar://150890424
2025-05-08 19:21:08 +02:00
Erik Eckstein
9348f5e8d4 MandatoryPerformanceOptimizations: force inlining of transparent co-routines
This might be necessary if `-enable-testing` is turned on, because in this mode function linkages are different than in a regular build.
2025-05-08 19:21:08 +02:00
Erik Eckstein
08ce2f00f2 embedded: avoid false error "Deinit of non-copyable type not visible in the current module" in SourceKit
As SourceKit explicitly disables WMO, silence the diagnostic in this case (but leave it enabled for explicit non-WMO builds otherwise).

rdar://150596807
2025-05-05 09:19:08 +02:00
Erik Eckstein
6c31eb0c43 embedded: rewrite the diagnostic pass for embedded swift
1. move embedded diagnostics out of the PerformanceDiagnostics pass. It was completely separated from the other logic in this pass, anyway.
2. rewrite it in swift
3. fix several bugs, that means: missed diagnostics, which led to IRGen crashes
  * look at all methods in witness tables, including base protocols and associated conformances
  * visit all functions in the call tree, including generic functions with class bound generic arguments
  * handle all instructions, e.g. concurrency builtins
4. improve error messages by adding meaningful call-site information. For example:
  * if the error is in a specialized function, report where the generic function is originally specialized with concrete types
  * if the error is in a protocol witness method, report where the existential is created
2025-04-18 06:58:40 +02:00
Erik Eckstein
d222cf20f1 MandatoryPerformanceOptimizations: support default methods for class existentials
For example:
```
protocol P: AnyObject {
  func foo()
}
extension P {
  func foo() {}
}
class C: P {}

let e: any P = C()
```

Such default methods are SILGen'd with a generic self argument. Therefore we need to specialize such witness methods, even if the conforming type is not generic.

rdar://145855851
2025-04-18 06:58:40 +02:00
Erik Eckstein
1c9a7cd562 SwiftCompilerSources: refactor DiagnosticEngine
* move it from the SIL to the AST module (where it belongs)
* change the signature of `diagnose` from `diagnose(location, .some_error)` to `diagnose(.some_error, at: location)`
* add an overload to allow passing a `SIL.Location` directly to `diagnose`
* add a `Diagnostic : Error` utility struct which allows throwing a `Diagnostic`
2025-04-18 06:58:38 +02:00
Artem Chikin
281f84da0f [Compile Time Values] Rewrite the 'Diagnose Unknown Compile Time Values' diagnostic pass in Swift 2025-03-28 10:30:07 -07:00
Artem Chikin
72a420919a [Compile Time Values] Add mandatory optimization pipeline driver for '@const' globals 2025-03-27 14:33:38 -07:00
Erik Eckstein
3f95ce9645 Optimizer: fix spelling of the Simplifiable protocol
Rename `Simplifyable` -> `Simplifiable`

NFC
2025-02-12 09:01:11 +01:00
Erik Eckstein
33761454b7 MandatoryPerformanceOptimizations: use eliminateRedundantLoads instead of optimizeMemoryAccesses to optimize redundant loads 2025-02-07 11:30:35 +01:00
Kuba Mracek
e1d22b5076 [embedded] Avoid changing lookupStdlibFunction, load findStringSwitchCaseWithCache in MPO instead 2025-01-28 10:03:14 -08:00
Kuba Mracek
92b0c5f5d0 [embedded] Support _findStringSwitchCaseWithCache in Embedded Swift 2025-01-25 09:09:47 -08:00
Erik Eckstein
f0633d5638 AccessUtils: support computing "constant" access paths
Add `Value.constantAccessPath`. It is like `accessPath`, but ensures that the projectionPath only contains "constant" elements.
This means: if the access contains an `index_addr` projection with a non-constant index, the `projectionPath` does _not_ contain the `index_addr`.
Instead, the `base` is an `AccessBase.index` which refers to the `index_addr`.
2024-11-04 19:26:44 +01:00
Erik Eckstein
709dfc2d21 MandatoryPerformanceOptimization: don't let not-inlinable functions to be inlined
Also refactor canInline.

Fixes a compiler crash.
rdar://137544788
2024-10-15 12:19:50 +02:00
Erik Eckstein
5c8fe55449 embedded: fix several issues with vtable specialization
* fix a false error if a derived class has different generic parameters than its base class
* fix a similar problem if a non-generic class derives from a generic class
* fix a compiler crash for calling a class method on a class metatype

rdar://137692055
2024-10-14 14:43:11 +02:00
Erik Eckstein
a3e6e86383 Embedded: support existentials with inherited conformances
That means: derived classes where the base conforms to a protocol
2024-10-07 12:25:18 +02:00
Erik Eckstein
c05234e677 MandatoryPerformanceOptimizations: specialize witness_method instructions
In Embedded Swift, witness method lookup is done from specialized witness tables.
For this to work, the type of witness_method must be specialized as well.
Otherwise the method call would be done with wrong parameter conventions (indirect instead of direct).
2024-10-07 09:00:31 +02:00
Erik Eckstein
25728853b8 GenericSpecialization: change how new specialized witness tables are added to MandatoryPerformanceOptimization's worklist
Do it by passing a closure instead of returning the new witness table.
This allows to add more than one new witness table to the worklist
2024-10-07 08:49:56 +02:00
Erik Eckstein
6fdb713e94 SwiftCompilerSources: implement WitnessTable.Entry as enum 2024-10-02 07:10:30 +02:00
Erik Eckstein
4a1d6925e9 MandatoryPerformanceOptimizations: specialize witness tables to support class existentials with generic classes 2024-09-25 19:32:14 +02:00
Erik Eckstein
7ffd270008 embedded: move the VTableSpecializer pass into MandatoryPerformanceOptimizations
MandatoryPerformanceOptimizations already did most of the vtable specialization work.
So it makes sense to remove the VTableSpecializerPass completely and do everything in MandatoryPerformanceOptimizations.
2024-09-25 19:32:14 +02:00
Erik Eckstein
46d3909471 SIL: improve VTable and WitnessTable
* add missing APIs
* bridge the entries as values and not as pointers
* add lookup functions in `Context`
* make WitnessTable.Entry.Kind enum cases lower case
2024-09-25 19:32:07 +02:00
Erik Eckstein
b7e5ec825a MandatoryPerformanceOptimizations: force inlining of transparent functions and de-virtualization
Do this even if the function then contains references to other functions with wrong linkage. Instead fix the linkage later.
Fixes a false error in embedded swift.

rdar://134352676
2024-08-22 09:15:14 +02:00
Erik Eckstein
c96b196ffa SwiftCompilerSources: bridge SILLinkage
Make SILLInkage available in SIL as `SIL.Linkage`.
Also, rename the misleading Function and GlobalVariable ABI `isAvailableExternally` to `isDefinedExternally`
2024-08-22 08:56:27 +02:00
Erik Eckstein
19dccf78ce MandatoryPerformanceOptimizations: don't crash with empty global-init-once functions
Instead ignore empty global-init-once functions

https://github.com/swiftlang/swift/issues/73487
rdar://130041582
2024-07-02 14:03:01 +02:00
Erik Eckstein
1f9588e5c6 MandatoryPerformanceOptimizations: prevent inlining of dynamic-self class methods
This fixes a compiler crash in embedded swift.

rdar://129241915
2024-06-24 15:01:02 +02:00
Andrew Trick
a0b2ae9c2c Add AccessBase.storeBorrow.
Don't treat StoreBorrow addresses as unknown bases. While they are never the base of a formal access, they are returned
as the AccessBase when querying the enclosing scope of an address.
2024-03-22 11:51:58 -07:00
Kuba Mracek
f5797941b7 [embedded] Specialize superclasses in VTableSpecializer as part of MandatoryPerformanceOptimizations 2024-03-15 21:30:08 -07:00
Andrew Trick
a3cbc28078 SwiftCompilerSources: Add enum BeginAccess.AccessKind
So we can switch over it.
2024-02-25 10:22:06 -08:00
Erik Eckstein
38bfc03994 MandatoryPerformanceOptimizations: set the [perf_constraint] flag on functions with performance constraints 2024-02-05 12:38:53 +01:00
Erik Eckstein
bd7db677ed MandatoryPerformanceOptimizations: perform function signature optimization to remove metatype arguments
Generic specialization already takes care of removing metatype arguments of generic functions.
But sometimes non-generic functions have metatype arguments which must be removed.
We need handle this case with a function signature optimization.

This enables, for example, to use `OptionSet` in embedded swift.

rdar://121206953
2024-01-31 17:16:18 +01:00
Erik Eckstein
80051feb4c MandatoryPerformanceOptimizations: trigger another iteration if dead allocations were removed 2024-01-31 17:16:18 +01:00
Erik Eckstein
31f3102540 swift SIL: fix Function.isGeneric
It didn't work for functions in generic contexts, which don't add a generic parameter themselves
2024-01-31 17:16:18 +01:00
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