[llvm-branch-commits] [llvm] [SPIRV] Move `SPIRVTypeInst` to its own header (PR #181668)
Juan Manuel Martinez CaamaƱo via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Feb 16 06:05:48 PST 2026
https://github.com/jmmartinez updated https://github.com/llvm/llvm-project/pull/181668
>From af0f4abafcdead4a73fa84defa4aaa36c76b9b4b 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 14:40:16 +0100
Subject: [PATCH] [SPIRV] Move `SPIRVTypeInst` to its own header and fix its
DenseMapInfo implementaiton
---
llvm/lib/Target/SPIRV/CMakeLists.txt | 1 +
llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h | 66 +-----------------
llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp | 26 +++++++
llvm/lib/Target/SPIRV/SPIRVTypeInst.h | 77 +++++++++++++++++++++
llvm/lib/Target/SPIRV/SPIRVUtils.h | 2 +
5 files changed, 107 insertions(+), 65 deletions(-)
create mode 100644 llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp
create mode 100644 llvm/lib/Target/SPIRV/SPIRVTypeInst.h
diff --git a/llvm/lib/Target/SPIRV/CMakeLists.txt b/llvm/lib/Target/SPIRV/CMakeLists.txt
index ec44e0a9f4876..58989237ad3ea 100644
--- a/llvm/lib/Target/SPIRV/CMakeLists.txt
+++ b/llvm/lib/Target/SPIRV/CMakeLists.txt
@@ -49,6 +49,7 @@ add_llvm_target(SPIRVCodeGen
SPIRVSubtarget.cpp
SPIRVTargetMachine.cpp
SPIRVTargetTransformInfo.cpp
+ SPIRVTypeInst.cpp
SPIRVUtils.cpp
SPIRVEmitNonSemanticDI.cpp
SPIRVCBufferAccess.cpp
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
index 54f5ce3ed1127..cbc8ad0cd04c1 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
@@ -19,80 +19,16 @@
#include "MCTargetDesc/SPIRVBaseInfo.h"
#include "SPIRVIRMapping.h"
#include "SPIRVInstrInfo.h"
+#include "SPIRVTypeInst.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/TypedPointerType.h"
namespace llvm {
class SPIRVSubtarget;
-/// @deprecated Use SPIRVTypeInst instead
-/// SPIRVType is supposed to represent a MachineInstr that defines a SPIRV Type
-/// (e.g. an OpTypeInt intruction). It is misused in several places and we're
-/// getting rid of it.
-using SPIRVType = const MachineInstr;
using StructOffsetDecorator = std::function<void(Register)>;
-class SPIRVTypeInst {
- const MachineInstr *MI;
-
- static bool definesATypeRegister(const MachineInstr &MI) {
- const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
- return MRI.getRegClass(MI.getOperand(0).getReg()) == &SPIRV::TYPERegClass;
- }
-
- // Used by DenseMapInfo to bypass the assertion. The thombstone and empty keys
- // are not null. They are -1 and -2 aligned to the appropiate pointer size.
- struct BypassAssertion {};
- SPIRVTypeInst(const MachineInstr *MI, BypassAssertion) : MI(MI) {};
-
-public:
- SPIRVTypeInst(const MachineInstr &MI) : SPIRVTypeInst(&MI) {}
- SPIRVTypeInst(const MachineInstr *MI) : MI(MI) {
- // A SPIRV Type whose result is not a type is invalid.
- assert(!MI || definesATypeRegister(*MI));
- }
-
- // No need to verify the register since it's already verified by the copied
- // object.
- SPIRVTypeInst(const SPIRVTypeInst &Other) : MI(Other.MI) {}
-
- SPIRVTypeInst &operator=(const SPIRVTypeInst &Other) {
- MI = Other.MI;
- return *this;
- }
-
- 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; }
-
- bool operator==(const MachineInstr *Other) const { return MI == Other; }
- bool operator!=(const MachineInstr *Other) const { return MI != Other; }
-
- operator bool() const { return MI; }
-
- friend struct DenseMapInfo<SPIRVTypeInst>;
-};
-
-template <> struct DenseMapInfo<SPIRVTypeInst> {
- using MIInfo = DenseMapInfo<MachineInstr *>;
- static SPIRVTypeInst getEmptyKey() {
- return {MIInfo::getEmptyKey(), SPIRVTypeInst::BypassAssertion()};
- }
- static SPIRVTypeInst getTombstoneKey() {
- return {MIInfo::getTombstoneKey(), SPIRVTypeInst::BypassAssertion()};
- }
- static unsigned getHashValue(SPIRVTypeInst Ty) {
- return MIInfo::getHashValue(Ty.MI);
- }
- static bool isEqual(SPIRVTypeInst Ty1, SPIRVTypeInst Ty2) {
- return Ty1 == Ty2;
- }
-};
-
class SPIRVGlobalRegistry : public SPIRVIRMapping {
// Registers holding values which have types associated with them.
// Initialized upon VReg definition in IRTranslator.
diff --git a/llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp b/llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp
new file mode 100644
index 0000000000000..e7c746ae1cefd
--- /dev/null
+++ b/llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp
@@ -0,0 +1,26 @@
+//===-- SPIRVTypeInst.cpp - SPIR-V Type Instruction -------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Implementation associated to SPIRVTypeInst.h.
+//
+//===----------------------------------------------------------------------===//
+
+#include "SPIRVTypeInst.h"
+#include "SPIRVInstrInfo.h"
+
+namespace llvm {
+static bool definesATypeRegister(const MachineInstr &MI) {
+ const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
+ return MRI.getRegClass(MI.getOperand(0).getReg()) == &SPIRV::TYPERegClass;
+}
+
+SPIRVTypeInst::SPIRVTypeInst(const MachineInstr *MI) : MI(MI) {
+ // A SPIRV Type whose result is not a type is invalid.
+ assert(!MI || definesATypeRegister(*MI));
+}
+} // namespace llvm
diff --git a/llvm/lib/Target/SPIRV/SPIRVTypeInst.h b/llvm/lib/Target/SPIRV/SPIRVTypeInst.h
new file mode 100644
index 0000000000000..fdb6adda0c633
--- /dev/null
+++ b/llvm/lib/Target/SPIRV/SPIRVTypeInst.h
@@ -0,0 +1,77 @@
+//===-- SPIRVTypeInst.h - SPIR-V Type Instruction ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// SPIRVTypeInst is used to represent a SPIR-V type instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVTYPEINST_H
+#define LLVM_LIB_TARGET_SPIRV_SPIRVTYPEINST_H
+
+#include "llvm/ADT/DenseMapInfo.h"
+
+namespace llvm {
+class MachineInstr;
+class MachineRegisterInfo;
+
+/// @deprecated Use SPIRVTypeInst instead
+/// SPIRVType is supposed to represent a MachineInstr that defines a SPIRV Type
+/// (e.g. an OpTypeInt intruction). It is misused in several places and we're
+/// getting rid of it.
+using SPIRVType = const MachineInstr;
+
+class SPIRVTypeInst {
+ const MachineInstr *MI;
+
+ // Used by DenseMapInfo to bypass the assertion. The thombstone and empty keys
+ // are not null. They are -1 and -2 aligned to the appropiate pointer size.
+ struct BypassAssertion {};
+ SPIRVTypeInst(const MachineInstr *MI, BypassAssertion) : MI(MI) {};
+
+public:
+ SPIRVTypeInst(const MachineInstr &MI) : SPIRVTypeInst(&MI) {}
+ SPIRVTypeInst(const MachineInstr *MI);
+
+ // No need to verify the register since it's already verified by the copied
+ // object.
+ SPIRVTypeInst(const SPIRVTypeInst &Other) = default;
+ SPIRVTypeInst &operator=(const SPIRVTypeInst &Other) = default;
+
+ 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; }
+
+ bool operator==(const MachineInstr *Other) const { return MI == Other; }
+ bool operator!=(const MachineInstr *Other) const { return MI != Other; }
+
+ operator bool() const { return MI; }
+
+ friend struct DenseMapInfo<SPIRVTypeInst>;
+};
+
+template <> struct DenseMapInfo<SPIRVTypeInst> {
+ using MIInfo = DenseMapInfo<MachineInstr *>;
+ static SPIRVTypeInst getEmptyKey() {
+ return {MIInfo::getEmptyKey(), SPIRVTypeInst::BypassAssertion()};
+ }
+ static SPIRVTypeInst getTombstoneKey() {
+ return {MIInfo::getTombstoneKey(), SPIRVTypeInst::BypassAssertion()};
+ }
+ static unsigned getHashValue(SPIRVTypeInst Ty) {
+ return MIInfo::getHashValue(Ty.MI);
+ }
+ static bool isEqual(SPIRVTypeInst Ty1, SPIRVTypeInst Ty2) {
+ return Ty1 == Ty2;
+ }
+};
+
+} // namespace llvm
+#endif
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h
index 50042c90a86d3..c7ab8b70bc723 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.h
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h
@@ -25,6 +25,8 @@
#include <unordered_map>
#include <unordered_set>
+#include "SPIRVTypeInst.h"
+
namespace llvm {
class MCInst;
class MachineFunction;
More information about the llvm-branch-commits
mailing list