Skip to content

Commit 224cac5

Browse files
authored
Merge pull request #59916 from eeckstein/perf-annotation-fixes
Several fixes for performance diagnostics
2 parents 4ff3863 + abfa0a3 commit 224cac5

23 files changed

+461
-73
lines changed

docs/ABI/Mangling.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ Some kinds need arguments, which precede ``Tf``.
11331133
spec-arg ::= identifier
11341134
spec-arg ::= type
11351135

1136-
SPEC-INFO ::= FRAGILE? PASSID
1136+
SPEC-INFO ::= MT-REMOVED? FRAGILE? PASSID
11371137

11381138
PASSID ::= '0' // AllocBoxToStack,
11391139
PASSID ::= '1' // ClosureSpecializer,
@@ -1142,6 +1142,8 @@ Some kinds need arguments, which precede ``Tf``.
11421142
PASSID ::= '4' // FunctionSignatureOpts,
11431143
PASSID ::= '5' // GenericSpecializer,
11441144

1145+
MT-REMOVED ::= 'm' // non-generic metatype arguments are removed in the specialized function
1146+
11451147
FRAGILE ::= 'q'
11461148

11471149
ARG-SPEC-KIND ::= 'n' // Unmodified argument

include/swift/Demangling/DemangleNodes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,7 @@ NODE(UniqueExtendedExistentialTypeShapeSymbolicReference)
344344
NODE(NonUniqueExtendedExistentialTypeShapeSymbolicReference)
345345
NODE(SymbolicExtendedExistentialType)
346346

347+
NODE(MetatypeParamsRemoved)
348+
347349
#undef CONTEXT_NODE
348350
#undef NODE

include/swift/SIL/GenericSpecializationMangler.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,19 @@ class GenericSpecializationMangler : public SpecializationMangler {
9090

9191
std::string mangleNotReabstracted(SubstitutionMap subs);
9292

93-
std::string mangleReabstracted(SubstitutionMap subs, bool alternativeMangling);
93+
/// Mangle a generic specialization with re-abstracted parameters.
94+
///
95+
/// Re-abstracted means that indirect (generic) parameters/returns are
96+
/// converted to direct parameters/returns.
97+
///
98+
/// This is the default for generic specializations.
99+
///
100+
/// \param alternativeMangling true for specialized functions with a
101+
/// differet resilience expansion.
102+
/// \param metatyeParamsRemoved true if non-generic metatype parameters are
103+
/// removed in the specialized function.
104+
std::string mangleReabstracted(SubstitutionMap subs, bool alternativeMangling,
105+
bool metatyeParamsRemoved = false);
94106

95107
std::string mangleForDebugInfo(GenericSignature sig, SubstitutionMap subs,
96108
bool forInlining);

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class ReabstractionInfo {
6262
/// argument has a trivial type.
6363
SmallBitVector TrivialArgs;
6464

65+
/// A 1-bit means that the argument is a metatype argument. The argument is
66+
/// dropped and replaced by a `metatype` instruction in the entry block.
67+
/// Only used if `dropMetatypeArgs` is true.
68+
SmallBitVector droppedMetatypeArgs;
69+
6570
/// Set to true if the function has a re-abstracted (= converted from
6671
/// indirect to direct) resilient argument or return type. This can happen if
6772
/// the function is compiled within the type's resilience domain, i.e. in
@@ -79,6 +84,10 @@ class ReabstractionInfo {
7984
/// specializer.
8085
bool ConvertIndirectToDirect;
8186

87+
/// If true, drop metatype arguments.
88+
/// See `droppedMetatypeArgs`.
89+
bool dropMetatypeArgs = false;
90+
8291
/// The first NumResults bits in Conversions refer to formal indirect
8392
/// out-parameters.
8493
unsigned NumFormalIndirectResults;
@@ -191,6 +200,7 @@ class ReabstractionInfo {
191200
SubstitutionMap ParamSubs,
192201
IsSerialized_t Serialized,
193202
bool ConvertIndirectToDirect = true,
203+
bool dropMetatypeArgs = false,
194204
OptRemark::Emitter *ORE = nullptr);
195205

196206
/// Constructs the ReabstractionInfo for generic function \p Callee with
@@ -243,6 +253,16 @@ class ReabstractionInfo {
243253
/// Returns true if there are any conversions from indirect to direct values.
244254
bool hasConversions() const { return Conversions.any(); }
245255

256+
/// Returns true if the argument at `ArgIdx` is a dropped metatype argument.
257+
/// See `droppedMetatypeArgs`.
258+
bool isDroppedMetatypeArg(unsigned ArgIdx) const {
259+
return droppedMetatypeArgs.test(ArgIdx);
260+
}
261+
262+
/// Returns true if there are any dropped metatype arguments.
263+
/// See `droppedMetatypeArgs`.
264+
bool hasDroppedMetatypeArgs() const { return droppedMetatypeArgs.any(); }
265+
246266
/// Remove the arguments of a partial apply, leaving the arguments for the
247267
/// partial apply result function.
248268
void prunePartialApplyArgs(unsigned numPartialApplyArgs) {

lib/Demangling/Demangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,13 +3040,18 @@ NodePointer Demangler::addFuncSpecParamNumber(NodePointer Param,
30403040
}
30413041

30423042
NodePointer Demangler::demangleSpecAttributes(Node::Kind SpecKind) {
3043+
bool metatypeParamsRemoved = nextIf('m');
30433044
bool isSerialized = nextIf('q');
30443045

30453046
int PassID = (int)nextChar() - '0';
30463047
if (PassID < 0 || PassID > 9)
30473048
return nullptr;
30483049

30493050
NodePointer SpecNd = createNode(SpecKind);
3051+
3052+
if (metatypeParamsRemoved)
3053+
SpecNd->addChild(createNode(Node::Kind::MetatypeParamsRemoved), *this);
3054+
30503055
if (isSerialized)
30513056
SpecNd->addChild(createNode(Node::Kind::IsSerialized),
30523057
*this);

lib/Demangling/NodePrinter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ class NodePrinter {
496496
case Node::Kind::SILBoxMutableField:
497497
case Node::Kind::SILBoxImmutableField:
498498
case Node::Kind::IsSerialized:
499+
case Node::Kind::MetatypeParamsRemoved:
499500
case Node::Kind::SpecializationPassID:
500501
case Node::Kind::Static:
501502
case Node::Kind::Subscript:
@@ -1127,7 +1128,8 @@ void NodePrinter::printSpecializationPrefix(NodePointer node,
11271128
for (NodePointer child : *node) {
11281129
switch (child->getKind()) {
11291130
case Node::Kind::SpecializationPassID:
1130-
// We skip the SpecializationPassID since it does not contain any
1131+
case Node::Kind::MetatypeParamsRemoved:
1132+
// We skip those nodes since it does not contain any
11311133
// information that is useful to our users.
11321134
break;
11331135

@@ -1549,6 +1551,9 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
15491551
case Node::Kind::IsSerialized:
15501552
Printer << "serialized";
15511553
return nullptr;
1554+
case Node::Kind::MetatypeParamsRemoved:
1555+
Printer << "metatypes-removed";
1556+
return nullptr;
15521557
case Node::Kind::GenericSpecializationParam:
15531558
print(Node->getChild(0), depth + 1);
15541559
for (unsigned i = 1, e = Node->getNumChildren(); i < e; ++i) {

lib/Demangling/OldRemangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,11 @@ ManglingError Remangler::mangleIsSerialized(Node *node, unsigned depth) {
446446
return ManglingError::Success;
447447
}
448448

449+
ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
450+
Buffer << "m";
451+
return ManglingError::Success;
452+
}
453+
449454
ManglingError
450455
Remangler::mangleFunctionSignatureSpecializationReturn(Node *node,
451456
unsigned depth) {

lib/Demangling/Remangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,11 @@ ManglingError Remangler::mangleIsSerialized(Node *node, unsigned depth) {
28012801
return ManglingError::Success;
28022802
}
28032803

2804+
ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
2805+
Buffer << 'm';
2806+
return ManglingError::Success;
2807+
}
2808+
28042809
ManglingError Remangler::mangleStatic(Node *node, unsigned depth) {
28052810
RETURN_IF_ERROR(mangleSingleChildNode(node, depth + 1));
28062811
Buffer << 'Z';

lib/SIL/Utils/GenericSpecializationMangler.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,18 @@ mangleNotReabstracted(SubstitutionMap subs) {
105105
}
106106

107107
std::string GenericSpecializationMangler::
108-
mangleReabstracted(SubstitutionMap subs, bool alternativeMangling) {
108+
mangleReabstracted(SubstitutionMap subs, bool alternativeMangling,
109+
bool metatyeParamsRemoved) {
109110
beginMangling();
110111
appendSubstitutions(getGenericSignature(), subs);
111112

112113
// See ReabstractionInfo::hasConvertedResilientParams for why and when to use
113114
// the alternative mangling.
114-
appendSpecializationOperator(alternativeMangling ? "TB" : "Tg");
115+
if (metatyeParamsRemoved) {
116+
appendSpecializationOperator(alternativeMangling ? "TBm" : "Tgm");
117+
} else {
118+
appendSpecializationOperator(alternativeMangling ? "TB" : "Tg");
119+
}
115120
return finalize();
116121
}
117122

lib/SIL/Utils/InstructionUtils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
596596
case SILInstructionKind::AllocGlobalInst: {
597597
SILType glTy = cast<AllocGlobalInst>(inst)->getReferencedGlobal()->
598598
getLoweredType();
599+
if (glTy.isLoadable(*inst->getFunction()))
600+
return RuntimeEffect::NoEffect;
599601
if (glTy.hasOpaqueArchetype()) {
600602
impactType = glTy;
601603
return RuntimeEffect::Allocating | RuntimeEffect::MetaData;

lib/SILOptimizer/IRGenTransforms/TargetConstantFolding.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ class TargetConstantFolding : public SILModuleTransform {
6767
// Scan all instructions in the module for constant foldable instructions.
6868
for (SILFunction &function : *module) {
6969

70-
if (!function.shouldOptimize())
71-
continue;
72-
7370
bool changed = false;
7471
for (SILBasicBlock &block : function) {
7572
InstructionDeleter deleter;

0 commit comments

Comments
 (0)