[llvm] [IR] Use pretty printing for mask in is_fpclass (PR #207653)

Serge Pavlov via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 5 22:28:57 PDT 2026


https://github.com/spavloff created https://github.com/llvm/llvm-project/pull/207653

The second argument of `llvm.is_fpclass` is a mask, where each bit represents a floating-point class. Understanding the check that the call makes in an IR dump requires additional effort. To simplify the interpretation of IR dumps, a previously implemented pretty-printing mechanism can be used:
https://github.com/llvm/llvm-project/commit/39e7712ac520ccfc43383b3e9d6ea8cf2958b8e3. This commit implements this mechanism for printing the arguments of `llvm.is_fpclass`.

Unconditional use of pretty-printing caused about 40 tests to fail. To make the change less invasive, the printing of explanatory comments is disabled by default. To enable it, a new option `-print-formatted-constants` is introduced. For compatibility with the existing implementation this option has no effect for target intrinsics,- comments are always printed for them.

This change also fixes the printing of argument names: if an argument name is absent, it is simply not printed.

>From 200a001025b9226475e5f9b44a20321cb245a5f8 Mon Sep 17 00:00:00 2001
From: Serge Pavlov <sepavloff at gmail.com>
Date: Fri, 3 Jul 2026 00:24:39 +0700
Subject: [PATCH] [IR] Use pretty printing for mask in is_fpclass

The second argument of `llvm.is_fpclass` is a mask, where each bit
represents a floating-point class. Understanding the check that the call
makes in an IR dump requires additional effort. To simplify the
interpretation of IR dumps, a previously implemented pretty-printing
mechanism can be used:
https://github.com/llvm/llvm-project/commit/39e7712ac520ccfc43383b3e9d6ea8cf2958b8e3.
This commit implements this mechanism for printing the arguments of
`llvm.is_fpclass`.

Unconditional use of pretty-printing caused about 40 tests to fail. To
make the change less invasive, the printing of explanatory comments is
disabled by default. To enable it, a new option
`-print-formatted-constants` is introduced. For compatibility with the
existing implementation this option has no effect for target intrinsics,
- comments are always printed for them.

This change also fixes the printing of argument names: if an argument
name is absent, it is simply not printed.
---
 llvm/include/llvm/IR/Intrinsics.h             |  2 +
 llvm/include/llvm/IR/Intrinsics.td            |  4 +-
 llvm/lib/IR/AsmWriter.cpp                     | 21 ++++++--
 llvm/lib/IR/Intrinsics.cpp                    | 54 +++++++++++++++++++
 llvm/test/Assembler/print-formatted.ll        | 47 ++++++++++++++++
 .../utils/TableGen/Basic/IntrinsicEmitter.cpp |  3 +-
 6 files changed, 124 insertions(+), 7 deletions(-)
 create mode 100644 llvm/test/Assembler/print-formatted.ll

diff --git a/llvm/include/llvm/IR/Intrinsics.h b/llvm/include/llvm/IR/Intrinsics.h
index 2f9239aa7b77a..a4799751832bf 100644
--- a/llvm/include/llvm/IR/Intrinsics.h
+++ b/llvm/include/llvm/IR/Intrinsics.h
@@ -325,6 +325,8 @@ LLVM_ABI Intrinsic::ID getDeinterleaveIntrinsicID(unsigned Factor);
 LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS,
                           const Constant *ImmArgVal);
 
+LLVM_ABI void printFPClassMask(raw_ostream &OS, const Constant *ImmArgVal);
+
 } // namespace Intrinsic
 
 } // namespace llvm
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index f75335cd2c60a..1c8012769cd95 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -1363,7 +1363,9 @@ def int_is_fpclass
     : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
                             [llvm_anyfloat_ty, llvm_i32_ty],
                             [IntrNoMem, IntrSpeculatable,
-                             IntrNoCreateUndefOrPoison, ImmArg<ArgIndex<1>>]>;
+                             IntrNoCreateUndefOrPoison, ImmArg<ArgIndex<1>>,
+                             ArgInfo<ArgIndex<1>,
+                                     [ImmArgPrinter<"printFPClassMask">]>]>;
 
 //===--------------- Constrained Floating Point Intrinsics ----------------===//
 //
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 665d7122af2c7..4d562ba771ac9 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -111,6 +111,11 @@ static cl::opt<bool> PrintAddrspaceName("print-addrspace-name", cl::Hidden,
                                         cl::init(false),
                                         cl::desc("Print address space names"));
 
+static cl::opt<bool> PrintFormattedConstants(
+    "print-formatted-constants",
+    cl::desc("Pretty print constants using custom formatters"), cl::Hidden,
+    cl::init(false));
+
 // Make virtual table appear in this compilation unit.
 AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
 
@@ -4619,24 +4624,30 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
     Out << ' ';
     writeOperand(Operand, false);
     Out << '(';
+    Intrinsic::ID IID = CI->getIntrinsicID();
     bool HasPrettyPrintedArgs =
-        isa<IntrinsicInst>(CI) &&
-        Intrinsic::hasPrettyPrintedArgs(CI->getIntrinsicID());
+        isa<IntrinsicInst>(CI) && Intrinsic::hasPrettyPrintedArgs(IID);
+
+    // For compatibility always use formatters for target intrinsics. For others
+    // take command-line option into account.
+    if (IID && !Intrinsic::isTargetIntrinsic(IID)) {
+      if (!PrintFormattedConstants)
+        HasPrettyPrintedArgs = false;
+    }
 
-    ListSeparator LS;
-    Function *CalledFunc = CI->getCalledFunction();
     auto PrintArgComment = [&](unsigned ArgNo) {
       const auto *ConstArg = dyn_cast<Constant>(CI->getArgOperand(ArgNo));
       if (!ConstArg)
         return;
       std::string ArgComment;
       raw_string_ostream ArgCommentStream(ArgComment);
-      Intrinsic::ID IID = CalledFunc->getIntrinsicID();
       Intrinsic::printImmArg(IID, ArgNo, ArgCommentStream, ConstArg);
       if (ArgComment.empty())
         return;
       Out << "/* " << ArgComment << " */ ";
     };
+
+    ListSeparator LS;
     if (HasPrettyPrintedArgs) {
       for (unsigned ArgNo = 0, NumArgs = CI->arg_size(); ArgNo < NumArgs;
            ++ArgNo) {
diff --git a/llvm/lib/IR/Intrinsics.cpp b/llvm/lib/IR/Intrinsics.cpp
index 6ddfc18c56501..700a8e730c800 100644
--- a/llvm/lib/IR/Intrinsics.cpp
+++ b/llvm/lib/IR/Intrinsics.cpp
@@ -1472,6 +1472,60 @@ Intrinsic::ID Intrinsic::getDeinterleaveIntrinsicID(unsigned Factor) {
   return InterleaveIntrinsics[Factor - 2].Deinterleave;
 }
 
+LLVM_ABI void Intrinsic::printFPClassMask(raw_ostream &OS,
+                                          const Constant *ImmArgVal) {
+  if (const auto *CI = dyn_cast<ConstantInt>(ImmArgVal)) {
+    uint64_t Val = CI->getZExtValue();
+    if (Val <= fcAllFlags) {
+      SmallVector<StringRef, 5> Classes;
+      if (Val & fcZero) {
+        if ((Val & fcZero) == fcZero)
+          Classes.push_back("zero");
+        else if (Val & fcPosZero)
+          Classes.push_back("+zero");
+        else
+          Classes.push_back("-zero");
+      }
+      if (Val & fcSubnormal) {
+        if ((Val & fcSubnormal) == fcSubnormal)
+          Classes.push_back("subnormal");
+        else if (Val & fcPosSubnormal)
+          Classes.push_back("+subnormal");
+        else
+          Classes.push_back("-subnormal");
+      }
+      if (Val & fcNormal) {
+        if ((Val & fcNormal) == fcNormal)
+          Classes.push_back("normal");
+        else if (Val & fcPosNormal)
+          Classes.push_back("+normal");
+        else
+          Classes.push_back("-normal");
+      }
+      if (Val & fcInf) {
+        if ((Val & fcInf) == fcInf)
+          Classes.push_back("inf");
+        else if (Val & fcPosInf)
+          Classes.push_back("+inf");
+        else
+          Classes.push_back("-inf");
+      }
+      if (Val & fcNan) {
+        if ((Val & fcNan) == fcNan)
+          Classes.push_back("nan");
+        else if (Val & fcQNan)
+          Classes.push_back("qnan");
+        else
+          Classes.push_back("snan");
+      }
+      OS << llvm::join(Classes, "|");
+      return;
+    }
+  }
+  llvm_unreachable("printFPClassMask called with invalid value for "
+                   "immediate argument");
+}
+
 #define GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
 #include "llvm/IR/IntrinsicImpl.inc"
 
diff --git a/llvm/test/Assembler/print-formatted.ll b/llvm/test/Assembler/print-formatted.ll
new file mode 100644
index 0000000000000..3b806688c7947
--- /dev/null
+++ b/llvm/test/Assembler/print-formatted.ll
@@ -0,0 +1,47 @@
+; RUN: llvm-as < %s | llvm-dis -print-formatted-constants | FileCheck %s
+
+define i1 @print_is_fpclass(float %x) {
+  %1 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 1)
+  %2 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 2)
+  %3 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 4)
+  %4 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 8)
+  %5 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 16)
+  %6 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 32)
+  %7 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 64)
+  %8 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 128)
+  %9 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 256)
+  %10 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 512)
+  %11 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 3)
+  %12 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 96)
+  %13 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 144)
+  %14 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 264)
+  %15 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 516)
+  %16 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 56)
+  %17 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 448)
+  %18 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 960)
+  %19 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 60)
+  %20 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 666)
+  ret i1 %20
+}
+
+; CHECK-LABEL: print_is_fpclass(float %x) {
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* snan */ i32 1)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* qnan */ i32 2)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* -inf */ i32 4)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* -normal */ i32 8)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* -subnormal */ i32 16)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* -zero */ i32 32)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* +zero */ i32 64)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* +subnormal */ i32 128)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* +normal */ i32 256)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* +inf */ i32 512)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* nan */ i32 3)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* zero */ i32 96)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* subnormal */ i32 144)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* normal */ i32 264)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* inf */ i32 516)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* -zero|-subnormal|-normal */ i32 56)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* +zero|+subnormal|+normal */ i32 448)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* +zero|+subnormal|+normal|+inf */ i32 960)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* -zero|-subnormal|-normal|-inf */ i32 60)
+; CHECK: call i1 @llvm.is.fpclass.f32(float %x, /* subnormal|-normal|+inf|qnan */ i32 666)
\ No newline at end of file
diff --git a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
index 330de1063d666..23086c422ec1c 100644
--- a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp
@@ -912,7 +912,8 @@ void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Cons
     OS << "    switch (ArgIdx) {\n";
     for (const auto [ArgIdx, ArgName, FuncName] : Int.PrettyPrintFunctions) {
       OS << "    case " << ArgIdx << ":\n";
-      OS << "      OS << \"" << ArgName << "=\";\n";
+      if (!ArgName.empty())
+        OS << "      OS << \"" << ArgName << "=\";\n";
       if (!FuncName.empty()) {
         OS << "      ";
         if (!Int.TargetPrefix.empty())



More information about the llvm-commits mailing list