[llvm] [LLVM][AsmWriter] Fix ConstantFP zeroinitializer check (PR #196097)
Paul Walker via llvm-commits
llvm-commits at lists.llvm.org
Wed May 6 08:34:35 PDT 2026
https://github.com/paulwalker-arm created https://github.com/llvm/llvm-project/pull/196097
It turns out ppc_fp128 has a value where isPosZero() returns true but isNullValue() returns false.
I wondered if DoubleAPFloat might be wrong but when trying to "fix" it I found more and more places that do not care about the second double, so I reverted to this localised fix. The change to `Constant::isNullValue()` is not required, but given the comment I figured it was worth keeping the related code consistent.
>From b9aa87b7d5b717bb05a049b6da800a256faf273f Mon Sep 17 00:00:00 2001
From: Paul Walker <paul.walker at arm.com>
Date: Wed, 6 May 2026 12:39:24 +0000
Subject: [PATCH 1/2] Add test showing bogus asm printing.
---
llvm/test/Assembler/constant-splat.ll | 3 +++
1 file changed, 3 insertions(+)
diff --git a/llvm/test/Assembler/constant-splat.ll b/llvm/test/Assembler/constant-splat.ll
index 82e25adda0e10..a91468766fa73 100644
--- a/llvm/test/Assembler/constant-splat.ll
+++ b/llvm/test/Assembler/constant-splat.ll
@@ -36,6 +36,9 @@
; CHECK: @constant.splat.ppc_fp128 = constant <1 x ppc_fp128> splat (ppc_fp128 0xM80000000000000000000000000000000)
@constant.splat.ppc_fp128 = constant <1 x ppc_fp128> splat (ppc_fp128 0xM80000000000000000000000000000000)
+; CHECK: @constant.splat.ppc_fp128.zero.1 = constant <1 x ppc_fp128> zeroinitializer
+ at constant.splat.ppc_fp128.zero.1 = constant <1 x ppc_fp128> splat (ppc_fp128 0xM00000000000000000000000000000001)
+
; CHECK: @constant.splat.global.ptr = constant <4 x ptr> <ptr @my_global, ptr @my_global, ptr @my_global, ptr @my_global>
@constant.splat.global.ptr = constant <4 x ptr> splat (ptr @my_global)
>From 2e09fb42c71a1b25db65690abc6276d73370d9a5 Mon Sep 17 00:00:00 2001
From: Paul Walker <paul.walker at arm.com>
Date: Wed, 6 May 2026 13:02:30 +0000
Subject: [PATCH 2/2] [LLVM][AsmWriter] Fix ConstantFP zeroinitializer check.
It turns out ppc_fp128 has a value where isPosZero returns true but
isNullValue returns false.
---
llvm/lib/IR/AsmWriter.cpp | 2 +-
llvm/lib/IR/Constants.cpp | 5 ++---
llvm/test/Assembler/constant-splat.ll | 2 +-
3 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index ab1e7c5dca6e3..e209b47a855df 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -1672,7 +1672,7 @@ static void writeConstantInternal(raw_ostream &Out, const Constant *CV,
Type *Ty = CFP->getType();
if (Ty->isVectorTy()) {
- if (CFP->isPosZero()) {
+ if (CFP->getValue().bitcastToAPInt().isZero()) {
Out << "zeroinitializer";
return;
}
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index ed30b5dac51fd..f0c9ee0b774fe 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -96,11 +96,10 @@ bool Constant::isNullValue() const {
if (const ConstantByte *CB = dyn_cast<ConstantByte>(this))
return CB->isZero();
- // +0.0 is null.
if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
// ppc_fp128 determine isZero using high order double only
- // Should check the bitwise value to make sure all bits are zero.
- return CFP->isExactlyValue(+0.0);
+ // so check the bitwise value to make sure all bits are zero.
+ return CFP->getValue().bitcastToAPInt().isZero();
// constant zero is zero for aggregates, cpnull is null for pointers, none for
// tokens.
diff --git a/llvm/test/Assembler/constant-splat.ll b/llvm/test/Assembler/constant-splat.ll
index a91468766fa73..c4c335b684ebd 100644
--- a/llvm/test/Assembler/constant-splat.ll
+++ b/llvm/test/Assembler/constant-splat.ll
@@ -36,7 +36,7 @@
; CHECK: @constant.splat.ppc_fp128 = constant <1 x ppc_fp128> splat (ppc_fp128 0xM80000000000000000000000000000000)
@constant.splat.ppc_fp128 = constant <1 x ppc_fp128> splat (ppc_fp128 0xM80000000000000000000000000000000)
-; CHECK: @constant.splat.ppc_fp128.zero.1 = constant <1 x ppc_fp128> zeroinitializer
+; CHECK: @constant.splat.ppc_fp128.zero.1 = constant <1 x ppc_fp128> splat (ppc_fp128 0xM00000000000000000000000000000001)
@constant.splat.ppc_fp128.zero.1 = constant <1 x ppc_fp128> splat (ppc_fp128 0xM00000000000000000000000000000001)
; CHECK: @constant.splat.global.ptr = constant <4 x ptr> <ptr @my_global, ptr @my_global, ptr @my_global, ptr @my_global>
More information about the llvm-commits
mailing list