Skip to content

Commit 0c78d0a

Browse files
necipfazilPrabhuk
authored andcommitted
[𝘀𝗽𝗿] changes introduced through rebase
Created using spr 1.3.6-beta.1 [skip ci]
1 parent 2cac836 commit 0c78d0a

18 files changed

+174
-35
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ class AsmPrinter : public MachineFunctionPass {
356356
DwarfUsesRelocationsAcrossSections = Enable;
357357
}
358358

359+
/// Generate and emit labels for callees of the indirect callsites which will
360+
/// be used to populate the .callgraph section.
361+
void emitIndirectCalleeLabels(
362+
FunctionInfo &FuncInfo,
363+
const MachineFunction::CallSiteInfoMap &CallSitesInfoMap,
364+
MachineInstr &MI);
365+
359366
//===------------------------------------------------------------------===//
360367
// XRay instrumentation implementation.
361368
//===------------------------------------------------------------------===//

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,12 @@ class LLVM_ABI MachineFunction {
534534
unsigned TargetFlags;
535535
};
536536

537+
using CallSiteInfoMap = DenseMap<const MachineInstr *, CallSiteInfo>;
538+
537539
private:
538540
Delegate *TheDelegate = nullptr;
539541
GISelChangeObserver *Observer = nullptr;
540542

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

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ static ConstantInt *extractNumericCGTypeId(const Function &F) {
16661666
return ConstantInt::get(Int64Ty, TypeIdVal);
16671667
}
16681668

1669-
/// Emits call graph section.
1669+
/// Emits .callgraph section.
16701670
void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
16711671
FunctionInfo &FuncInfo) {
16721672
if (!MF.getTarget().Options.EmitCallGraphSection)
@@ -1675,7 +1675,7 @@ void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
16751675
// Switch to the call graph section for the function
16761676
MCSection *FuncCGSection =
16771677
getObjFileLowering().getCallGraphSection(*getCurrentSection());
1678-
assert(FuncCGSection && "null call graph section");
1678+
assert(FuncCGSection && "null callgraph section");
16791679
OutStreamer->pushSection();
16801680
OutStreamer->switchSection(FuncCGSection);
16811681

@@ -1728,7 +1728,7 @@ void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
17281728
OutStreamer->emitInt64(CallSiteLabels.size());
17291729

17301730
// Emit the type id and call site label pairs.
1731-
for (const auto& [TypeId, Label] : CallSiteLabels) {
1731+
for (const auto &[TypeId, Label] : CallSiteLabels) {
17321732
OutStreamer->emitInt64(TypeId);
17331733
OutStreamer->emitSymbolValue(Label, TM.getProgramPointerSize());
17341734
}
@@ -1867,6 +1867,30 @@ static StringRef getMIMnemonic(const MachineInstr &MI, MCStreamer &Streamer) {
18671867
return Name;
18681868
}
18691869

1870+
void AsmPrinter::emitIndirectCalleeLabels(
1871+
FunctionInfo &FuncInfo,
1872+
const MachineFunction::CallSiteInfoMap &CallSitesInfoMap,
1873+
MachineInstr &MI) {
1874+
// Only indirect calls have type identifiers set.
1875+
const auto &CallSiteInfo = CallSitesInfoMap.find(&MI);
1876+
if (CallSiteInfo == CallSitesInfoMap.end())
1877+
return;
1878+
if (CallSiteInfo->second.CalleeTypeIds.empty())
1879+
return;
1880+
1881+
for (auto *CalleeTypeId : CallSiteInfo->second.CalleeTypeIds) {
1882+
// Emit label.
1883+
MCSymbol *S = MF->getContext().createTempSymbol();
1884+
OutStreamer->emitLabel(S);
1885+
1886+
// Get numeric callee_type id value.
1887+
uint64_t CalleeTypeIdVal = CalleeTypeId->getZExtValue();
1888+
1889+
// Add to function's callsite labels.
1890+
FuncInfo.CallSiteLabels.emplace_back(CalleeTypeIdVal, S);
1891+
}
1892+
}
1893+
18701894
/// EmitFunctionBody - This method emits the body and trailer for a
18711895
/// function.
18721896
void AsmPrinter::emitFunctionBody() {
@@ -2045,28 +2069,9 @@ void AsmPrinter::emitFunctionBody() {
20452069
break;
20462070
}
20472071

2048-
// FIXME: Some indirect calls can get lowered to jump instructions,
2049-
// resulting in emitting labels for them. The extra information can
2050-
// be neglected while disassembling but still takes space in the binary.
2051-
if (TM.Options.EmitCallGraphSection && MI.isCall()) {
2052-
// Only indirect calls have type identifiers set.
2053-
const auto &CallSiteInfo = CallSitesInfoMap.find(&MI);
2054-
if (CallSiteInfo != CallSitesInfoMap.end()) {
2055-
if (!CallSiteInfo->second.CalleeTypeIds.empty()) {
2056-
for (auto *CalleeTypeId : CallSiteInfo->second.CalleeTypeIds) {
2057-
// Emit label.
2058-
MCSymbol *S = MF->getContext().createTempSymbol();
2059-
OutStreamer->emitLabel(S);
2060-
2061-
// Get numeric callee_type id value.
2062-
uint64_t CalleeTypeIdVal = CalleeTypeId->getZExtValue();
2063-
2064-
// Add to function's callsite labels.
2065-
FuncInfo.CallSiteLabels.emplace_back(CalleeTypeIdVal, S);
2066-
}
2067-
}
2068-
}
2069-
}
2072+
if (TM.Options.EmitCallGraphSection && MI.isCall())
2073+
emitIndirectCalleeLabels(FuncInfo, CallSitesInfoMap, MI);
2074+
20702075
// If there is a post-instruction symbol, emit a label for it here.
20712076
if (MCSymbol *S = MI.getPostInstrSymbol())
20722077
OutStreamer->emitLabel(S);

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4111,6 +4111,11 @@ Instruction *InstCombinerImpl::visitCallBase(CallBase &Call) {
41114111
Call, Builder.CreateBitOrPointerCast(ReturnedArg, CallTy));
41124112
}
41134113

4114+
// Drop unnecessary callee_type metadata from calls that were converted
4115+
// into direct calls.
4116+
if (Call.getMetadata(LLVMContext::MD_callee_type) && !Call.isIndirectCall())
4117+
Call.setMetadata(LLVMContext::MD_callee_type, nullptr);
4118+
41144119
// Drop unnecessary kcfi operand bundles from calls that were converted
41154120
// into direct calls.
41164121
auto Bundle = Call.getOperandBundle(LLVMContext::OB_kcfi);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
;; Tests that call site callee type ids can be extracted and set from
2+
;; callee_type metadata for indirect tail calls.
3+
4+
;; Verify the exact calleeTypeId value to ensure it is not garbage but the value
5+
;; computed as the type id from the callee_type metadata.
6+
; RUN: llc --call-graph-section -mtriple aarch64-linux-gnu < %s -stop-after=finalize-isel -o - | FileCheck %s
7+
8+
define dso_local noundef i32 @_Z13call_indirectPFicEc(ptr noundef readonly captures(none) %func, i8 noundef signext %x) local_unnamed_addr !type !0 {
9+
entry:
10+
; CHECK: callSites:
11+
; CHECK-NEXT: - { bb: {{.*}}, offset: {{.*}}, fwdArgRegs: [], calleeTypeIds:
12+
; CHECK-NEXT: [ 3498816979441845844 ] }
13+
%call = tail call noundef i32 %func(i8 noundef signext %x), !callee_type !1
14+
ret i32 %call
15+
}
16+
17+
!0 = !{i64 0, !"_ZTSFiPvcE.generalized"}
18+
!1 = !{!2}
19+
!2 = !{i64 0, !"_ZTSFicE.generalized"}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
;; Tests that call site callee type ids can be extracted and set from
2+
;; callee_type metadata for indirect tail calls.
3+
4+
;; Verify the exact calleeTypeId value to ensure it is not garbage but the value
5+
;; computed as the type id from the callee_type metadata.
6+
; RUN: llc --call-graph-section -mtriple arm-linux-gnu < %s -stop-after=finalize-isel -o - | FileCheck %s
7+
8+
define dso_local noundef i32 @_Z13call_indirectPFicEc(ptr noundef readonly captures(none) %func, i8 noundef signext %x) local_unnamed_addr !type !0 {
9+
entry:
10+
; CHECK: callSites:
11+
; CHECK-NEXT: - { bb: {{.*}}, offset: {{.*}}, fwdArgRegs: [], calleeTypeIds:
12+
; CHECK-NEXT: [ 3498816979441845844 ] }
13+
%call = tail call noundef i32 %func(i8 noundef signext %x), !callee_type !1
14+
ret i32 %call
15+
}
16+
17+
!0 = !{i64 0, !"_ZTSFiPvcE.generalized"}
18+
!1 = !{!2}
19+
!2 = !{i64 0, !"_ZTSFicE.generalized"}

llvm/test/CodeGen/MIR/X86/call-site-info-direct-calls-typeid.mir

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
# CHECK-NEXT: - { bb: {{.*}}, offset: {{.*}}, fwdArgRegs: []
1414

1515
--- |
16-
declare noundef i32 @_Z4fizzii(i32 noundef %x, i32 noundef %y)
17-
18-
declare noundef i32 @_Z4buzzii(i32 noundef %x, i32 noundef %y)
19-
20-
define dso_local noundef i32 @_Z3barii(i32 noundef %x, i32 noundef %y) {
16+
declare !type !0 noundef i32 @_Z4fizzii(i32 noundef %x, i32 noundef %y)
17+
18+
declare !type !0 noundef i32 @_Z4buzzii(i32 noundef %x, i32 noundef %y)
19+
20+
define dso_local noundef i32 @_Z3barii(i32 noundef %x, i32 noundef %y) !type !0 {
2121
entry:
2222
%x.addr = alloca i32, align 4
2323
%y.addr = alloca i32, align 4
@@ -33,7 +33,7 @@
3333
ret i32 %sub
3434
}
3535

36-
define dso_local noundef i32 @_Z3fooii(i32 noundef %x, i32 noundef %y) {
36+
define dso_local noundef i32 @_Z3fooii(i32 noundef %x, i32 noundef %y) !type !0 {
3737
entry:
3838
%x.addr = alloca i32, align 4
3939
%y.addr = alloca i32, align 4

0 commit comments

Comments
 (0)