[llvm] [SPIR-V] Emit builtin variable OpVariable into entry block (PR #189958)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 1 09:18:19 PDT 2026


================
@@ -518,11 +518,17 @@ static Register buildBuiltinVariableLoad(
       VariableType, MIRBuilder, SPIRV::StorageClass::Input);
   GR->assignSPIRVTypeToVReg(PtrType, NewRegister, MIRBuilder.getMF());
 
-  // Set up the global OpVariable with the necessary builtin decorations.
+  // Emit the OpVariable and its decorations into the first MBB to ensure
+  // VReg definition dependencies are valid across all MBBs.
+  MachineBasicBlock &EntryBB = MIRBuilder.getMF().front();
+  MachineIRBuilder FirstBlockBuilder;
+  FirstBlockBuilder.setMF(MIRBuilder.getMF());
+  FirstBlockBuilder.setMBB(EntryBB);
----------------
jmmartinez wrote:

This is setting the insertion point at the end of the entry block. 

I'm not super familiar with this code path; but is it possible that the `MIRBuilder` insertion point is also in the entry block? So we would get uses of the global before its definition.

One alternative in that case could be to:
```cpp
MachineFunciton *MF = MIRBuilder.getMF();
MachineBasicBlock &EntryBB = MF.front();
MachineIRBuilder GVBuilder(MIRBuilder.getState()); 
if(GVBuilder.getMBB() != &EntryBB)
  GVBuilder.setMBB(EntryBB.getFirstTerminator()); // I'm surprised it didn't complain about the block not ending with a terminator. But maybe it's relaxed in MachineIR.
```

https://github.com/llvm/llvm-project/pull/189958


More information about the llvm-commits mailing list