[llvm] [SPIRV] Add FPVariant tracking for floating-point registers in SPIR-V (PR #156871)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 8 05:38:40 PDT 2025


https://github.com/YixingZhang007 updated https://github.com/llvm/llvm-project/pull/156871

>From f44dccefc0dc98d43b583218def7e1880212e85a Mon Sep 17 00:00:00 2001
From: "Zhang, Yixing" <yixing.zhang at intel.com>
Date: Thu, 4 Sep 2025 05:14:39 -0700
Subject: [PATCH 1/4] add the support for bfloat in SPIRV

---
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 26 ++++++++++++++++++-
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h   | 13 ++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index cfe24c84941a9..0f258e03b23c8 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -1122,7 +1122,19 @@ SPIRVType *SPIRVGlobalRegistry::restOfCreateSPIRVType(
   SPIRVType *SpirvType = createSPIRVType(Ty, MIRBuilder, AccessQual,
                                          ExplicitLayoutRequired, EmitIR);
   TypesInProcessing.erase(Ty);
-  VRegToTypeMap[&MIRBuilder.getMF()][getSPIRVTypeID(SpirvType)] = SpirvType;
+
+  // Record the FPVariant of the floating-point registers in the
+  // VRegFPVariantMap.
+  MachineFunction *MF = &MIRBuilder.getMF();
+  Register TypeReg = getSPIRVTypeID(SpirvType);
+  if (Ty->isFloatingPointTy()) {
+    if (Ty->isBFloatTy()) {
+      VRegFPVariantMap[MF][TypeReg] = FPVariant::BRAIN_FLOAT;
+    } else {
+      VRegFPVariantMap[MF][TypeReg] = FPVariant::IEEE_FLOAT;
+    }
+  }
+  VRegToTypeMap[MF][TypeReg] = SpirvType;
 
   // TODO: We could end up with two SPIR-V types pointing to the same llvm type.
   // Is that a problem?
@@ -2088,3 +2100,15 @@ bool SPIRVGlobalRegistry::hasBlockDecoration(SPIRVType *Type) const {
   }
   return false;
 }
+
+SPIRVGlobalRegistry::FPVariant
+SPIRVGlobalRegistry::getFPVariantForVReg(Register VReg,
+                                         const MachineFunction *MF) {
+  auto t = VRegFPVariantMap.find(MF ? MF : CurMF);
+  if (t != VRegFPVariantMap.end()) {
+    auto tt = t->second.find(VReg);
+    if (tt != t->second.end())
+      return tt->second;
+  }
+  return FPVariant::NONE;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
index 7ef812828b7cc..1f8c30dc01f7f 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
@@ -29,6 +29,10 @@ using SPIRVType = const MachineInstr;
 using StructOffsetDecorator = std::function<void(Register)>;
 
 class SPIRVGlobalRegistry : public SPIRVIRMapping {
+public:
+  enum class FPVariant { NONE, IEEE_FLOAT, BRAIN_FLOAT };
+
+private:
   // Registers holding values which have types associated with them.
   // Initialized upon VReg definition in IRTranslator.
   // Do not confuse this with DuplicatesTracker as DT maps Type* to <MF, Reg>
@@ -88,6 +92,11 @@ class SPIRVGlobalRegistry : public SPIRVIRMapping {
   // map of aliasing decorations to aliasing metadata
   std::unordered_map<const MDNode *, MachineInstr *> AliasInstMDMap;
 
+  // Maps floating point Registers to their FPVariant (float type kind), given
+  // the MachineFunction.
+  DenseMap<const MachineFunction *, DenseMap<Register, FPVariant>>
+      VRegFPVariantMap;
+
   // Add a new OpTypeXXX instruction without checking for duplicates.
   SPIRVType *createSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
                              SPIRV::AccessQualifier::AccessQualifier AQ,
@@ -422,6 +431,10 @@ class SPIRVGlobalRegistry : public SPIRVIRMapping {
   // structures referring this instruction.
   void invalidateMachineInstr(MachineInstr *MI);
 
+  // Return the FPVariant of to the given floating-point regiester.
+  FPVariant getFPVariantForVReg(Register VReg,
+                                const MachineFunction *MF = nullptr);
+
 private:
   SPIRVType *getOpTypeBool(MachineIRBuilder &MIRBuilder);
 

>From a67050e7d47c853c5721e08a7990eef34c4e25f2 Mon Sep 17 00:00:00 2001
From: "Zhang, Yixing" <yixing.zhang at intel.com>
Date: Thu, 4 Sep 2025 05:56:56 -0700
Subject: [PATCH 2/4] revert all the change

---
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 49 +++++++++++--------
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h   | 14 +++---
 2 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index 0f258e03b23c8..adf6c64357a7e 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -1122,19 +1122,20 @@ SPIRVType *SPIRVGlobalRegistry::restOfCreateSPIRVType(
   SPIRVType *SpirvType = createSPIRVType(Ty, MIRBuilder, AccessQual,
                                          ExplicitLayoutRequired, EmitIR);
   TypesInProcessing.erase(Ty);
+  VRegToTypeMap[&MIRBuilder.getMF()][getSPIRVTypeID(SpirvType)] = SpirvType;
 
   // Record the FPVariant of the floating-point registers in the
   // VRegFPVariantMap.
-  MachineFunction *MF = &MIRBuilder.getMF();
-  Register TypeReg = getSPIRVTypeID(SpirvType);
-  if (Ty->isFloatingPointTy()) {
-    if (Ty->isBFloatTy()) {
-      VRegFPVariantMap[MF][TypeReg] = FPVariant::BRAIN_FLOAT;
-    } else {
-      VRegFPVariantMap[MF][TypeReg] = FPVariant::IEEE_FLOAT;
-    }
-  }
-  VRegToTypeMap[MF][TypeReg] = SpirvType;
+  // MachineFunction *MF = &MIRBuilder.getMF();
+  // Register TypeReg = getSPIRVTypeID(SpirvType);
+  // if (Ty->isFloatingPointTy()) {
+  //   if (Ty->isBFloatTy()) {
+  //     VRegFPVariantMap[MF][TypeReg] = FPVariant::BRAIN_FLOAT;
+  //   } else {
+  //     VRegFPVariantMap[MF][TypeReg] = FPVariant::IEEE_FLOAT;
+  //   }
+  // }
+  // VRegToTypeMap[MF][TypeReg] = SpirvType;
 
   // TODO: We could end up with two SPIR-V types pointing to the same llvm type.
   // Is that a problem?
@@ -2101,14 +2102,20 @@ bool SPIRVGlobalRegistry::hasBlockDecoration(SPIRVType *Type) const {
   return false;
 }
 
-SPIRVGlobalRegistry::FPVariant
-SPIRVGlobalRegistry::getFPVariantForVReg(Register VReg,
-                                         const MachineFunction *MF) {
-  auto t = VRegFPVariantMap.find(MF ? MF : CurMF);
-  if (t != VRegFPVariantMap.end()) {
-    auto tt = t->second.find(VReg);
-    if (tt != t->second.end())
-      return tt->second;
-  }
-  return FPVariant::NONE;
-}
\ No newline at end of file
+// SPIRVGlobalRegistry::FPVariant
+// SPIRVGlobalRegistry::getFPVariantForVReg(Register VReg,
+//                                          const MachineFunction *MF) {
+//   const MachineFunction *Func = MF ? MF : CurMF;
+//   DenseMap<const MachineFunction *,
+//            DenseMap<Register, FPVariant>>::const_iterator FuncIt =
+//       VRegFPVariantMap.find(Func);
+
+//   if (FuncIt != VRegFPVariantMap.end()) {
+//     const DenseMap<Register, FPVariant> &VRegMap = FuncIt->second;
+//     DenseMap<Register, FPVariant>::const_iterator VRegIt = VRegMap.find(VReg);
+
+//     if (VRegIt != VRegMap.end())
+//       return VRegIt->second;
+//   }
+//   return FPVariant::NONE;
+// }
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
index 1f8c30dc01f7f..fa397c5410dd8 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
@@ -29,10 +29,10 @@ using SPIRVType = const MachineInstr;
 using StructOffsetDecorator = std::function<void(Register)>;
 
 class SPIRVGlobalRegistry : public SPIRVIRMapping {
-public:
-  enum class FPVariant { NONE, IEEE_FLOAT, BRAIN_FLOAT };
+// public:
+//   enum class FPVariant { NONE, IEEE_FLOAT, BRAIN_FLOAT };
 
-private:
+// private:
   // Registers holding values which have types associated with them.
   // Initialized upon VReg definition in IRTranslator.
   // Do not confuse this with DuplicatesTracker as DT maps Type* to <MF, Reg>
@@ -94,8 +94,8 @@ class SPIRVGlobalRegistry : public SPIRVIRMapping {
 
   // Maps floating point Registers to their FPVariant (float type kind), given
   // the MachineFunction.
-  DenseMap<const MachineFunction *, DenseMap<Register, FPVariant>>
-      VRegFPVariantMap;
+  // DenseMap<const MachineFunction *, DenseMap<Register, FPVariant>>
+  //     VRegFPVariantMap;
 
   // Add a new OpTypeXXX instruction without checking for duplicates.
   SPIRVType *createSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
@@ -432,8 +432,8 @@ class SPIRVGlobalRegistry : public SPIRVIRMapping {
   void invalidateMachineInstr(MachineInstr *MI);
 
   // Return the FPVariant of to the given floating-point regiester.
-  FPVariant getFPVariantForVReg(Register VReg,
-                                const MachineFunction *MF = nullptr);
+  // FPVariant getFPVariantForVReg(Register VReg,
+  //                               const MachineFunction *MF = nullptr);
 
 private:
   SPIRVType *getOpTypeBool(MachineIRBuilder &MIRBuilder);

>From 4e405b569661f34f25f34dd7d78c4737423e05fc Mon Sep 17 00:00:00 2001
From: "Zhang, Yixing" <yixing.zhang at intel.com>
Date: Fri, 5 Sep 2025 13:11:58 -0700
Subject: [PATCH 3/4] add all the changes

---
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 51 +++++++++----------
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h   | 14 ++---
 2 files changed, 32 insertions(+), 33 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index adf6c64357a7e..9ff5408f65d1a 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -1122,20 +1122,19 @@ SPIRVType *SPIRVGlobalRegistry::restOfCreateSPIRVType(
   SPIRVType *SpirvType = createSPIRVType(Ty, MIRBuilder, AccessQual,
                                          ExplicitLayoutRequired, EmitIR);
   TypesInProcessing.erase(Ty);
-  VRegToTypeMap[&MIRBuilder.getMF()][getSPIRVTypeID(SpirvType)] = SpirvType;
 
   // Record the FPVariant of the floating-point registers in the
   // VRegFPVariantMap.
-  // MachineFunction *MF = &MIRBuilder.getMF();
-  // Register TypeReg = getSPIRVTypeID(SpirvType);
-  // if (Ty->isFloatingPointTy()) {
-  //   if (Ty->isBFloatTy()) {
-  //     VRegFPVariantMap[MF][TypeReg] = FPVariant::BRAIN_FLOAT;
-  //   } else {
-  //     VRegFPVariantMap[MF][TypeReg] = FPVariant::IEEE_FLOAT;
-  //   }
-  // }
-  // VRegToTypeMap[MF][TypeReg] = SpirvType;
+  MachineFunction *MF = &MIRBuilder.getMF();
+  Register TypeReg = getSPIRVTypeID(SpirvType);
+  if (Ty->isFloatingPointTy()) {
+    if (Ty->isBFloatTy()) {
+      VRegFPVariantMap[MF][TypeReg] = FPVariant::BRAIN_FLOAT;
+    } else {
+      VRegFPVariantMap[MF][TypeReg] = FPVariant::IEEE_FLOAT;
+    }
+  }
+  VRegToTypeMap[MF][TypeReg] = SpirvType;
 
   // TODO: We could end up with two SPIR-V types pointing to the same llvm type.
   // Is that a problem?
@@ -2102,20 +2101,20 @@ bool SPIRVGlobalRegistry::hasBlockDecoration(SPIRVType *Type) const {
   return false;
 }
 
-// SPIRVGlobalRegistry::FPVariant
-// SPIRVGlobalRegistry::getFPVariantForVReg(Register VReg,
-//                                          const MachineFunction *MF) {
-//   const MachineFunction *Func = MF ? MF : CurMF;
-//   DenseMap<const MachineFunction *,
-//            DenseMap<Register, FPVariant>>::const_iterator FuncIt =
-//       VRegFPVariantMap.find(Func);
+SPIRVGlobalRegistry::FPVariant
+SPIRVGlobalRegistry::getFPVariantForVReg(Register VReg,
+                                         const MachineFunction *MF) {
+  const MachineFunction *Func = MF ? MF : CurMF;
+  DenseMap<const MachineFunction *,
+           DenseMap<Register, FPVariant>>::const_iterator FuncIt =
+      VRegFPVariantMap.find(Func);
 
-//   if (FuncIt != VRegFPVariantMap.end()) {
-//     const DenseMap<Register, FPVariant> &VRegMap = FuncIt->second;
-//     DenseMap<Register, FPVariant>::const_iterator VRegIt = VRegMap.find(VReg);
+  if (FuncIt != VRegFPVariantMap.end()) {
+    const DenseMap<Register, FPVariant> &VRegMap = FuncIt->second;
+    DenseMap<Register, FPVariant>::const_iterator VRegIt = VRegMap.find(VReg);
 
-//     if (VRegIt != VRegMap.end())
-//       return VRegIt->second;
-//   }
-//   return FPVariant::NONE;
-// }
+    if (VRegIt != VRegMap.end())
+      return VRegIt->second;
+  }
+  return FPVariant::NONE;
+}
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
index fa397c5410dd8..1f8c30dc01f7f 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
@@ -29,10 +29,10 @@ using SPIRVType = const MachineInstr;
 using StructOffsetDecorator = std::function<void(Register)>;
 
 class SPIRVGlobalRegistry : public SPIRVIRMapping {
-// public:
-//   enum class FPVariant { NONE, IEEE_FLOAT, BRAIN_FLOAT };
+public:
+  enum class FPVariant { NONE, IEEE_FLOAT, BRAIN_FLOAT };
 
-// private:
+private:
   // Registers holding values which have types associated with them.
   // Initialized upon VReg definition in IRTranslator.
   // Do not confuse this with DuplicatesTracker as DT maps Type* to <MF, Reg>
@@ -94,8 +94,8 @@ class SPIRVGlobalRegistry : public SPIRVIRMapping {
 
   // Maps floating point Registers to their FPVariant (float type kind), given
   // the MachineFunction.
-  // DenseMap<const MachineFunction *, DenseMap<Register, FPVariant>>
-  //     VRegFPVariantMap;
+  DenseMap<const MachineFunction *, DenseMap<Register, FPVariant>>
+      VRegFPVariantMap;
 
   // Add a new OpTypeXXX instruction without checking for duplicates.
   SPIRVType *createSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
@@ -432,8 +432,8 @@ class SPIRVGlobalRegistry : public SPIRVIRMapping {
   void invalidateMachineInstr(MachineInstr *MI);
 
   // Return the FPVariant of to the given floating-point regiester.
-  // FPVariant getFPVariantForVReg(Register VReg,
-  //                               const MachineFunction *MF = nullptr);
+  FPVariant getFPVariantForVReg(Register VReg,
+                                const MachineFunction *MF = nullptr);
 
 private:
   SPIRVType *getOpTypeBool(MachineIRBuilder &MIRBuilder);

>From 01448f339341e9844d84cfb2359cd406b13db6b0 Mon Sep 17 00:00:00 2001
From: "Zhang, Yixing" <yixing.zhang at intel.com>
Date: Mon, 8 Sep 2025 05:38:28 -0700
Subject: [PATCH 4/4] change based on comment

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

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index 9ff5408f65d1a..349c826846fd1 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -2105,14 +2105,10 @@ SPIRVGlobalRegistry::FPVariant
 SPIRVGlobalRegistry::getFPVariantForVReg(Register VReg,
                                          const MachineFunction *MF) {
   const MachineFunction *Func = MF ? MF : CurMF;
-  DenseMap<const MachineFunction *,
-           DenseMap<Register, FPVariant>>::const_iterator FuncIt =
-      VRegFPVariantMap.find(Func);
-
+  auto FuncIt = VRegFPVariantMap.find(Func);
   if (FuncIt != VRegFPVariantMap.end()) {
     const DenseMap<Register, FPVariant> &VRegMap = FuncIt->second;
-    DenseMap<Register, FPVariant>::const_iterator VRegIt = VRegMap.find(VReg);
-
+    auto VRegIt = VRegMap.find(VReg);
     if (VRegIt != VRegMap.end())
       return VRegIt->second;
   }



More information about the llvm-commits mailing list