Skip to content

release/20.x: [RelLookupTableConverter] Drop unnamed_addr for GVs in entries to avoid generating GOTPCREL relocations (#146068) #146191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/20.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 21 additions & 16 deletions llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ static bool shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV) {
if (!ElemType->isPointerTy() || DL.getPointerTypeSizeInBits(ElemType) != 64)
return false;

SmallVector<GlobalVariable *, 4> GVOps;
Triple TT(M.getTargetTriple());
// FIXME: This should be removed in the future.
bool ShouldDropUnnamedAddr =
// Drop unnamed_addr to avoid matching pattern in
// `handleIndirectSymViaGOTPCRel`, which generates GOTPCREL relocations
// not supported by the GNU linker and LLD versions below 18 on aarch64.
TT.isAArch64()
// Apple's ld64 (and ld-prime on Xcode 15.2) miscompile something on
// x86_64-apple-darwin. See
// https://github.com/rust-lang/rust/issues/140686 and
// https://github.com/rust-lang/rust/issues/141306.
|| (TT.isX86() && TT.isOSDarwin());

for (const Use &Op : Array->operands()) {
Constant *ConstOp = cast<Constant>(&Op);
GlobalValue *GVOp;
Expand All @@ -86,8 +100,15 @@ static bool shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV) {
!GlovalVarOp->isDSOLocal() ||
!GlovalVarOp->isImplicitDSOLocal())
return false;

if (ShouldDropUnnamedAddr)
GVOps.push_back(GlovalVarOp);
}

if (ShouldDropUnnamedAddr)
for (auto *GVOp : GVOps)
GVOp->setUnnamedAddr(GlobalValue::UnnamedAddr::None);

return true;
}

Expand All @@ -109,24 +130,8 @@ static GlobalVariable *createRelLookupTable(Function &Func,
uint64_t Idx = 0;
SmallVector<Constant *, 64> RelLookupTableContents(NumElts);

Triple TT(M.getTargetTriple());
// FIXME: This should be removed in the future.
bool ShouldDropUnnamedAddr =
// Drop unnamed_addr to avoid matching pattern in
// `handleIndirectSymViaGOTPCRel`, which generates GOTPCREL relocations
// not supported by the GNU linker and LLD versions below 18 on aarch64.
TT.isAArch64()
// Apple's ld64 (and ld-prime on Xcode 15.2) miscompile something on
// x86_64-apple-darwin. See
// https://github.com/rust-lang/rust/issues/140686 and
// https://github.com/rust-lang/rust/issues/141306.
|| (TT.isX86() && TT.isOSDarwin());

for (Use &Operand : LookupTableArr->operands()) {
Constant *Element = cast<Constant>(Operand);
if (ShouldDropUnnamedAddr)
if (auto *GlobalElement = dyn_cast<GlobalValue>(Element))
GlobalElement->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
Type *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
Constant *Base = llvm::ConstantExpr::getPtrToInt(RelLookupTable, IntPtrTy);
Constant *Target = llvm::ConstantExpr::getPtrToInt(Element, IntPtrTy);
Expand Down
45 changes: 45 additions & 0 deletions llvm/test/Transforms/RelLookupTableConverter/unnamed_addr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
@y3 = internal unnamed_addr constant ptr @x0
@load_relative_2.table = private unnamed_addr constant [4 x ptr] [ptr @y3, ptr @y2, ptr @y1, ptr @y0]

@b0 = private unnamed_addr constant [8 x i8] c"00000000"
@b1 = private unnamed_addr constant [8 x i8] c"11111111"
@b2 = private unnamed_addr constant [8 x i8] c"22222222"
@load_relative_3.table = private unnamed_addr constant [3 x ptr] [
ptr getelementptr inbounds (i8, ptr @b0, i64 8),
ptr getelementptr inbounds (i8, ptr @b1, i64 8),
ptr getelementptr inbounds (i8, ptr @b2, i64 8)]

;.
; x86_64-apple-darwin: @a0 = private constant i32 0
; x86_64-apple-darwin: @a1 = private constant i32 1
Expand All @@ -34,6 +42,10 @@
; x86_64-apple-darwin: @y2 = internal constant ptr @x1
; x86_64-apple-darwin: @y3 = internal constant ptr @x0
; x86_64-apple-darwin: @load_relative_2.table.rel = private unnamed_addr constant [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr @y3 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y2 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y1 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y0 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32)], align 4
; x86_64-apple-darwin: @b0 = private constant [8 x i8] c"00000000"
; x86_64-apple-darwin: @b1 = private constant [8 x i8] c"11111111"
; x86_64-apple-darwin: @b2 = private constant [8 x i8] c"22222222"
; x86_64-apple-darwin: @load_relative_3.table.rel = private unnamed_addr constant [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b0, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b1, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b2, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32)], align 4
;.
; aarch64: @a0 = private constant i32 0
; aarch64: @a1 = private constant i32 1
Expand All @@ -48,6 +60,10 @@
; aarch64: @y2 = internal constant ptr @x1
; aarch64: @y3 = internal constant ptr @x0
; aarch64: @load_relative_2.table.rel = private unnamed_addr constant [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr @y3 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y2 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y1 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y0 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32)], align 4
; aarch64: @b0 = private constant [8 x i8] c"00000000"
; aarch64: @b1 = private constant [8 x i8] c"11111111"
; aarch64: @b2 = private constant [8 x i8] c"22222222"
; aarch64: @load_relative_3.table.rel = private unnamed_addr constant [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b0, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b1, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b2, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32)], align 4
;.
; x86_64: @a0 = private unnamed_addr constant i32 0
; x86_64: @a1 = private unnamed_addr constant i32 1
Expand All @@ -62,6 +78,10 @@
; x86_64: @y2 = internal unnamed_addr constant ptr @x1
; x86_64: @y3 = internal unnamed_addr constant ptr @x0
; x86_64: @load_relative_2.table.rel = private unnamed_addr constant [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr @y3 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y2 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y1 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @y0 to i64), i64 ptrtoint (ptr @load_relative_2.table.rel to i64)) to i32)], align 4
; x86_64: @b0 = private unnamed_addr constant [8 x i8] c"00000000"
; x86_64: @b1 = private unnamed_addr constant [8 x i8] c"11111111"
; x86_64: @b2 = private unnamed_addr constant [8 x i8] c"22222222"
; x86_64: @load_relative_3.table.rel = private unnamed_addr constant [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b0, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b1, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @b2, i64 8) to i64), i64 ptrtoint (ptr @load_relative_3.table.rel to i64)) to i32)], align 4
;.
define ptr @load_relative_1(i64 %offset) {
; x86_64-apple-darwin-LABEL: define ptr @load_relative_1(
Expand Down Expand Up @@ -110,6 +130,31 @@ define ptr @load_relative_2(i64 %offset) {
%load = load ptr, ptr %gep
ret ptr %load
}

define ptr @load_relative_3(i64 %offset) {
; x86_64-apple-darwin-LABEL: define ptr @load_relative_3(
; x86_64-apple-darwin-SAME: i64 [[OFFSET:%.*]]) {
; x86_64-apple-darwin-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i64 [[OFFSET]], 2
; x86_64-apple-darwin-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i64(ptr @load_relative_3.table.rel, i64 [[RELTABLE_SHIFT]])
; x86_64-apple-darwin-NEXT: ret ptr [[RELTABLE_INTRINSIC]]
;
; aarch64-LABEL: define ptr @load_relative_3(
; aarch64-SAME: i64 [[OFFSET:%.*]]) {
; aarch64-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i64 [[OFFSET]], 2
; aarch64-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i64(ptr @load_relative_3.table.rel, i64 [[RELTABLE_SHIFT]])
; aarch64-NEXT: ret ptr [[RELTABLE_INTRINSIC]]
;
; x86_64-LABEL: define ptr @load_relative_3(
; x86_64-SAME: i64 [[OFFSET:%.*]]) {
; x86_64-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i64 [[OFFSET]], 2
; x86_64-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i64(ptr @load_relative_3.table.rel, i64 [[RELTABLE_SHIFT]])
; x86_64-NEXT: ret ptr [[RELTABLE_INTRINSIC]]
;
%gep = getelementptr inbounds [3 x ptr], ptr @load_relative_3.table, i64 0, i64 %offset
%load = load ptr, ptr %gep
ret ptr %load
}

;.
; x86_64-apple-darwin: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind willreturn memory(argmem: read) }
;.
Expand Down
Loading