[llvm] 2990f95 - [SPIR-V] Deduplicate createVariable and createExitVariable helpers into SPIRVUtils (#202512)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 9 01:29:31 PDT 2026


Author: Arseniy Obolenskiy
Date: 2026-06-09T10:29:27+02:00
New Revision: 2990f95ae247808e5c0f413d9e91658302ea5b6f

URL: https://github.com/llvm/llvm-project/commit/2990f95ae247808e5c0f413d9e91658302ea5b6f
DIFF: https://github.com/llvm/llvm-project/commit/2990f95ae247808e5c0f413d9e91658302ea5b6f.diff

LOG: [SPIR-V] Deduplicate createVariable and createExitVariable helpers into SPIRVUtils (#202512)

Added: 
    

Modified: 
    llvm/lib/Target/SPIRV/SPIRVMergeRegionExitTargets.cpp
    llvm/lib/Target/SPIRV/SPIRVStructurizer.cpp
    llvm/lib/Target/SPIRV/SPIRVUtils.cpp
    llvm/lib/Target/SPIRV/SPIRVUtils.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/SPIRV/SPIRVMergeRegionExitTargets.cpp b/llvm/lib/Target/SPIRV/SPIRVMergeRegionExitTargets.cpp
index 959e7eb1e9e04..3d64758ff8586 100644
--- a/llvm/lib/Target/SPIRV/SPIRVMergeRegionExitTargets.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVMergeRegionExitTargets.cpp
@@ -32,40 +32,6 @@ using namespace llvm;
 
 namespace {
 
-/// Create a value in BB set to the value associated with the branch the block
-/// terminator will take.
-static llvm::Value *
-createExitVariable(BasicBlock *BB,
-                   const DenseMap<BasicBlock *, ConstantInt *> &TargetToValue) {
-  auto *T = BB->getTerminator();
-  if (isa<ReturnInst>(T))
-    return nullptr;
-  if (auto *BI = dyn_cast<UncondBrInst>(T))
-    return TargetToValue.lookup(BI->getSuccessor());
-
-  IRBuilder<> Builder(BB);
-  Builder.SetInsertPoint(T);
-
-  if (auto *BI = dyn_cast<CondBrInst>(T)) {
-    Value *LHS = TargetToValue.lookup(BI->getSuccessor(0));
-    Value *RHS = TargetToValue.lookup(BI->getSuccessor(1));
-
-    if (LHS == nullptr || RHS == nullptr)
-      return LHS == nullptr ? RHS : LHS;
-    return Builder.CreateSelect(BI->getCondition(), LHS, RHS);
-  }
-
-  // TODO: add support for switch cases.
-  llvm_unreachable("Unhandled terminator type.");
-}
-
-static AllocaInst *createVariable(Function &F, Type *Type,
-                                  BasicBlock::iterator Position) {
-  const DataLayout &DL = F.getDataLayout();
-  return new AllocaInst(Type, DL.getAllocaAddrSpace(), nullptr, "reg",
-                        Position);
-}
-
 // Run the pass on the given convergence region, ignoring the sub-regions.
 // Returns true if the CFG changed, false otherwise.
 static bool runOnConvergenceRegionNoRecurse(LoopInfo &LI,
@@ -88,8 +54,7 @@ static bool runOnConvergenceRegionNoRecurse(LoopInfo &LI,
   auto NewExitTarget = BasicBlock::Create(F->getContext(), "new.exit", F);
   IRBuilder<> Builder(NewExitTarget);
 
-  AllocaInst *Variable = createVariable(*F, Builder.getInt32Ty(),
-                                        F->begin()->getFirstInsertionPt());
+  AllocaInst *Variable = createVariable(*F, Builder.getInt32Ty());
 
   // CodeGen output needs to be stable. Using the set as-is would order
   // the targets 
diff erently depending on the allocation pattern.

diff  --git a/llvm/lib/Target/SPIRV/SPIRVStructurizer.cpp b/llvm/lib/Target/SPIRV/SPIRVStructurizer.cpp
index d2befa50789fa..682fceadb5a6e 100644
--- a/llvm/lib/Target/SPIRV/SPIRVStructurizer.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVStructurizer.cpp
@@ -450,13 +450,6 @@ class SPIRVStructurizer : public FunctionPass {
       return Output;
     }
 
-    AllocaInst *CreateVariable(Function &F, Type *Type,
-                               BasicBlock::iterator Position) {
-      const DataLayout &DL = F.getDataLayout();
-      return new AllocaInst(Type, DL.getAllocaAddrSpace(), nullptr, "reg",
-                            Position);
-    }
-
     // Given a construct defined by |Header|, and a list of exiting edges
     // |Edges|, creates a new single exit node, fixing up those edges.
     BasicBlock *createSingleExitNode(BasicBlock *Header,
@@ -484,8 +477,7 @@ class SPIRVStructurizer : public FunctionPass {
         return NewExit;
       }
 
-      AllocaInst *Variable = CreateVariable(F, ExitBuilder.getInt32Ty(),
-                                            F.begin()->getFirstInsertionPt());
+      AllocaInst *Variable = createVariable(F, ExitBuilder.getInt32Ty());
       for (auto &[Src, Dst] : FixedEdges) {
         IRBuilder<> B2(Src);
         B2.SetInsertPoint(Src->getFirstInsertionPt());
@@ -512,33 +504,6 @@ class SPIRVStructurizer : public FunctionPass {
     }
   };
 
-  /// Create a value in BB set to the value associated with the branch the block
-  /// terminator will take.
-  Value *createExitVariable(
-      BasicBlock *BB,
-      const DenseMap<BasicBlock *, ConstantInt *> &TargetToValue) {
-    auto *T = BB->getTerminator();
-    if (isa<ReturnInst>(T))
-      return nullptr;
-    if (auto *BI = dyn_cast<UncondBrInst>(T))
-      return TargetToValue.lookup(BI->getSuccessor());
-
-    IRBuilder<> Builder(BB);
-    Builder.SetInsertPoint(T);
-
-    if (auto *BI = dyn_cast<CondBrInst>(T)) {
-      Value *LHS = TargetToValue.lookup(BI->getSuccessor(0));
-      Value *RHS = TargetToValue.lookup(BI->getSuccessor(1));
-
-      if (LHS == nullptr || RHS == nullptr)
-        return LHS == nullptr ? RHS : LHS;
-      return Builder.CreateSelect(BI->getCondition(), LHS, RHS);
-    }
-
-    // TODO: add support for switch cases.
-    llvm_unreachable("Unhandled terminator type.");
-  }
-
   // Creates a new basic block in F with a single OpUnreachable instruction.
   BasicBlock *CreateUnreachable(Function &F) {
     BasicBlock *BB = BasicBlock::Create(F.getContext(), "unreachable", &F);

diff  --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index 6c5619c4585ef..2b48986826b6a 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -883,6 +883,37 @@ bool sortBlocks(Function &F) {
   return Modified;
 }
 
+AllocaInst *createVariable(Function &F, Type *Type) {
+  const DataLayout &DL = F.getDataLayout();
+  return new AllocaInst(Type, DL.getAllocaAddrSpace(), nullptr, "reg",
+                        F.begin()->getFirstInsertionPt());
+}
+
+Value *
+createExitVariable(BasicBlock *BB,
+                   const DenseMap<BasicBlock *, ConstantInt *> &TargetToValue) {
+  auto *T = BB->getTerminator();
+  if (isa<ReturnInst>(T))
+    return nullptr;
+  if (auto *BI = dyn_cast<UncondBrInst>(T))
+    return TargetToValue.lookup(BI->getSuccessor());
+
+  IRBuilder<> Builder(BB);
+  Builder.SetInsertPoint(T);
+
+  if (auto *BI = dyn_cast<CondBrInst>(T)) {
+    Value *LHS = TargetToValue.lookup(BI->getSuccessor(0));
+    Value *RHS = TargetToValue.lookup(BI->getSuccessor(1));
+
+    if (LHS == nullptr || RHS == nullptr)
+      return LHS == nullptr ? RHS : LHS;
+    return Builder.CreateSelect(BI->getCondition(), LHS, RHS);
+  }
+
+  // TODO: add support for switch cases.
+  llvm_unreachable("Unhandled terminator type.");
+}
+
 MachineInstr *getVRegDef(MachineRegisterInfo &MRI, Register Reg) {
   MachineInstr *MaybeDef = MRI.getVRegDef(Reg);
   if (MaybeDef && MaybeDef->getOpcode() == SPIRV::ASSIGN_TYPE)

diff  --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h
index 83cbc4e451ad5..dd74d4c7476e5 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.h
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h
@@ -334,6 +334,15 @@ Type *parseBasicTypeName(StringRef &TypeName, LLVMContext &Ctx);
 // Returns true if the function was changed.
 bool sortBlocks(Function &F);
 
+// Create a stack slot in the entry block of F for a value of the given type.
+AllocaInst *createVariable(Function &F, Type *Type);
+
+// Create a value in BB set to the value associated with the branch the block
+// terminator will take.
+Value *
+createExitVariable(BasicBlock *BB,
+                   const DenseMap<BasicBlock *, ConstantInt *> &TargetToValue);
+
 // Check for peeled array structs and recursively reconstitute them. In HLSL
 // CBuffers, arrays may have padding between the elements, but not after the
 // last element. To represent this in LLVM IR an array [N x T] will be


        


More information about the llvm-commits mailing list