[llvm] [HipStdPar] Fix globle variable (PR #90627)

Yaxun Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 09:57:38 PDT 2024


https://github.com/yxsamliu created https://github.com/llvm/llvm-project/pull/90627

HipStdParAcceleratorCodeSelectionPass changes linkage of global variables to extern_weak, which does not allow initializer.

An extern_weak global variable with initializer will cause llvm-as and llc to fail.

>From b2f7042288bc69286313ffb991ed3c6b5b598ea3 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Tue, 30 Apr 2024 12:48:44 -0400
Subject: [PATCH] [HipStdPar] Fix globle variable

HipStdParAcceleratorCodeSelectionPass changes linkage of
global variables to extern_weak, which does not allow
initializer.

An extern_weak global variable with initializer will
cause llvm-as and llc to fail.
---
 llvm/lib/Transforms/HipStdPar/HipStdPar.cpp  |  1 +
 llvm/test/Transforms/HipStdPar/global-var.ll | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 llvm/test/Transforms/HipStdPar/global-var.ll

diff --git a/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp b/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp
index fb7cba9edbdb8b..1a8096f647d847 100644
--- a/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp
+++ b/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp
@@ -133,6 +133,7 @@ static inline void maybeHandleGlobals(Module &M) {
       continue;
 
     G.setLinkage(GlobalVariable::ExternalWeakLinkage);
+    G.setInitializer(nullptr);
     G.setExternallyInitialized(true);
   }
 }
diff --git a/llvm/test/Transforms/HipStdPar/global-var.ll b/llvm/test/Transforms/HipStdPar/global-var.ll
new file mode 100644
index 00000000000000..30340bd6c7ca06
--- /dev/null
+++ b/llvm/test/Transforms/HipStdPar/global-var.ll
@@ -0,0 +1,11 @@
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=hipstdpar-select-accelerator-code \
+; RUN: %s | FileCheck %s
+
+; CHECK: @var = extern_weak addrspace(1) externally_initialized global i32, align 4
+ at var = addrspace(1) global i32 0, align 4
+
+define amdgpu_kernel void @kernel() {
+entry:
+  store i32 1, ptr addrspace(1) @var, align 4
+  ret void
+}



More information about the llvm-commits mailing list