2121
2222using namespace llvm ;
2323
24- static bool
25- shouldConvertToRelLookupTable (Module &M, GlobalVariable &GV,
26- SmallVectorImpl<GlobalVariable *> &GVOps,
27- bool ShouldDropUnnamedAddr) {
24+ static bool shouldConvertToRelLookupTable (Module &M, GlobalVariable &GV) {
2825 // If lookup table has more than one user,
2926 // do not generate a relative lookup table.
3027 // This is to simplify the analysis that needs to be done for this pass.
@@ -69,6 +66,20 @@ shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV,
6966 if (!ElemType->isPointerTy () || DL.getPointerTypeSizeInBits (ElemType) != 64 )
7067 return false ;
7168
69+ SmallVector<GlobalVariable *, 4 > GVOps;
70+ Triple TT = M.getTargetTriple ();
71+ // FIXME: This should be removed in the future.
72+ bool ShouldDropUnnamedAddr =
73+ // Drop unnamed_addr to avoid matching pattern in
74+ // `handleIndirectSymViaGOTPCRel`, which generates GOTPCREL relocations
75+ // not supported by the GNU linker and LLD versions below 18 on aarch64.
76+ TT.isAArch64 ()
77+ // Apple's ld64 (and ld-prime on Xcode 15.2) miscompile something on
78+ // x86_64-apple-darwin. See
79+ // https://github.com/rust-lang/rust/issues/140686 and
80+ // https://github.com/rust-lang/rust/issues/141306.
81+ || (TT.isX86 () && TT.isOSDarwin ());
82+
7283 for (const Use &Op : Array->operands ()) {
7384 Constant *ConstOp = cast<Constant>(&Op);
7485 GlobalValue *GVOp;
@@ -93,6 +104,10 @@ shouldConvertToRelLookupTable(Module &M, GlobalVariable &GV,
93104 GVOps.push_back (GlovalVarOp);
94105 }
95106
107+ if (ShouldDropUnnamedAddr)
108+ for (auto *GVOp : GVOps)
109+ GVOp->setUnnamedAddr (GlobalValue::UnnamedAddr::None);
110+
96111 return true ;
97112}
98113
@@ -189,28 +204,10 @@ static bool convertToRelativeLookupTables(
189204
190205 bool Changed = false ;
191206
192- Triple TT = M.getTargetTriple ();
193- // FIXME: This should be removed in the future.
194- bool ShouldDropUnnamedAddr =
195- // Drop unnamed_addr to avoid matching pattern in
196- // `handleIndirectSymViaGOTPCRel`, which generates GOTPCREL relocations
197- // not supported by the GNU linker and LLD versions below 18 on aarch64.
198- TT.isAArch64 ()
199- // Apple's ld64 (and ld-prime on Xcode 15.2) miscompile something on
200- // x86_64-apple-darwin. See
201- // https://github.com/rust-lang/rust/issues/140686 and
202- // https://github.com/rust-lang/rust/issues/141306.
203- || (TT.isX86 () && TT.isOSDarwin ());
204207 for (GlobalVariable &GV : llvm::make_early_inc_range (M.globals ())) {
205- SmallVector<GlobalVariable *, 4 > GVOps;
206-
207- if (!shouldConvertToRelLookupTable (M, GV, GVOps, ShouldDropUnnamedAddr))
208+ if (!shouldConvertToRelLookupTable (M, GV))
208209 continue ;
209210
210- if (ShouldDropUnnamedAddr)
211- for (auto *GVOp : GVOps)
212- GVOp->setUnnamedAddr (GlobalValue::UnnamedAddr::None);
213-
214211 convertToRelLookupTable (GV);
215212
216213 // Remove the original lookup table.
0 commit comments