[llvm] [SPIRV] Fix `SPIRVTypeInst` `DenseMapInfo` implementation (PR #181667)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 16 06:02:47 PST 2026


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

The previous `DenseMapInfo` implementation was wrong.

The new implementation relies completely on `DenseMapInfo<MachineInst*>`'s. To do this, we use "tag dispatch" to call a special constructor only accessible by `DenseMapInfo` that bypasses the assertion when building the emtpy/tombstone keys.

>From a5bfff75b8f6bb47dccea8a7208a81adbcc2d319 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:58:36 +0100
Subject: [PATCH] [SPIRV] Fix `SPIRVTypeInst` `DenseMapInfo` implementation

The previous `DenseMapInfo` implementation was wrong.

The new implementation relies completely on `DenseMapInfo<MachineInst*>`'s.
To do this, we use "tag dispatch" to call a special constructor only
accessible by `DenseMapInfo` that bypasses the assertion when building
the emtpy/tombstone keys.
---
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h | 24 +++++++++++++++------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
index 0dd0bbaf36bc0..54f5ce3ed1127 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
@@ -36,12 +36,17 @@ 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;
   }
 
+  // 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.
@@ -69,15 +74,20 @@ class SPIRVTypeInst {
 
   operator bool() const { return MI; }
 
-  unsigned getHashValue() const {
-    return DenseMapInfo<const MachineInstr *>::getHashValue(MI);
-  }
+  friend struct DenseMapInfo<SPIRVTypeInst>;
 };
 
 template <> struct DenseMapInfo<SPIRVTypeInst> {
-  static SPIRVTypeInst getEmptyKey() { return SPIRVTypeInst(nullptr); }
-  static SPIRVTypeInst getTombstoneKey() { return SPIRVTypeInst(nullptr); }
-  static unsigned getHashValue(SPIRVTypeInst Ty) { return Ty.getHashValue(); }
+  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;
   }



More information about the llvm-commits mailing list