[llvm] [SPIRV] Stop using `createOpType` in `SPIRVGlobalRegistry::getOrCreateUndef` (PR #181726)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 16 11:19:28 PST 2026


https://github.com/jmmartinez created https://github.com/llvm/llvm-project/pull/181726

I'm starting to replace the easy uses for `createOpType` that are clearly wrong.

`createOpType` swaps the insertion point of the `MIRBuilder` that is passed as a parameter, such that the type/constant instructions are inserted at the entry block.

I took the simplest approach and set the insertion point in the beginning of the entry-block.

In this case we did not see any change; but for other more widely used constants we could see some reordering of the instructions.

>From a4d3a35ed27b21b00aa4c40a6249fe82b7b539e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
 <jmartinezcaamao at gmail.com>
Date: Mon, 16 Feb 2026 20:11:30 +0100
Subject: [PATCH] [SPIRV] Stop using createOpType in
 SPIRVGlobalRegistry::getOrCreateUndef

---
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 24 +++++++------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index fb44305cdfae6..1db4c09688bd9 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -2014,22 +2014,14 @@ Register SPIRVGlobalRegistry::getOrCreateUndef(MachineInstr &I,
   CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass);
   assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
 
-  MachineInstr *DepMI =
-      const_cast<MachineInstr *>(static_cast<const MachineInstr *>(SpvType));
-  MachineIRBuilder MIRBuilder(*DepMI->getParent(), DepMI->getIterator());
-  const MachineInstr *NewMI =
-      createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
-        auto MIB = BuildMI(MIRBuilder.getMBB(), *MIRBuilder.getInsertPt(),
-                           MIRBuilder.getDL(), TII.get(SPIRV::OpUndef))
-                       .addDef(Res)
-                       .addUse(getSPIRVTypeID(SpvType));
-        const auto &ST = CurMF->getSubtarget();
-        constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
-                                         *ST.getRegisterInfo(),
-                                         *ST.getRegBankInfo());
-        return MIB;
-      });
-  add(UV, NewMI);
+  MachineIRBuilder MIRBuilder(CurMF->front(), CurMF->front().begin());
+  auto MIB = MIRBuilder.buildInstr(SPIRV::OpUndef)
+                 .addDef(Res)
+                 .addUse(getSPIRVTypeID(SpvType));
+  const auto &ST = CurMF->getSubtarget();
+  MIB.constrainAllUses(*ST.getInstrInfo(), *ST.getRegisterInfo(),
+                       *ST.getRegBankInfo());
+  add(UV, MIB.getInstr());
   return Res;
 }
 



More information about the llvm-commits mailing list