Skip to content

SIL: Use a TypeExpansionContext in SIL lowering to expand opaque type archetypes as part of lowering #28044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

aschwaighofer
Copy link
Contributor

As part of SIL type lowering opaque archetypes should be expanded to
their underlying types depending on the context -- the function they are
in. This means there is only ever one SIL type for an opaque archetype
AST type inside a function body: either the original opaque archetype or
an underlying type substitution but not both.

This fixes ambiguities in type substitution that would be introduced by
specializing opaque archetypes.

protocol Q {
  associatedtype Assoc
  func bind() -> Assoc
}
struct S : Q {
  func bind() -> some P {
    return 0
  }
}

apply %func<some P, S>(...) : $@convention(method) <t_0_0><t_0_1 where t_0_0 == t_1_0.Assoc, t_1_0 : Q>

  specialization (some P => Int)

apply %func<Int, S>(...) : $@convention(method) <t_0_0><t_0_1 where t_0_0 == t_1_0.Assoc, t_1_0 : Q>

  error: Int != S.Assoc (some P)

rdar://52706501

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test source compat

@swift-ci
Copy link
Contributor

swift-ci commented Nov 3, 2019

Build failed
Swift Test Linux Platform
Git Sha - 47e15b4512dcb598022c8d4b4da62dd342956c50

@swift-ci
Copy link
Contributor

swift-ci commented Nov 3, 2019

Build failed
Swift Test OS X Platform
Git Sha - 47e15b4512dcb598022c8d4b4da62dd342956c50

@aschwaighofer aschwaighofer force-pushed the sil_type_expansion_context branch from 47e15b4 to 159f988 Compare November 4, 2019 15:42
@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci please test

@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci please test

1 similar comment
@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Nov 4, 2019

Build failed
Swift Test Linux Platform
Git Sha - 159f9886ba87126bf75f240bf16693106ee3a3b6

@swift-ci
Copy link
Contributor

swift-ci commented Nov 4, 2019

Build failed
Swift Test OS X Platform
Git Sha - 159f9886ba87126bf75f240bf16693106ee3a3b6

@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci please test

@aschwaighofer aschwaighofer force-pushed the sil_type_expansion_context branch from 05db35f to 291b5ed Compare November 4, 2019 20:20
@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Nov 4, 2019

Build failed
Swift Test Linux Platform
Git Sha - 05db35fb55b1e433449e459a5cecdebd9428fec7

@swift-ci
Copy link
Contributor

swift-ci commented Nov 4, 2019

Build failed
Swift Test OS X Platform
Git Sha - 05db35fb55b1e433449e459a5cecdebd9428fec7

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test source compatibility

1 similar comment
@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test source compatibility

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test os x

@swift-ci
Copy link
Contributor

swift-ci commented Nov 5, 2019

Build failed
Swift Test OS X Platform
Git Sha - 291b5ed2d268105db9a18d415b21cc5c7397cf6a

@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci Please test os x

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test source compat

FunctionRefInst(SILDebugLocation DebugLoc, SILFunction *F);
/// \param context The type expansion context of the function reference.
FunctionRefInst(SILDebugLocation DebugLoc, SILFunction *F,
TypeExpansionContext context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instructions and basic block arguments, could they get their TypeExpansionContext from the containing SILFunction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SILInstructions don't have access to their parent basic block (or SILFunction) on creation. The TypeExpansionContext is needed at creation to compute the SILType of the instruction.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I see that the SILBuilder APIs haven't changed and use the builder's current context too. Thanks!

lib/AST/Type.cpp Outdated

bool TypeBase::hasOpaqueArchetypePropertiesOrCases() {
if (hasOpaqueArchetype())
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A type could be parameterized by an opaque archetype, but not use that type parameter in any of its fields or cases. Was this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But I admit it is confusing. I will remove this hasOpaqueArchetype check in TypeBase and move it to the users of this API.

The place in TypeLowering that uses this predicate wants to know about either. It is used to map different type expansion context type lowerings to one map entry if we know that a type does not contain any opaque archetypes and is not resilient in any resilience domain.

VarDecl *field);

OwnedAddress projectPhysicalClassMemberAddress(
TypeExpansionContext context, IRGenFunction &IGF, llvm::Value *base,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a SILFunction always has a particular TypeExpansionContext, then similarly, maybe IRGenFunction could as well. For IRGenSILFunction it could use the context from the function being lowered, and for other IGFs, maybe we could add it as a constructor argument?

Copy link
Contributor

@jckarter jckarter Nov 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…though by IRGen time, maybe we can also always use a maximal TypeExpansionContext, since we don't have to worry about cross-module SIL inlining anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right.
I have IRGenModule::getMaximalTypeExpansionContext() which I should have used here instead.

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Nov 8, 2019

Build failed
Swift Test Linux Platform
Git Sha - 291b5ed2d268105db9a18d415b21cc5c7397cf6a

@swift-ci
Copy link
Contributor

swift-ci commented Nov 8, 2019

Build failed
Swift Test OS X Platform
Git Sha - 291b5ed2d268105db9a18d415b21cc5c7397cf6a

@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci test

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test source compat

@aschwaighofer aschwaighofer force-pushed the sil_type_expansion_context branch from 07224f2 to f3a5d69 Compare November 12, 2019 13:36
@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 07224f2b923cf99c2d74e0867598bf59363a0e35

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 07224f2b923cf99c2d74e0867598bf59363a0e35

@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci test os x

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f3a5d69

@aschwaighofer
Copy link
Contributor Author

test with swiftlang/llvm-project#144
@swift-ci test os x

@aschwaighofer aschwaighofer merged commit a30f804 into swiftlang:master Nov 12, 2019
nate-chandler added a commit to nate-chandler/swift that referenced this pull request Aug 9, 2023
Back in 33f4f57 of
swiftlang#28044 fame,
non-`CaptureKind::Constant:` uses of `Entry.val` in
`SILGenFunction::emitCaptures` were replaced with a use of the newly
added lambda `getAddressValue` applied at `Entry.val`.

The replacement of `Entry.value` with `getAddressValue(Entry.value)` in
the case of `CaptureKind::Box` had no effect.

Back then, the reason was that the condition

    SGM.Types
        .getTypeLowering(
            valueType,
            TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(
                expansion.getResilienceExpansion()))
            .isAddressOnly() &&
        !entryValue->getType().isAddress()

under which something other than the input was returned would never
hold: CaptureKind::Box is used for LValues and those never satisfy
`!entryValue->getType().isAddress()`.

Since that PR, the getAddressValue lambda has grown.  There are two
additional aspects to consider: (1) forceCopy, (2) isPack.  (1) is not
relevant because `false` was being passed for the `CaptureKind::Box`
case.  (2) can not currently happen because pack lvalues haven't been
implemented.  But even if they were implemented, the argument passed to
the lambda would still need to be an address.

The same all holds for the `CaptureKind::ImmutableBox` case which only
differs from the `CaptureKind::Box` by a couple of lines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants