[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
Tue Feb 21 13:33:09 PST 2023


nawalcopty created this revision.
nawalcopty added reviewers: jcranmer, steffenlarsen, aeubanks.
Herald added subscribers: ormris, hiraditya, arichardson.
Herald added a project: All.
nawalcopty requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

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,58 @@
+; RUN: opt -S -passes=globalopt < %s | FileCheck %s
+;; Check that Global Opt preserves address space of llvm.used and
+;; llvm.compiler.used variables.
+
+; ModuleID = 'global-opt-addrspace.ll'
+source_filename = "device_global_internal_failure.cpp"
+target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
+target triple = "spir64-unknown-unknown"
+
+%struct.FakeDeviceGlobal = type { ptr addrspace(4) }
+%class.anon = type { i8 }
+
+$_ZTSZ4mainEUlvE_ = comdat any
+
+ at _ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8 #0
+ at llvm.compiler.used = appending global [1 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))], section "llvm.metadata"
+
+; CHECK: @llvm.compiler.used = appending global [1 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))], section "llvm.metadata"
+
+; Function Attrs: convergent mustprogress noinline norecurse optnone
+define weak_odr dso_local spir_kernel void @_ZTSZ4mainEUlvE_() #1 comdat !kernel_arg_buffer_location !5 {
+entry:
+  %0 = alloca %class.anon, align 1
+  %1 = addrspacecast ptr %0 to ptr addrspace(4)
+  call spir_func void @_ZZ4mainENKUlvE_clEv(ptr addrspace(4) noundef align 1 dereferenceable_or_null(1) %1) #3
+  ret void
+}
+
+; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
+define internal spir_func void @_ZZ4mainENKUlvE_clEv(ptr addrspace(4) noundef align 1 dereferenceable_or_null(1) %this) #2 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
+  %this1 = load ptr addrspace(4), ptr addrspace(4) %this.addr.ascast, align 8
+  %0 = load ptr addrspace(4), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4)), align 8
+  store i32 42, ptr addrspace(4) %0, align 4
+  ret void
+}
+
+declare dso_local spir_func i32 @_Z18__spirv_ocl_printfPU3AS2Kcz(ptr addrspace(2), ...)
+
+attributes #0 = { "sycl-unique-id"="c5b4ae0b52852573____ZL1C" }
+attributes #1 = { convergent mustprogress noinline norecurse optnone "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "sycl-module-id"="device_global_internal_failure.cpp" "uniform-work-group-size"="true" }
+attributes #2 = { convergent mustprogress noinline norecurse nounwind optnone "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #3 = { convergent }
+
+!llvm.module.flags = !{!0, !1}
+!opencl.spir.version = !{!2}
+!spirv.Source = !{!3}
+!llvm.ident = !{!4}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{i32 1, i32 2}
+!3 = !{i32 4, i32 100000}
+!4 = !{!"clang version 15.0.0 (https://github.com/intel/llvm.git 537e51b08b74438ed64eb7de6e254922e8ddeaa0)"}
+!5 = !{}
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2142,8 +2142,16 @@
     return;
   }
 
+  // Get address space of pointers in the array of pointers.
+  unsigned ElemAddrSpace = 0;
+  const Type *UsedArrayType = V.getValueType();
+
+  if (const auto *VAT = dyn_cast<ArrayType>(UsedArrayType))
+    if (const auto *VEPT = dyn_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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144518.499278.patch
Type: text/x-patch
Size: 4118 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230221/f190c11a/attachment-0001.bin>


More information about the llvm-commits mailing list