[llvm] 6e54a57 - Preserve the address space for llvm.used and llvm.compiler.used global variables in GlobalOpt pass.

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 13:07:16 PDT 2023


Author: Nawal Copty
Date: 2023-04-25T13:07:01-07:00
New Revision: 6e54a57c61af6a959210f3628df9e21e3d7033f5

URL: https://github.com/llvm/llvm-project/commit/6e54a57c61af6a959210f3628df9e21e3d7033f5
DIFF: https://github.com/llvm/llvm-project/commit/6e54a57c61af6a959210f3628df9e21e3d7033f5.diff

LOG: Preserve the address space for llvm.used and llvm.compiler.used global variables in GlobalOpt pass.

The llvm.used (or llvm.compiler.used) global variable is an array that contains a list of pointers to global variables and functions.

The GlobalOpt (Global Variable Optimizer) pass is not preserving the address space for llvm.used and llvm.compiler.used global variables.This patch updates the setUsedInitializer() function in GlobalOpt.cpp, so the address space is preserved.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D144518

Added: 
    llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll

Modified: 
    llvm/lib/Transforms/IPO/GlobalOpt.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 96162509289b8..4b6e8e397db69 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2113,15 +2113,22 @@ static void setUsedInitializer(GlobalVariable &V,
     return;
   }
 
+  // Get address space of pointers in the array of pointers.
+  const Type *UsedArrayType = V.getValueType();
+  const auto *VAT = cast<ArrayType>(UsedArrayType);
+  const auto *VEPT = cast<PointerType>(VAT->getArrayElementType());
+
   // Type of pointer to the array of pointers.
-  PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
+  PointerType *Int8PtrTy =
+      Type::getInt8PtrTy(V.getContext(), VEPT->getAddressSpace());
 
   SmallVector<Constant *, 8> UsedArray;
   for (GlobalValue *GV : Init) {
-    Constant *Cast
-      = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy);
+    Constant *Cast =
+        ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy);
     UsedArray.push_back(Cast);
   }
+
   // Sort to get deterministic order.
   array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
   ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size());

diff  --git a/llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll b/llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll
new file mode 100644
index 0000000000000..9df8a4bfbfae1
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll
@@ -0,0 +1,34 @@
+; RUN: opt -S -passes=globalopt < %s | FileCheck %s
+;; Check that Global Opt preserves address space of llvm.used and
+;; llvm.compiler.used variables.
+
+%struct.FakeDeviceGlobal = type { ptr addrspace(4) }
+%class.anon = type { i8 }
+
+ at _ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
+ at _ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
+
+ at llvm.compiler.used = appending global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))]
+
+; CHECK: @llvm.compiler.used = appending global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4))]
+
+define weak_odr dso_local void @foo() {
+entry:
+  %A = alloca %class.anon, align 1
+  %A.addrspacecast = addrspacecast ptr %A to ptr addrspace(4)
+  call void @bar(ptr addrspace(4) noundef align 1 dereferenceable_or_null(1) %A.addrspacecast)
+  ret void
+}
+
+define internal void @bar(ptr addrspace(4) noundef align 1 dereferenceable_or_null(1) %this) align 2 {
+entry:
+  %this.addr = alloca ptr addrspace(4), align 8
+  %this.addr.ascast = addrspacecast ptr %this.addr to ptr addrspace(4)
+  store ptr addrspace(4) %this, ptr addrspace(4) %this.addr.ascast, align 8
+  %v1 = load ptr addrspace(4), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), align 8
+  store i32 42, ptr addrspace(4) %v1, align 4
+  %v2 = load ptr addrspace(4), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4)), align 8
+  store i32 42, ptr addrspace(4) %v2, align 4
+  ret void
+}
+


        


More information about the llvm-commits mailing list