[llvm] [LLVM] Maintain element type of @llvm.compiler.used/@llvm.used if it already exists (PR #162660)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 21 07:50:34 PDT 2025


================
@@ -671,3 +672,80 @@ void GlobalIFunc::applyAlongResolverPath(
   DenseSet<const GlobalAlias *> Aliases;
   findBaseObject(getResolver(), Aliases, Op);
 }
+
+static void collectUsedGlobals(GlobalVariable *GV,
+                               SmallSetVector<Constant *, 16> &Init) {
+  if (!GV || !GV->hasInitializer())
+    return;
+
+  auto *CA = cast<ConstantArray>(GV->getInitializer());
+  for (Use &Op : CA->operands())
+    Init.insert(cast<Constant>(Op));
+}
+
+static void appendToUsedList(Module &M, StringRef Name,
+                             ArrayRef<GlobalValue *> Values) {
+  GlobalVariable *GV = M.getGlobalVariable(Name);
+
+  SmallSetVector<Constant *, 16> Init;
+  collectUsedGlobals(GV, Init);
+  Type *ArrayEltTy = GV ? GV->getValueType()->getArrayElementType()
+                        : PointerType::getUnqual(M.getContext());
----------------
jmmartinez wrote:

My idea was to have an intermediate step were we keep the current state, but where the crash is fixed.
Then update Clang to emit the AS 0 (just filed https://github.com/llvm/llvm-project/pull/164432).
Then use these helpers in Clang (which should be an NFC).
Then add the check in the verifier and update the `appendToUsed/appendToCompilerUsed` methods to only use the AS 0 pointer.

https://github.com/llvm/llvm-project/pull/162660


More information about the llvm-commits mailing list