Commit Graph

2441 Commits

Author SHA1 Message Date
Hamish Knight
fd97268393 Merge pull request #70983 from hamishknight/another-cleanup
[AST] Remove SerializedLocalDeclContext
2024-01-22 18:49:42 +00:00
Nate Chandler
48e19dcbb6 [NFC] TypeLowering: Strengthen assertion.
Now that verification bails out on the top level in the face of types
with type parameters, there's no need to bail out when visiting leaves.
2024-01-19 16:13:45 -08:00
Joe Groff
960938f87e SILGen: Always match noncopyable values by borrowing.
Even if the final pattern ends up consuming the value, the match itself
must be nondestructive, because any match condition could fail and cause
us to have to go back to the original aggregate. For copyable values,
we can always copy our way out of consuming operations, but we don't
have that luxury for noncopyable types, so the entire match operation
has to be done as a borrow.

For address-only enums, this requires codifying part of our tag layout
algorithm in SIL, namely that an address-only enum will never use
spare bits or other overlapping storage for the enum tag. This allows
us to assume that `unchecked_take_enum_data_addr` is safely non-side-
effecting and match an address-only noncopyable enum as a borrow.
I put TODOs to remove defensive copies from various parts of our
copyable enum codegen, as well as to have the instruction report
its memory behavior as `None` when the projection is nondestructive,
but this disturbs SILGen for existing code in ways SIL passes aren't
yet ready for, so I'll leave those as is for now.

This patch is enough to get simple examples of noncopyable enum switches
to SILGen correctly. Additional work is necessary to stage in the binding
step of the pattern match; for a consuming switch, we'll need to end
the borrow(s) and then reproject the matched components so we can
consume them moving them into the owned bindings. The move-only checker
also needs to be updated because it currently always tries to convert
a switch into a consuming operation.
2024-01-19 13:59:04 -08:00
Michael Gottesman
9513d298ec [region-isolation] Add parsing/serialization/type system support for a transferring OwnershipSpecifier. 2024-01-18 13:20:28 -08:00
Hamish Knight
3f4b45b012 [AST] Remove SerializedLocalDeclContext
It's not clear that its worth keeping this as a
base class for SerializedAbstractClosure and
SerializedTopLevelCodeDecl, most clients are
interested in the concrete kinds, not only whether
the context is serialized.
2024-01-18 12:03:52 +00:00
Slava Pestov
485135bb59 SIL: Don't call checkConformance() on an interface type 2024-01-17 21:15:39 -05:00
Slava Pestov
2246dd13e1 Merge pull request #70951 from slavapestov/sendable-type-and-more
Fix @retroactive for conditional conformances, and more
2024-01-17 18:28:45 -05:00
Felipe de Azevedo Piovezan
d0344e5071 Merge pull request #70882 from felipepiovezan/felipe/fix_getscopeoffirstnonmeta
[DebugInfo] Fix getScopeOfFirstNonMetaInstruction
2024-01-17 08:21:39 -08:00
Slava Pestov
a7f484b3a4 AST: Clean up isSendableType() 2024-01-16 22:44:43 -05:00
Slava Pestov
14d1fcb51a AST: TypeChecker::conformsToProtocol() => ModuleDecl::checkConformance() 2024-01-16 17:08:00 -05:00
nate-chandler
7dd1d3f7c1 Merge pull request #70872 from nate-chandler/rdar19519745
[BitwiseCopyable] Infer and check constraint.
2024-01-16 07:01:31 -08:00
Nate Chandler
bac9e94a1d [BitwiseCopyable] Infer and check constraint.
When the BitwiseCopyable experimental feature is enabled, infer types to
conform to `_BitwiseCopyable`.  The `_BitwiseCopyable` inference broadly
follows the approach taken to infer `Sendable`.

(1) Special types are conformed:
- function types if trivial
- metatypes
- builtin types if trivial

(2) TheTupleType is conditionally conformed.

(3) Nominal types are conformed if:
- non-public or public+fixed-layout
- enum or struct (non-class)
- every field conforms to _BitwiseCopyable

Additionally, check that nominal types which are explicitly conformed to
`_BitwiseCopyable` satisfy the latter two conditions of (3).

For a public, non-fixed-layout type to conform to `_BitwiseCopyable`,
the user must conform the type explicitly.

Finally, verify that conformances correspond to TypeLowering's notion of
triviality to the appropriate extent:
- if a type isn't trivial, it doesn't conform to `_BitwiseCopyable`
  unless it's an archetype
- if a type is trivial, it conforms to `_BitwiseCopyable` unless some
  field in its layout doesn't conform to `_BitwiseCopyable`, which is
  only permitted under certain circumstances (the type has generic
  parameters, the type is public non-fixed-layout, the type is a
  reference but has ReferenceStorage::Unmanaged, the type is a
  ModuleType, etc.)
2024-01-15 17:08:32 -08:00
Doug Gregor
0e6a913f98 Merge pull request #70907 from DougGregor/se-0413-result-typed-throws
[SE-0413] Adopt typed throws in Result
2024-01-13 13:21:05 -08:00
Doug Gregor
d19f082665 Extend @_silgen_name to apply to allocating initializers, too
This enables its use on enum and struct initializers.
2024-01-13 06:29:27 -08:00
Nate Chandler
b98679bfb6 [NFC] TypeLowering: Extracted one field verify.
Moved verification of lexical field into its own function in preparation
for verifying a second field.
2024-01-12 21:05:41 -08:00
Felipe de Azevedo Piovezan
6ce2a6f90a [DebugInfo] Fix getScopeOfFirstNonMetaInstruction
This function is clearly returning the opposite of what its name says:

```
const SILDebugScope *SILBasicBlock::getScopeOfFirstNonMetaInstruction() {
  for (auto &Inst : *this)
    if (Inst.isMetaInstruction())
      return Inst.getDebugScope();
  return begin()->getDebugScope();
}
```

Looking at the PR history (sadly GH doesn't preserve old versions of the patch...)
https://github.com/apple/swift/pull/15575

There was this snippet of code:

```
// Find the correct debug scope for alloc stack. We want to give to the
// expanded sequence the correct debug scope so we skip over instructions
// that aren't lowered to anything real (e.g. debug_value).
static const SILDebugScope *findAllocStackDebugScope(SILBasicBlock &BB) {
  auto It = BB.begin();
  while (It != BB.end()) {
    if (!isMaintenanceInst(&*It))
```

We don't know what used to be after that line but, based on the comments, it
must have been a `return It->getDebugScope()`.

Adrian then asked the author to make this a different helper function
`getScopeOfFirstNonMetaInstruction`, and the subsequence force-push had the code
we see today. So maybe it was in this conversion that the author made the
mistake?

Fixing the implementation doesn't cause any tests to fail (sadly the original PR
did not add any SIL->SIL tests, which would have been ideal).
2024-01-12 14:47:38 -03:00
Allan Shortlidge
42aab7451b SIL: Remove swift3ImplicitObjCEntrypoint built-in.
With the removal of `-enable-swift3-objc-inference`, no calls to the
`swift3ImplicitObjCEntrypoint` built-in should be generated anymore.
2024-01-11 15:40:04 -08:00
Egor Zhdan
b138f223c2 Merge pull request #70044 from apple/egorzhdan/default-arguments
[cxx-interop] Support C++ default arguments
2024-01-11 00:57:35 +00:00
Slava Pestov
25789fd767 Merge pull request #70768 from slavapestov/fix-119822466
SIL: Fix DynamicSelfType references from inside init accessor
2024-01-10 13:40:46 -05:00
Egor Zhdan
494474b021 [cxx-interop] Support C++ default arguments
This allows calling a C++ function with default arguments from Swift without having to explicitly specify the values of all arguments.

rdar://103975014
2024-01-10 16:37:42 +00:00
Erik Eckstein
fa3a959524 ObjectOutliner: support outlining of classes in global let variables
```
  let c = SomeClass()
```

is turned into

```
  private let outlinedVariable = SomeClass()  // statically initialized and allocated in the data section
  let c = outlinedVariable
```

rdar://111021230
rdar://115502043

Also, make the ObjectOutliner work for OSSA. Though, it currently doesn't run in the OSSA pipeline.
2024-01-10 09:34:01 +01:00
Erik Eckstein
bc99986cf9 SIL: add a dependency token operand to global_addr
Optionally, the dependency to the initialization of the global can be specified with a dependency token `depends_on <token>`.
This is usually a `builtin "once"` which calls the initializer for the global variable.
2024-01-10 09:33:58 +01:00
Becca Royal-Gordon
33c7326365 Merge pull request #70731 from beccadax/dont-let-labels-define-you
Make @_cdecl @implementation support __asm__
2024-01-09 16:09:51 -08:00
Yuta Saito
7cccbcc84f [DiscardingTG] Remove reabstraction thunk for () -> Void to () -> T (#70537)
Concurrency runtime expects discarding task operation entrypoint
function not to have result type, but the current SILGen
implementation generates reabstraction thunk to convert `() -> Void`
to `() -> T` for the operation function.

Since the `T` is always `Void` for DiscardingTG, the mismatch of result
type expectation does not cause any problem on most platforms, but the
signature mismatch causes a problem on WebAssembly.

This patch introduces new builtin operations for creating discarding
task, which always takes `() -> Void` as the operation function type.
2024-01-10 07:17:15 +09:00
Slava Pestov
1a06313ac9 SIL: Fix DynamicSelfType references from inside init accessor
We need the self metatype parameter to correctly lower
DynamicSelfType in IRGen, so plumb this through to all
calls of init accessors, and inside the prolog of an
init accessor definition.

This does not break the public ABI, because init
accessors are never public. Also for value types, the
metatype is thin, so it should not change generated
code.

For classes we need the metatype in the general case
because of `Self`, but hopefully in most cases the
init accessor can be inlined away and the value_metatype
instruction subject to dead code elimination.

Fixes rdar://problem/119822466.
2024-01-09 10:52:54 -05:00
Andrew Trick
119d40d99b Merge pull request #70705 from atrick/markdep-nonescaping
Add mark_dependence [nonescaping] flag.
2024-01-05 11:33:33 -08:00
nate-chandler
bb24b8c045 Merge pull request #70720 from nate-chandler/rdar119829826
[IRGen] Pack metadata may be allocated based on type layouts.
2024-01-04 19:31:47 -08:00
Becca Royal-Gordon
85ea884a64 Make @_cdecl @implementation support __asm__
When @_objcImplementation is used to implement a C function marked with __asm__, take the attribute into account when mangling SIL references.

In theory this change should also make `clang::OverloadableAttr` functions mangle correctly, but in practice the matching logic for @_cdecl @_objcImplementation doesn’t currently support overloadable functions (and it’s not wise to try anyway, since clang doesn’t promise that their ABI won’t change).

Fixes rdar://120503717.
2024-01-04 18:25:07 -08:00
Andrew Trick
264cbaea42 Add mark_dependence [nonescaping] flag.
The dependent 'value' may be marked 'nonescaping', which guarantees that the
lifetime dependence is statically enforceable. In this case, the compiler
must be able to follow all values forwarded from the dependent 'value', and
recognize all final (non-forwarded, non-escaping) use points. This implies
that `findPointerEscape` is false. A diagnostic pass checks that the
incoming SIL to verify that these use points are all initially within the
'base' lifetime. Regular 'mark_dependence' semantics ensure that
optimizations cannot violate the lifetime dependence after diagnostics.
2024-01-04 14:47:35 -08:00
Nate Chandler
73d68a3f28 [IRGen] Pack metadata may be alloc'd for layouts.
Previously, mayRequirePackMetadata only considered whether a type
involved a pack.  That failed to account for the case of outlined value
functions that require pack metadata when the type involves a pack in
its layout.  Here, mayRequirePackMetadata now considers also whether the
layout corresponding to a type involves a pack.

rdar://119829826
2024-01-04 12:21:47 -08:00
Nate Chandler
75b270791b [TypeLowering] Added hasPack field.
Record whether any sort of pack was seen when visiting the type.
2024-01-04 10:54:24 -08:00
Slava Pestov
47cab70ee3 Merge pull request #70692 from slavapestov/sil-type-linkage-fix
SIL: Remove the incorrect old getTypeLinkage()
2024-01-04 10:39:31 -05:00
Andrew Trick
861216cb38 Add release diagnostics for partial_apply [on_stack] uses. 2024-01-03 12:35:53 -08:00
Slava Pestov
79d4260ae2 SIL: Remove the incorrect old getTypeLinkage() 2024-01-03 14:45:29 -05:00
Andrew Trick
a4070e627c Prevent partial_apply cloning.
partial_apply cannot be cloned, even in OSSA. OSSA lowering does
not know how to allocate for multiple partial applies.

Fixes rdar://119768691 (OwnershipModelEliminator triggers assertion
when lowering certain [on_stack] partial_applys in certain
circumstances)
2024-01-03 10:31:01 -08:00
Michael Gottesman
6d00750ad0 Merge pull request #70653 from gottesmm/transferring
[region-isolation] Initial refactoring work for supporting Transferring Args and Results
2024-01-02 18:51:44 -08:00
Slava Pestov
8e2aa52f7a Merge pull request #70667 from slavapestov/assorted-small-cleanups
Assorted small cleanups
2024-01-02 19:00:59 -05:00
Michael Gottesman
1764bd2479 [sil] Change SILParameterInfo/SILResultInfo's differentiability parameter to use an OptionSet so we can add other options.
I am doing this in preparation for adding options to SILParameterInfo/
SILResultInfo that state that a parameter/result is transferring. Even though I
could have just introduced a new bit here, I instead streamlined the interface
of SILParameterInfo/SILResultInfo to use an OptionSet instead of individual bits
to make it easier to add new flags here. The reason why it is easier is that
along API (e.x.: function argument) boundaries one does not have to marshal each
field or pass each field. Instead one can just pass the whole OptionSet as an
opaque thing. Using this I was able to change serialization/deserialization of
SILParameterInfo/SILResultInfo so that one does not need to update them if one
adds new fields!

The reason why I am doing this for both SILParameterInfo/SILResultInfo in the
same commit is because they share code in the demangler that I did not want to
have to duplicate in an intervening commit. By changing them both at the same
type, I didn't have to change anything without an actual need to.

I am doing this in a separate commit from adding transferring support so I can
validate correctness using the tests for the options already supported
(currently only differentiability).
2024-01-02 15:03:05 -08:00
Slava Pestov
47ca44c4a8 SIL: Fix printing of opaque parameter types
While printing them as `some P` makes sense in the AST since they
only ever appear at their definition point, in the body of a SIL
function, opaque parameter types can be referenced by various
instructions, like any other generic parameter type.

Instead of printing out `some P` or `<anonymous>` depending on
context, neither of which actually parsed, instead print them
with the canonical type `τ_d_i` notation. Since it's printed this
way in the generic parameter list as well, it parses back in.

Fixes rdar://problem/119823811.
2024-01-02 15:41:13 -05:00
Slava Pestov
88c76aa185 SIL: Correctly form CanPackExpansionType 2024-01-02 15:41:13 -05:00
Slava Pestov
05dcc4b27c SIL: Don't need a GenericEnvironment in isUnsupportedKeyPathValueType() 2024-01-02 15:41:13 -05:00
Meghana Gupta
ba4cedd5e5 Merge pull request #70582 from meg-gupta/resultdependsoninsil
Make _resultDependsOn/_resultDependsOnSelf available in SIL
2024-01-02 10:49:20 -08:00
Doug Gregor
06a9e13fd1 Merge pull request #70633 from DougGregor/sil-witness-lowering-thrown-error 2024-01-02 08:35:05 -08:00
Meghana Gupta
1a1a8ec679 Make _resultDependsOnSelf available on SILFunction 2023-12-29 02:00:21 -08:00
Meghana Gupta
d3e921dc47 Make _resultDependsOn available on SILFunctionArgument 2023-12-29 01:57:19 -08:00
Doug Gregor
ad75dae2ba [SIL] Fix thrown error computation for lowering of witnesses 2023-12-26 21:25:27 -08:00
Kavon Farvardin
dfe3d2a42c Merge pull request #70488 from kavon/ncgenerics-stdlib-building-v2
[NCGenerics] Fix more issues while building the stdlib
2023-12-19 10:19:25 -08:00
Kavon Farvardin
34f359d8d0 [NCGenerics] fix TypeLowering 2023-12-18 18:55:56 -08:00
Kavon Farvardin
d80719827c [NCGenerics] fix missing contextual type 2023-12-18 18:55:56 -08:00
Allan Shortlidge
d16d9799f2 SIL: Allow package decls to be serialized when appropriate.
https://github.com/apple/swift/pull/70100 prohibited `package` declarations from
ever being serialized in order to solve a problem in which the declarations
were being serialized inappropriately. That's too heavy handed, though, because
an `@_alwaysEmitIntoClient` function with `package` access *must* be serialized
because it has public non-abi linkage.

Resolves rdar://104711625
2023-12-18 15:06:19 -08:00