diff options
author | Nawal Copty <nawal.copty@intel.com> | 2023-04-25 12:45:33 -0700 |
---|---|---|
committer | Arthur Eubanks <aeubanks@google.com> | 2023-04-25 13:07:01 -0700 |
commit | 6e54a57c61af6a959210f3628df9e21e3d7033f5 (patch) | |
tree | d7c2cff3562765dfb58424fb9d402de333b8e7da | |
parent | bab6cfede92830bb38d08282c62144df79b13600 (diff) | |
download | llvm-6e54a57c61af6a959210f3628df9e21e3d7033f5.zip llvm-6e54a57c61af6a959210f3628df9e21e3d7033f5.tar.gz llvm-6e54a57c61af6a959210f3628df9e21e3d7033f5.tar.bz2 |
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
-rw-r--r-- | llvm/lib/Transforms/IPO/GlobalOpt.cpp | 13 | ||||
-rw-r--r-- | llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll | 34 |
2 files changed, 44 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index 9616250..4b6e8e3 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 0000000..9df8a4b --- /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 } + +@_ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8 +@_ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8 + +@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 +} + |