Skip to content

Commit c46336b

Browse files
authored
Reapply "[llvm] Add CalleeTypeIds field to CallSiteInfo" (#150335) (#150990)
This reverts commit 05e08cd. Adding the missing -mtriple flags in MIR/X86 test files which caused these tests to fail which was the reason for reverting the patch.
1 parent ccc96e6 commit c46336b

File tree

11 files changed

+156
-11
lines changed

11 files changed

+156
-11
lines changed

llvm/include/llvm/CodeGen/CommandFlags.h

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

134134
LLVM_ABI bool getEnableAddrsig();
135135

136+
LLVM_ABI bool getEnableCallGraphSection();
137+
136138
LLVM_ABI bool getEmitCallSiteInfo();
137139

138140
LLVM_ABI bool getEnableMachineFunctionSplitter();

llvm/include/llvm/CodeGen/MIRYamlMapping.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ struct CallSiteInfo {
482482

483483
MachineInstrLoc CallLocation;
484484
std::vector<ArgRegPair> ArgForwardingRegs;
485+
/// Numeric callee type identifiers for the callgraph section.
486+
std::vector<uint64_t> CalleeTypeIds;
485487

486488
bool operator==(const CallSiteInfo &Other) const {
487489
return CallLocation.BlockNum == Other.CallLocation.BlockNum &&
@@ -511,6 +513,7 @@ template <> struct MappingTraits<CallSiteInfo> {
511513
YamlIO.mapRequired("offset", CSInfo.CallLocation.Offset);
512514
YamlIO.mapOptional("fwdArgRegs", CSInfo.ArgForwardingRegs,
513515
std::vector<CallSiteInfo::ArgRegPair>());
516+
YamlIO.mapOptional("calleeTypeIds", CSInfo.CalleeTypeIds);
514517
}
515518

516519
static const bool flow = true;

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ class LLVM_ABI MachineFunction {
515515
struct CallSiteInfo {
516516
/// Vector of call argument and its forwarding register.
517517
SmallVector<ArgRegPair, 1> ArgRegPairs;
518+
/// Callee type ids.
519+
SmallVector<ConstantInt *, 4> CalleeTypeIds;
518520
};
519521

520522
struct CalledGlobalInfo {

llvm/include/llvm/Target/TargetOptions.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ class TargetOptions {
133133
EmitStackSizeSection(false), EnableMachineOutliner(false),
134134
EnableMachineFunctionSplitter(false),
135135
EnableStaticDataPartitioning(false), SupportsDefaultOutlining(false),
136-
EmitAddrsig(false), BBAddrMap(false), EmitCallSiteInfo(false),
137-
SupportsDebugEntryValues(false), EnableDebugEntryValues(false),
138-
ValueTrackingVariableLocations(false), ForceDwarfFrameSection(false),
139-
XRayFunctionIndex(true), DebugStrictDwarf(false), Hotpatch(false),
136+
EmitAddrsig(false), BBAddrMap(false), EmitCallGraphSection(false),
137+
EmitCallSiteInfo(false), SupportsDebugEntryValues(false),
138+
EnableDebugEntryValues(false), ValueTrackingVariableLocations(false),
139+
ForceDwarfFrameSection(false), XRayFunctionIndex(true),
140+
DebugStrictDwarf(false), Hotpatch(false),
140141
PPCGenScalarMASSEntries(false), JMCInstrument(false),
141142
EnableCFIFixup(false), MisExpect(false), XCOFFReadOnlyPointers(false),
142143
VerifyArgABICompliance(true),
@@ -319,6 +320,9 @@ class TargetOptions {
319320
/// to selectively generate basic block sections.
320321
std::shared_ptr<MemoryBuffer> BBSectionsFuncListBuf;
321322

323+
/// Emit section containing call graph metadata.
324+
unsigned EmitCallGraphSection : 1;
325+
322326
/// The flag enables call site info production. It is used only for debug
323327
/// info, and it is restricted only to optimized code. This can be used for
324328
/// something else, so that should be controlled in the frontend.

llvm/lib/CodeGen/CommandFlags.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ CGOPT(EABI, EABIVersion)
101101
CGOPT(DebuggerKind, DebuggerTuningOpt)
102102
CGOPT(bool, EnableStackSizeSection)
103103
CGOPT(bool, EnableAddrsig)
104+
CGOPT(bool, EnableCallGraphSection)
104105
CGOPT(bool, EmitCallSiteInfo)
105106
CGOPT(bool, EnableMachineFunctionSplitter)
106107
CGOPT(bool, EnableStaticDataPartitioning)
@@ -461,6 +462,11 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
461462
cl::init(false));
462463
CGBINDOPT(EnableAddrsig);
463464

465+
static cl::opt<bool> EnableCallGraphSection(
466+
"call-graph-section", cl::desc("Emit a call graph section"),
467+
cl::init(false));
468+
CGBINDOPT(EnableCallGraphSection);
469+
464470
static cl::opt<bool> EmitCallSiteInfo(
465471
"emit-call-site-info",
466472
cl::desc(
@@ -595,6 +601,7 @@ codegen::InitTargetOptionsFromCodeGenFlags(const Triple &TheTriple) {
595601
Options.EnableMachineFunctionSplitter = getEnableMachineFunctionSplitter();
596602
Options.EnableStaticDataPartitioning = getEnableStaticDataPartitioning();
597603
Options.EmitAddrsig = getEnableAddrsig();
604+
Options.EmitCallGraphSection = getEnableCallGraphSection();
598605
Options.EmitCallSiteInfo = getEmitCallSiteInfo();
599606
Options.EnableDebugEntryValues = getEnableDebugEntryValues();
600607
Options.ForceDwarfFrameSection = getForceDwarfFrameSection();

llvm/lib/CodeGen/MIRParser/MIRParser.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,21 @@ bool MIRParserImpl::initializeCallSiteInfo(
504504
return error(Error, ArgRegPair.Reg.SourceRange);
505505
CSInfo.ArgRegPairs.emplace_back(Reg, ArgRegPair.ArgNo);
506506
}
507+
if (!YamlCSInfo.CalleeTypeIds.empty()) {
508+
for (auto CalleeTypeId : YamlCSInfo.CalleeTypeIds) {
509+
IntegerType *Int64Ty = Type::getInt64Ty(Context);
510+
CSInfo.CalleeTypeIds.push_back(ConstantInt::get(Int64Ty, CalleeTypeId,
511+
/*isSigned=*/false));
512+
}
513+
}
507514

508-
if (TM.Options.EmitCallSiteInfo)
515+
if (TM.Options.EmitCallSiteInfo || TM.Options.EmitCallGraphSection)
509516
MF.addCallSiteInfo(&*CallI, std::move(CSInfo));
510517
}
511518

512-
if (YamlMF.CallSitesInfo.size() && !TM.Options.EmitCallSiteInfo)
513-
return error(Twine("Call site info provided but not used"));
519+
if (!YamlMF.CallSitesInfo.empty() &&
520+
!(TM.Options.EmitCallSiteInfo || TM.Options.EmitCallGraphSection))
521+
return error("call site info provided but not used");
514522
return false;
515523
}
516524

llvm/lib/CodeGen/MIRPrinter.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,24 +525,30 @@ static void convertCallSiteObjects(yaml::MachineFunction &YMF,
525525
const MachineFunction &MF,
526526
ModuleSlotTracker &MST) {
527527
const auto *TRI = MF.getSubtarget().getRegisterInfo();
528-
for (auto CSInfo : MF.getCallSitesInfo()) {
528+
for (auto [MI, CallSiteInfo] : MF.getCallSitesInfo()) {
529529
yaml::CallSiteInfo YmlCS;
530530
yaml::MachineInstrLoc CallLocation;
531531

532532
// Prepare instruction position.
533-
MachineBasicBlock::const_instr_iterator CallI = CSInfo.first->getIterator();
533+
MachineBasicBlock::const_instr_iterator CallI = MI->getIterator();
534534
CallLocation.BlockNum = CallI->getParent()->getNumber();
535535
// Get call instruction offset from the beginning of block.
536536
CallLocation.Offset =
537537
std::distance(CallI->getParent()->instr_begin(), CallI);
538538
YmlCS.CallLocation = CallLocation;
539+
540+
auto [ArgRegPairs, CalleeTypeIds] = CallSiteInfo;
539541
// Construct call arguments and theirs forwarding register info.
540-
for (auto ArgReg : CSInfo.second.ArgRegPairs) {
542+
for (auto ArgReg : ArgRegPairs) {
541543
yaml::CallSiteInfo::ArgRegPair YmlArgReg;
542544
YmlArgReg.ArgNo = ArgReg.ArgNo;
543545
printRegMIR(ArgReg.Reg, YmlArgReg.Reg, TRI);
544546
YmlCS.ArgForwardingRegs.emplace_back(YmlArgReg);
545547
}
548+
// Get type ids.
549+
for (auto *CalleeTypeId : CalleeTypeIds) {
550+
YmlCS.CalleeTypeIds.push_back(CalleeTypeId->getZExtValue());
551+
}
546552
YMF.CallSitesInfo.push_back(std::move(YmlCS));
547553
}
548554

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ MachineFunction::getCallSiteInfo(const MachineInstr *MI) {
919919
assert(MI->isCandidateForAdditionalCallInfo() &&
920920
"Call site info refers only to call (MI) candidates");
921921

922-
if (!Target.Options.EmitCallSiteInfo)
922+
if (!Target.Options.EmitCallSiteInfo && !Target.Options.EmitCallGraphSection)
923923
return CallSitesInfo.end();
924924
return CallSitesInfo.find(MI);
925925
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Test MIR printer and parser to check if a call instruction with multiple
2+
# callee types are handled correctly.
3+
4+
# RUN: llc -mtriple=x86_64 --call-graph-section %s -run-pass=none -o - | FileCheck --match-full-lines %s
5+
# CHECK: name: ambiguous_caller
6+
# CHECK: callSites:
7+
# CHECK-NEXT: - { bb: {{.*}}, offset: {{.*}}, fwdArgRegs: {{.*}}, calleeTypeIds:
8+
# CHECK-NEXT: [ 1234, 5678 ] }
9+
10+
--- |
11+
define ptr @ambiguous_caller() {
12+
entry:
13+
%fn = alloca ptr, align 8
14+
%call1 = call ptr %fn(i64 4), !callee_type !0
15+
ret ptr %call1
16+
}
17+
18+
!0 = !{!1, !2}
19+
!1 = !{i64 0, !"callee_type0.generalized"}
20+
!2 = !{i64 0, !"callee_type2.generalized"}
21+
...
22+
---
23+
name: ambiguous_caller
24+
callSites:
25+
- { bb: 0, offset: 1, fwdArgRegs: [], calleeTypeIds: [ 1234, 5678 ] }
26+
body: |
27+
bb.0.entry:
28+
%0:gr64 = MOV32ri64 4
29+
CALL64r killed %0, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, implicit-def $rax
30+
RET 0, $rax
31+
...
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Test MIR printer and parser to NOT have `CalleeTypeIds` field in callSites.
2+
# `CalleeTypeId` is used for propagating call site type identifiers for
3+
# indirect targets only. This test does not contain any indirect targets.
4+
5+
# RUN: llc -mtriple=x86_64 --call-graph-section %s -run-pass=none -o - | FileCheck --match-full-lines %s
6+
# CHECK-NOT: calleeTypeIds
7+
# CHECK: name: bar
8+
# CHECK: callSites:
9+
# CHECK-NEXT: - { bb: {{.*}}, offset: {{.*}}, fwdArgRegs: [] }
10+
# CHECK-NEXT: - { bb: {{.*}}, offset: {{.*}}, fwdArgRegs: [] }
11+
# CHECK: name: foo
12+
# CHECK: callSites:
13+
# CHECK-NEXT: - { bb: {{.*}}, offset: {{.*}}, fwdArgRegs: [] }
14+
15+
--- |
16+
declare i32 @fizz(i32, i32)
17+
18+
declare i32 @buzz(i32, i32)
19+
20+
define i32 @bar(i32 %x, i32 %y) !type !0 {
21+
entry:
22+
%call = call i32 @buzz(i32 %x, i32 %x)
23+
%call1 = call i32 @fizz(i32 %x, i32 %x)
24+
ret i32 0
25+
}
26+
27+
define i32 @foo(i32 %x, i32 %y) !type !0 {
28+
entry:
29+
%call1 = call i32 @bar(i32 %x, i32 %x)
30+
ret i32 0
31+
}
32+
33+
!0 = !{i64 0, !"_ZTSFiiiE.generalized"}
34+
...
35+
---
36+
name: bar
37+
callSites:
38+
- { bb: 0, offset: 0, fwdArgRegs: [] }
39+
- { bb: 0, offset: 1, fwdArgRegs: [] }
40+
body: |
41+
bb.0.entry:
42+
CALL64pcrel32 target-flags(x86-plt) @buzz, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit $esi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax
43+
CALL64pcrel32 target-flags(x86-plt) @fizz, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit $esi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax
44+
45+
...
46+
---
47+
name: foo
48+
callSites:
49+
- { bb: 0, offset: 0, fwdArgRegs: [] }
50+
body: |
51+
bb.0.entry:
52+
CALL64pcrel32 target-flags(x86-plt) @bar, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit $esi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax
53+
54+
...

0 commit comments

Comments
 (0)