Commit Graph

47 Commits

Author SHA1 Message Date
Slava Pestov
03de964154 AST: Change RequirementEnvironment::getRequirementToWitnessThunkSubs() to use contextual types
In the provided test case, the generic signature of the
protocol requirement is

    <Self, T where Self == T.A, T: P2>

The conformance requirement `Self: P1` is derived from `T: P2`
and `Self.A: P1` in P1. So given a substitution map
`{ Self := X, T := T }` that replaces T with a concrete type X
and T with a type parameter T, there are two ways to recover a
substituted conformance for `Self: P1`:

- We can apply the substitution map to Self to get X, and look
  up conformance of X to P1, to get a concrete conformance.

- We can evaluate the conformance path `(T: P2)(Self.A: P1)`,
  to get an abstract conformance.

Both answers are correct, but SILGenModule::emitProtocolWitness()
was assuming it would always get a concrete conformance back.

This was the case until e3c8f423bc,
but then we started returning an abstract conformance. SILGen
would then mangle the protocol witness thunk in a way that was
not sufficiently unique, and as a result, we could miscompile
a program where two witness tables both hit this same scenario.

By using contextual types in the getRequirementToWitnessThunkSubs()
substitution map, we ensure that evaluating the conformance path
against the substitution map produces the same result as performing
the global lookup.

Also, to prevent this from happening again, add a check to SILGen
to guard against emitting two protocol witness thunks with the
same mangled name.

Unfortunately, this is done intentionally as part of some
backward deployment logic for coroutine accessors. There is a
hack to allow duplicate thunks with the same name in this case,
but this should be revisited some day.

Fixes rdar://problem/155624135..
2025-07-11 13:31:35 -04:00
Erik Eckstein
7cceaff5f3 SIL: don't print operand types in textual SIL
Type annotations for instruction operands are omitted, e.g.

```
  %3 = struct $S(%1, %2)
```

Operand types are redundant anyway and were only used for sanity checking in the SIL parser.

But: operand types _are_ printed if the definition of the operand value was not printed yet.
This happens:

* if the block with the definition appears after the block where the operand's instruction is located

* if a block or instruction is printed in isolation, e.g. in a debugger

The old behavior can be restored with `-Xllvm -sil-print-types`.
This option is added to many existing test files which check for operand types in their check-lines.
2024-11-21 18:49:52 +01:00
Slava Pestov
bb97eaacf2 SILGen: Fix emission of 'modify' witness when all generic params are concrete
Fixes https://github.com/swiftlang/swift/issues/74648.
2024-06-24 17:38:59 -04:00
Slava Pestov
e584195f44 SIL: Use the right generic signature for computing SIL function type for coroutine accessors
The function type's generic signature will have already been dropped
at this point if all generic parameters were fully concrete, so use
the original generic signature to compute the reduced type of the
yield type.

Fixes https://github.com/apple/swift/issues/61040.
2022-09-19 18:11:32 -04:00
Slava Pestov
c9f550c60f Parse: Parse an explicit 'nil' default argument as DefaultArgumentKind::NilLiteral
We support a special DefaultArgumentKind::NilLiteral, but it was only
used for synthesized and imported default arguments as well.

Let's also use it for parsed default arguments that are exactly 'nil',
so that rethrows checking can special-case them.
2021-04-02 23:56:59 -04:00
Slava Pestov
85b94a4300 SILGen: Refactor emitMemberInitializers() to use Initialization
Instead of constructing an LValue and storing the result of the
stored property initializer to the lvalue, let's emit the call
of the stored property initializer directly into an initialization
pointing at the stored properties named by the pattern.
2020-10-11 12:48:30 -04:00
Joe Groff
77b012af1d SIL: Add an [exact_self_class] function attribute.
This indicates that the "self" argument to the current function is always dynamically of the exact
static base class type, allowing metadata accesses in IRGen to use the local self metadata to answer
metadata requests for the class type. Set this attribute on allocating entry points of designated
inits, which is one of the most common places where we emit redundant metadata accesses.
2019-08-08 14:03:07 -07:00
Slava Pestov
c84553cc33 SIL: Fix crash when enum is nested inside a fully-constrained extension
Fixes <https://bugs.swift.org/browse/SR-10023>.
2019-03-01 21:01:51 -05:00
Michael Gottesman
40a09c9c21 Fixup tests for -assume-parsing-unqualified-ownership-sil => [ossa] transition. 2018-12-18 00:49:32 -08:00
Michael Gottesman
9e13779702 [ownership] Remove most -enable-sil-ownership from SILGen now that %target-swift-emit-silgen does it automatically.
I did this using a sed pattern and verified by hand that I was only touching
target-swift-emit-silgen lines.
2018-12-13 11:54:54 -08:00
Erik Eckstein
39bb14b094 change mangling prefix from $S to $s
This is the final ABI mangling prefix

rdar://problem/38471478
2018-09-19 13:55:11 -07:00
John McCall
b80618fc80 Replace materializeForSet with the modify coroutine.
Most of this patch is just removing special cases for materializeForSet
or other fairly mechanical replacements.  Unfortunately, the rest is
still a fairly big change, and not one that can be easily split apart
because of the quite reasonable reliance on metaprogramming throughout
the compiler.  And, of course, there are a bunch of test updates that
have to be sync'ed with the actual change to code-generation.

This is SR-7134.
2018-08-27 03:24:43 -04:00
Slava Pestov
5e5d59c377 Migrate SILGen tests to Swift 4 2018-06-22 01:55:33 -07:00
Slava Pestov
b7d0a7931f Merge pull request #17321 from slavapestov/swift4-default-tests
Run tests with -swift-version 4
2018-06-20 07:37:57 -07:00
Doug Gregor
a66df57aaf [Mangling] Update SILGen and IRGen tests for mangling change. 2018-06-19 23:24:38 -07:00
Doug Gregor
f232af5535 [Mangling] Allow standard substitutions in protocol mangling.
Protocol name mangling didn’t always go through a path that allowed the use
of standard substitutions. Enable standard substitutions for protocol name
manglings where they make sense.

Removes ~277k from the standard library binary size.
2018-06-19 23:24:38 -07:00
Slava Pestov
5d2752f7d2 Run tests with -swift-version 4 by default
Some test now fail, so add an explicit -swift-version 3.
2018-06-19 23:24:19 -07:00
Alex Hoppen
560c22b18e [tests] Verify the libSyntax tree on SILGen tests
The SILGen testsuite consists of valid Swift code covering most language
features. We use these tests to verify that no unknown nodes are in the
file's libSyntax tree. That way we will (hopefully) catch any future
changes or additions to the language which are not implemented in
libSyntax.
2018-04-27 09:33:03 -07:00
Michael Gottesman
cb80f65f1e Remove plus_zero_test,plus_one_test from lit tests since they are no longer needed.
I am going to leave in the infrastructure around this just in case. But there is
no reason to keep this in the tests themselves. I can always just revert this
and I don't think merge conflicts are likely due to previous work I did around
the tooling for this.
2018-03-21 20:49:52 -07:00
Michael Gottesman
e567bc9028 [+0-all-args] Enable +0 normal arguments.
rdar://34222540
2018-03-19 20:25:31 -07:00
Michael Gottesman
6f4e87ad3f [+0-all-args] Be explicit about the module-name for tests that have plus_zero_* counterparts.
Otherwise, the plus_zero_* tests will have plus_zero_* as a module name, causing
massive FileCheck problems.

The reason why I am doing it with the main tests is so that I can use it when
syncing branches/etc.

radar://34222540
2018-03-11 21:55:24 -07:00
Michael Gottesman
8dd5ea9b60 [+0-all-args] Add a space after REQUIRES: plus_one_runtime to eliminate avoidable merge conflicts when editing other parts of the file.
This helps my tooling for enabling +0.
2018-03-11 16:19:09 -07:00
Slava Pestov
760a9a76ff Merge pull request #15140 from slavapestov/materialize-for-set-fixes
Fixes for exotic materializeForSet
2018-03-10 14:53:31 -08:00
Slava Pestov
268acbc0db SIL: Use @in_guaranteed convention for 'self' parameter of materializeForSet callback of non-mutating setter
We cannot in general use @guaranteed here, otherwise classes will not
be able to conform to protocols with mutable property requirements
(or we could always open-code materializeForSet witness thunks for
classes, but that has its own downsides so its not a clear win).

Fixes <rdar://problem/36867783>.
2018-03-10 03:39:08 -08:00
Michael Gottesman
e6e55df5ea [+0-all-args] Mark all tests that will need updates for +0 as requiring a plus_one_runtime. 2018-03-10 02:37:51 -08:00
Erik Eckstein
cd3d50a5d9 ABI: Change the mangling prefix from _T0 to $S 2018-01-06 13:55:59 -08:00
Pavel Yaskevich
6519d99736 [Mangling/ABI] NFC: Fix SILGen tests to reflect label mangling changes 2017-12-18 15:44:24 -08:00
Doug Gregor
6e6b6b9837 [Mangler] Only mangle new requirements for constrained extensions.
Rather than mangling the complete generic signature of a constrained
extension, only mangle the requirements not already satisfied by the
nominal type. For example, given:

    extension Dictionary where Value: Equatable {
      // OLD: _T0s10DictionaryV2t3s8HashableRzs9EquatableR_r0_lE3baryyF
      // NEW: _T0s10DictionaryV2t3s9EquatableR_rlE3baryyF
      public func bar() { }
   }

In the existing mangling, we mangle the `Key: Hashable` requirement that’s
part of the generic signature. With this change, we only mangle the new
requirement (`Value: Equatable`).

This is a win for constrained extensions *except* in the case of a
constrained extension of a nominal type with a single, unconstrained
generic parameter:

    extension Array where Element: Equatable {
      // OLD: _T0Sa2t3s9EquatableRzlE3baryyF
      // NEW would be: _T0Sa2t3s9EquatableRzrlE3baryyF
      public func bar() { }
    }

Check explicily for this shortcut mangling and fall back to the old
path, so this change is a strict improvement.
2017-10-11 19:23:58 -07:00
Alex Hoppen
1c7e289b96 [Mangling] Adjust subscript mangling to not include "subscript"
Change the mangling of accessors to have a variable or subscript node
as their only child node, while subscript nodes no longer contain a decl
name.
2017-09-10 19:44:07 +02:00
Michael Gottesman
20c3e1e92f [semantic-sil] Update 45 SILGen tests for ownership.
Very roughly this increases the total coverage of SILGen tests with ownership
enabled to ~20%.

rdar://33358110
2017-08-20 19:11:55 -07:00
Huon Wilson
26e69df03f [SILGen] MaterializeForSet closures should never be serialized and private.
If the closure is serialized, it needs to be shared.
2017-07-24 14:03:38 -07:00
Huon Wilson
2c6e365862 [Test] Stop a test dumping multiple kLOCs of SIL/IR when it fails. 2017-06-26 13:55:24 -07:00
Slava Pestov
9c7afa7bdd SILGen: Fix issues with types nested inside fully-concrete extensions
Add a new version of TypeConverter::getEffectiveGenericSignature()
which takes a DeclContext, which uses the same logic as the
AnyFunctionRef version, and use it when computing lowered types
for destructors, stored property initializers and ivar destroyers.

Also, canonicalize the interface type of 'Self' as part of building
these types, to fold away concrete generic parameters.

Fixes <rdar://problem/32120987>.
2017-05-22 19:49:53 -07:00
Slava Pestov
2994656f4b SILGen: Fix crash when emitting reference to member initializer from constrained extension
When we SILGen a constructor defined in an extension, we need to
initialize stored properties with default values. These use the
generic signature of the original type, and not the generic
signature of the extension, which can add additional constraints.

Fixes <https://bugs.swift.org/browse/SR-4956>.
2017-05-22 18:48:25 -07:00
Huon Wilson
06fb015743 [SILGen] MaterializeForSet callbacks shouldn't be public. 2017-05-09 21:15:00 +10:00
Greg Lutz
ed483362e6 Addition of fix to test/SILGen/constrained_extensions.swift to pull request #9046 2017-05-01 22:04:26 -07:00
Slava Pestov
8fe8b89b0f SIL: Terminology change: [fragile] => [serialized]
Also, add a third [serializable] state for functions whose bodies we
*can* serialize, but only do so if they're referenced from another
serialized function.

This will be used for bodies synthesized for imported definitions,
such as init(rawValue:), etc, and various thunks, but for now this
change is NFC.
2017-03-29 16:47:28 -07:00
Slava Pestov
efff6a6922 SILGen: Fix a couple of problems with materializeForSet in constrained extensions
- Property defined in constrained extension of a class

- Subscript defined in a constrained extension with non-canonical index type

Fixes <rdar://problem/31222187>.
2017-03-27 00:18:26 -07:00
Erik Eckstein
c4a11f4c92 tests: remove the now unused option -new-mangling-for-tests 2017-03-22 11:28:43 -07:00
Erik Eckstein
a04a29af4f mangling: efficient mangling of repeated substitutions
Instead of appending a character for each substitution, we now prefix the substitution with the repeat count, e.g.
AbbbbB -> A5B

The same is done for known-type substitutions, e.g.
SiSiSi -> S3i

This significantly shrinks mangled names which contain large lists of the same type, like
  func foo(_ x: (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int))

rdar://problem/30707433
2017-03-05 17:41:43 -08:00
Slava Pestov
0af2845c6d SILGen: Emission of materializeForSet for generic subscripts
First, use the correct generic environment to compute the substituted
storage type. Substitutions derived from 'self' are not enough,
because we also want the archetypes of the generic subscript's
innermost generic parameters.

Also, use the method and witness_method calling conventions for the
materializeForSet callback, depending on if we have a protocol
witness or concrete implementation.

Since the materializeForSet callback is called with a more
abstract type at the call site than the actual function type
of the callback, we used to rely on these two SIL types being
ABI compatible:

@convention(thin) <Self : P, T, U) (..., Self.Type) -> ()
@convention(thin) <T, U> (..., Foo<T, U>.Type) -> ()

The IRGen lowering is roughly the following -- the call site
passes two unused parameters, but that's fine:

(..., Self.Type*, Self.Type*, Self.P*)
(..., Foo<T, U>.Type*)

However if the callback has its own generic parameters because
the subscript is generic, we might have SIL types like so,

@convention(thin) <Self : P, T, U, V) (..., Self.Type) -> ()
@convention(thin) <T, U, V> (..., Foo<T, U>.Type) -> ()

And the IRGen lowering is the following:

(..., Self.Type*, Self.Type*, Self.P*, V.Type*)
(..., Foo<T, U>.Type*, V.Type*)

The parameters no longer line up, because the caller still passes
the two discarded arguments, and type metadata for V cannot be
derived from the Self metadata so must be passed separately.

The witness_method calling convention is designed to solve this
problem; it puts the Self metadata and protocol conformance last,
so if you have these SIL types:

@convention(witness_method) <Self : P, T, U, V) (..., swiftself Self.Type) -> ()
@convention(witness_method) <T, U, V> (..., swiftself Foo<T, U>.Type) -> ()

The IRGen lowering is the following:

(..., Self.Type*, V.Type*, Self.Type*, Self.P*)
(..., Foo<T, U>.Type*, V.Type*, Self.Type*, unused i8*)

However, the problem is now that witness_method and thin functions
are not ABI compatible, because thin functions don't have a
distinguished 'self', which is passed differently in LLVM's swiftcc
calling convention:

@convention(witness_method) <Self : P, T, U, V) (..., Self.Type) -> ()
@convention(thin) <T, U, V> (..., Foo<T, U>.Type) -> ()

So instead of using 'thin' representation for the concrete callback
case, use 'method', which is essentially the same as 'thin' except if
the last parameter is pointer-size, it is passed as the 'self' value.

This makes everything work out.
2017-02-26 21:27:04 -08:00
Slava Pestov
19f54e1e8b SILGen: Clean up materializeForSet emission a bit
We can get the generic signature from the generic environment
now, and for generic subscript protocol witnesses, using the
signature of the conformance is wrong; it won't have the
generic parameters of the subscript itself.

Also, emit the materializeForSet callback in the right place in
SILModule. Instead of adding it at the end, put it before the
materializeForSet itself. This makes tests a bit easier to write.
2017-02-25 15:48:13 -08:00
Slava Pestov
33a8ce6e7b AST: Canonicalize types with respect to generic signature when mangling
In the following example, the two declarations should have
the same mangled type:

protocol P {
  associatedtype P
}

func f1<T : P>(_: T) -> T.P where T.P == Int {}
func f2<T : P>(_: T) -> Int where T.P == Int {}

To ensure this is the case, canonicalize the entire
GenericFunctionType before taking it apart, instead of
canonicalizing structural components of it.
2017-02-20 16:41:37 -08:00
Erik Eckstein
1d3724666f tests: convert about 400 tests to the new mangling by using the -new-mangling-for-tests option
When the new mangling is enabled permanently, the option can be removed from the RUN command lines again.
2017-01-24 15:27:45 -08:00
Slava Pestov
5ab94a6ef0 AST: Add "re-sugaring" to GenericEnvironment
Sugared GenericTypeParamTypes point to GenericTypeParamDecls,
allowing the name of the parameter as written by the user to be
recovered. Canonical GenericTypeParamTypes on the other hand
only store a depth and index, without referencing the original
declaration.

When printing SIL, we wish to output the original generic parameter
names, even though SIL only uses canonical types. Previously,
we used to accomplish this by mapping the generic parameter to an
archetype and printing the name of the archetype. This was not
adequate if multiple generic parameters mapped to the same
archetype, or if a generic parameter was mapped to a concrete type.

The new approach preserves the original sugared types in the
GenericEnvironment, adding a new GenericEnvironment::getSugaredType()
method.

There are also some other assorted simplifications made possible
by this.

Unfortunately this makes GenericEnvironments use a bit more memory,
however I have more improvements coming that will offset the gains,
in addition to making substitution lists smaller also.
2016-10-12 18:56:16 -07:00
Slava Pestov
5893657df5 SILGen: Allow concrete constrained extensions to define constructors
Make sure we go through the common code path for constructors,
like we do for methods and everything else, so that we correctly
drop the generic signature in the case where all generic
parameters were made concrete.

This fixes the example from <rdar://problem/17821143>.
2016-10-11 16:02:54 -07:00
Slava Pestov
8f88d19518 Sema: Allow extensions to make generic parameters concrete via same-type constraints
Fixes <https://bugs.swift.org/browse/SR-1009>.
2016-10-04 20:36:56 -04:00