Skip to content

Commit 2b5d430

Browse files
committed
[𝘀𝗽𝗿] changes introduced through rebase
Created using spr 1.3.6-beta.1 [skip ci]
1 parent c59725c commit 2b5d430

32 files changed

+650
-15
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ CODEGENOPT(EnableNoundefAttrs, 1, 0) ///< Enable emitting `noundef` attributes o
7878
CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
7979
///< pass manager.
8080
CODEGENOPT(DisableRedZone , 1, 0) ///< Set when -mno-red-zone is enabled.
81+
CODEGENOPT(CallGraphSection, 1, 0) ///< Emit a call graph section into the
82+
///< object file.
8183
CODEGENOPT(EmitCallSiteInfo, 1, 0) ///< Emit call site info only in the case of
8284
///< '-g' + 'O>0' level.
8385
CODEGENOPT(IndirectTlsSegRefs, 1, 0) ///< Set when -mno-tls-direct-seg-refs

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4291,6 +4291,10 @@ defm data_sections : BoolFOption<"data-sections",
42914291
PosFlag<SetTrue, [], [ClangOption, CC1Option],
42924292
"Place each data in its own section">,
42934293
NegFlag<SetFalse>>;
4294+
defm call_graph_section : BoolFOption<"call-graph-section",
4295+
CodeGenOpts<"CallGraphSection">, DefaultFalse,
4296+
PosFlag<SetTrue, [], [CC1Option], "Emit a call graph section">,
4297+
NegFlag<SetFalse>>;
42944298
defm stack_size_section : BoolFOption<"stack-size-section",
42954299
CodeGenOpts<"StackSizeSection">, DefaultFalse,
42964300
PosFlag<SetTrue, [], [ClangOption, CC1Option],

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
455455
Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;
456456
Options.EmitAddrsig = CodeGenOpts.Addrsig;
457457
Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
458+
Options.EmitCallGraphSection = CodeGenOpts.CallGraphSection;
458459
Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
459460
Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI;
460461
Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6609,6 +6609,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
66096609
CmdArgs.push_back(A->getValue());
66106610
}
66116611

6612+
if (Args.hasFlag(options::OPT_fcall_graph_section,
6613+
options::OPT_fno_call_graph_section, false))
6614+
CmdArgs.push_back("-fcall-graph-section");
6615+
66126616
Args.addOptInFlag(CmdArgs, options::OPT_fstack_size_section,
66136617
options::OPT_fno_stack_size_section);
66146618

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %clang -### -S -fcall-graph-section %s 2>&1 | FileCheck --check-prefix=CALL-GRAPH-SECTION %s
2+
// RUN: %clang -### -S -fcall-graph-section -fno-call-graph-section %s 2>&1 | FileCheck --check-prefix=NO-CALL-GRAPH-SECTION %s
3+
4+
// CALL-GRAPH-SECTION: "-fcall-graph-section"
5+
// NO-CALL-GRAPH-SECTION-NOT: "-fcall-graph-section"

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,32 @@ class AsmPrinter : public MachineFunctionPass {
173173
/// Emit comments in assembly output if this is true.
174174
bool VerboseAsm;
175175

176+
/// Store symbols and type identifiers used to create call graph section
177+
/// entries related to a function.
178+
struct FunctionInfo {
179+
/// Numeric type identifier used in call graph section for indirect calls
180+
/// and targets.
181+
using CGTypeId = uint64_t;
182+
183+
/// Enumeration of function kinds, and their mapping to function kind values
184+
/// stored in call graph section entries.
185+
/// Must match the enum in llvm/tools/llvm-objdump/llvm-objdump.cpp.
186+
enum FunctionKind {
187+
/// Function cannot be target to indirect calls.
188+
NOT_INDIRECT_TARGET = 0,
189+
190+
/// Function may be target to indirect calls but its type id is unknown.
191+
INDIRECT_TARGET_UNKNOWN_TID = 1,
192+
193+
/// Function may be target to indirect calls and its type id is known.
194+
INDIRECT_TARGET_KNOWN_TID = 2,
195+
};
196+
197+
/// Map type identifiers to callsite labels. Labels are only for indirect
198+
/// calls and inclusive of all indirect calls of the function.
199+
SmallVector<std::pair<CGTypeId, MCSymbol *>> CallSiteLabels;
200+
};
201+
176202
/// Output stream for the stack usage file (i.e., .su file).
177203
std::unique_ptr<raw_fd_ostream> StackUsageStream;
178204

@@ -414,6 +440,8 @@ class AsmPrinter : public MachineFunctionPass {
414440
void emitKCFITrapEntry(const MachineFunction &MF, const MCSymbol *Symbol);
415441
virtual void emitKCFITypeId(const MachineFunction &MF);
416442

443+
void emitCallGraphSection(const MachineFunction &MF, FunctionInfo &FuncInfo);
444+
417445
void emitPseudoProbe(const MachineInstr &MI);
418446

419447
void emitRemarksSection(remarks::RemarkStreamer &RS);

llvm/include/llvm/CodeGen/CommandFlags.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ bool getEnableStackSizeSection();
132132

133133
bool getEnableAddrsig();
134134

135+
bool getEnableCallGraphSection();
136+
135137
bool getEmitCallSiteInfo();
136138

137139
bool getEnableMachineFunctionSplitter();

llvm/include/llvm/CodeGen/MIRYamlMapping.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ struct CallSiteInfo {
483483
MachineInstrLoc CallLocation;
484484
std::vector<ArgRegPair> ArgForwardingRegs;
485485

486+
/// Numeric callee type identifier used for call graph section.
487+
using TypeIdTy = std::optional<uint64_t>;
488+
TypeIdTy TypeId;
489+
486490
bool operator==(const CallSiteInfo &Other) const {
487491
return CallLocation.BlockNum == Other.CallLocation.BlockNum &&
488492
CallLocation.Offset == Other.CallLocation.Offset;
@@ -511,6 +515,7 @@ template <> struct MappingTraits<CallSiteInfo> {
511515
YamlIO.mapRequired("offset", CSInfo.CallLocation.Offset);
512516
YamlIO.mapOptional("fwdArgRegs", CSInfo.ArgForwardingRegs,
513517
std::vector<CallSiteInfo::ArgRegPair>());
518+
YamlIO.mapOptional("typeId", CSInfo.TypeId);
514519
}
515520

516521
static const bool flow = true;

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
#include "llvm/CodeGen/MachineBasicBlock.h"
2727
#include "llvm/CodeGen/MachineInstr.h"
2828
#include "llvm/CodeGen/MachineMemOperand.h"
29+
#include "llvm/IR/Constants.h"
2930
#include "llvm/IR/EHPersonalities.h"
31+
#include "llvm/IR/Instructions.h"
3032
#include "llvm/Support/Allocator.h"
3133
#include "llvm/Support/ArrayRecycler.h"
3234
#include "llvm/Support/AtomicOrdering.h"
3335
#include "llvm/Support/Compiler.h"
36+
#include "llvm/Support/MD5.h"
3437
#include "llvm/Support/Recycler.h"
3538
#include "llvm/Target/TargetOptions.h"
3639
#include <bitset>
@@ -486,6 +489,40 @@ class LLVM_ABI MachineFunction {
486489
struct CallSiteInfo {
487490
/// Vector of call argument and its forwarding register.
488491
SmallVector<ArgRegPair, 1> ArgRegPairs;
492+
493+
/// Callee type id.
494+
ConstantInt *TypeId = nullptr;
495+
496+
CallSiteInfo() = default;
497+
498+
/// Extracts the numeric type id from the CallBase's type operand bundle,
499+
/// and sets TypeId. This is used as type id for the indirect call in the
500+
/// call graph section.
501+
CallSiteInfo(const CallBase &CB) {
502+
// Call graph section needs numeric type id only for indirect calls.
503+
if (!CB.isIndirectCall())
504+
return;
505+
506+
std::optional<OperandBundleUse> Opt =
507+
CB.getOperandBundle(LLVMContext::OB_type);
508+
// Return if the operand bundle for call graph section cannot be found.
509+
if (!Opt)
510+
return;
511+
512+
// Get generalized type id string
513+
auto OB = *Opt;
514+
assert(OB.Inputs.size() == 1 && "invalid input size");
515+
auto *OBVal = OB.Inputs.front().get();
516+
auto *TypeIdMD = cast<MetadataAsValue>(OBVal)->getMetadata();
517+
auto *TypeIdStr = cast<MDString>(TypeIdMD);
518+
assert(TypeIdStr->getString().ends_with(".generalized") &&
519+
"invalid type identifier");
520+
521+
// Compute numeric type id from generalized type id string
522+
uint64_t TypeIdVal = MD5Hash(TypeIdStr->getString());
523+
IntegerType *Int64Ty = Type::getInt64Ty(CB.getContext());
524+
TypeId = ConstantInt::get(Int64Ty, TypeIdVal, /*IsSigned=*/false);
525+
}
489526
};
490527

491528
private:

llvm/include/llvm/MC/MCObjectFileInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class MCObjectFileInfo {
6868
/// Language Specific Data Area information is emitted to.
6969
MCSection *LSDASection = nullptr;
7070

71+
/// Section containing metadata on call graph.
72+
MCSection *CallGraphSection = nullptr;
73+
7174
/// If exception handling is supported by the target and the target can
7275
/// support a compact representation of the CIE and FDE, this is the section
7376
/// to emit them into.
@@ -355,6 +358,8 @@ class MCObjectFileInfo {
355358
MCSection *getFaultMapSection() const { return FaultMapSection; }
356359
MCSection *getRemarksSection() const { return RemarksSection; }
357360

361+
MCSection *getCallGraphSection(const MCSection &TextSec) const;
362+
358363
MCSection *getStackSizesSection(const MCSection &TextSec) const;
359364

360365
MCSection *getBBAddrMapSection(const MCSection &TextSec) const;

0 commit comments

Comments
 (0)