Commit Graph

49 Commits

Author SHA1 Message Date
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
Ben Barham
a2fda1d9f3 [Embedded] Do not produce cannot_specialize_class for live issues
SourceKit explicitly disables WMO, silence the diagnostic in this case
(but leave it enabled for explicit non-WMO builds otherwise).
2024-12-19 15:31:41 -08:00
Doug Gregor
1d3332d471 Remove the now-unused NonErrorHandlingBlocks 2024-11-21 16:06:45 -08:00
Doug Gregor
c09bbf4c10 [Performance diagnostics] Enable checking of throw instructions
When performance diagnostics were introduced, typed throws didn't exist
so it was not generally possible to have throws anywhere without
triggering performance diagnostics. As a short term hack, we disabled
checking of `throw` instructions and the basic blocks that terminate
in a `throw`.

Now that typed throws is available and can be used to eliminate
allocations with error handling, remove all of the hacks. We'll now
diagnose attempts to throw or catch existential values (e.g., the `any
Error` used for untyped throws), but typed throws are fine.
2024-11-21 16:06:44 -08: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
Kuba Mracek
6b9a3051e3 [embedded] Introduce class-bound existentials into Embedded Swift
Motivated by need for protocol-based dynamic dispatch, which hasn't been possible in Embedded Swift due to a full ban on existentials. This lifts that restriction but only for class-bound existentials: Class-bound existentials are already (even in desktop Swift) much more lightweight than full existentials, as they don't need type metadata, their containers are typically 2 words only (reference + wtable pointer), don't incur copies (only retains+releases).

Included in this PR:
[x] Non-generic class-bound existentials, executable tests for those.
[x] Extension methods on protocols and using those from a class-bound existential.
[x] RuntimeEffects now differentiate between Existential and ExistentialClassBound.
[x] PerformanceDiagnostics don't flag ExistentialClassBound in Embedded Swift.
[x] WTables are generated in IRGen when needed.

Left for follow-up PRs:
[ ] Generic classes support
2024-09-19 07:49:50 -07:00
Kuba Mracek
e7b74051e8 [embedded] Fix generic function skipping in PerfDiags, diagnose KeyPathInsts in closures 2024-09-13 15:19:49 -07:00
Kuba Mracek
80056c15cd [embedded] Explicitly disable PerfDiags from SourceKit instead of always disabling when WMO is off 2024-08-10 14:50:22 -07:00
Kuba Mracek
3ad777b942 [embedded] Don't produce PerfDiags when in non-WMO mode (e.g. when building during indexing) 2024-08-10 14:48:24 -07:00
Erik Eckstein
1a308ef2fe PerformanceDiagnostic: give an error if a generic non-copyable value with a deinit is captured by an escaping closure.
Otherwise IRGen would crash.
It needs a bit of work to support alloc_box of generic non-copyable structs/enums with deinit, because we need to specialize the deinit functions, though they are not explicitly referenced in SIL.
Until this is supported, give an error in such cases.

Fixes a compiler crash in IRGen
rdar://130283111
2024-07-08 10:05:19 +02:00
Erik Eckstein
369021f2b0 PerformanceDiagnostics: diagnose dynamic casts
Dynamic casts need metadata and therefore cannot be used in embedded swift.
Fixes an internal IRGen error.
2024-05-17 15:07:40 +02:00
Kuba (Brecka) Mracek
829f442e84 Merge pull request #70441 from kubamracek/embedded-diagnose-alloc-ref-dynamic
[embedded] Start flagging AllocRefDynamicInst usage in embedded Swift
2024-05-10 08:55:27 -07:00
Kuba Mracek
91f4d47c7e [embedded] Add explaining comment about location-less SIL functions in PerformanceDiagnostics 2024-04-08 09:32:48 -07:00
Kuba Mracek
dd9c99f27a [embedded] Avoid a crash on location-less SIL functions in PerformanceDiagnostics 2024-04-03 15:26:49 -07:00
Kuba Mracek
b642d771be [embedded] Compile-time (literal) KeyPaths for Embedded Swift
Enable KeyPath/AnyKeyPath/PartialKeyPath/WritableKeyPath in Embedded Swift, but
for compile-time use only:

- Add keypath optimizations into the mandatory optimizations pipeline
- Allow keypath optimizations to look through begin_borrow, to make them work
  even in OSSA.
- If a use of a KeyPath doesn't optimize away, diagnose in PerformanceDiagnostics
- Make UnsafePointer.pointer(to:) transparent to allow the keypath optimization
  to happen in the callers of UnsafePointer.pointer(to:).
2024-03-20 15:35:46 -07:00
Erik Eckstein
171cca4ec7 PerformanceDiagnostics: print an error in embedded swift if a value type deinit cannot be devirtualized
Not de-virtualized value type deinits can require metatype in case the deinit needs to be called via the value witness table.
Usually this does not happen because deinits are mandatory de-virtualized. But it can show up if e.g. wrong build options are used.

rdar://122651706
2024-02-15 14:01:45 +01:00
Erik Eckstein
1a94f1ccb7 PerformanceDiagnostics: allow metatype arguments to _diagnoseUnexpectedEnumCaseValue
This is a fatal error function, used for imported C enums.

rdar://117520459
2023-12-14 12:51:01 +01:00
Kuba Mracek
12f8e62a20 [embedded] Start flagging AllocRefDynamicInst usage in embedded Swift 2023-12-13 10:48:15 -08:00
Kuba Mracek
08053b6794 [embedded] Order externally visible functions first in PerformanceDiagnostics when diagnosing embedded Swift restrictions 2023-12-11 14:42:54 -08:00
Kuba Mracek
ff1f8adac8 [embedded] Implement non-allocating embedded Swift mode, under -no-allocations flag 2023-12-11 09:00:50 -08:00
Kuba Mracek
ea47c813ec [embedded] Print demangled function names in PerformanceDiagnostics 2023-12-10 16:33:37 -08:00
Kuba Mracek
75450c7d2f [embedded] When missing a source location in a PerformanceDiagnostic, at least print the function name 2023-12-10 16:24:15 -08:00
zoecarver
69498e2f2e [opt] Add three new perf annotations: @_noRuntime, @_noExistential, and @_noObjCBridging. 2023-12-01 09:13:24 -07:00
Erik Eckstein
c26134fbae PerformanceDiagnostics: correctly handle functions with multiple throws
This is a follow-up of https://github.com/apple/swift/pull/69300, which didn't handle function with multiple throws correctly.

rdar://117857767
2023-11-03 10:37:23 +01:00
Erik Eckstein
1ec71a3568 PerformanceDiagnostics: exclude error handling from performance-checked code
This was changed in https://github.com/apple/swift/pull/69121. But it's a bit to early because we don't have typed throws, yet.

rdar://117219154
2023-10-20 10:33:48 +02:00
Kuba Mracek
db10e78eb2 [embedded] Start diagnosing metatype/value_metatype instructions in embedded Swift 2023-10-18 16:51:10 -07:00
Kuba (Brecka) Mracek
b257a02e69 Merge pull request #69081 from kubamracek/embedded-existential-diag
[embedded] Improve the diagnostic message when using an existential
2023-10-14 18:17:06 -07:00
Erik Eckstein
e2a268ed70 PerformanceDiagnostics: fix handling of infinite loops and change error handling
* Don't exclude code which end up in an infinite loop. rdar://116705459
* Don't exclude error handling code (throw, catch). Errors are existentials and will always allocate. Once we have typed throws it will be possible to do error handling without allocations.
2023-10-11 12:02:51 +02:00
Kuba Mracek
d5b1abb726 [embedded] Improve the diagnostic message when using an existential 2023-10-09 16:20:08 -07:00
Kuba Mracek
7c5962b8a7 [embedded] Prefer Module.getOptions().EmbeddedSwift in SIL code 2023-09-27 09:04:02 -07:00
zoecarver
2d61de8364 [embedded] Introduce RuntimeEffect::Existential and guard diagnostics on embedded feature being enabled. 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
Erik Eckstein
1603035927 PerformanceDiagnostics: fix handling of nested closures
Need to handle `mark_dependence` instruction in the use-def walk for the closure value.

Fixes a false performance error.

rdar://114008787
2023-08-18 09:08:04 +02:00
zoecarver
f6b1775f2c [nfc][opt] Add more stack traces and make them more specific. 2023-07-17 12:56:21 -07:00
zoecarver
a58cc2f0a5 [nfc][opts] Add a few pretty-stack traces to performance diagnostics.
It can be very hard to debug (from a compiler engineers perspective) why the perforamnce diagnostics aren't allowing a certain pattern. Usually this means adding a bunch of random SIL dumps to debug. With these stack traces, you can now just add `-Xllvm -swift-diagnostics-assert-on-error=1` and the stack dumps will do the rest.
2023-07-17 12:25: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
Kuba (Brecka) Mracek
d427696bf9 Allow @_silgen_name to be used on globals and add a @_silgen_name(raw: ...) version that skips mangling (#66540)
Attribute @_silgen_name is today only allowed to be used on functions, this change allows usage on globals as well. The motivation for that is to be able to "forward declare" globals just like it's today possible to do with functions (for the cases where it's not practical or convenient to use a bridging header).

Separately, this change also adds a @_silgen_name(raw: ...) syntax, which simply avoids mangling the name (by using the \01 name prefix that LLVM uses). The motivation for that is to be able to reference the "magic Darwin linker symbols" that can be used to look up section bounds (in the current dylib/module) -- those symbols don't use the underscore prefix in their mangled names.
2023-06-29 08:37:51 -07:00
Erik Eckstein
bfb5d21312 PerformanceDiagnostics: fix two small issues which result in false alarms
* Look through `begin_borrow` when analyzing closure values
* Treat non-escaping closures as trivial values when passed to a `partial_apply`

rdar://111046264
2023-06-21 12:46:39 +02:00
Erik Eckstein
fe87b99e4d PerformanceDiagnostics: when checking closure values, look through convert_function 2023-06-15 21:42:01 +02:00
Kuba (Brecka) Mracek
2d5f33e2e3 Add @_used and @_section attributes for global variables and top-level functions (#65901)
* Add @_used and @_section attributes for global variables and top-level functions

This adds:
- @_used attribute that flags as a global variable or a top-level function as
  "do not dead-strip" via llvm.used, roughly the equivalent of
  __attribute__((used)) in C/C++.
- @_section("...") attribute that places a global variable or a top-level
  function into a section with that name, roughly the equivalent of
  __attribute__((section("..."))) in C/C++.
2023-05-26 14:02:32 -07:00
Erik Eckstein
85052a1489 deprecate the -experimental-performance-annotations option
Performance annotations can now be used without specifying this option
2023-05-11 08:03:19 +02:00
Erik Eckstein
8ea848a937 PerformanceDiagnostics: fix missing diagnostics 2023-05-11 08:03:19 +02:00
Joe Groff
69e4b95fb8 SIL: Model noescape partial_applys with ownership in OSSA.
Although nonescaping closures are representationally trivial pointers to their
on-stack context, it is useful to model them as borrowing their captures, which
allows for checking correct use of move-only values across the closure, and
lets us model the lifetime dependence between a closure and its captures without
an ad-hoc web of `mark_dependence` instructions.

During ownership elimination, We eliminate copy/destroy_value instructions and
end the partial_apply's lifetime with an explicit dealloc_stack as before,
for compatibility with existing IRGen and non-OSSA aware passes.
2023-02-16 21:43:53 -08:00
Erik Eckstein
f9130065f8 PerformanceDiagnostics: handle closures
Check if no-escaping closures meet the performance constraints.
Do this already when passing a closure to a function.

rdar://94729207
2022-07-07 08:35:04 +02:00
Erik Eckstein
481381b72e PerformanceDiagnostics: correctly handle initializers of global variables
So far, initializers of global variables were ignored.
The fix is to visit the called initializer of the builtin `once`.

rdar://94780620
2022-07-07 08:35:04 +02:00
Erik Eckstein
45c8c5f54d PerformanceDiagnostics: fix a crash when emitting a module
rdar://93439187
2022-05-27 19:03:55 +02:00
Erik Eckstein
88219da06c PerformanceDiagnostics: don't issue a metatype diagnostics when using MemoryLayout
The metatype is not code-gend for the memory layout builtins.
Also fix the wrong help test for the -experimental-performance-annotations option.
2022-02-10 16:46:09 +01:00
Erik Eckstein
9f8b155c6c PerformanceDiagnostics: a pass to print errors for performance violations in annotated functions.
The PerformanceDiagnostics pass issues performance diagnostics for functions which are annotated with performance annotations, like @_noLocks, @_noAllocation.
This is done recursively for all functions which are called from performance-annotated functions.

rdar://83882635
2021-10-28 18:44:46 +02:00