Skip to content
Merged
Show file tree
Hide file tree
Changes from 114 commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
fbe540b
[𝘀𝗽𝗿] changes to main this commit is based on
necipfazil Apr 3, 2024
d163d80
[𝘀𝗽𝗿] initial version
necipfazil Apr 3, 2024
4c15a50
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Apr 22, 2024
dd26405
Updated fixme on PR 87570
necipfazil Apr 22, 2024
e7111f0
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Apr 24, 2024
b5b03fd
Rebased on top of main
necipfazil Apr 24, 2024
2b3551e
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Apr 29, 2024
b90a4d5
dyn_cast to isa
necipfazil Apr 29, 2024
a692109
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil May 1, 2024
332f2aa
Rebased on upstream main.
necipfazil May 1, 2024
826c83e
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Nov 14, 2024
1209081
Rebase patchset
necipfazil Nov 14, 2024
60f93a8
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Nov 14, 2024
1981340
Update inline comment as suggested.
necipfazil Nov 14, 2024
a2daca9
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Nov 19, 2024
423d5f2
Addressed review comments.
necipfazil Nov 19, 2024
17ee91d
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Nov 20, 2024
e51ce07
Rebase on top of upstream main.
necipfazil Nov 20, 2024
21ec0f7
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Nov 20, 2024
e4684b8
Break clang and llvm parts into separate commits.
necipfazil Nov 20, 2024
5caf81d
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Dec 10, 2024
7f15e6b
Reorder commits.
necipfazil Dec 10, 2024
2d88d20
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Feb 2, 2025
3fad066
Rebase on top of main.
necipfazil Feb 2, 2025
1f6e111
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Feb 5, 2025
1fd9775
Rename OB_type to OB_callee_type.
necipfazil Feb 5, 2025
4615cce
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Feb 11, 2025
2f763c0
Rebase on top of main
necipfazil Feb 11, 2025
5857d51
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Feb 11, 2025
5e6675b
Update IR verifier.
necipfazil Feb 11, 2025
d9322de
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Mar 13, 2025
39e5b93
Add requested tests part 1.
necipfazil Mar 13, 2025
6df6234
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Mar 13, 2025
f18f492
Update comments in tests.
necipfazil Mar 13, 2025
6b67376
Updated the test as reviewers suggested.
necipfazil Mar 13, 2025
d2bb381
Scoped enum. Simplify test.
necipfazil Mar 13, 2025
e3c95b5
Remove unnecessary cast.
necipfazil Mar 13, 2025
607b992
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Mar 13, 2025
835c2e2
Remove unnecessary asserts. Remove autos for better readability.
necipfazil Mar 13, 2025
678008e
Reorder IR metadata and rename temporary var names in test.
necipfazil Mar 13, 2025
23f00d0
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Mar 14, 2025
5831ed4
Add RISC-V support. Clean up test files.
necipfazil Mar 14, 2025
a0eb067
[𝘀𝗽𝗿] changes introduced through rebase
necipfazil Mar 15, 2025
77ce9ab
Clean up test files.
necipfazil Mar 15, 2025
a4a1171
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Mar 19, 2025
7d8418c
Address code refactoring comments.
necipfazil Mar 19, 2025
42c830f
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 19, 2025
19043db
Use CalleeTypeIds list to emit callgraph section.
necipfazil Apr 19, 2025
4789571
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 19, 2025
0754d49
Address review comments.
necipfazil Apr 19, 2025
86e5b9a
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 23, 2025
cf2e510
Extract callsite label emission as a private method.
necipfazil Apr 23, 2025
4b04333
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 23, 2025
80df0c0
Address review comments.
necipfazil Apr 23, 2025
caf45da
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 23, 2025
20679e7
Address review comments.
necipfazil Apr 23, 2025
63e56f2
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 24, 2025
48e878c
Address review comments.
necipfazil Apr 24, 2025
d072c83
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 24, 2025
aeaa155
Separate the assembly test for callgraph section and related labels i…
necipfazil Apr 24, 2025
a1834c9
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 24, 2025
f248de4
Rebase on parent.
necipfazil Apr 24, 2025
02d721d
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Apr 28, 2025
516dee0
Add llvm-mc test. Address comments.
necipfazil Apr 28, 2025
b45026e
Update test inline comments.
necipfazil Apr 29, 2025
370d4f0
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 1, 2025
106364f
Rebase on parent.
necipfazil May 1, 2025
f1051d5
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 5, 2025
9a561db
Rebase on parent.
necipfazil May 5, 2025
4462f6b
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 10, 2025
5778238
Rebase on parent change.
necipfazil May 10, 2025
5626438
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 13, 2025
caae8ac
Remove dso_local and noundef from tests.
necipfazil May 13, 2025
850fe68
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 13, 2025
dcf4495
Rebase on parent.
necipfazil May 13, 2025
d651899
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 14, 2025
9d25b19
Rebase on parent
necipfazil May 14, 2025
6a0f9b5
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 14, 2025
837e254
Rebase on main.
necipfazil May 14, 2025
f8f741c
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 27, 2025
65a4435
Drop local_unnamed_addr.
necipfazil May 27, 2025
58417bc
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk May 27, 2025
e1e3fd1
Rebase change.
necipfazil May 27, 2025
dc9cca4
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jun 11, 2025
7102170
Rebase.
necipfazil Jun 11, 2025
dd8808b
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jun 11, 2025
aeac1c3
Rebase on parent
necipfazil Jun 11, 2025
d332966
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 10, 2025
b95355d
Rebase on top of main.
necipfazil Jul 10, 2025
1975538
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 18, 2025
2397f11
Update hasGeneralizedMDstring API call.
necipfazil Jul 18, 2025
5723d40
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 18, 2025
ceef7e4
Rebase on parent
necipfazil Jul 18, 2025
f07f515
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 18, 2025
4422763
Rebase on top of parent change.
necipfazil Jul 18, 2025
52a4b6c
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 18, 2025
317bf8e
Rebase on parent.
necipfazil Jul 18, 2025
73508dd
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 18, 2025
0096eaf
Rebase on parent.
necipfazil Jul 18, 2025
f1a4e6c
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 18, 2025
df57b4f
Address review comments.
necipfazil Jul 18, 2025
f81b59e
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 21, 2025
d0abedc
Use llvm-reduce to simplify codegen test.
necipfazil Jul 21, 2025
1bd19e6
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 22, 2025
388b958
Rebase.
Prabhuk Jul 22, 2025
b94b3e0
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 23, 2025
13efd72
Reduce tests. Update comments in tests.
Prabhuk Jul 23, 2025
2c63a33
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 23, 2025
b327bab
Rebase on main.
Prabhuk Jul 23, 2025
9e6843a
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 23, 2025
f4c8b22
Rebase.
Prabhuk Jul 23, 2025
b498381
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 28, 2025
c6cac61
Rebase on parent.
Prabhuk Jul 28, 2025
28bcfbe
Check for .L.* instead of .Ltmp[0-9]+ in test.
Prabhuk Jul 28, 2025
a9401f0
[𝘀𝗽𝗿] changes introduced through rebase
Prabhuk Jul 30, 2025
d0590e9
Rebase on main.
Prabhuk Jul 30, 2025
7a2f29a
Fix inline comments.
Prabhuk Jul 31, 2025
608ba94
Fix alignment in test file.
Prabhuk Jul 31, 2025
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
39 changes: 39 additions & 0 deletions llvm/include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
/// Emit comments in assembly output if this is true.
bool VerboseAsm;

/// Store symbols and type identifiers used to create call graph section
/// entries related to a function.
struct FunctionInfo {
/// Numeric type identifier used in call graph section for indirect calls
/// and targets.
using CGTypeId = uint64_t;

/// Enumeration of function kinds, and their mapping to function kind values
/// stored in call graph section entries.
/// Must match the enum in llvm/tools/llvm-objdump/llvm-objdump.cpp.
enum class FunctionKind : uint64_t {
/// Function cannot be target to indirect calls.
NOT_INDIRECT_TARGET = 0,

/// Function may be target to indirect calls but its type id is unknown.
INDIRECT_TARGET_UNKNOWN_TID = 1,

/// Function may be target to indirect calls and its type id is known.
INDIRECT_TARGET_KNOWN_TID = 2,
};

/// Map type identifiers to callsite labels. Labels are only for indirect
/// calls and inclusive of all indirect calls of the function.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Map type identifiers to callsite labels. Labels are only for indirect
/// calls and inclusive of all indirect calls of the function.
/// Map type identifiers to callsite labels. Labels are only for indirect
/// calls and are inclusive of all indirect calls of the function.

I'm also not 100% sure what you mean by inclusive of al indirect calls of the function. Can you clarify?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reworded the inline comments. PTAL.

SmallVector<std::pair<CGTypeId, MCSymbol *>> CallSiteLabels;
};

enum CallGraphSectionFormatVersion : uint64_t {
V_0 = 0,
};

/// Output stream for the stack usage file (i.e., .su file).
std::unique_ptr<raw_fd_ostream> StackUsageStream;

Expand Down Expand Up @@ -355,6 +385,13 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
/// are available. Returns empty string otherwise.
StringRef getConstantSectionSuffix(const Constant *C) const;

/// Generate and emit labels for callees of the indirect callsites which will
/// be used to populate the .callgraph section.
void emitIndirectCalleeLabels(
FunctionInfo &FuncInfo,
const MachineFunction::CallSiteInfoMap &CallSitesInfoMap,
const MachineInstr &MI);

//===------------------------------------------------------------------===//
// XRay instrumentation implementation.
//===------------------------------------------------------------------===//
Expand Down Expand Up @@ -442,6 +479,8 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
void emitKCFITrapEntry(const MachineFunction &MF, const MCSymbol *Symbol);
virtual void emitKCFITypeId(const MachineFunction &MF);

void emitCallGraphSection(const MachineFunction &MF, FunctionInfo &FuncInfo);

void emitPseudoProbe(const MachineInstr &MI);

void emitRemarksSection(remarks::RemarkStreamer &RS);
Expand Down
32 changes: 31 additions & 1 deletion llvm/include/llvm/CodeGen/MachineFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/EHPersonalities.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/ArrayRecycler.h"
#include "llvm/Support/AtomicOrdering.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/Recycler.h"
#include "llvm/Target/TargetOptions.h"
#include <bitset>
Expand Down Expand Up @@ -517,18 +520,45 @@ class LLVM_ABI MachineFunction {
SmallVector<ArgRegPair, 1> ArgRegPairs;
/// Callee type ids.
SmallVector<ConstantInt *, 4> CalleeTypeIds;

CallSiteInfo() = default;

/// Extracts the numeric type id from the CallBase's callee_type Metadata,
/// and sets CalleeTypeIds. This is used as type id for the indirect call in
/// the call graph section.
CallSiteInfo(const CallBase &CB) {
// Call graph section needs numeric callee_type id only for indirect
// calls.
if (!CB.isIndirectCall())
return;

MDNode *CalleeTypeList = CB.getMetadata(LLVMContext::MD_callee_type);
if (!CalleeTypeList)
return;

for (const MDOperand &Op : CalleeTypeList->operands()) {
MDNode *TypeMD = cast<MDNode>(Op);
MDString *TypeIdStr = cast<MDString>(TypeMD->getOperand(1));
// Compute numeric type id from generalized type id string
uint64_t TypeIdVal = MD5Hash(TypeIdStr->getString());
IntegerType *Int64Ty = Type::getInt64Ty(CB.getContext());
CalleeTypeIds.push_back(
ConstantInt::get(Int64Ty, TypeIdVal, /*IsSigned=*/false));
}
}
};

struct CalledGlobalInfo {
const GlobalValue *Callee;
unsigned TargetFlags;
};

using CallSiteInfoMap = DenseMap<const MachineInstr *, CallSiteInfo>;

private:
Delegate *TheDelegate = nullptr;
GISelChangeObserver *Observer = nullptr;

using CallSiteInfoMap = DenseMap<const MachineInstr *, CallSiteInfo>;
/// Map a call instruction to call site arguments forwarding info.
CallSiteInfoMap CallSitesInfo;

Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/MC/MCObjectFileInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class LLVM_ABI MCObjectFileInfo {
/// Language Specific Data Area information is emitted to.
MCSection *LSDASection = nullptr;

/// Section containing call graph metadata.
MCSection *CallGraphSection = nullptr;

/// If exception handling is supported by the target and the target can
/// support a compact representation of the CIE and FDE, this is the section
/// to emit them into.
Expand Down Expand Up @@ -359,6 +362,8 @@ class LLVM_ABI MCObjectFileInfo {
MCSection *getFaultMapSection() const { return FaultMapSection; }
MCSection *getRemarksSection() const { return RemarksSection; }

MCSection *getCallGraphSection(const MCSection &TextSec) const;

MCSection *getStackSizesSection(const MCSection &TextSec) const;

MCSection *getBBAddrMapSection(const MCSection &TextSec) const;
Expand Down
108 changes: 108 additions & 0 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,88 @@ void AsmPrinter::emitStackUsage(const MachineFunction &MF) {
*StackUsageStream << "static\n";
}

/// Extracts a generalized numeric type identifier of a Function's type from
/// type metadata. Returns null if metadata cannot be found.
static ConstantInt *extractNumericCGTypeId(const Function &F) {
SmallVector<MDNode *, 2> Types;
F.getMetadata(LLVMContext::MD_type, Types);
for (const auto &Type : Types) {
if (Type->hasGeneralizedMDString()) {
MDString *MDGeneralizedTypeId = cast<MDString>(Type->getOperand(1));
uint64_t TypeIdVal = llvm::MD5Hash(MDGeneralizedTypeId->getString());
IntegerType *Int64Ty = Type::getInt64Ty(F.getContext());
return ConstantInt::get(Int64Ty, TypeIdVal);
}
}
return nullptr;
}

/// Emits .callgraph section.
void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
FunctionInfo &FuncInfo) {
if (!MF.getTarget().Options.EmitCallGraphSection)
return;

// Switch to the call graph section for the function
MCSection *FuncCGSection =
getObjFileLowering().getCallGraphSection(*getCurrentSection());
assert(FuncCGSection && "null callgraph section");
OutStreamer->pushSection();
OutStreamer->switchSection(FuncCGSection);

// Emit format version number.
OutStreamer->emitInt64(CallGraphSectionFormatVersion::V_0);

// Emit function's self information, which is composed of:
// 1) FunctionEntryPc
// 2) FunctionKind: Whether the function is indirect target, and if so,
// whether its type id is known.
// 3) FunctionTypeId: Emit only when the function is an indirect target
// and its type id is known.

// Emit function entry pc.
const MCSymbol *FunctionSymbol = getFunctionBegin();
OutStreamer->emitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());

// If this function has external linkage or has its address taken and
// it is not a callback, then anything could call it.
const Function &F = MF.getFunction();
bool IsIndirectTarget =
!F.hasLocalLinkage() || F.hasAddressTaken(nullptr,
/*IgnoreCallbackUses=*/true,
/*IgnoreAssumeLikeCalls=*/true,
/*IgnoreLLVMUsed=*/false);

// FIXME: FunctionKind takes a few values but emitted as a 64-bit value.
// Can be optimized to occupy 2 bits instead.
// Emit function kind, and type id if available.
if (!IsIndirectTarget) {
OutStreamer->emitInt64(
static_cast<uint64_t>(FunctionInfo::FunctionKind::NOT_INDIRECT_TARGET));
} else {
if (const auto *TypeId = extractNumericCGTypeId(F)) {
OutStreamer->emitInt64(static_cast<uint64_t>(
FunctionInfo::FunctionKind::INDIRECT_TARGET_KNOWN_TID));
OutStreamer->emitInt64(TypeId->getZExtValue());
} else {
OutStreamer->emitInt64(static_cast<uint64_t>(
FunctionInfo::FunctionKind::INDIRECT_TARGET_UNKNOWN_TID));
}
}

// Emit callsite labels, where each element is a pair of type id and
// indirect callsite pc.
const auto &CallSiteLabels = FuncInfo.CallSiteLabels;
OutStreamer->emitInt64(CallSiteLabels.size());
for (const auto &[TypeId, Label] : CallSiteLabels) {
OutStreamer->emitInt64(TypeId);
OutStreamer->emitSymbolValue(Label, TM.getProgramPointerSize());
}
FuncInfo.CallSiteLabels.clear();

OutStreamer->popSection();
}

void AsmPrinter::emitPCSectionsLabel(const MachineFunction &MF,
const MDNode &MD) {
MCSymbol *S = MF.getContext().createTempSymbol("pcsection");
Expand Down Expand Up @@ -1784,6 +1866,23 @@ static StringRef getMIMnemonic(const MachineInstr &MI, MCStreamer &Streamer) {
return Name;
}

void AsmPrinter::emitIndirectCalleeLabels(
FunctionInfo &FuncInfo,
const MachineFunction::CallSiteInfoMap &CallSitesInfoMap,
const MachineInstr &MI) {
// Only indirect calls have type identifiers set.
const auto &CallSiteInfo = CallSitesInfoMap.find(&MI);
if (CallSiteInfo == CallSitesInfoMap.end())
return;

for (ConstantInt *CalleeTypeId : CallSiteInfo->second.CalleeTypeIds) {
MCSymbol *S = MF->getContext().createTempSymbol();
OutStreamer->emitLabel(S);
uint64_t CalleeTypeIdVal = CalleeTypeId->getZExtValue();
FuncInfo.CallSiteLabels.emplace_back(CalleeTypeIdVal, S);
}
}

/// EmitFunctionBody - This method emits the body and trailer for a
/// function.
void AsmPrinter::emitFunctionBody() {
Expand Down Expand Up @@ -1830,6 +1929,8 @@ void AsmPrinter::emitFunctionBody() {
MBBSectionRanges[MF->front().getSectionID()] =
MBBSectionRange{CurrentFnBegin, nullptr};

FunctionInfo FuncInfo;
const auto &CallSitesInfoMap = MF->getCallSitesInfo();
for (auto &MBB : *MF) {
// Print a label for the basic block.
emitBasicBlockStart(MBB);
Expand Down Expand Up @@ -1963,6 +2064,9 @@ void AsmPrinter::emitFunctionBody() {
break;
}

if (TM.Options.EmitCallGraphSection && MI.isCall())
emitIndirectCalleeLabels(FuncInfo, CallSitesInfoMap, MI);

// If there is a post-instruction symbol, emit a label for it here.
if (MCSymbol *S = MI.getPostInstrSymbol())
OutStreamer->emitLabel(S);
Expand Down Expand Up @@ -2142,6 +2246,9 @@ void AsmPrinter::emitFunctionBody() {
// Emit section containing stack size metadata.
emitStackSizeSection(*MF);

// Emit section containing call graph metadata.
emitCallGraphSection(*MF, FuncInfo);

// Emit .su file containing function stack size information.
emitStackUsage(*MF);

Expand Down Expand Up @@ -2841,6 +2948,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
F.hasFnAttribute("xray-instruction-threshold") ||
needFuncLabels(MF, *this) || NeedsLocalForSize ||
MF.getTarget().Options.EmitStackSizeSection ||
MF.getTarget().Options.EmitCallGraphSection ||
MF.getTarget().Options.BBAddrMap) {
CurrentFnBegin = createTempSymbol("func_begin");
if (NeedsLocalForSize)
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
}

if (MI->isCandidateForAdditionalCallInfo()) {
if (DAG->getTarget().Options.EmitCallSiteInfo)
if (DAG->getTarget().Options.EmitCallSiteInfo ||
DAG->getTarget().Options.EmitCallGraphSection)
MF.addCallSiteInfo(MI, DAG->getCallSiteInfo(Node));

if (auto CalledGlobal = DAG->getCalledGlobal(Node))
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/MC/MCObjectFileInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
EHFrameSection =
Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);

CallGraphSection = Ctx->getELFSection(".callgraph", ELF::SHT_PROGBITS, 0);

StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0);

PseudoProbeSection = Ctx->getELFSection(".pseudo_probe", DebugSecType, 0);
Expand Down Expand Up @@ -1120,6 +1122,24 @@ MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
llvm_unreachable("Unknown ObjectFormatType");
}

MCSection *
MCObjectFileInfo::getCallGraphSection(const MCSection &TextSec) const {
if (Ctx->getObjectFileType() != MCContext::IsELF)
return CallGraphSection;

const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
unsigned Flags = ELF::SHF_LINK_ORDER;
StringRef GroupName;
if (const MCSymbol *Group = ElfSec.getGroup()) {
GroupName = Group->getName();
Flags |= ELF::SHF_GROUP;
}

return Ctx->getELFSection(".callgraph", ELF::SHT_PROGBITS, Flags, 0,
GroupName, true, ElfSec.getUniqueID(),
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
}

MCSection *
MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
if ((Ctx->getObjectFileType() != MCContext::IsELF) ||
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8952,6 +8952,7 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
bool &IsTailCall = CLI.IsTailCall;
CallingConv::ID &CallConv = CLI.CallConv;
bool IsVarArg = CLI.IsVarArg;
const CallBase *CB = CLI.CB;

MachineFunction &MF = DAG.getMachineFunction();
MachineFunction::CallSiteInfo CSInfo;
Expand Down Expand Up @@ -8991,6 +8992,10 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
*DAG.getContext());
RetCCInfo.AnalyzeCallResult(Ins, RetCC);

// Set type id for call site info.
if (MF.getTarget().Options.EmitCallGraphSection && CB && CB->isIndirectCall())
CSInfo = MachineFunction::CallSiteInfo(*CB);

// Check callee args/returns for SVE registers and set calling convention
// accordingly.
if (CallConv == CallingConv::C || CallConv == CallingConv::Fast) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/ARM/ARMISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,7 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
CallingConv::ID CallConv = CLI.CallConv;
bool doesNotRet = CLI.DoesNotReturn;
bool isVarArg = CLI.IsVarArg;
const CallBase *CB = CLI.CB;

MachineFunction &MF = DAG.getMachineFunction();
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Expand All @@ -2446,6 +2447,10 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
!Subtarget->noBTIAtReturnTwice())
GuardWithBTI = AFI->branchTargetEnforcement();

// Set type id for call site info.
if (MF.getTarget().Options.EmitCallGraphSection && CB && CB->isIndirectCall())
CSInfo = MachineFunction::CallSiteInfo(*CB);

// Determine whether this is a non-secure function call.
if (CLI.CB && CLI.CB->getAttributes().hasFnAttr("cmse_nonsecure_call"))
isCmseNSCall = true;
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Target/Mips/MipsISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3341,6 +3341,7 @@ MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
bool &IsTailCall = CLI.IsTailCall;
CallingConv::ID CallConv = CLI.CallConv;
bool IsVarArg = CLI.IsVarArg;
const CallBase *CB = CLI.CB;

MachineFunction &MF = DAG.getMachineFunction();
MachineFrameInfo &MFI = MF.getFrameInfo();
Expand Down Expand Up @@ -3397,8 +3398,11 @@ MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
// Get a count of how many bytes are to be pushed on the stack.
unsigned StackSize = CCInfo.getStackSize();

// Call site info for function parameters tracking.
// Call site info for function parameters tracking and call base type info.
MachineFunction::CallSiteInfo CSInfo;
// Set type id for call site info.
if (MF.getTarget().Options.EmitCallGraphSection && CB && CB->isIndirectCall())
CSInfo = MachineFunction::CallSiteInfo(*CB);

// Check if it's really possible to do a tail call. Restrict it to functions
// that are part of this compilation unit.
Expand Down
Loading