Skip to content

Commit f3bc1ad

Browse files
authored
Merge pull request swiftlang#76081 from eeckstein/fix-reabstraction
GenericSpecializer: drop unused indirect arguments.
2 parents f7efa9a + b54117c commit f3bc1ad

35 files changed

+294
-192
lines changed

docs/ABI/Mangling.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,14 +1265,17 @@ Function Specializations
12651265

12661266
::
12671267

1268-
specialization ::= type '_' type* 'Tg' SPEC-INFO // Generic re-abstracted specialization
1269-
specialization ::= type '_' type* 'TB' SPEC-INFO // Alternative mangling for generic re-abstracted specializations,
1270-
// used for functions with re-abstracted resilient parameter types.
1268+
specialization ::= type '_' type* 'T' dropped-arg* 'g' SPEC-INFO // Generic re-abstracted specialization
1269+
specialization ::= type '_' type* 'T' dropped-arg* 'B' SPEC-INFO // Alternative mangling for generic re-abstracted specializations,
1270+
// used for functions with re-abstracted resilient parameter types.
1271+
specialization ::= type '_' type* 'T' dropped-arg* 'G' SPEC-INFO // Generic not re-abstracted specialization
12711272
specialization ::= type '_' type* 'Ts' SPEC-INFO // Generic re-abstracted prespecialization
1272-
specialization ::= type '_' type* 'TG' SPEC-INFO // Generic not re-abstracted specialization
12731273
specialization ::= type '_' type* 'Ti' SPEC-INFO // Inlined function with generic substitutions.
12741274
specialization ::= type '_' type* 'Ta' SPEC-INFO // Non-async specialization
12751275

1276+
dropped-arg ::= 't' // The first argument is dropped
1277+
dropped-arg ::= 't' NATURAL // The `N+1`th argument is dropped
1278+
12761279
The types are the replacement types of the substitution list.
12771280

12781281
::
@@ -1294,7 +1297,7 @@ Some kinds need arguments, which precede ``Tf``.
12941297
spec-arg ::= identifier
12951298
spec-arg ::= type
12961299

1297-
SPEC-INFO ::= MT-REMOVED? FRAGILE? ASYNC-REMOVED? PASSID
1300+
SPEC-INFO ::= FRAGILE? ASYNC-REMOVED? PASSID
12981301

12991302
PASSID ::= '0' // AllocBoxToStack,
13001303
PASSID ::= '1' // ClosureSpecializer,
@@ -1305,8 +1308,6 @@ Some kinds need arguments, which precede ``Tf``.
13051308
PASSID ::= '6' // MoveDiagnosticInOutToOut,
13061309
PASSID ::= '7' // AsyncDemotion,
13071310

1308-
MT-REMOVED ::= 'm' // non-generic metatype arguments are removed in the specialized function
1309-
13101311
FRAGILE ::= 'q'
13111312

13121313
ASYNC-REMOVED ::= 'a' // async effect removed

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ NODE(NonUniqueExtendedExistentialTypeShapeSymbolicReference)
375375
NODE(SymbolicExtendedExistentialType)
376376

377377
// Added in Swift 5.8
378-
NODE(MetatypeParamsRemoved)
378+
NODE(DroppedArgument)
379379
NODE(HasSymbolQuery)
380380
NODE(OpaqueReturnTypeIndex)
381381
NODE(OpaqueReturnTypeParent)

include/swift/Demangling/Demangler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,9 @@ class Demangler : public NodeFactory {
594594
NodePointer popDependentAssociatedConformance();
595595
NodePointer demangleDependentProtocolConformanceAssociated();
596596
NodePointer demangleThunkOrSpecialization();
597-
NodePointer demangleGenericSpecialization(Node::Kind SpecKind);
597+
NodePointer demangleGenericSpecialization(Node::Kind SpecKind,
598+
NodePointer droppedArguments);
599+
NodePointer demangleGenericSpecializationWithDroppedArguments();
598600
NodePointer demangleFunctionSpecialization();
599601
NodePointer demangleFuncSpecParam(Node::Kind Kind);
600602
NodePointer addFuncSpecParamNumber(NodePointer Param,

include/swift/SIL/GenericSpecializationMangler.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ class GenericSpecializationMangler : public SpecializationMangler {
8787
std::string manglePrespecialized(GenericSignature sig,
8888
SubstitutionMap subs);
8989

90+
void appendRemovedParams(const SmallBitVector &paramsRemoved);
91+
9092
public:
9193
GenericSpecializationMangler(SILFunction *F, swift::SerializedKind_t Serialized)
9294
: SpecializationMangler(SpecializationPass::GenericSpecializer,
9395
Serialized, F) {}
9496

9597
std::string mangleNotReabstracted(SubstitutionMap subs,
96-
bool metatyeParamsRemoved);
98+
const SmallBitVector &paramsRemoved = SmallBitVector());
9799

98100
/// Mangle a generic specialization with re-abstracted parameters.
99101
///
@@ -107,7 +109,7 @@ class GenericSpecializationMangler : public SpecializationMangler {
107109
/// \param metatyeParamsRemoved true if non-generic metatype parameters are
108110
/// removed in the specialized function.
109111
std::string mangleReabstracted(SubstitutionMap subs, bool alternativeMangling,
110-
bool metatyeParamsRemoved = false);
112+
const SmallBitVector &paramsRemoved = SmallBitVector());
111113

112114
std::string mangleForDebugInfo(GenericSignature sig, SubstitutionMap subs,
113115
bool forInlining);

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ReabstractionInfo {
6565
/// A 1-bit means that the argument is a metatype argument. The argument is
6666
/// dropped and replaced by a `metatype` instruction in the entry block.
6767
/// Only used if `dropMetatypeArgs` is true.
68-
SmallBitVector droppedMetatypeArgs;
68+
SmallBitVector droppedArguments;
6969

7070
/// Set to true if the function has a re-abstracted (= converted from
7171
/// indirect to direct) resilient argument or return type. This can happen if
@@ -84,10 +84,10 @@ class ReabstractionInfo {
8484
/// specializer.
8585
bool ConvertIndirectToDirect = true;
8686

87-
/// If true, drop metatype arguments.
88-
/// See `droppedMetatypeArgs`.
89-
bool dropMetatypeArgs = false;
90-
87+
/// If true, drop unused arguments.
88+
/// See `droppedArguments`.
89+
bool dropUnusedArguments = false;
90+
9191
bool hasIndirectErrorResult = false;
9292

9393
/// The first NumResults bits in Conversions refer to formal indirect
@@ -277,15 +277,13 @@ class ReabstractionInfo {
277277
/// Returns true if there are any conversions from indirect to direct values.
278278
bool hasConversions() const { return Conversions.any(); }
279279

280-
/// Returns true if the argument at `ArgIdx` is a dropped metatype argument.
281-
/// See `droppedMetatypeArgs`.
282-
bool isDroppedMetatypeArg(unsigned ArgIdx) const {
283-
return droppedMetatypeArgs.test(ArgIdx);
280+
/// Returns true if the argument at `ArgIdx` is a dropped argument.
281+
/// See `droppedArguments`.
282+
bool isDroppedArgument(unsigned ArgIdx) const {
283+
return droppedArguments.test(ArgIdx);
284284
}
285285

286-
/// Returns true if there are any dropped metatype arguments.
287-
/// See `droppedMetatypeArgs`.
288-
bool hasDroppedMetatypeArgs() const { return droppedMetatypeArgs.any(); }
286+
const SmallBitVector &getDroppedArgs() const { return droppedArguments; }
289287

290288
/// Remove the arguments of a partial apply, leaving the arguments for the
291289
/// partial apply result function.

lib/Demangling/Demangler.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,18 +2844,20 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
28442844
return Thunk;
28452845
}
28462846
case 'g':
2847-
return demangleGenericSpecialization(Node::Kind::GenericSpecialization);
2847+
return demangleGenericSpecialization(Node::Kind::GenericSpecialization, nullptr);
28482848
case 'G':
28492849
return demangleGenericSpecialization(Node::Kind::
2850-
GenericSpecializationNotReAbstracted);
2850+
GenericSpecializationNotReAbstracted, nullptr);
28512851
case 'B':
28522852
return demangleGenericSpecialization(Node::Kind::
2853-
GenericSpecializationInResilienceDomain);
2853+
GenericSpecializationInResilienceDomain, nullptr);
2854+
case 't':
2855+
return demangleGenericSpecializationWithDroppedArguments();
28542856
case 's':
28552857
return demangleGenericSpecialization(
2856-
Node::Kind::GenericSpecializationPrespecialized);
2858+
Node::Kind::GenericSpecializationPrespecialized, nullptr);
28572859
case 'i':
2858-
return demangleGenericSpecialization(Node::Kind::InlinedGenericFunction);
2860+
return demangleGenericSpecialization(Node::Kind::InlinedGenericFunction, nullptr);
28592861
case 'p': {
28602862
NodePointer Spec = demangleSpecAttributes(Node::Kind::
28612863
GenericPartialSpecialization);
@@ -3202,10 +3204,18 @@ std::string Demangler::demangleBridgedMethodParams() {
32023204
return Str;
32033205
}
32043206

3205-
NodePointer Demangler::demangleGenericSpecialization(Node::Kind SpecKind) {
3207+
NodePointer Demangler::demangleGenericSpecialization(Node::Kind SpecKind,
3208+
NodePointer droppedArguments) {
32063209
NodePointer Spec = demangleSpecAttributes(SpecKind);
32073210
if (!Spec)
32083211
return nullptr;
3212+
3213+
if (droppedArguments) {
3214+
for (NodePointer a : *droppedArguments) {
3215+
Spec->addChild(a, *this);
3216+
}
3217+
}
3218+
32093219
NodePointer TyList = popTypeList();
32103220
if (!TyList)
32113221
return nullptr;
@@ -3216,6 +3226,24 @@ NodePointer Demangler::demangleGenericSpecialization(Node::Kind SpecKind) {
32163226
return Spec;
32173227
}
32183228

3229+
NodePointer Demangler::demangleGenericSpecializationWithDroppedArguments() {
3230+
pushBack();
3231+
NodePointer tmp = createNode(Node::Kind::GenericSpecialization);
3232+
while (nextIf('t')) {
3233+
int n = demangleNatural();
3234+
addChild(tmp, createNode(Node::Kind::DroppedArgument, n < 0 ? 0 : n + 1));
3235+
}
3236+
Node::Kind specKind;
3237+
switch (nextChar()) {
3238+
case 'g': specKind = Node::Kind::GenericSpecialization; break;
3239+
case 'G': specKind = Node::Kind::GenericSpecializationNotReAbstracted; break;
3240+
case 'B': specKind = Node::Kind::GenericSpecializationInResilienceDomain; break;
3241+
default:
3242+
return nullptr;
3243+
}
3244+
return demangleGenericSpecialization(specKind, tmp);
3245+
}
3246+
32193247
NodePointer Demangler::demangleFunctionSpecialization() {
32203248
NodePointer Spec = demangleSpecAttributes(
32213249
Node::Kind::FunctionSignatureSpecialization);
@@ -3429,21 +3457,16 @@ NodePointer Demangler::addFuncSpecParamNumber(NodePointer Param,
34293457
}
34303458

34313459
NodePointer Demangler::demangleSpecAttributes(Node::Kind SpecKind) {
3432-
bool metatypeParamsRemoved = nextIf('m');
34333460
bool isSerialized = nextIf('q');
34343461
bool asyncRemoved = nextIf('a');
34353462

34363463
int PassID = (int)nextChar() - '0';
34373464
if (PassID < 0 || PassID >= MAX_SPECIALIZATION_PASS) {
3438-
assert(false && "unexpected pass id");
34393465
return nullptr;
34403466
}
34413467

34423468
NodePointer SpecNd = createNode(SpecKind);
34433469

3444-
if (metatypeParamsRemoved)
3445-
SpecNd->addChild(createNode(Node::Kind::MetatypeParamsRemoved), *this);
3446-
34473470
if (isSerialized)
34483471
SpecNd->addChild(createNode(Node::Kind::IsSerialized),
34493472
*this);

lib/Demangling/NodePrinter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ class NodePrinter {
525525
case Node::Kind::SILBoxMutableField:
526526
case Node::Kind::SILBoxImmutableField:
527527
case Node::Kind::IsSerialized:
528-
case Node::Kind::MetatypeParamsRemoved:
528+
case Node::Kind::DroppedArgument:
529529
case Node::Kind::SpecializationPassID:
530530
case Node::Kind::Static:
531531
case Node::Kind::Subscript:
@@ -1290,7 +1290,7 @@ void NodePrinter::printSpecializationPrefix(NodePointer node,
12901290
for (NodePointer child : *node) {
12911291
switch (child->getKind()) {
12921292
case Node::Kind::SpecializationPassID:
1293-
case Node::Kind::MetatypeParamsRemoved:
1293+
case Node::Kind::DroppedArgument:
12941294
// We skip those nodes since it does not contain any
12951295
// information that is useful to our users.
12961296
break;
@@ -1836,8 +1836,8 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
18361836
case Node::Kind::IsSerialized:
18371837
Printer << "serialized";
18381838
return nullptr;
1839-
case Node::Kind::MetatypeParamsRemoved:
1840-
Printer << "metatypes-removed";
1839+
case Node::Kind::DroppedArgument:
1840+
Printer << "param" << Node->getIndex() << "-removed";
18411841
return nullptr;
18421842
case Node::Kind::GenericSpecializationParam:
18431843
print(Node->getChild(0), depth + 1);

lib/Demangling/OldRemangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ ManglingError Remangler::mangleAsyncRemoved(Node *node, unsigned depth) {
451451
return ManglingError::Success;
452452
}
453453

454-
ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
455-
Buffer << "m";
454+
ManglingError Remangler::mangleDroppedArgument(Node *node, unsigned depth) {
455+
Buffer << "t" << node->getIndex();
456456
return ManglingError::Success;
457457
}
458458

lib/Demangling/Remangler.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class Remangler : public RemanglerBase {
359359
}
360360

361361
ManglingError mangleGenericSpecializationNode(Node *node,
362-
const char *operatorStr,
362+
char specKind,
363363
unsigned depth);
364364
ManglingError mangleAnyNominalType(Node *node, unsigned depth);
365365
ManglingError mangleAnyGenericType(Node *node, StringRef TypeOp,
@@ -1693,7 +1693,7 @@ Remangler::mangleGenericPartialSpecializationNotReAbstracted(Node *node,
16931693
}
16941694

16951695
ManglingError
1696-
Remangler::mangleGenericSpecializationNode(Node *node, const char *operatorStr,
1696+
Remangler::mangleGenericSpecializationNode(Node *node, char specKind,
16971697
unsigned depth) {
16981698
bool FirstParam = true;
16991699
for (NodePointer Child : *node) {
@@ -1705,42 +1705,52 @@ Remangler::mangleGenericSpecializationNode(Node *node, const char *operatorStr,
17051705
DEMANGLER_ASSERT(
17061706
!FirstParam && "generic specialization with no substitutions", node);
17071707

1708-
Buffer << operatorStr;
1708+
Buffer << 'T';
17091709

17101710
for (NodePointer Child : *node) {
1711-
if (Child->getKind() != Node::Kind::GenericSpecializationParam)
1711+
if (Child->getKind() == Node::Kind::DroppedArgument)
17121712
RETURN_IF_ERROR(mangle(Child, depth + 1));
17131713
}
17141714

1715+
1716+
Buffer << specKind;
1717+
1718+
for (NodePointer Child : *node) {
1719+
if (Child->getKind() != Node::Kind::GenericSpecializationParam &&
1720+
Child->getKind() != Node::Kind::DroppedArgument) {
1721+
RETURN_IF_ERROR(mangle(Child, depth + 1));
1722+
}
1723+
}
1724+
17151725
return ManglingError::Success;
17161726
}
17171727

17181728
ManglingError Remangler::mangleGenericSpecialization(Node *node,
17191729
unsigned depth) {
1720-
return mangleGenericSpecializationNode(node, "Tg", depth + 1);
1730+
return mangleGenericSpecializationNode(node, 'g', depth + 1);
17211731
}
17221732

17231733
ManglingError
17241734
Remangler::mangleGenericSpecializationPrespecialized(Node *node,
17251735
unsigned depth) {
1726-
return mangleGenericSpecializationNode(node, "Ts", depth + 1);
1736+
return mangleGenericSpecializationNode(node, 's', depth + 1);
17271737
}
17281738

17291739
ManglingError
17301740
Remangler::mangleGenericSpecializationNotReAbstracted(Node *node,
17311741
unsigned depth) {
1732-
return mangleGenericSpecializationNode(node, "TG", depth + 1);
1742+
return mangleGenericSpecializationNode(node, 'G', depth + 1);
17331743
}
17341744

17351745
ManglingError
17361746
Remangler::mangleGenericSpecializationInResilienceDomain(Node *node,
17371747
unsigned depth) {
1738-
return mangleGenericSpecializationNode(node, "TB", depth + 1);
1748+
return mangleGenericSpecializationNode(node, 'B', depth + 1);
17391749
}
17401750

17411751
ManglingError Remangler::mangleInlinedGenericFunction(Node *node,
17421752
unsigned depth) {
1743-
return mangleGenericSpecializationNode(node, "Ti", depth + 1);
1753+
return mangleGenericSpecializationNode(node, 'i', depth + 1);
17441754
}
17451755

17461756
ManglingError Remangler::mangleGenericSpecializationParam(Node *node,
@@ -3067,8 +3077,11 @@ ManglingError Remangler::mangleAsyncRemoved(Node *node, unsigned depth) {
30673077
return ManglingError::Success;
30683078
}
30693079

3070-
ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
3071-
Buffer << 'm';
3080+
ManglingError Remangler::mangleDroppedArgument(Node *node, unsigned depth) {
3081+
Buffer << "t";
3082+
int n = node->getIndex();
3083+
if (n > 0)
3084+
Buffer << (n-1);
30723085
return ManglingError::Success;
30733086
}
30743087

0 commit comments

Comments
 (0)