[PATCH] D144518: Preserve the address space for llvm.used and llvm.compiler.used global variables in GlobalOpt pass.
Nawal Copty via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 31 11:45:51 PDT 2023
nawalcopty updated this revision to Diff 510090.
nawalcopty added a comment.
Fixed up the diff to include all changes (including the ones in the previous diff). Thanks.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D144518/new/
https://reviews.llvm.org/D144518
Files:
llvm/lib/Transforms/IPO/GlobalOpt.cpp
llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll
Index: llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll
===================================================================
--- /dev/null
+++ 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
+}
+
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2115,8 +2115,16 @@
return;
}
+ // Get address space of pointers in the array of pointers.
+ unsigned ElemAddrSpace = 0;
+ const Type *UsedArrayType = V.getValueType();
+
+ if (const auto *VAT = cast<ArrayType>(UsedArrayType))
+ if (const auto *VEPT = cast<PointerType>(VAT->getArrayElementType()))
+ ElemAddrSpace = VEPT->getAddressSpace();
+
// Type of pointer to the array of pointers.
- PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
+ PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), ElemAddrSpace);
SmallVector<Constant *, 8> UsedArray;
for (GlobalValue *GV : Init) {
@@ -2124,6 +2132,7 @@
= 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());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144518.510090.patch
Type: text/x-patch
Size: 3153 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230331/5951be38/attachment.bin>
More information about the llvm-commits
mailing list