Skip to content

Backport #145690, #146068 and #144058 #182

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 3 commits into
base: rustc/20.1-2025-02-13
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
24 changes: 17 additions & 7 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,16 +2139,20 @@ void AsmPrinter::emitFunctionBody() {
}

/// Compute the number of Global Variables that uses a Constant.
static unsigned getNumGlobalVariableUses(const Constant *C) {
if (!C)
static unsigned getNumGlobalVariableUses(const Constant *C,
bool &HasNonGlobalUsers) {
if (!C) {
HasNonGlobalUsers = true;
return 0;
}

if (isa<GlobalVariable>(C))
return 1;

unsigned NumUses = 0;
for (const auto *CU : C->users())
NumUses += getNumGlobalVariableUses(dyn_cast<Constant>(CU));
NumUses +=
getNumGlobalVariableUses(dyn_cast<Constant>(CU), HasNonGlobalUsers);

return NumUses;
}
Expand All @@ -2159,7 +2163,8 @@ static unsigned getNumGlobalVariableUses(const Constant *C) {
/// candidates are skipped and are emitted later in case at least one cstexpr
/// isn't replaced by a PC relative GOT entry access.
static bool isGOTEquivalentCandidate(const GlobalVariable *GV,
unsigned &NumGOTEquivUsers) {
unsigned &NumGOTEquivUsers,
bool &HasNonGlobalUsers) {
// Global GOT equivalents are unnamed private globals with a constant
// pointer initializer to another global symbol. They must point to a
// GlobalVariable or Function, i.e., as GlobalValue.
Expand All @@ -2171,7 +2176,8 @@ static bool isGOTEquivalentCandidate(const GlobalVariable *GV,
// To be a got equivalent, at least one of its users need to be a constant
// expression used by another global variable.
for (const auto *U : GV->users())
NumGOTEquivUsers += getNumGlobalVariableUses(dyn_cast<Constant>(U));
NumGOTEquivUsers +=
getNumGlobalVariableUses(dyn_cast<Constant>(U), HasNonGlobalUsers);

return NumGOTEquivUsers > 0;
}
Expand All @@ -2189,9 +2195,13 @@ void AsmPrinter::computeGlobalGOTEquivs(Module &M) {

for (const auto &G : M.globals()) {
unsigned NumGOTEquivUsers = 0;
if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers))
bool HasNonGlobalUsers = false;
if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers, HasNonGlobalUsers))
continue;

// If non-global variables use it, we still need to emit it.
// Add 1 here, then emit it in `emitGlobalGOTEquivs`.
if (HasNonGlobalUsers)
NumGOTEquivUsers += 1;
const MCSymbol *GOTEquivSym = getSymbol(&G);
GlobalGOTEquivs[GOTEquivSym] = std::make_pair(&G, NumGOTEquivUsers);
}
Expand Down
8 changes: 0 additions & 8 deletions llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
const bool IsObjectFile =
O.Header.FileType == MachO::HeaderFileType::MH_OBJECT;
uint64_t Offset = IsObjectFile ? (HeaderSize + O.Header.SizeOfCmds) : 0;
// If we are emitting an encryptable binary, our load commands must have a
// separate (non-encrypted) page to themselves.
bool RequiresFirstSectionOutsideFirstPage =
O.EncryptionInfoCommandIndex.has_value();
for (LoadCommand &LC : O.LoadCommands) {
auto &MLC = LC.MachOLoadCommand;
StringRef Segname;
Expand Down Expand Up @@ -173,10 +169,6 @@ uint64_t MachOLayoutBuilder::layoutSegments() {
if (!Sec->hasValidOffset()) {
Sec->Offset = 0;
} else {
if (RequiresFirstSectionOutsideFirstPage) {
SectOffset = alignToPowerOf2(SectOffset, PageSize);
RequiresFirstSectionOutsideFirstPage = false;
}
Sec->Offset = SegOffset + SectOffset;
Sec->Size = Sec->Content.size();
SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);
Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/ObjCopy/MachO/MachOObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ void Object::updateLoadCommandIndexes() {
case MachO::LC_DYLD_EXPORTS_TRIE:
ExportsTrieCommandIndex = Index;
break;
case MachO::LC_ENCRYPTION_INFO:
case MachO::LC_ENCRYPTION_INFO_64:
EncryptionInfoCommandIndex = Index;
break;
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/ObjCopy/MachO/MachOObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,6 @@ struct Object {
/// The index of the LC_SEGMENT or LC_SEGMENT_64 load command
/// corresponding to the __TEXT segment.
std::optional<size_t> TextSegmentCommandIndex;
/// The index of the LC_ENCRYPTION_INFO or LC_ENCRYPTION_INFO_64 load command
/// if present.
std::optional<size_t> EncryptionInfoCommandIndex;

BumpPtrAllocator Alloc;
StringSaver NewSectionsContents;
Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/ObjCopy/MachO/MachOReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ Error MachOReader::readLoadCommands(Object &O) const {
case MachO::LC_DYLD_CHAINED_FIXUPS:
O.ChainedFixupsCommandIndex = O.LoadCommands.size();
break;
case MachO::LC_ENCRYPTION_INFO:
case MachO::LC_ENCRYPTION_INFO_64:
O.EncryptionInfoCommandIndex = O.LoadCommands.size();
break;
}
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
case MachO::LCName: \
Expand Down
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
36 changes: 36 additions & 0 deletions llvm/test/MC/X86/gotpcrel-non-globals.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; RUN: llc < %s | FileCheck %s

target triple = "x86_64-unknown-linux-gnu"

; Check that we emit the `@bar_*` symbols, and that we don't emit multiple symbols.

; CHECK-LABEL: .Lrel_0:
; CHECK: .long foo_0@GOTPCREL+0
; CHECK-LABEL: .Lrel_1_failed:
; CHECK: .long bar_1-foo_0
; CHECK-LABEL: .Lrel_2:
; CHECK: .long foo_2@GOTPCREL+0

; CHECK: bar_0:
; CHECK: bar_1:
; CHECK: bar_2_indirect:

@rel_0 = private unnamed_addr constant [1 x i32] [
i32 trunc (i64 sub (i64 ptrtoint (ptr @bar_0 to i64), i64 ptrtoint (ptr @rel_0 to i64)) to i32)]
@rel_1_failed = private unnamed_addr constant [1 x i32] [
i32 trunc (i64 sub (i64 ptrtoint (ptr @bar_1 to i64), i64 ptrtoint (ptr @foo_0 to i64)) to i32)]
@rel_2 = private unnamed_addr constant [1 x i32] [
i32 trunc (i64 sub (i64 ptrtoint (ptr @bar_2_indirect to i64), i64 ptrtoint (ptr @rel_2 to i64)) to i32)]
@bar_0 = internal unnamed_addr constant ptr @foo_0, align 8
@bar_1 = internal unnamed_addr constant ptr @foo_1, align 8
@bar_2_indirect = internal unnamed_addr constant ptr @foo_2, align 8
@foo_0 = external global ptr, align 8
@foo_1 = external global ptr, align 8
@foo_2 = external global ptr, align 8

define void @foo(ptr %arg0, ptr %arg1) {
store ptr @bar_0, ptr %arg0, align 8
store ptr @bar_1, ptr %arg1, align 8
store ptr getelementptr (i8, ptr @bar_2_indirect, i32 1), ptr %arg1, align 8
ret void
}
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