Skip to content

Commit 86c5828

Browse files
authored
Merge branch 'release/20.x' into 20.x-ci
2 parents 1abc898 + 25bcf11 commit 86c5828

File tree

8 files changed

+1406
-38
lines changed

8 files changed

+1406
-38
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,16 +2139,20 @@ void AsmPrinter::emitFunctionBody() {
21392139
}
21402140

21412141
/// Compute the number of Global Variables that uses a Constant.
2142-
static unsigned getNumGlobalVariableUses(const Constant *C) {
2143-
if (!C)
2142+
static unsigned getNumGlobalVariableUses(const Constant *C,
2143+
bool &HasNonGlobalUsers) {
2144+
if (!C) {
2145+
HasNonGlobalUsers = true;
21442146
return 0;
2147+
}
21452148

21462149
if (isa<GlobalVariable>(C))
21472150
return 1;
21482151

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

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

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

21902196
for (const auto &G : M.globals()) {
21912197
unsigned NumGOTEquivUsers = 0;
2192-
if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers))
2198+
bool HasNonGlobalUsers = false;
2199+
if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers, HasNonGlobalUsers))
21932200
continue;
2194-
2201+
// If non-global variables use it, we still need to emit it.
2202+
// Add 1 here, then emit it in `emitGlobalGOTEquivs`.
2203+
if (HasNonGlobalUsers)
2204+
NumGOTEquivUsers += 1;
21952205
const MCSymbol *GOTEquivSym = getSymbol(&G);
21962206
GlobalGOTEquivs[GOTEquivSym] = std::make_pair(&G, NumGOTEquivUsers);
21972207
}

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,20 +2501,33 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
25012501

25022502
// Deallocate the SVE area.
25032503
if (SVEStackSize) {
2504-
// If we have stack realignment or variable sized objects on the stack,
2505-
// restore the stack pointer from the frame pointer prior to SVE CSR
2506-
// restoration.
2507-
if (AFI->isStackRealigned() || MFI.hasVarSizedObjects()) {
2508-
if (int64_t CalleeSavedSize = AFI->getSVECalleeSavedStackSize()) {
2509-
// Set SP to start of SVE callee-save area from which they can
2510-
// be reloaded. The code below will deallocate the stack space
2511-
// space by moving FP -> SP.
2512-
emitFrameOffset(MBB, RestoreBegin, DL, AArch64::SP, AArch64::FP,
2513-
StackOffset::getScalable(-CalleeSavedSize), TII,
2504+
int64_t SVECalleeSavedSize = AFI->getSVECalleeSavedStackSize();
2505+
// If we have stack realignment or variable-sized objects we must use the
2506+
// FP to restore SVE callee saves (as there is an unknown amount of
2507+
// data/padding between the SP and SVE CS area).
2508+
Register BaseForSVEDealloc =
2509+
(AFI->isStackRealigned() || MFI.hasVarSizedObjects()) ? AArch64::FP
2510+
: AArch64::SP;
2511+
if (SVECalleeSavedSize && BaseForSVEDealloc == AArch64::FP) {
2512+
Register CalleeSaveBase = AArch64::FP;
2513+
if (int64_t CalleeSaveBaseOffset =
2514+
AFI->getCalleeSaveBaseToFrameRecordOffset()) {
2515+
// If we have have an non-zero offset to the non-SVE CS base we need to
2516+
// compute the base address by subtracting the offest in a temporary
2517+
// register first (to avoid briefly deallocating the SVE CS).
2518+
CalleeSaveBase = MBB.getParent()->getRegInfo().createVirtualRegister(
2519+
&AArch64::GPR64RegClass);
2520+
emitFrameOffset(MBB, RestoreBegin, DL, CalleeSaveBase, AArch64::FP,
2521+
StackOffset::getFixed(-CalleeSaveBaseOffset), TII,
25142522
MachineInstr::FrameDestroy);
25152523
}
2516-
} else {
2517-
if (AFI->getSVECalleeSavedStackSize()) {
2524+
// The code below will deallocate the stack space space by moving the
2525+
// SP to the start of the SVE callee-save area.
2526+
emitFrameOffset(MBB, RestoreBegin, DL, AArch64::SP, CalleeSaveBase,
2527+
StackOffset::getScalable(-SVECalleeSavedSize), TII,
2528+
MachineInstr::FrameDestroy);
2529+
} else if (BaseForSVEDealloc == AArch64::SP) {
2530+
if (SVECalleeSavedSize) {
25182531
// Deallocate the non-SVE locals first before we can deallocate (and
25192532
// restore callee saves) from the SVE area.
25202533
emitFrameOffset(

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,11 +2135,13 @@ TargetStackID::Value RISCVFrameLowering::getStackIDForScalableVectors() const {
21352135
}
21362136

21372137
// Synthesize the probe loop.
2138-
static void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
2139-
MachineBasicBlock::iterator MBBI, DebugLoc DL,
2138+
static void emitStackProbeInline(MachineBasicBlock::iterator MBBI, DebugLoc DL,
21402139
Register TargetReg, bool IsRVV) {
21412140
assert(TargetReg != RISCV::X2 && "New top of stack cannot already be in SP");
21422141

2142+
MachineBasicBlock &MBB = *MBBI->getParent();
2143+
MachineFunction &MF = *MBB.getParent();
2144+
21432145
auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
21442146
const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
21452147
bool IsRV64 = Subtarget.is64Bit();
@@ -2228,7 +2230,7 @@ void RISCVFrameLowering::inlineStackProbe(MachineFunction &MF,
22282230
MachineBasicBlock::iterator MBBI = MI->getIterator();
22292231
DebugLoc DL = MBB.findDebugLoc(MBBI);
22302232
Register TargetReg = MI->getOperand(1).getReg();
2231-
emitStackProbeInline(MF, MBB, MBBI, DL, TargetReg,
2233+
emitStackProbeInline(MBBI, DL, TargetReg,
22322234
(MI->getOpcode() == RISCV::PROBED_STACKALLOC_RVV));
22332235
MBBI->eraseFromParent();
22342236
}

llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ static bool shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV) {
6767
if (!ElemType->isPointerTy() || DL.getPointerTypeSizeInBits(ElemType) != 64)
6868
return false;
6969

70+
SmallVector<GlobalVariable *, 4> GVOps;
71+
Triple TT(M.getTargetTriple());
72+
// FIXME: This should be removed in the future.
73+
bool ShouldDropUnnamedAddr =
74+
// Drop unnamed_addr to avoid matching pattern in
75+
// `handleIndirectSymViaGOTPCRel`, which generates GOTPCREL relocations
76+
// not supported by the GNU linker and LLD versions below 18 on aarch64.
77+
TT.isAArch64()
78+
// Apple's ld64 (and ld-prime on Xcode 15.2) miscompile something on
79+
// x86_64-apple-darwin. See
80+
// https://github.com/rust-lang/rust/issues/140686 and
81+
// https://github.com/rust-lang/rust/issues/141306.
82+
|| (TT.isX86() && TT.isOSDarwin());
83+
7084
for (const Use &Op : Array->operands()) {
7185
Constant *ConstOp = cast<Constant>(&Op);
7286
GlobalValue *GVOp;
@@ -86,8 +100,15 @@ static bool shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV) {
86100
!GlovalVarOp->isDSOLocal() ||
87101
!GlovalVarOp->isImplicitDSOLocal())
88102
return false;
103+
104+
if (ShouldDropUnnamedAddr)
105+
GVOps.push_back(GlovalVarOp);
89106
}
90107

108+
if (ShouldDropUnnamedAddr)
109+
for (auto *GVOp : GVOps)
110+
GVOp->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
111+
91112
return true;
92113
}
93114

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

112-
Triple TT(M.getTargetTriple());
113-
// FIXME: This should be removed in the future.
114-
bool ShouldDropUnnamedAddr =
115-
// Drop unnamed_addr to avoid matching pattern in
116-
// `handleIndirectSymViaGOTPCRel`, which generates GOTPCREL relocations
117-
// not supported by the GNU linker and LLD versions below 18 on aarch64.
118-
TT.isAArch64()
119-
// Apple's ld64 (and ld-prime on Xcode 15.2) miscompile something on
120-
// x86_64-apple-darwin. See
121-
// https://github.com/rust-lang/rust/issues/140686 and
122-
// https://github.com/rust-lang/rust/issues/141306.
123-
|| (TT.isX86() && TT.isOSDarwin());
124-
125133
for (Use &Operand : LookupTableArr->operands()) {
126134
Constant *Element = cast<Constant>(Operand);
127-
if (ShouldDropUnnamedAddr)
128-
if (auto *GlobalElement = dyn_cast<GlobalValue>(Element))
129-
GlobalElement->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
130135
Type *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext());
131136
Constant *Base = llvm::ConstantExpr::getPtrToInt(RelLookupTable, IntPtrTy);
132137
Constant *Target = llvm::ConstantExpr::getPtrToInt(Element, IntPtrTy);

0 commit comments

Comments
 (0)