[llvm] [SPIRV] Add a `SPIRVTypeInst` type with some guardrails (PR #179947)
Juan Manuel Martinez CaamaƱo via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 10 03:08:52 PST 2026
https://github.com/jmmartinez updated https://github.com/llvm/llvm-project/pull/179947
>From eb97fb62fdf29314fff47e3cfbd021cc95c486a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Thu, 5 Feb 2026 14:09:52 +0100
Subject: [PATCH 1/3] [SPIRV] Add a `SPIRVTypeInst` type with some guardrails
Currently `SPIRVType` is an alias of `MachineInstr`:
```cpp
using SPIRVType = const MachineInstr;
```
Consider the function below from the backend:
```cpp
inline Register getTypeReg(MachineRegisterInfo *MRI, Register OpReg) {
SPIRVType *TypeInst = MRI->getVRegDef(OpReg);
return TypeInst && TypeInst->getOpcode() ==
SPIRV::OpFunctionParameter
? TypeInst->getOperand(1).getReg()
: OpReg;
}
```
How can `TypeInst` be an `OpFunctionParameter` which returns a value?
In all our tests `OpReg` is a value register. Why do we return the type
operand for `OpFunctionParamter` and something else anything else?
This code doesn't make any sense.
To avoid continuing down this path, this patch proposes `SPIRVTypeInst`
which is a wrapper around `MachineInstr` with an extra `assert`.
The idea is to replace the use of `SPIRVType` little-by-little.
---
llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h | 23 +++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
index fa45f169f8561..a9b0abb385ec8 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
@@ -28,6 +28,29 @@ class SPIRVSubtarget;
using SPIRVType = const MachineInstr;
using StructOffsetDecorator = std::function<void(Register)>;
+class SPIRVTypeInst {
+ const MachineInstr *MI;
+
+public:
+ static bool definesATypeRegister(const MachineInstr *MI) {
+ const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
+ return MRI.getRegClass(MI->getOperand(0).getReg()) == &SPIRV::TYPERegClass;
+ }
+
+ SPIRVTypeInst(const MachineInstr &MI) : SPIRVTypeInst(&MI) {}
+ SPIRVTypeInst(const MachineInstr *MI) : MI(MI) {
+ // A SPIRV Type whose result is not a type is invalid.
+ assert(definesATypeRegister(MI));
+ }
+
+ const MachineInstr &operator*() const { return *MI; }
+ const MachineInstr *operator->() const { return MI; }
+ operator const MachineInstr *() const { return MI; }
+
+ bool operator==(const SPIRVTypeInst &Other) const { return MI == Other.MI; }
+ bool operator!=(const SPIRVTypeInst &Other) const { return MI != Other.MI; }
+};
+
class SPIRVGlobalRegistry : public SPIRVIRMapping {
// Registers holding values which have types associated with them.
// Initialized upon VReg definition in IRTranslator.
>From ac0c7f21ff411475ce8b925b13baab5d01a2fdfc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Thu, 5 Feb 2026 14:37:34 +0100
Subject: [PATCH 2/3] [SPIRV][NFC] Replace `SPIRVType` with `SPIRVTypeInst`
---
llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp
index 3e5ce4b90ea4a..778c6dbd92b94 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp
@@ -30,7 +30,8 @@ SPIRVTargetLowering::SPIRVTargetLowering(const TargetMachine &TM,
// Returns true of the types logically match, as defined in
// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpCopyLogical.
-static bool typesLogicallyMatch(const SPIRVType *Ty1, const SPIRVType *Ty2,
+static bool typesLogicallyMatch(const SPIRVTypeInst Ty1,
+ const SPIRVTypeInst Ty2,
SPIRVGlobalRegistry &GR) {
if (Ty1->getOpcode() != Ty2->getOpcode())
return false;
@@ -43,17 +44,19 @@ static bool typesLogicallyMatch(const SPIRVType *Ty1, const SPIRVType *Ty2,
if (Ty1->getOperand(2).getReg() != Ty2->getOperand(2).getReg())
return false;
- SPIRVType *ElemType1 = GR.getSPIRVTypeForVReg(Ty1->getOperand(1).getReg());
- SPIRVType *ElemType2 = GR.getSPIRVTypeForVReg(Ty2->getOperand(1).getReg());
+ SPIRVTypeInst ElemType1 =
+ GR.getSPIRVTypeForVReg(Ty1->getOperand(1).getReg());
+ SPIRVTypeInst ElemType2 =
+ GR.getSPIRVTypeForVReg(Ty2->getOperand(1).getReg());
return ElemType1 == ElemType2 ||
typesLogicallyMatch(ElemType1, ElemType2, GR);
}
if (Ty1->getOpcode() == SPIRV::OpTypeStruct) {
for (unsigned I = 1; I < Ty1->getNumOperands(); I++) {
- SPIRVType *ElemType1 =
+ SPIRVTypeInst ElemType1 =
GR.getSPIRVTypeForVReg(Ty1->getOperand(I).getReg());
- SPIRVType *ElemType2 =
+ SPIRVTypeInst ElemType2 =
GR.getSPIRVTypeForVReg(Ty2->getOperand(I).getReg());
if (ElemType1 != ElemType2 &&
!typesLogicallyMatch(ElemType1, ElemType2, GR))
>From 57fcf5573ad409f241f7660582ee339857e4a98a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Mon, 9 Feb 2026 13:41:35 +0100
Subject: [PATCH 3/3] [SPIRV] Deprecate SPIRVType
---
llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
index a9b0abb385ec8..d945ea53f3b6a 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
@@ -25,7 +25,7 @@
namespace llvm {
class SPIRVSubtarget;
-using SPIRVType = const MachineInstr;
+using SPIRVType [[deprecated]] = const MachineInstr;
using StructOffsetDecorator = std::function<void(Register)>;
class SPIRVTypeInst {
More information about the llvm-commits
mailing list