[llvm] [SPIR-V] Support printing OpConstantF literals wider than 64 bits (PR #208219)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 06:26:09 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/208219

printOpConstantVarOps asserted for OpConstantF with more than two 32-bit literal words (e.g. fp128), which the fixed createConstFP now produces

Reconstruct the APFloat and print it exactly

>From f5fb7ff4a47c98fb2a2759a217890e28df66cef3 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 8 Jul 2026 15:24:51 +0200
Subject: [PATCH] [SPIR-V] Support printing OpConstantF literals wider than 64
 bits

printOpConstantVarOps asserted for OpConstantF with more than two 32-bit literal words (e.g. fp128), which the fixed createConstFP now produces

Reconstruct the APFloat and print it exactly
---
 .../SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp   | 18 ++++++++++++
 .../constant/wide-float-literal-printer.mir   | 29 +++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/constant/wide-float-literal-printer.mir

diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
index b7dcd10bac809..ad47a9333ab08 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
@@ -14,6 +14,7 @@
 #include "SPIRV.h"
 #include "SPIRVBaseInfo.h"
 #include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCInst.h"
@@ -70,6 +71,23 @@ void SPIRVInstPrinter::printOpConstantVarOps(const MCInst *MI,
     return;
   }
 
+  if (MI->getOpcode() == SPIRV::OpConstantF && NumVarOps > 2) {
+    // Wide floating-point literals (e.g. fp128) are encoded via more than two
+    // 32-bit words. Reconstruct the value and print it exactly.
+    const unsigned TotalBits = NumVarOps * 32;
+    APInt Val(TotalBits, 0);
+    for (unsigned I = 0; I < NumVarOps; ++I) {
+      uint64_t Word = MI->getOperand(StartIndex + I).getImm();
+      Val |= APInt(TotalBits, Word) << (I * 32);
+    }
+    assert(TotalBits == 128 && "Unsupported floating-point literal width");
+    APFloat FP(APFloat::IEEEquad(), Val);
+    SmallString<40> Str;
+    FP.toString(Str);
+    O << ' ' << Str;
+    return;
+  }
+
   assert((NumVarOps == 1 || NumVarOps == 2) &&
          "Unsupported number of bits for literal variable");
 
diff --git a/llvm/test/CodeGen/SPIRV/constant/wide-float-literal-printer.mir b/llvm/test/CodeGen/SPIRV/constant/wide-float-literal-printer.mir
new file mode 100644
index 0000000000000..98e9ef18408f0
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/constant/wide-float-literal-printer.mir
@@ -0,0 +1,29 @@
+# RUN: llc -O0 -mtriple=spirv32-unknown-unknown -start-after=finalize-isel -verify-machineinstrs %s -o - | FileCheck %s
+
+# Exercises the SPIRVInstPrinter's OpConstantF path for literals wider than
+# 64 bits (e.g. fp128), independent of how the OpConstantF instruction with
+# more than two literal words was produced.
+
+--- |
+  define fp128 @getConstantFP128() {
+    ret fp128 0xL00000000000000004001000000000000
+  }
+...
+---
+name:            getConstantFP128
+legalized:       true
+regBankSelected: true
+selected:        true
+tracksRegLiveness: true
+body:             |
+  bb.1 (%ir-block.0):
+    %0:type = OpTypeFloat 128
+    %1:fid = OpConstantF %0, 0, 0, 0, 1073807360
+    %2:type = OpTypeFunction %0, %0
+    %3:iid = OpFunction %0, 0, %2
+    OpFunctionEnd
+    OpReturnValue %1
+
+# CHECK: %[[#FP128:]] = OpTypeFloat 128
+# CHECK: %[[#CST_FP128:]] = OpConstant %[[#FP128]] 4
+# CHECK: OpReturnValue %[[#CST_FP128]]



More information about the llvm-commits mailing list