[llvm] generate a name of an unnamed global variable for Instruction Selection (PR #78293)

Vyacheslav Levytskyy via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 12:28:57 PST 2024


https://github.com/VyacheslavLevytskyy updated https://github.com/llvm/llvm-project/pull/78293

>From b27b2a3db5aeaafdd58db1de8953be863d9c02af Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Tue, 16 Jan 2024 06:31:18 -0800
Subject: [PATCH 1/2] generate a name of a global variable for the Instruction
 Select pass

---
 .../Target/SPIRV/SPIRVInstructionSelector.cpp    | 16 +++++++++++++++-
 llvm/test/CodeGen/SPIRV/unnamed_global.ll        | 15 +++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/unnamed_global.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index 5ad47de4fc5414..8c1dfc5e626dbe 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -53,6 +53,10 @@ class SPIRVInstructionSelector : public InstructionSelector {
   SPIRVGlobalRegistry &GR;
   MachineRegisterInfo *MRI;
 
+  /// We need to keep track of the number we give to anonymous global values to
+  /// generate the same name every time when this is needed.
+  mutable DenseMap<const GlobalValue *, unsigned> UnnamedGlobalIDs;
+
 public:
   SPIRVInstructionSelector(const SPIRVTargetMachine &TM,
                            const SPIRVSubtarget &ST,
@@ -1519,7 +1523,17 @@ bool SPIRVInstructionSelector::selectGlobalValue(
   SPIRVType *ResType = GR.getOrCreateSPIRVPointerType(
       PointerBaseType, I, TII,
       addressSpaceToStorageClass(GV->getAddressSpace()));
-  std::string GlobalIdent = GV->getGlobalIdentifier();
+
+  std::string GlobalIdent;
+  if (!GV->hasName()) {
+    unsigned &ID = UnnamedGlobalIDs[GV];
+    if (ID == 0)
+      ID = UnnamedGlobalIDs.size();
+    GlobalIdent = "__unnamed_" + Twine(ID).str();
+  } else {
+    GlobalIdent = GV->getGlobalIdentifier();
+  }
+
   // We have functions as operands in tests with blocks of instruction e.g. in
   // transcoding/global_block.ll. These operands are not used and should be
   // substituted by zero constants. Their type is expected to be always
diff --git a/llvm/test/CodeGen/SPIRV/unnamed_global.ll b/llvm/test/CodeGen/SPIRV/unnamed_global.ll
new file mode 100644
index 00000000000000..26a1314e23da2d
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/unnamed_global.ll
@@ -0,0 +1,15 @@
+; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
+
+; CHECK: %[[TyInt:.*]] = OpTypeInt 8 0
+; CHECK: %[[ConstInt:.*]] = OpConstant %[[TyInt]] 123
+; CHECK: %[[TyPtr:.*]] = OpTypePointer {{[a-zA-Z]+}} %[[TyInt]]
+; CHECK: %[[VarId:.*]] = OpVariable %[[TyPtr]] {{[a-zA-Z]+}} %[[ConstInt]]
+
+ at 0 = addrspace(1) global i8 123
+
+; Function Attrs: nounwind
+define spir_kernel void @foo() #0 {
+  ret void
+}
+
+attributes #0 = { nounwind }

>From 0dcd68c8ce15a69dbe86cb157b2650e3a5ee1178 Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Thu, 25 Jan 2024 12:28:40 -0800
Subject: [PATCH 2/2] change the name of the test to use dashes instead of
 underscores

---
 llvm/test/CodeGen/SPIRV/{unnamed_global.ll => unnamed-global.ll} | 1 +
 1 file changed, 1 insertion(+)
 rename llvm/test/CodeGen/SPIRV/{unnamed_global.ll => unnamed-global.ll} (80%)

diff --git a/llvm/test/CodeGen/SPIRV/unnamed_global.ll b/llvm/test/CodeGen/SPIRV/unnamed-global.ll
similarity index 80%
rename from llvm/test/CodeGen/SPIRV/unnamed_global.ll
rename to llvm/test/CodeGen/SPIRV/unnamed-global.ll
index 26a1314e23da2d..d2cd4ea8cafc36 100644
--- a/llvm/test/CodeGen/SPIRV/unnamed_global.ll
+++ b/llvm/test/CodeGen/SPIRV/unnamed-global.ll
@@ -1,4 +1,5 @@
 ; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
+; TODO: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
 
 ; CHECK: %[[TyInt:.*]] = OpTypeInt 8 0
 ; CHECK: %[[ConstInt:.*]] = OpConstant %[[TyInt]] 123



More information about the llvm-commits mailing list