Skip to content

[SilOpt] Add new layout _BridgeObject and add pre-specialization supp… #70239

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 1 commit into from
Dec 8, 2023
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
1 change: 1 addition & 0 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ now codified into the ABI; the index 0 is therefore reserved.
LAYOUT-CONSTRAINT ::= 'M' LAYOUT-SIZE-AND-ALIGNMENT // Trivial of size at most N bits
LAYOUT-CONSTRAINT ::= 'm' LAYOUT-SIZE // Trivial of size at most N bits
LAYOUT-CONSTRAINT ::= 'U' // Unknown layout
LAYOUT-CONSTRAINT ::= 'B' // BridgeObject

LAYOUT-SIZE ::= INDEX // Size only
LAYOUT-SIZE-AND-ALIGNMENT ::= INDEX INDEX // Size followed by alignment
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ IDENTIFIER_WITH_NAME(RefCountedObjectLayout, "_RefCountedObject")
IDENTIFIER_WITH_NAME(NativeRefCountedObjectLayout, "_NativeRefCountedObject")
IDENTIFIER_WITH_NAME(ClassLayout, "_Class")
IDENTIFIER_WITH_NAME(NativeClassLayout, "_NativeClass")
IDENTIFIER_WITH_NAME(BridgeObjectLayout, "_BridgeObject")

// Operators
IDENTIFIER_WITH_NAME(MatchOperator, "~=")
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/LayoutConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class LayoutConstraintInfo
return isNativeRefCounted(Kind);
}

bool isBridgeObject() const { return isBridgeObject(Kind); }

unsigned getTrivialSizeInBytes() const {
assert(isKnownSizeTrivial());
return (SizeInBits + 7) / 8;
Expand Down Expand Up @@ -200,6 +202,8 @@ class LayoutConstraintInfo

static bool isNativeRefCounted(LayoutConstraintKind Kind);

static bool isBridgeObject(LayoutConstraintKind Kind);

/// Uniquing for the LayoutConstraintInfo.
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Kind, SizeInBits, Alignment);
Expand All @@ -217,6 +221,7 @@ class LayoutConstraintInfo
static LayoutConstraintInfo ClassConstraintInfo;
static LayoutConstraintInfo NativeClassConstraintInfo;
static LayoutConstraintInfo TrivialConstraintInfo;
static LayoutConstraintInfo BridgeObjectConstraintInfo;
};

/// A wrapper class containing a reference to the actual LayoutConstraintInfo
Expand Down
4 changes: 3 additions & 1 deletion include/swift/AST/LayoutConstraintKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ enum class LayoutConstraintKind : uint8_t {
RefCountedObject,
// It is a layout constraint representing a native reference counted object.
NativeRefCountedObject,
LastLayout = NativeRefCountedObject,
// It is a layout constraint representing a bridge object
BridgeObject,
LastLayout = BridgeObject,
};

#endif
1 change: 1 addition & 0 deletions include/swift/Demangling/TypeDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ void decodeRequirement(NodePointer node,
.Case("C", LayoutConstraintKind::Class)
.Case("D", LayoutConstraintKind::NativeClass)
.Case("T", LayoutConstraintKind::Trivial)
.Case("B", LayoutConstraintKind::BridgeObject)
.Cases("E", "e", LayoutConstraintKind::TrivialOfExactSize)
.Cases("M", "m", LayoutConstraintKind::TrivialOfAtMostSize)
.Default(llvm::None);
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ class SILType {
return is<BuiltinVectorType>();
}

bool isBuiltinBridgeObject() const { return is<BuiltinBridgeObjectType>(); }

SWIFT_IMPORT_UNSAFE
SILType getBuiltinVectorElementType() const {
auto vector = castTo<BuiltinVectorType>();
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3801,6 +3801,9 @@ void ASTMangler::appendOpParamForLayoutConstraint(LayoutConstraint layout) {
appendOperatorParam("M", Index(layout->getTrivialSizeInBits()),
Index(layout->getAlignmentInBits()));
break;
case LayoutConstraintKind::BridgeObject:
appendOperatorParam("B");
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7683,6 +7683,7 @@ void LayoutConstraintInfo::print(ASTPrinter &Printer,
case LayoutConstraintKind::Class:
case LayoutConstraintKind::NativeClass:
case LayoutConstraintKind::Trivial:
case LayoutConstraintKind::BridgeObject:
return; // non-parameterized cases
case LayoutConstraintKind::TrivialOfAtMostSize:
case LayoutConstraintKind::TrivialOfExactSize:
Expand Down
16 changes: 12 additions & 4 deletions lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,18 @@ GenericSignature GenericSignature::typeErased(ArrayRef<Type> typeErasedParams) c
auto other = req.getFirstType();
return t->isEqual(other);
});
if (found) {
requirementsErased.push_back(Requirement(RequirementKind::SameType,
req.getFirstType(),
Ptr->getASTContext().getAnyObjectType()));
if (found && req.getKind() == RequirementKind::Layout) {
if (req.getLayoutConstraint()->isClass()) {
requirementsErased.push_back(
Requirement(RequirementKind::SameType, req.getFirstType(),
Ptr->getASTContext().getAnyObjectType()));
} else if (req.getLayoutConstraint()->isBridgeObject()) {
requirementsErased.push_back(
Requirement(RequirementKind::SameType, req.getFirstType(),
Ptr->getASTContext().TheBridgeObjectType));
} else {
requirementsErased.push_back(req);
}
} else {
requirementsErased.push_back(req);
}
Expand Down
38 changes: 29 additions & 9 deletions lib/AST/LayoutConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ LayoutConstraint getLayoutConstraint(Identifier ID, ASTContext &Ctx) {
return LayoutConstraint::getLayoutConstraint(
LayoutConstraintKind::NativeClass, Ctx);

if (ID == Ctx.Id_BridgeObjectLayout)
return LayoutConstraint::getLayoutConstraint(
LayoutConstraintKind::BridgeObject, Ctx);

return LayoutConstraint::getLayoutConstraint(
LayoutConstraintKind::UnknownLayout, Ctx);
}
Expand All @@ -73,6 +77,8 @@ StringRef LayoutConstraintInfo::getName(LayoutConstraintKind Kind, bool internal
return "_TrivialAtMost";
case LayoutConstraintKind::TrivialOfExactSize:
return "_Trivial";
case LayoutConstraintKind::BridgeObject:
return "_BridgeObject";
}

llvm_unreachable("Unhandled LayoutConstraintKind in switch.");
Expand Down Expand Up @@ -132,14 +138,18 @@ bool LayoutConstraintInfo::isNativeClass(LayoutConstraintKind Kind) {
}

bool LayoutConstraintInfo::isRefCounted(LayoutConstraintKind Kind) {
return isAnyRefCountedObject(Kind) || isClass(Kind);
return isAnyRefCountedObject(Kind) || isClass(Kind) || isBridgeObject(Kind);
}

bool LayoutConstraintInfo::isNativeRefCounted(LayoutConstraintKind Kind) {
return Kind == LayoutConstraintKind::NativeRefCountedObject ||
Kind == LayoutConstraintKind::NativeClass;
}

bool LayoutConstraintInfo::isBridgeObject(LayoutConstraintKind Kind) {
return Kind == LayoutConstraintKind::BridgeObject;
}

SourceRange LayoutConstraintLoc::getSourceRange() const { return getLoc(); }

#define MERGE_LOOKUP(lhs, rhs) \
Expand Down Expand Up @@ -170,50 +180,55 @@ static LayoutConstraintKind mergeTable[unsigned(E(LastLayout)) +
E(/* TrivialOfAtMostSize */ TrivialOfAtMostSize), E(/* Trivial */ Trivial),
E(/* Class */ Class), E(/* NativeClass */ NativeClass),
E(/* RefCountedObject*/ RefCountedObject),
E(/* NativeRefCountedObject */ NativeRefCountedObject)},
E(/* NativeRefCountedObject */ NativeRefCountedObject), MERGE_CONFLICT},

// Initialize the row for TrivialOfExactSize.
{E(/* UnknownLayout */ TrivialOfExactSize),
E(/* TrivialOfExactSize */ TrivialOfExactSize), MERGE_CONFLICT,
E(/* Trivial */ TrivialOfExactSize), MERGE_CONFLICT, MERGE_CONFLICT,
MERGE_CONFLICT, MERGE_CONFLICT},
MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT},

// Initialize the row for TrivialOfAtMostSize.
{E(/* UnknownLayout */ TrivialOfAtMostSize), MERGE_CONFLICT,
E(/* TrivialOfAtMostSize */ TrivialOfAtMostSize),
E(/* Trivial */ TrivialOfAtMostSize), MERGE_CONFLICT, MERGE_CONFLICT,
MERGE_CONFLICT, MERGE_CONFLICT},
MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT},

// Initialize the row for Trivial.
{E(/* UnknownLayout */ Trivial),
E(/* TrivialOfExactSize */ TrivialOfExactSize),
E(/* TrivialOfAtMostSize */ TrivialOfAtMostSize), E(/* Trivial */ Trivial),
MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT},
MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT,
MERGE_CONFLICT},

// Initialize the row for Class.
{E(/* UnknownLayout*/ Class), MERGE_CONFLICT, MERGE_CONFLICT,
MERGE_CONFLICT, E(/* Class */ Class), E(/* NativeClass */ NativeClass),
E(/* RefCountedObject */ Class),
E(/* NativeRefCountedObject */ NativeClass)},
E(/* NativeRefCountedObject */ NativeClass), MERGE_CONFLICT},

// Initialize the row for NativeClass.
{E(/* UnknownLayout */ NativeClass), MERGE_CONFLICT, MERGE_CONFLICT,
MERGE_CONFLICT, E(/* Class */ NativeClass),
E(/* NativeClass */ NativeClass), E(/* RefCountedObject */ NativeClass),
E(/* NativeRefCountedObject */ NativeClass)},
E(/* NativeRefCountedObject */ NativeClass), MERGE_CONFLICT},

// Initialize the row for RefCountedObject.
{E(/* UnknownLayout */ RefCountedObject), MERGE_CONFLICT, MERGE_CONFLICT,
MERGE_CONFLICT, E(/* Class */ Class), E(/* NativeClass */ NativeClass),
E(/* RefCountedObject */ RefCountedObject),
E(/* NativeRefCountedObject */ NativeRefCountedObject)},
E(/* NativeRefCountedObject */ NativeRefCountedObject), MERGE_CONFLICT},

// Initialize the row for NativeRefCountedObject.
{E(/* UnknownLayout */ NativeRefCountedObject), MERGE_CONFLICT,
MERGE_CONFLICT, MERGE_CONFLICT, E(/* Class */ NativeClass),
E(/* NativeClass */ NativeClass),
E(/* RefCountedObject */ NativeRefCountedObject),
E(/* NativeRefCountedObject*/ NativeRefCountedObject)},
E(/* NativeRefCountedObject*/ NativeRefCountedObject), MERGE_CONFLICT},

{E(BridgeObject), MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT,
MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT, MERGE_CONFLICT,
E(/*BridgeObject*/ BridgeObject)},
};

#undef E
Expand Down Expand Up @@ -324,6 +339,8 @@ LayoutConstraint::getLayoutConstraint(LayoutConstraintKind Kind) {
&LayoutConstraintInfo::RefCountedObjectConstraintInfo);
case LayoutConstraintKind::UnknownLayout:
return LayoutConstraint(&LayoutConstraintInfo::UnknownLayoutConstraintInfo);
case LayoutConstraintKind::BridgeObject:
return LayoutConstraint(&LayoutConstraintInfo::BridgeObjectConstraintInfo);
case LayoutConstraintKind::TrivialOfAtMostSize:
case LayoutConstraintKind::TrivialOfExactSize:
llvm_unreachable("Wrong layout constraint kind");
Expand Down Expand Up @@ -352,6 +369,9 @@ LayoutConstraintInfo LayoutConstraintInfo::NativeClassConstraintInfo(
LayoutConstraintInfo LayoutConstraintInfo::TrivialConstraintInfo(
LayoutConstraintKind::Trivial);

LayoutConstraintInfo LayoutConstraintInfo::BridgeObjectConstraintInfo(
LayoutConstraintKind::BridgeObject);

int LayoutConstraint::compare(LayoutConstraint rhs) const {
if (Ptr->getKind() != rhs->getKind())
return int(rhs->getKind()) - int(Ptr->getKind());
Expand Down
2 changes: 2 additions & 0 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4028,6 +4028,8 @@ NodePointer Demangler::demangleGenericRequirement() {
name = "D";
} else if (c == 'T') {
name = "T";
} else if (c == 'B') {
name = "B";
} else if (c == 'E') {
size = demangleIndexAsNode();
if (!size)
Expand Down
3 changes: 3 additions & 0 deletions lib/Demangling/OldDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,9 @@ class OldDemangler {
} else if (Mangled.nextIf('T')) {
kind = Node::Kind::Identifier;
name = "T";
} else if (Mangled.nextIf('B')) {
kind = Node::Kind::Identifier;
name = "B";
} else if (Mangled.nextIf('E')) {
kind = Node::Kind::Identifier;
if (!demangleNatural(size, depth + 1))
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/AbstractionPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,7 @@ class SubstFunctionTypePatternVisitor
// Keep these layout constraints as is.
case LayoutConstraintKind::RefCountedObject:
case LayoutConstraintKind::TrivialOfAtMostSize:
case LayoutConstraintKind::BridgeObject:
break;

case LayoutConstraintKind::UnknownLayout:
Expand Down
45 changes: 31 additions & 14 deletions lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,25 @@ void SpecializedFunction::computeTypeReplacements(const ApplySite &apply) {
auto resultType =
fn->getConventions().getSILResultType(fn->getTypeExpansionContext());
SmallVector<SILResultInfo, 4> indirectResults(substConv.getIndirectSILResults());
SmallVector<SILResultInfo, 4> targetIndirectResults(
fn->getConventions().getIndirectSILResults());

for (auto pair : llvm::enumerate(apply.getArgumentOperands())) {
if (pair.index() < substConv.getSILArgIndexOfFirstParam()) {
auto formalIndex = substConv.getIndirectFormalResultIndexForSILArg(pair.index());
auto fnResult = indirectResults[formalIndex];
if (fnResult.isFormalIndirect()) {
// FIXME: properly get the type
auto indirectResultTy = M.getASTContext().getAnyObjectType(); //fnResult.getReturnValueType(M, fnType, expansion);
CanType indirectResultTy;
if (targetIndirectResults.size() > formalIndex) {
indirectResultTy =
targetIndirectResults[formalIndex].getReturnValueType(
M, fnType, expansion);
} else {
indirectResultTy =
fnType->getResults()[formalIndex].getReturnValueType(M, fnType,
expansion);
}

addIndirectResultType(formalIndex, indirectResultTy);
}

Expand Down Expand Up @@ -2967,7 +2978,7 @@ bool usePrespecialized(

if (specializedReInfo.getSpecializedType() != reInfo.getSpecializedType()) {
SmallVector<Type, 4> newSubs;
auto specializedSig = SA->getSpecializedSignature();
auto specializedSig = SA->getUnerasedSpecializedSignature();

auto erasedParams = SA->getTypeErasedParams();
if(!ctxt.LangOpts.hasFeature(Feature::LayoutPrespecialization) || erasedParams.empty()) {
Expand All @@ -2985,8 +2996,12 @@ bool usePrespecialized(
});

auto layout = specializedSig->getLayoutConstraint(genericParam);
if (!specializedSig->getRequiredProtocols(genericParam).empty()) {
llvm::report_fatal_error("Unexpected protocol requirements");
}

if (!erased || !layout || !layout->isClass()) {
if (!erased || !layout ||
(!layout->isClass() && !layout->isBridgeObject())) {
newSubs.push_back(entry.value());
continue;
}
Expand All @@ -2997,17 +3012,19 @@ bool usePrespecialized(
lowered = singleton;
}

if (!lowered.hasRetainablePointerRepresentation()) {
// non-reference type can't be applied
break;
} else if (!specializedSig->getRequiredProtocols(genericParam)
.empty()) {
llvm::report_fatal_error("Unexpected protocol requirements");
} else if (layout->isNativeClass()) {
newSubs.push_back(genericParam->getASTContext().TheNativeObjectType);
score += 1;
if (lowered.isBuiltinBridgeObject() && layout->isBridgeObject()) {
newSubs.push_back(genericParam->getASTContext().TheBridgeObjectType);
} else if (lowered.hasRetainablePointerRepresentation()) {
if (layout->isNativeClass()) {
newSubs.push_back(
genericParam->getASTContext().TheNativeObjectType);
score += 1;
} else {
newSubs.push_back(genericParam->getASTContext().getAnyObjectType());
}
} else {
newSubs.push_back(genericParam->getASTContext().getAnyObjectType());
// no match
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ getActualLayoutConstraintKind(uint64_t rawKind) {
CASE(Class)
CASE(NativeClass)
CASE(UnknownLayout)
CASE(BridgeObject)
}
#undef CASE

Expand Down
7 changes: 4 additions & 3 deletions lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 823; // _resultDependsOn
const uint16_t SWIFTMODULE_VERSION_MINOR = 824; // LayoutRequirementKindField

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -482,9 +482,10 @@ enum LayoutRequirementKind : uint8_t {
RefCountedObject = 4,
NativeRefCountedObject = 5,
Class = 6,
NativeClass = 7
NativeClass = 7,
BridgeObject = 8,
};
using LayoutRequirementKindField = BCFixed<3>;
using LayoutRequirementKindField = BCFixed<4>;

// These IDs must \em not be renumbered or reordered without incrementing
// the module version.
Expand Down
3 changes: 3 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,9 @@ void Serializer::serializeGenericRequirements(
case LayoutConstraintKind::UnknownLayout:
rawKind = LayoutRequirementKind::UnknownLayout;
break;
case LayoutConstraintKind::BridgeObject:
rawKind = LayoutRequirementKind::BridgeObject;
break;
}
scratch.push_back(rawKind);
scratch.push_back(addTypeRef(req.getFirstType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SomeClass {
@_specialize(exported: true, where T == Int)
@_specialize(exported: true, where T == Double)
@_specialize(exported: true, where @_noMetadata T : _Class)
@_specialize(exported: true, where @_noMetadata T : _BridgeObject)
@_specialize(exported: true, availability: macOS 10.50, *; where T == SomeData)
public func publicPrespecialized<T>(_ t: T) {
}
Expand Down Expand Up @@ -131,6 +132,7 @@ public func useInternalThing<T>(_ t: T) {
}

@_specialize(exported: true, where @_noMetadata T : _Class, @_noMetadata V : _Class)
@_specialize(exported: true, where @_noMetadata T : _BridgeObject, @_noMetadata V : _BridgeObject)
public func publicPresepcializedMultipleIndirectResults<T, V>(_ t: T, _ v: V, _ x: Int64)-> (V, Int64, T) {
return (v, x, t)
}
}
Loading