Skip to content

Commit 2b8917f

Browse files
committed
[pgo] Avoid introducing relocations by using private alias
Instead of using the public, interposable symbol, we can use a private alias and avoid relocations and addends. Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D137982
1 parent 2323a4e commit 2b8917f

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,9 +1014,17 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfInstBase *Inc) {
10141014
};
10151015
auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
10161016

1017-
Constant *FunctionAddr = shouldRecordFunctionAddr(Fn)
1018-
? ConstantExpr::getBitCast(Fn, Int8PtrTy)
1019-
: ConstantPointerNull::get(Int8PtrTy);
1017+
Constant *FunctionAddr;
1018+
if (shouldRecordFunctionAddr(Fn)) {
1019+
// When possible use a private alias to avoid relocations
1020+
Constant *Addr = !Fn->isDeclarationForLinker()
1021+
? static_cast<Constant *>(GlobalAlias::create(
1022+
GlobalValue::PrivateLinkage, Fn->getName(), Fn))
1023+
: Fn;
1024+
FunctionAddr = ConstantExpr::getBitCast(Addr, Int8PtrTy);
1025+
} else {
1026+
FunctionAddr = ConstantPointerNull::get(Int8PtrTy);
1027+
}
10201028

10211029
Constant *Int16ArrayVals[IPVK_Last + 1];
10221030
for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: opt -S -passes=pgo-instr-gen,instrprof < %s | FileCheck %s
2+
3+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
; Test that we use private aliases to reference function addresses inside profile data
7+
8+
; CHECK: @__profd_foo = private global {{.*}}, ptr @foo.1,
9+
; CHECK-NOT: @__profd_foo = private global {{.*}}, ptr @foo,
10+
; CHECK: @foo.1 = private alias i32 (i32), ptr @foo
11+
12+
define i32 @foo(i32 %0) {
13+
; CHECK-LABEL: @foo(
14+
; CHECK-NEXT: entry:
15+
; CHECK-NEXT: [[PGOCOUNT:%.*]] = load i64, ptr @__profc_foo, align 8
16+
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[PGOCOUNT]], 1
17+
; CHECK-NEXT: store i64 [[TMP1]], ptr @__profc_foo, align 8
18+
; CHECK-NEXT: ret i32 0
19+
;
20+
entry:
21+
ret i32 0
22+
}
23+

0 commit comments

Comments
 (0)