Skip to content

ModuleUtils: Use proper address space defaults in a few places. #119136

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

resistor
Copy link
Collaborator

@resistor resistor commented Dec 8, 2024

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Dec 8, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Owen Anderson (resistor)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/119136.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/ModuleUtils.cpp (+11-6)
diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
index 7249571f344938..0bf68eca5299f4 100644
--- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -31,6 +31,11 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
   IRBuilder<> IRB(M.getContext());
   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
 
+  unsigned CtorPtrAS = M.getDataLayout().getProgramAddressSpace();
+  unsigned GlobalsAS = M.getDataLayout().getDefaultGlobalsAddressSpace();
+  llvm::Type *CtorPFTy = llvm::PointerType::get(FnTy, CtorPtrAS);
+  llvm::Type *ArgTy = IRB.getPtrTy(GlobalsAS);
+
   // Get the current set of static global constructors and add the new ctor
   // to the list.
   SmallVector<Constant *, 16> CurrentCtors;
@@ -45,17 +50,16 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
     }
     GVCtor->eraseFromParent();
   } else {
-    EltTy = StructType::get(IRB.getInt32Ty(),
-                            PointerType::get(FnTy, F->getAddressSpace()),
-                            IRB.getPtrTy());
+    EltTy = StructType::get(
+        IRB.getInt32Ty(), PointerType::get(FnTy, F->getAddressSpace()), ArgTy);
   }
 
   // Build a 3 field global_ctor entry.  We don't take a comdat key.
   Constant *CSVals[3];
   CSVals[0] = IRB.getInt32(Priority);
   CSVals[1] = F;
-  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getPtrTy())
-                   : Constant::getNullValue(IRB.getPtrTy());
+  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, ArgTy)
+                   : Constant::getNullValue(ArgTy);
   Constant *RuntimeCtorInit =
       ConstantStruct::get(EltTy, ArrayRef(CSVals, EltTy->getNumElements()));
 
@@ -483,7 +487,8 @@ bool llvm::lowerGlobalIFuncUsersAsGlobalCtor(
 
   InitBuilder.CreateRetVoid();
 
-  PointerType *ConstantDataTy = PointerType::get(Ctx, 0);
+  PointerType *ConstantDataTy =
+      PointerType::get(Ctx, DL.getDefaultGlobalsAddressSpace());
 
   // TODO: Is this the right priority? Probably should be before any other
   // constructors?

@resistor resistor marked this pull request as draft December 9, 2024 23:28
@jrtc27
Copy link
Collaborator

jrtc27 commented Dec 10, 2024

This overlaps with #93914. There's important discussion there and in the parent #93601 about some of the "pointers" involved here not being real pointers.

@resistor
Copy link
Collaborator Author

This overlaps with #93914. There's important discussion there and in the parent #93601 about some of the "pointers" involved here not being real pointers.

Are those stalled out? I'm happy to abandon this in favor of those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants