Skip to content

[KeyInstr] Add DISubprogram::keyInstructions bit #144107

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,8 @@ CGDebugInfo::createInlinedSubprogram(StringRef FuncName,
/*ScopeLine=*/0,
/*Flags=*/llvm::DINode::FlagArtificial,
/*SPFlags=*/llvm::DISubprogram::SPFlagDefinition,
/*TParams=*/nullptr, /*ThrownTypes=*/nullptr, /*Annotations=*/nullptr);
/*TParams=*/nullptr, /*ThrownTypes=*/nullptr, /*Annotations=*/nullptr,
nullptr, StringRef(), CGM.getCodeGenOpts().DebugKeyInstructions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awkward style consistency -- all the other arguments have names attached except the ones you're adding here; also in other hunks.

}

return SP;
Expand Down Expand Up @@ -2284,7 +2285,8 @@ llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
llvm::DISubprogram *SP = DBuilder.createMethod(
RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
MethodTy, VIndex, ThisAdjustment, ContainingType, Flags, SPFlags,
TParamsArray.get());
TParamsArray.get(), /*ThrownTypes*/ nullptr,
CGM.getCodeGenOpts().DebugKeyInstructions);

SPCache[Method->getCanonicalDecl()].reset(SP);

Expand Down Expand Up @@ -4350,7 +4352,9 @@ llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD,
return DBuilder.createFunction(
DContext, Name, LinkageName, Unit, Line,
getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags,
TParamsArray.get(), getFunctionDeclaration(FD));
TParamsArray.get(), getFunctionDeclaration(FD), /*ThrownTypes*/ nullptr,
/*Annotations*/ nullptr, /*TargetFuncName*/ "",
CGM.getCodeGenOpts().DebugKeyInstructions);
}

llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
Expand Down Expand Up @@ -4686,8 +4690,9 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
llvm::DISubprogram *SP = DBuilder.createFunction(
FDContext, Name, LinkageName, Unit, LineNo, DIFnType, ScopeLine,
FlagsForDef, SPFlagsForDef, TParamsArray.get(), Decl, nullptr,
Annotations);
Annotations, "", CGM.getCodeGenOpts().DebugKeyInstructions);
Fn->setSubprogram(SP);

// We might get here with a VarDecl in the case we're generating
// code for the initialization of globals. Do not record these decls
// as they will overwrite the actual VarDecl Decl in the cache.
Expand Down Expand Up @@ -4746,9 +4751,12 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,

llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
llvm::DISubroutineType *STy = getOrCreateFunctionType(D, FnType, Unit);
// Key Instructions: Don't set flag on declarations.
assert(~SPFlags & llvm::DISubprogram::SPFlagDefinition);
llvm::DISubprogram *SP = DBuilder.createFunction(
FDContext, Name, LinkageName, Unit, LineNo, STy, ScopeLine, Flags,
SPFlags, TParamsArray.get(), nullptr, nullptr, Annotations);
SPFlags, TParamsArray.get(), nullptr, nullptr, Annotations,
/*TargetFunctionName*/ "", /*UseKeyInstructions*/ false);

// Preserve btf_decl_tag attributes for parameters of extern functions
// for BPF target. The parameters created in this loop are attached as
Expand Down
23 changes: 13 additions & 10 deletions llvm/include/llvm/IR/DIBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -894,14 +894,17 @@ namespace llvm {
/// \param Annotations Attribute Annotations.
/// \param TargetFuncName The name of the target function if this is
/// a trampoline.
/// \param UseKeyInstructions Instruct DWARF emission to interpret Key
/// Instructions metadata on instructions to determine is_stmt placement.
LLVM_ABI DISubprogram *createFunction(
DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File,
unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
DINode::DIFlags Flags = DINode::FlagZero,
DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero,
DITemplateParameterArray TParams = nullptr,
DISubprogram *Decl = nullptr, DITypeArray ThrownTypes = nullptr,
DINodeArray Annotations = nullptr, StringRef TargetFuncName = "");
DINodeArray Annotations = nullptr, StringRef TargetFuncName = "",
bool UseKeyInstructions = false);

/// Identical to createFunction,
/// except that the resulting DbgNode is meant to be RAUWed.
Expand Down Expand Up @@ -932,15 +935,15 @@ namespace llvm {
/// \param SPFlags Additional flags specific to subprograms.
/// \param TParams Function template parameters.
/// \param ThrownTypes Exception types this function may throw.
LLVM_ABI DISubprogram *
createMethod(DIScope *Scope, StringRef Name, StringRef LinkageName,
DIFile *File, unsigned LineNo, DISubroutineType *Ty,
unsigned VTableIndex = 0, int ThisAdjustment = 0,
DIType *VTableHolder = nullptr,
DINode::DIFlags Flags = DINode::FlagZero,
DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero,
DITemplateParameterArray TParams = nullptr,
DITypeArray ThrownTypes = nullptr);
/// \param UseKeyInstructions Enable Key Instructions debug info.
LLVM_ABI DISubprogram *createMethod(
DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File,
unsigned LineNo, DISubroutineType *Ty, unsigned VTableIndex = 0,
int ThisAdjustment = 0, DIType *VTableHolder = nullptr,
DINode::DIFlags Flags = DINode::FlagZero,
DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero,
DITemplateParameterArray TParams = nullptr,
DITypeArray ThrownTypes = nullptr, bool UseKeyInstructions = false);

/// Create common block entry for a Fortran common block.
/// \param Scope Scope of this common block.
Expand Down
30 changes: 19 additions & 11 deletions llvm/include/llvm/IR/DebugInfoMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ class DILocalScope : public DIScope {
}
};

/// Subprogram description.
/// Subprogram description. Uses SubclassData1.
class DISubprogram : public DILocalScope {
friend class LLVMContextImpl;
friend class MDNode;
Expand Down Expand Up @@ -2009,7 +2009,8 @@ class DISubprogram : public DILocalScope {

DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment,
DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops);
DIFlags Flags, DISPFlags SPFlags, bool UsesKeyInstructions,
ArrayRef<Metadata *> Ops);
~DISubprogram() = default;

static DISubprogram *
Expand All @@ -2021,24 +2022,26 @@ class DISubprogram : public DILocalScope {
DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
DINodeArray RetainedNodes, DITypeArray ThrownTypes,
DINodeArray Annotations, StringRef TargetFuncName,
StorageType Storage, bool ShouldCreate = true) {
bool UsesKeyInstructions, StorageType Storage,
bool ShouldCreate = true) {
return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
getCanonicalMDString(Context, LinkageName), File, Line, Type,
ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
Flags, SPFlags, Unit, TemplateParams.get(), Declaration,
RetainedNodes.get(), ThrownTypes.get(), Annotations.get(),
getCanonicalMDString(Context, TargetFuncName),
Storage, ShouldCreate);
UsesKeyInstructions, Storage, ShouldCreate);
}

LLVM_ABI static DISubprogram *
getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex,
int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
Metadata *TemplateParams, Metadata *Declaration,
Metadata *RetainedNodes, Metadata *ThrownTypes, Metadata *Annotations,
MDString *TargetFuncName, StorageType Storage,
bool ShouldCreate = true);
MDString *TargetFuncName, bool UsesKeyInstructions,
StorageType Storage, bool ShouldCreate = true);

TempDISubprogram cloneImpl() const {
return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
Expand All @@ -2047,7 +2050,7 @@ class DISubprogram : public DILocalScope {
getThisAdjustment(), getFlags(), getSPFlags(),
getUnit(), getTemplateParams(), getDeclaration(),
getRetainedNodes(), getThrownTypes(), getAnnotations(),
getTargetFuncName());
getTargetFuncName(), getKeyInstructionsEnabled());
}

public:
Expand All @@ -2060,10 +2063,11 @@ class DISubprogram : public DILocalScope {
DITemplateParameterArray TemplateParams = nullptr,
DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
DITypeArray ThrownTypes = nullptr, DINodeArray Annotations = nullptr,
StringRef TargetFuncName = ""),
StringRef TargetFuncName = "", bool UsesKeyInstructions = false),
(Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName))
Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName,
UsesKeyInstructions))

DEFINE_MDNODE_GET(
DISubprogram,
Expand All @@ -2073,10 +2077,12 @@ class DISubprogram : public DILocalScope {
DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
Metadata *TemplateParams = nullptr, Metadata *Declaration = nullptr,
Metadata *RetainedNodes = nullptr, Metadata *ThrownTypes = nullptr,
Metadata *Annotations = nullptr, MDString *TargetFuncName = nullptr),
Metadata *Annotations = nullptr, MDString *TargetFuncName = nullptr,
bool UsesKeyInstructions = false),
(Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName))
Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName,
UsesKeyInstructions))

TempDISubprogram clone() const { return cloneImpl(); }

Expand All @@ -2087,6 +2093,8 @@ class DISubprogram : public DILocalScope {
return NewSP;
}

bool getKeyInstructionsEnabled() const { return SubclassData1; }

public:
unsigned getLine() const { return Line; }
unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; }
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5821,7 +5821,8 @@ bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) {
OPTIONAL(retainedNodes, MDField, ); \
OPTIONAL(thrownTypes, MDField, ); \
OPTIONAL(annotations, MDField, ); \
OPTIONAL(targetFuncName, MDStringField, );
OPTIONAL(targetFuncName, MDStringField, ); \
OPTIONAL(keyInstructions, MDBoolField, );
PARSE_MD_FIELDS();
#undef VISIT_MD_FIELDS

Expand All @@ -5841,7 +5842,7 @@ bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) {
type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val,
thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val,
declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val,
targetFuncName.Val));
targetFuncName.Val, keyInstructions.Val));
return false;
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,7 @@ static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
Printer.printMetadata("annotations", N->getRawAnnotations());
Printer.printString("targetFuncName", N->getTargetFuncName());
Printer.printBool("keyInstructions", N->getKeyInstructionsEnabled(), false);
Out << ")";
}

Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/IR/DIBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,14 +933,14 @@ DISubprogram *DIBuilder::createFunction(
unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags,
DITemplateParameterArray TParams, DISubprogram *Decl,
DITypeArray ThrownTypes, DINodeArray Annotations,
StringRef TargetFuncName) {
DITypeArray ThrownTypes, DINodeArray Annotations, StringRef TargetFuncName,
bool UseKeyInstructions) {
bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
auto *Node = getSubprogram(
/*IsDistinct=*/IsDefinition, VMContext, getNonCompileUnitScope(Context),
Name, LinkageName, File, LineNo, Ty, ScopeLine, nullptr, 0, 0, Flags,
SPFlags, IsDefinition ? CUNode : nullptr, TParams, Decl, nullptr,
ThrownTypes, Annotations, TargetFuncName);
ThrownTypes, Annotations, TargetFuncName, UseKeyInstructions);

AllSubprograms.push_back(Node);
trackIfUnresolved(Node);
Expand All @@ -967,7 +967,7 @@ DISubprogram *DIBuilder::createMethod(
unsigned LineNo, DISubroutineType *Ty, unsigned VIndex, int ThisAdjustment,
DIType *VTableHolder, DINode::DIFlags Flags,
DISubprogram::DISPFlags SPFlags, DITemplateParameterArray TParams,
DITypeArray ThrownTypes) {
DITypeArray ThrownTypes, bool UseKeyInstructions) {
assert(getNonCompileUnitScope(Context) &&
"Methods should have both a Context and a context that isn't "
"the compile unit.");
Expand All @@ -977,7 +977,7 @@ DISubprogram *DIBuilder::createMethod(
/*IsDistinct=*/IsDefinition, VMContext, cast<DIScope>(Context), Name,
LinkageName, F, LineNo, Ty, LineNo, VTableHolder, VIndex, ThisAdjustment,
Flags, SPFlags, IsDefinition ? CUNode : nullptr, TParams, nullptr,
nullptr, ThrownTypes);
nullptr, ThrownTypes, nullptr, "", IsDefinition && UseKeyInstructions);

AllSubprograms.push_back(SP);
trackIfUnresolved(SP);
Expand Down
15 changes: 8 additions & 7 deletions llvm/lib/IR/DebugInfoMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,11 +1293,12 @@ const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK) {
DISubprogram::DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
unsigned ScopeLine, unsigned VirtualIndex,
int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags,
ArrayRef<Metadata *> Ops)
bool UsesKeyInstructions, ArrayRef<Metadata *> Ops)
: DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram, Ops),
Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex),
ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags) {
static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range");
SubclassData1 = UsesKeyInstructions;
}
DISubprogram::DISPFlags
DISubprogram::toSPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized,
Expand Down Expand Up @@ -1396,7 +1397,7 @@ DISubprogram *DISubprogram::getImpl(
int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
Metadata *TemplateParams, Metadata *Declaration, Metadata *RetainedNodes,
Metadata *ThrownTypes, Metadata *Annotations, MDString *TargetFuncName,
StorageType Storage, bool ShouldCreate) {
bool UsesKeyInstructions, StorageType Storage, bool ShouldCreate) {
assert(isCanonical(Name) && "Expected canonical MDString");
assert(isCanonical(LinkageName) && "Expected canonical MDString");
assert(isCanonical(TargetFuncName) && "Expected canonical MDString");
Expand All @@ -1405,7 +1406,7 @@ DISubprogram *DISubprogram::getImpl(
ContainingType, VirtualIndex, ThisAdjustment, Flags,
SPFlags, Unit, TemplateParams, Declaration,
RetainedNodes, ThrownTypes, Annotations,
TargetFuncName));
TargetFuncName, UsesKeyInstructions));
SmallVector<Metadata *, 13> Ops = {
File, Scope, Name, LinkageName,
Type, Unit, Declaration, RetainedNodes,
Expand All @@ -1425,10 +1426,10 @@ DISubprogram *DISubprogram::getImpl(
}
}
}
DEFINE_GETIMPL_STORE_N(
DISubprogram,
(Line, ScopeLine, VirtualIndex, ThisAdjustment, Flags, SPFlags), Ops,
Ops.size());
DEFINE_GETIMPL_STORE_N(DISubprogram,
(Line, ScopeLine, VirtualIndex, ThisAdjustment, Flags,
SPFlags, UsesKeyInstructions),
Ops, Ops.size());
}

bool DISubprogram::describes(const Function *F) const {
Expand Down
18 changes: 11 additions & 7 deletions llvm/lib/IR/LLVMContextImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,8 @@ template <> struct MDNodeKeyImpl<DISubprogram> {
MDString *LinkageName;
Metadata *File;
unsigned Line;
Metadata *Type;
unsigned ScopeLine;
Metadata *Type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is space packing rather than being related to a functional changes, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, 136 -> 128 bytes (144 -> 136 with the new bool). I don't think it was important to performance, but seemed a harmless improvement. ymmv?

Metadata *ContainingType;
unsigned VirtualIndex;
int ThisAdjustment;
Expand All @@ -936,6 +936,7 @@ template <> struct MDNodeKeyImpl<DISubprogram> {
Metadata *ThrownTypes;
Metadata *Annotations;
MDString *TargetFuncName;
bool UsesKeyInstructions;

MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
Metadata *File, unsigned Line, Metadata *Type,
Expand All @@ -944,18 +945,19 @@ template <> struct MDNodeKeyImpl<DISubprogram> {
unsigned SPFlags, Metadata *Unit, Metadata *TemplateParams,
Metadata *Declaration, Metadata *RetainedNodes,
Metadata *ThrownTypes, Metadata *Annotations,
MDString *TargetFuncName)
MDString *TargetFuncName, bool UsesKeyInstructions)
: Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
Line(Line), Type(Type), ScopeLine(ScopeLine),
Line(Line), ScopeLine(ScopeLine), Type(Type),
ContainingType(ContainingType), VirtualIndex(VirtualIndex),
ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags),
Unit(Unit), TemplateParams(TemplateParams), Declaration(Declaration),
RetainedNodes(RetainedNodes), ThrownTypes(ThrownTypes),
Annotations(Annotations), TargetFuncName(TargetFuncName) {}
Annotations(Annotations), TargetFuncName(TargetFuncName),
UsesKeyInstructions(UsesKeyInstructions) {}
MDNodeKeyImpl(const DISubprogram *N)
: Scope(N->getRawScope()), Name(N->getRawName()),
LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
Line(N->getLine()), Type(N->getRawType()), ScopeLine(N->getScopeLine()),
Line(N->getLine()), ScopeLine(N->getScopeLine()), Type(N->getRawType()),
ContainingType(N->getRawContainingType()),
VirtualIndex(N->getVirtualIndex()),
ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()),
Expand All @@ -965,7 +967,8 @@ template <> struct MDNodeKeyImpl<DISubprogram> {
RetainedNodes(N->getRawRetainedNodes()),
ThrownTypes(N->getRawThrownTypes()),
Annotations(N->getRawAnnotations()),
TargetFuncName(N->getRawTargetFuncName()) {}
TargetFuncName(N->getRawTargetFuncName()),
UsesKeyInstructions(N->getKeyInstructionsEnabled()) {}

bool isKeyOf(const DISubprogram *RHS) const {
return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
Expand All @@ -982,7 +985,8 @@ template <> struct MDNodeKeyImpl<DISubprogram> {
RetainedNodes == RHS->getRawRetainedNodes() &&
ThrownTypes == RHS->getRawThrownTypes() &&
Annotations == RHS->getRawAnnotations() &&
TargetFuncName == RHS->getRawTargetFuncName();
TargetFuncName == RHS->getRawTargetFuncName() &&
UsesKeyInstructions == RHS->getKeyInstructionsEnabled();
}

bool isDefinition() const { return SPFlags & DISubprogram::SPFlagDefinition; }
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3160,6 +3160,12 @@ void Verifier::visitFunction(const Function &F) {
CheckDI(SP->describes(&F),
"!dbg attachment points at wrong subprogram for function", N, &F,
&I, DL, Scope, SP);

if (DL->getAtomGroup())
CheckDI(DL->getScope()->getSubprogram()->getKeyInstructionsEnabled(),
"DbgLoc uses atomGroup but DISubprogram doesn't have Key "
"Instructions enabled",
DL, DL->getScope()->getSubprogram());
};
for (auto &BB : F)
for (auto &I : BB) {
Expand Down
Loading
Loading