Skip to content

Several fixes for performance diagnostics #59916

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 10 commits into from
Jul 7, 2022
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
4 changes: 3 additions & 1 deletion docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ Some kinds need arguments, which precede ``Tf``.
spec-arg ::= identifier
spec-arg ::= type

SPEC-INFO ::= FRAGILE? PASSID
SPEC-INFO ::= MT-REMOVED? FRAGILE? PASSID

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

MT-REMOVED ::= 'm' // non-generic metatype arguments are removed in the specialized function

FRAGILE ::= 'q'

ARG-SPEC-KIND ::= 'n' // Unmodified argument
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,7 @@ NODE(UniqueExtendedExistentialTypeShapeSymbolicReference)
NODE(NonUniqueExtendedExistentialTypeShapeSymbolicReference)
NODE(SymbolicExtendedExistentialType)

NODE(MetatypeParamsRemoved)

#undef CONTEXT_NODE
#undef NODE
14 changes: 13 additions & 1 deletion include/swift/SIL/GenericSpecializationMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ class GenericSpecializationMangler : public SpecializationMangler {

std::string mangleNotReabstracted(SubstitutionMap subs);

std::string mangleReabstracted(SubstitutionMap subs, bool alternativeMangling);
/// Mangle a generic specialization with re-abstracted parameters.
///
/// Re-abstracted means that indirect (generic) parameters/returns are
/// converted to direct parameters/returns.
///
/// This is the default for generic specializations.
///
/// \param alternativeMangling true for specialized functions with a
/// differet resilience expansion.
/// \param metatyeParamsRemoved true if non-generic metatype parameters are
/// removed in the specialized function.
std::string mangleReabstracted(SubstitutionMap subs, bool alternativeMangling,
bool metatyeParamsRemoved = false);

std::string mangleForDebugInfo(GenericSignature sig, SubstitutionMap subs,
bool forInlining);
Expand Down
20 changes: 20 additions & 0 deletions include/swift/SILOptimizer/Utils/Generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class ReabstractionInfo {
/// argument has a trivial type.
SmallBitVector TrivialArgs;

/// A 1-bit means that the argument is a metatype argument. The argument is
/// dropped and replaced by a `metatype` instruction in the entry block.
/// Only used if `dropMetatypeArgs` is true.
SmallBitVector droppedMetatypeArgs;

/// Set to true if the function has a re-abstracted (= converted from
/// indirect to direct) resilient argument or return type. This can happen if
/// the function is compiled within the type's resilience domain, i.e. in
Expand All @@ -79,6 +84,10 @@ class ReabstractionInfo {
/// specializer.
bool ConvertIndirectToDirect;

/// If true, drop metatype arguments.
/// See `droppedMetatypeArgs`.
bool dropMetatypeArgs = false;

/// The first NumResults bits in Conversions refer to formal indirect
/// out-parameters.
unsigned NumFormalIndirectResults;
Expand Down Expand Up @@ -191,6 +200,7 @@ class ReabstractionInfo {
SubstitutionMap ParamSubs,
IsSerialized_t Serialized,
bool ConvertIndirectToDirect = true,
bool dropMetatypeArgs = false,
OptRemark::Emitter *ORE = nullptr);

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

/// Returns true if the argument at `ArgIdx` is a dropped metatype argument.
/// See `droppedMetatypeArgs`.
bool isDroppedMetatypeArg(unsigned ArgIdx) const {
return droppedMetatypeArgs.test(ArgIdx);
}

/// Returns true if there are any dropped metatype arguments.
/// See `droppedMetatypeArgs`.
bool hasDroppedMetatypeArgs() const { return droppedMetatypeArgs.any(); }

/// Remove the arguments of a partial apply, leaving the arguments for the
/// partial apply result function.
void prunePartialApplyArgs(unsigned numPartialApplyArgs) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3040,13 +3040,18 @@ NodePointer Demangler::addFuncSpecParamNumber(NodePointer Param,
}

NodePointer Demangler::demangleSpecAttributes(Node::Kind SpecKind) {
bool metatypeParamsRemoved = nextIf('m');
bool isSerialized = nextIf('q');

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

NodePointer SpecNd = createNode(SpecKind);

if (metatypeParamsRemoved)
SpecNd->addChild(createNode(Node::Kind::MetatypeParamsRemoved), *this);

if (isSerialized)
SpecNd->addChild(createNode(Node::Kind::IsSerialized),
*this);
Expand Down
7 changes: 6 additions & 1 deletion lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ class NodePrinter {
case Node::Kind::SILBoxMutableField:
case Node::Kind::SILBoxImmutableField:
case Node::Kind::IsSerialized:
case Node::Kind::MetatypeParamsRemoved:
case Node::Kind::SpecializationPassID:
case Node::Kind::Static:
case Node::Kind::Subscript:
Expand Down Expand Up @@ -1127,7 +1128,8 @@ void NodePrinter::printSpecializationPrefix(NodePointer node,
for (NodePointer child : *node) {
switch (child->getKind()) {
case Node::Kind::SpecializationPassID:
// We skip the SpecializationPassID since it does not contain any
case Node::Kind::MetatypeParamsRemoved:
// We skip those nodes since it does not contain any
// information that is useful to our users.
break;

Expand Down Expand Up @@ -1549,6 +1551,9 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
case Node::Kind::IsSerialized:
Printer << "serialized";
return nullptr;
case Node::Kind::MetatypeParamsRemoved:
Printer << "metatypes-removed";
return nullptr;
case Node::Kind::GenericSpecializationParam:
print(Node->getChild(0), depth + 1);
for (unsigned i = 1, e = Node->getNumChildren(); i < e; ++i) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@ ManglingError Remangler::mangleIsSerialized(Node *node, unsigned depth) {
return ManglingError::Success;
}

ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
Buffer << "m";
return ManglingError::Success;
}

ManglingError
Remangler::mangleFunctionSignatureSpecializationReturn(Node *node,
unsigned depth) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,11 @@ ManglingError Remangler::mangleIsSerialized(Node *node, unsigned depth) {
return ManglingError::Success;
}

ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
Buffer << 'm';
return ManglingError::Success;
}

ManglingError Remangler::mangleStatic(Node *node, unsigned depth) {
RETURN_IF_ERROR(mangleSingleChildNode(node, depth + 1));
Buffer << 'Z';
Expand Down
9 changes: 7 additions & 2 deletions lib/SIL/Utils/GenericSpecializationMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,18 @@ mangleNotReabstracted(SubstitutionMap subs) {
}

std::string GenericSpecializationMangler::
mangleReabstracted(SubstitutionMap subs, bool alternativeMangling) {
mangleReabstracted(SubstitutionMap subs, bool alternativeMangling,
bool metatyeParamsRemoved) {
beginMangling();
appendSubstitutions(getGenericSignature(), subs);

// See ReabstractionInfo::hasConvertedResilientParams for why and when to use
// the alternative mangling.
appendSpecializationOperator(alternativeMangling ? "TB" : "Tg");
if (metatyeParamsRemoved) {
appendSpecializationOperator(alternativeMangling ? "TBm" : "Tgm");
} else {
appendSpecializationOperator(alternativeMangling ? "TB" : "Tg");
}
return finalize();
}

Expand Down
2 changes: 2 additions & 0 deletions lib/SIL/Utils/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
case SILInstructionKind::AllocGlobalInst: {
SILType glTy = cast<AllocGlobalInst>(inst)->getReferencedGlobal()->
getLoweredType();
if (glTy.isLoadable(*inst->getFunction()))
return RuntimeEffect::NoEffect;
if (glTy.hasOpaqueArchetype()) {
impactType = glTy;
return RuntimeEffect::Allocating | RuntimeEffect::MetaData;
Expand Down
3 changes: 0 additions & 3 deletions lib/SILOptimizer/IRGenTransforms/TargetConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class TargetConstantFolding : public SILModuleTransform {
// Scan all instructions in the module for constant foldable instructions.
for (SILFunction &function : *module) {

if (!function.shouldOptimize())
continue;

bool changed = false;
for (SILBasicBlock &block : function) {
InstructionDeleter deleter;
Expand Down
Loading