Skip to content

[sil] Make SILFunctionTypeInfo a struct enum. #80312

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 67 additions & 25 deletions include/swift/AST/ExtInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,58 @@ class FunctionTypeIsolation {
/// are significantly reduced compared to AST function types.
/// Isolation is not part of the SIL function model after the
/// early portion of the pipeline.
enum class SILFunctionTypeIsolation {
/// We don't normally record isolation in SIL function types,
/// so the empty case here is "unknown".
Unknown,

/// The isolation of the function has been statically erased.
/// This corresponds to @isolated(any).
Erased,
class SILFunctionTypeIsolation {
public:
enum Kind : uint8_t {
/// We don't normally record isolation in SIL function types,
/// so the empty case here is "unknown".
Unknown,

/// The isolation of the function has been statically erased.
/// This corresponds to @isolated(any).
Erased,
};

static constexpr size_t NumBits = 3; // future-proof this slightly
static constexpr uintptr_t Mask = (uintptr_t(1) << NumBits) - 1;

private:
// We do not use a pointer int pair, since it is not a literal type.
llvm::PointerIntPair<CanType, NumBits, Kind> value;

SILFunctionTypeIsolation(Kind kind, CanType type = CanType())
: value(type, kind) {}

public:
static SILFunctionTypeIsolation forUnknown() { return {Kind::Unknown}; }

static SILFunctionTypeIsolation forErased() { return {Kind::Erased}; }

bool operator==(const SILFunctionTypeIsolation &other) const {
if (getKind() != other.getKind())
return false;

switch (getKind()) {
case Kind::Unknown:
case Kind::Erased:
return true;
}
}

Kind getKind() const { return value.getInt(); }

bool isUnknown() const { return getKind() == Kind::Unknown; }
bool isErased() const { return getKind() == Kind::Erased; }

// The opaque accessors below are just for the benefit of SILExtInfoBuilder,
// which finds it convenient to break down the type separately. Normal
// clients should use the accessors above.

CanType getOpaqueType() const { return value.getPointer(); }

static SILFunctionTypeIsolation fromOpaqueValues(Kind kind, CanType type) {
return SILFunctionTypeIsolation(kind, type);
}
};

// MARK: - ClangTypeInfo
Expand Down Expand Up @@ -967,17 +1011,16 @@ class SILExtInfoBuilder {
: bits(bits), clangTypeInfo(clangTypeInfo.getCanonical()),
lifetimeDependencies(lifetimeDependencies) {}

static constexpr unsigned makeBits(Representation rep, bool isPseudogeneric,
bool isNoEscape, bool isSendable,
bool isAsync, bool isUnimplementable,
SILFunctionTypeIsolation isolation,
DifferentiabilityKind diffKind) {
static unsigned makeBits(Representation rep, bool isPseudogeneric,
bool isNoEscape, bool isSendable, bool isAsync,
bool isUnimplementable,
SILFunctionTypeIsolation isolation,
DifferentiabilityKind diffKind) {
return ((unsigned)rep) | (isPseudogeneric ? PseudogenericMask : 0) |
(isNoEscape ? NoEscapeMask : 0) | (isSendable ? SendableMask : 0) |
(isAsync ? AsyncMask : 0) |
(isUnimplementable ? UnimplementableMask : 0) |
(isolation == SILFunctionTypeIsolation::Erased ? ErasedIsolationMask
: 0) |
(isolation.isErased() ? ErasedIsolationMask : 0) |
(((unsigned)diffKind << DifferentiabilityMaskOffset) &
DifferentiabilityMask);
}
Expand All @@ -988,7 +1031,7 @@ class SILExtInfoBuilder {
SILExtInfoBuilder()
: SILExtInfoBuilder(
makeBits(SILFunctionTypeRepresentation::Thick, false, false, false,
false, false, SILFunctionTypeIsolation::Unknown,
false, false, SILFunctionTypeIsolation::forUnknown(),
DifferentiabilityKind::NonDifferentiable),
ClangTypeInfo(nullptr), /*LifetimeDependenceInfo*/ std::nullopt) {}

Expand All @@ -1008,8 +1051,8 @@ class SILExtInfoBuilder {
info.isNoEscape(), info.isSendable(),
info.isAsync(), /*unimplementable*/ false,
info.getIsolation().isErased()
? SILFunctionTypeIsolation::Erased
: SILFunctionTypeIsolation::Unknown,
? SILFunctionTypeIsolation::forErased()
: SILFunctionTypeIsolation::forUnknown(),
info.getDifferentiabilityKind()),
info.getClangTypeInfo(),
info.getLifetimeDependencies()) {}
Expand Down Expand Up @@ -1059,10 +1102,9 @@ class SILExtInfoBuilder {
return bits & ErasedIsolationMask;
}

constexpr SILFunctionTypeIsolation getIsolation() const {
return hasErasedIsolation()
? SILFunctionTypeIsolation::Erased
: SILFunctionTypeIsolation::Unknown;
SILFunctionTypeIsolation getIsolation() const {
return hasErasedIsolation() ? SILFunctionTypeIsolation::forErased()
: SILFunctionTypeIsolation::forUnknown();
}

/// Get the underlying ClangTypeInfo value.
Expand Down Expand Up @@ -1157,7 +1199,7 @@ class SILExtInfoBuilder {
}
[[nodiscard]]
SILExtInfoBuilder withIsolation(SILFunctionTypeIsolation isolation) const {
switch (isolation) {
switch (isolation.getKind()) {
case SILFunctionTypeIsolation::Unknown:
return *this;
case SILFunctionTypeIsolation::Erased:
Expand Down Expand Up @@ -1241,7 +1283,7 @@ class SILExtInfo {
static SILExtInfo getThin() {
return SILExtInfoBuilder(
SILExtInfoBuilder::Representation::Thin, false, false, false,
false, false, SILFunctionTypeIsolation::Unknown,
false, false, SILFunctionTypeIsolation::forUnknown(),
DifferentiabilityKind::NonDifferentiable, nullptr, {})
.build();
}
Expand Down Expand Up @@ -1276,7 +1318,7 @@ class SILExtInfo {
constexpr bool hasErasedIsolation() const {
return builder.hasErasedIsolation();
}
constexpr SILFunctionTypeIsolation getIsolation() const {
SILFunctionTypeIsolation getIsolation() const {
return builder.getIsolation();
}

Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/ApplySite.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class ApplySite {
unsigned appliedArgIndex = getAppliedArgIndex(oper);
if (auto *pai = dyn_cast<PartialApplyInst>(Inst)) {
if (pai->getFunctionType()->getIsolation() ==
SILFunctionTypeIsolation::Erased) {
SILFunctionTypeIsolation::forErased()) {
assert(appliedArgIndex != 0 &&
"isolation(any) does not correspond to an AST argument");
appliedArgIndex -= 1;
Expand Down
20 changes: 10 additions & 10 deletions include/swift/SIL/SILBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,8 @@ bool BridgedInstruction::PartialApplyInst_isOnStack() const {
}

bool BridgedInstruction::PartialApplyInst_hasUnknownResultIsolation() const {
return getAs<swift::PartialApplyInst>()->getResultIsolation() == swift::SILFunctionTypeIsolation::Unknown;
return getAs<swift::PartialApplyInst>()->getResultIsolation() ==
swift::SILFunctionTypeIsolation::forUnknown();
}

bool BridgedInstruction::AllocStackInst_hasDynamicLifetime() const {
Expand Down Expand Up @@ -2264,16 +2265,15 @@ BridgedInstruction BridgedBuilder::createPartialApply(BridgedValue funcRef,
BridgedSubstitutionMap bridgedSubstitutionMap,
bool hasUnknownIsolation,
bool isOnStack) const {
llvm::SmallVector<swift::SILValue, 8> capturedArgs;
llvm::SmallVector<swift::SILValue, 8> capturedArgs;
return {unbridged().createPartialApply(
regularLoc(),
funcRef.getSILValue(),
bridgedSubstitutionMap.unbridged(),
bridgedCapturedArgs.getValues(capturedArgs),
getParameterConvention(calleeConvention),
hasUnknownIsolation ? swift::SILFunctionTypeIsolation::Unknown : swift::SILFunctionTypeIsolation::Erased,
isOnStack ? swift:: PartialApplyInst::OnStack : swift::PartialApplyInst::NotOnStack
)};
regularLoc(), funcRef.getSILValue(), bridgedSubstitutionMap.unbridged(),
bridgedCapturedArgs.getValues(capturedArgs),
getParameterConvention(calleeConvention),
hasUnknownIsolation ? swift::SILFunctionTypeIsolation::forUnknown()
: swift::SILFunctionTypeIsolation::forErased(),
isOnStack ? swift::PartialApplyInst::OnStack
: swift::PartialApplyInst::NotOnStack)};
}

BridgedInstruction BridgedBuilder::createBranch(BridgedBasicBlock destBlock, BridgedValueArray arguments) const {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ class SILBuilder {
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
ArrayRef<SILValue> Args, ParameterConvention CalleeConvention,
SILFunctionTypeIsolation ResultIsolation =
SILFunctionTypeIsolation::Unknown,
SILFunctionTypeIsolation::forUnknown(),
PartialApplyInst::OnStackKind OnStack =
PartialApplyInst::OnStackKind::NotOnStack,
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,9 @@ Type ASTBuilder::createImplFunctionType(
#undef SIMPLE_CASE
}

auto isolation = SILFunctionTypeIsolation::Unknown;
auto isolation = SILFunctionTypeIsolation::forUnknown();
if (flags.hasErasedIsolation())
isolation = SILFunctionTypeIsolation::Erased;
isolation = SILFunctionTypeIsolation::forErased();

// There's no representation of this in the mangling because it can't
// occur in well-formed programs.
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
appendOperator("Ty");
else
appendOperator("TR");

if (GlobalActorBound) {
appendType(GlobalActorBound, GenSig);
appendOperator("TU");
Expand Down Expand Up @@ -2289,7 +2289,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
if (!fn->isNoEscape())
OpArgs.push_back('e');

switch (fn->getIsolation()) {
switch (fn->getIsolation().getKind()) {
case SILFunctionTypeIsolation::Unknown:
break;
case SILFunctionTypeIsolation::Erased:
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/IR/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
case ParameterConvention::Pack_Inout:
llvm_unreachable("unexpected callee convention!");
}
switch (fnType->getIsolation()) {
switch (fnType->getIsolation().getKind()) {
case SILFunctionTypeIsolation::Unknown:
break;
case SILFunctionTypeIsolation::Erased:
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/Parser/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6797,7 +6797,7 @@ bool SILParser::parseCallInstruction(SILLocation InstLoc,
SmallVector<UnresolvedValueName, 4> ArgNames;

auto PartialApplyConvention = ParameterConvention::Direct_Owned;
auto PartialApplyIsolation = SILFunctionTypeIsolation::Unknown;
auto PartialApplyIsolation = SILFunctionTypeIsolation::forUnknown();
ApplyOptions ApplyOpts;
bool IsNoEscape = false;

Expand Down Expand Up @@ -6828,7 +6828,7 @@ bool SILParser::parseCallInstruction(SILLocation InstLoc,

if (AttrName == "isolated_any") {
assert(!bool(AttrValue));
PartialApplyIsolation = SILFunctionTypeIsolation::Erased;
PartialApplyIsolation = SILFunctionTypeIsolation::forErased();
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ class SILGenBuilder : public SILBuilder {
ArrayRef<ManagedValue> args,
ParameterConvention calleeConvention,
SILFunctionTypeIsolation resultIsolation =
SILFunctionTypeIsolation::Unknown);
SILFunctionTypeIsolation::forUnknown());
ManagedValue createPartialApply(SILLocation loc, ManagedValue fn,
SubstitutionMap subs,
ArrayRef<ManagedValue> args,
ParameterConvention calleeConvention,
SILFunctionTypeIsolation resultIsolation =
SILFunctionTypeIsolation::Unknown) {
SILFunctionTypeIsolation::forUnknown()) {
return createPartialApply(loc, fn.getValue(), subs, args,
calleeConvention, resultIsolation);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,8 @@ SILGenFunction::emitClosureValue(SILLocation loc, SILDeclRef constant,
auto calleeConvention = ParameterConvention::Direct_Guaranteed;

auto resultIsolation =
(hasErasedIsolation ? SILFunctionTypeIsolation::Erased
: SILFunctionTypeIsolation::Unknown);
(hasErasedIsolation ? SILFunctionTypeIsolation::forErased()
: SILFunctionTypeIsolation::forUnknown());
auto toClosure =
B.createPartialApply(loc, functionRef, subs, forwardedArgs,
calleeConvention, resultIsolation);
Expand Down Expand Up @@ -1847,7 +1847,7 @@ SILGenFunction::emitApplyOfSetterToBase(SILLocation loc, SILDeclRef setter,
PartialApplyInst *setterPAI =
B.createPartialApply(loc, setterFRef, substitutions, capturedArgs,
ParameterConvention::Direct_Guaranteed,
SILFunctionTypeIsolation::Unknown,
SILFunctionTypeIsolation::forUnknown(),
PartialApplyInst::OnStackKind::OnStack);
return emitManagedRValueWithCleanup(setterPAI).getValue();
}
Expand Down Expand Up @@ -1899,7 +1899,7 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
PartialApplyInst *initPAI =
B.createPartialApply(loc, initFRef, substitutions, selfMetatype,
ParameterConvention::Direct_Guaranteed,
SILFunctionTypeIsolation::Unknown,
SILFunctionTypeIsolation::forUnknown(),
PartialApplyInst::OnStackKind::OnStack);
initFRef = emitManagedRValueWithCleanup(initPAI).getValue();

Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4501,7 +4501,7 @@ NeverNullType TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
bool sendable = claim<SendableTypeAttr>(attrs);
bool async = claim<AsyncTypeAttr>(attrs);
bool unimplementable = claim<UnimplementableTypeAttr>(attrs);
auto isolation = SILFunctionTypeIsolation::Unknown;
auto isolation = SILFunctionTypeIsolation::forUnknown();

if (auto isolatedAttr = claim<IsolatedTypeAttr>(attrs)) {
switch (isolatedAttr->getIsolationKind()) {
Expand All @@ -4513,7 +4513,7 @@ NeverNullType TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
isolatedAttr->getIsolationKindName(),
conventionAttr->getConventionName());
} else {
isolation = SILFunctionTypeIsolation::Erased;
isolation = SILFunctionTypeIsolation::forErased();
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7852,9 +7852,9 @@ Expected<Type> DESERIALIZE_TYPE(SIL_FUNCTION_TYPE)(
clangFunctionType = clangType.get();
}

auto isolation = SILFunctionTypeIsolation::Unknown;
auto isolation = SILFunctionTypeIsolation::forUnknown();
if (erasedIsolation)
isolation = SILFunctionTypeIsolation::Erased;
isolation = SILFunctionTypeIsolation::forErased();

auto extInfo = SILFunctionType::ExtInfoBuilder(
*representation, pseudogeneric, noescape, sendable, async,
Expand Down