Commit Graph

1084 Commits

Author SHA1 Message Date
Michael Gottesman
05511c955a [sil] Add conformance to UnaryInstruction for IgnoredUseInst. 2025-01-23 10:58:45 -08:00
Michael Gottesman
7ae56aab2e [sil] Add a new instruction ignored_use.
This is used for synthetic uses like _ = x that do not act as a true use but
instead only suppress unused variable warnings. This patch just adds the
instruction.

Eventually, we can use it to move the unused variable warning from Sema to SIL
slimmming the type checker down a little bit... but for now I am using it so
that other diagnostic passes can have a SIL instruction (with SIL location) so
that we can emit diagnostics on code like _ = x. Today we just do not emit
anything at all for that case so a diagnostic SIL pass would not see any
instruction that it could emit a diagnostic upon. In the next patch of this
series, I am going to add SILGen support to do that.
2025-01-22 21:12:36 -08:00
eeckstein
2af03dd177 Merge pull request #78768 from eeckstein/fix-borrowed-from-updater
SIL: when updating borrowed-from instructions, remove the old enclosing values first
2025-01-22 18:40:26 +01:00
Andrew Trick
5066dd9f90 Merge pull request #78716 from atrick/fix-lifedep-out
Fix LifetimeDependenceDiagnostics for @out dependencies
2025-01-22 00:17:50 -08:00
Erik Eckstein
5dc67fab3f SIL Verifier: check that the enclosing values of a borrowed-from instructions are valid 2025-01-21 08:21:26 +01:00
Erik Eckstein
012aa6227d SIL: when updating borrowed-from instructions, remove the old enclosing values first
When an optimization updates borrowed-from instruction it might be necessary to remove the old enclosing values from the borrowed-from instructions.
An optimization might transform the SIL in a way that an existing enclosing value is not valid anymore.
Fixes a compiler crash:
rdar://142991910
2025-01-21 08:21:26 +01:00
Erik Eckstein
97db5d8a80 AliasAnalysis: handle global let-variables in ImmutableScope
Global let-variables are immutable, except in functions which initialize them.
This brings back handling of global let-variables in alias analysis, which was removed in the previous commit.
2025-01-20 08:50:56 +01:00
Erik Eckstein
a312732f84 AliasAnalysis: consider memory effects of a consume/destroy of a class on it's let-fields
Although a let-field can never be mutated, a release or consume of the class must be considered as writing to such a field.

This change removes the special handling of let-fields in two places, where they don't belong.
Class fields are handled by ImmutableScope anyway.
Handling of global let-variable is temporarily removed by this commit.

Fixes a miscompile.
rdar://142996449
2025-01-20 08:50:56 +01:00
Andrew Trick
cc07b4ba11 Fix LifetimeDependenceDiagnostics for @out dependencies.
Record a forwarding mark_dependence as a local access. This is necessary because
we now emit a mark_dependence for @out arguments, which will be the starting
point for diagnostics:

  %out = alloc_stack
  apply %f(%owned, %out) : $(Owner) -> @lifetime(borrow 0) @out View
  %unused = mark_dependence [unresolved] %out on %owner
  %dependentValue = load %out

This mark_dependence has no uses. Instead, it simply records the dependency of
the in-memory value on the owner. Consequently, simply walking the uses of
LifetimeDependence.dependentValue does fails to diagnose any escapes. Instead,
if the dependentValue is an address-type mark_dependence, treat it as a local
access to the address that it forwards. Then we find any reachable uses of that
local variable as a potential escape.

Fixes rdar://143040479
(Borrow diagnostics not triggered for @out return values)
2025-01-17 11:19:04 -08:00
Andrew Trick
afda6453a5 Improve LocalVariableAccessInfo: recognize more full-assignments.
Ignore marker instructions for the purpose of determining whether a store is a
full assignment:

  %a = alloc_stack
  %m = moveonlywrapper_to_copyable_addr %a
  store %0 to [init] %m // <=== full assignemt
2025-01-17 11:19:04 -08:00
Andrew Trick
c839e265df [NFC] Update an incomplete comment. 2025-01-15 16:53:03 -08:00
Andrew Trick
c8bec28e6a LifetimeDependence.Scope: recognize @in_guaranteed dependencies.
Unlike @in, treat @in_guaranteed like a caller-side dependence
scope because there is not need to look for the end of the lifetime in the
current function.

Completely fixes rdar://142847915 (Crash during lifetime checking
while building new swift standard library `Span`-related features)
2025-01-14 15:38:54 -08:00
Andrew Trick
a8caedfbec LifetimeDependenceInsertion: fix dependence on store_borrow.
For a lifetime dependent call that depends on a temporary store_borrow, the
generated mark_dependendence should be on the stored value, not the stack
location.

  %temp = alloc_stack $AnyObject
  %sb = store_borrow %arg to %temp
  apply %10(%out, %sb)
  mark_dependence [unresolved] %out on %arg
  end_borrow %sb

Fixes rdar://142847915 (Crash during lifetime checking while
building new swift standard library `Span`-related features)
2025-01-14 00:11:17 -08:00
Andrew Trick
6849b66bb3 LifetimeDependenceDiagnostics; remove a bootstrapping hack.
This temporary hack was preventing diagnostics from kicking in. This could even
result in invalid SIL after the diagnostic failed to trigger.
2025-01-13 08:30:41 -08:00
Andrew Trick
0326c98f72 LifetimeDependenceScopeFixup: handle unsafe addressors
Handle unsafe addressors just like projections of stored properties rather than
as separate variable accesses.
2025-01-11 15:40:22 -08:00
Andrew Trick
b0f2ca03a7 AccessUtils: allow mark_deps to be tracked by the EnclosingScope.
This encourages AccessPathWalker clients to handle enclosing mark_deps. In
some cases, it is necessary. The accessBaseWithScopes API now provides both
nested begin_access and mark_dependence.
2025-01-11 15:40:22 -08:00
eeckstein
8ec8a1229a Merge pull request #78543 from eeckstein/fix-copy-to-borrow-optimization
Fix two problems in the copy-to-borrow optimization
2025-01-10 19:44:00 +01:00
Erik Eckstein
e215ae990b CopyToBorrowOptimization: correctly handle unchecked_enum_data
A `unchecked_enum_data` which extracts a trivial payload out of a non-trivial enum ends the lifetime of its owned operand.

Fixes an ownership verification error
rdar://142644731
2025-01-10 09:08:31 +01:00
Erik Eckstein
a4c675ec88 CopyToBorrowOptimization: fix insertion order of end_borrows
In liferange exit blocks the end_borrows were inserted in the wrong order.

Fixes an ownership verification error
rdar://142632741
2025-01-10 08:56:42 +01:00
Erik Eckstein
df62235fe6 SIL Verifier: avoid crashing for a missing borrowed-from - instead report an error 2025-01-09 19:56:25 +01:00
Andrew Trick
195ca7ceae Improve LifetimeDependence.Scope for mutable trivial variables.
Recognize dependence on the address of a trivial 'var' as an "access" dependence
instead of an "unknown" dependence. This allows the mark_dependence to be
resolved as "[nonescaping]".
2025-01-07 22:21:15 -08:00
Andrew Trick
49136f237f Fix LifetimeDependenceScopeFixup to avoid rewriting mark_dependence.
This pass rewrites mark_depenendence to ignore "useless" borrow scopes. It was
also accidentally rewriting a dependence on a loaded value, which may redirect the
dependence to the access scope used to load that value. That access scope may be
narrower than the lifetime of the loaded value which could result in invalid
SIL. Do not rewrite this mark_dependence:

  %access = begin_access [read] [unknown] %base
  %load = load [trivial] %access
  end_access %access
  %adr = pointer_to_address
  %md = mark_dependence [unresolved] %adr on %load

Fixes rdar://142424000 (Swift compiler crashes with Assertion failed
(isa<UnreachableInst>(block->getTerminator())))
2025-01-07 15:36:52 -08:00
eeckstein
f096787871 Merge pull request #78384 from eeckstein/enable-code-motion-in-ossa
Optimizer: enable SILCodeMotion in OSSA, but only for trivial values.
2025-01-02 20:36:29 +01:00
Erik Eckstein
c58f04f165 PhiUpdater: fix a problem when incrementally updating borrowed-from instructions
When replacing all uses, ignore existing borrowed-from instructions.

Fixes a SIL verifier error.
2025-01-02 10:42:00 +01:00
Erik Eckstein
9aff288be4 Optimizer: re-implement the pointer_to_address SILCombine peephole optimizations in swift
Which consists of
* removing redundant `address_to_pointer`-`pointer_to_address` pairs
* optimize `index_raw_pointer` of a manually computed stride to `index_addr`
* remove or increase the alignment based on a "assumeAlignment" builtin

This is a big code cleanup but also has some functional differences for the `address_to_pointer`-`pointer_to_address` pair removal:

* It's not done if the resulting SIL would result in a (detectable) use-after-dealloc_stack memory lifetime failure.
* It's not done if `copy_value`s must be inserted or borrow-scopes must be extended to comply with ownership rules (this was the task of the OwnershipRAUWHelper).

Inserting copies is bad anyway.
Extending borrow-scopes would only be required if the original lifetime of the pointer extends a borrow scope - which shouldn't happen in save code. Therefore this is a very rare case which is not worth handling.
2024-12-21 08:28:22 +01:00
Erik Eckstein
51c6a60d9d SIL Verifier: check that an instruction with a guaranteed result is either a BeginBorrowValue or a ForwardingInstruction 2024-12-21 08:28:21 +01:00
Erik Eckstein
c5b14c2b93 BorrowUtils: make unchecked_ownership_conversion to guaranteed ownership a BeginBorrowValue
BeginBorrowValue needs to handle all cases where a guaranteed value is produced.
Fixes a compiler crash.
2024-12-21 08:28:21 +01:00
Erik Eckstein
43b6fd4edb SIL: add the IndexingInstruction protocol
It defines (and implements) the `base` and `index` properties, which are used in the conforming classes `IndexRawPointerInst`, `IndexAddrInst` and `TailAddrInst`
2024-12-21 08:28:21 +01:00
Erik Eckstein
969a788e7f SIL: add the Builtin.name getter and Builtin.arguments 2024-12-21 08:28:21 +01:00
Erik Eckstein
1856d4e94c SIL: add APIs to set and get the alignment of a pointer_to_address instruction.
Also add a getter for the `isInvariant` property.
2024-12-21 08:28:21 +01:00
Erik Eckstein
2d10da001a SIL: add some Builder-create functions 2024-12-21 08:28:20 +01:00
Ben Barham
8111fe9343 Merge pull request #78262 from bnbarham/skip-non-wmo-diag
[Embedded] Do not produce `cannot_specialize_class` for live issues
2024-12-20 10:40:07 -08:00
Erik Eckstein
f8e013a21c Fix a compiler warning in SIL/Function.swift 2024-12-20 09:10:17 +01: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
Erik Eckstein
89353506f5 SimplifyBuiltin: support metatype and function types when optimizing Builtin.sizeof/alignof/strideof/destroyArray
Get the maximally abstracted lowered type instead of bailing if the ast type isn't equal to the lowered SIL type.
2024-12-19 20:53:46 +01:00
Erik Eckstein
4fdf16de1b SIL: add CanonicalType.canBeClass
And move the implementation of `SIL.Type.canBeClass` to the AST Type. The SIL Type just calls the AST Type implementation.
Also rename `SIL.Type.canonicalASTType` -> `SIL.Type.astType`.
2024-12-19 20:53:46 +01:00
Erik Eckstein
cd7c533d42 Optimizer: add SingleValueInstruction.replace(with:) and use it throughout the optimizer
It replaces all uses and then erases the instruction.
2024-12-19 20:53:45 +01:00
Erik Eckstein
ca7facde35 SIL: add some use-list APIs
* `users`: maps from a sequence of operands to a sequence of instructions
* `users(ofType:)` : as `users`, but filters users of a certain instruction type
* `Value.users`: = `Value.uses.users`
2024-12-19 20:53:45 +01:00
Erik Eckstein
1545e01ab5 SIL: Let SubstitutionMap.replacementTypes return AST types rather than optional SIL types.
This is what the C++ SubstitutionMap does. One has to use `Type.loweredType` to get from the AST type to the SIL type.
2024-12-19 20:53:45 +01:00
Erik Eckstein
1f4332ad39 SIL: add AST.Type.loweredType and CanonicalType.loweredType 2024-12-19 20:53:45 +01:00
Erik Eckstein
50bf66e217 SIL: replace CanonicalType.objectType and CanonicalType.addressType with silType
`SIL.Type` itself has `objectType` and `addressType` to convert between both variants.
2024-12-19 20:53:45 +01:00
Hamish Knight
95ae8d1b16 Merge pull request #78288 from hamishknight/use-typename
[SwiftCompilerSources] Use interpolation instead of `String(describing:)`
2024-12-19 19:26:50 +00:00
Hamish Knight
f728466273 [SwiftCompilerSources] Use interpolation instead of String(describing:)
`String(describing:)` does a bunch of dynamic casts
that can be pretty slow. Use interpolation instead,
which bypasses them.

For `swift-frontend`, this brings the time taken
for type-checking an empty file down from ~100ms
to ~70ms.

For `swift build`, this brings the time taken for
a null build down from ~600ms to ~450ms (the
larger delta is presumably due to the fact that
there's much more Swift code in `swift-package`).
2024-12-19 15:33:39 +00:00
Andrew Trick
1f6a74449d [lifetime] disable trivial dependence enforcement in .swiftinterface
Briefly, some versions of Span in the standard library violated trivial
lifetimes; versions of the compiler built at that time simply ignored
dependencies on trivial values. For now, disable trivial dependencies to allow
newer compilers to build against those older standard libraries. This check is
only relevant for ~6 mo (until July 2025).
2024-12-18 16:57:16 -08:00
Andrew Trick
7a11d5a3f1 SwiftCompilerSources: bridge Function.sourceFileKind. 2024-12-18 16:56:45 -08:00
Andrew Trick
30d81c9e97 Update a comment; an artifact of merging. 2024-12-17 09:53:25 -08:00
Andrew Trick
3897929e4a LifetimeDependence: handle dependence on trivial values. 2024-12-16 16:09:37 -08:00
Andrew Trick
9f74282680 Redesign LifetimeDependenceScopeFixup to handle accessors.
Handle all combinations of nested dependence scopes: access scopes, coroutines,
and borrow scopes.

This is required to enforce ~Escapable _read accessors and unsafeAddress addressors.

Fixes rdar://140424699 (Invalid SIL is generated by some passes for certain
@lifetime annotations)
2024-12-16 15:28:26 -08:00
Andrew Trick
01279c4a07 Redesign LifetimeDependenceInsertion for coroutines
Generalize the design. Extend it to handle different kinds of coroutine
dependencies: address/value, scoped/inherited.
2024-12-16 15:28:25 -08:00
Andrew Trick
dc4e4665f9 AddressUtils: handle address initialization inside _modify.
We can assume that memory is already initialized at the point of a 'yield'; a
yield use does not need to invalidate the single-initialization property for
temporary stack allocations.
2024-12-16 15:28:00 -08:00