[llvm] constant fold float to float bitcasts (PR #177663)
Karol Zwolak via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 23 11:24:56 PST 2026
https://github.com/karolzwolak created https://github.com/llvm/llvm-project/pull/177663
None
>From 41b1fe8822b43e241bf617b5e8bc1a3fddae5e16 Mon Sep 17 00:00:00 2001
From: Karol Zwolak <karolzwolak7 at gmail.com>
Date: Fri, 23 Jan 2026 20:20:13 +0100
Subject: [PATCH 1/2] pre-commited tests
---
.../InstSimplify/fold-float-bitcasts.ll | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll
diff --git a/llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll b/llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll
new file mode 100644
index 0000000000000..49de798d1aa13
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+define bfloat @bfloat_to_half() {
+; CHECK-LABEL: define bfloat @bfloat_to_half() {
+; CHECK-NEXT: ret bfloat bitcast (half 0xH7C00 to bfloat)
+;
+ %val = bitcast half 0xH7C00 to bfloat
+ ret bfloat %val
+}
+
+define half @half_to_bfloat() {
+; CHECK-LABEL: define half @half_to_bfloat() {
+; CHECK-NEXT: ret half bitcast (bfloat 0xR7C00 to half)
+;
+ %val = bitcast bfloat 0xR7C00 to half
+ ret half %val
+}
>From df059805834a896bbcdb40bc38b2651cf9452198 Mon Sep 17 00:00:00 2001
From: Karol Zwolak <karolzwolak7 at gmail.com>
Date: Fri, 23 Jan 2026 20:20:37 +0100
Subject: [PATCH 2/2] constant fold float to float bitcasts
---
llvm/lib/Analysis/ConstantFolding.cpp | 10 ++++++++++
.../Transforms/InstSimplify/fold-float-bitcasts.ll | 4 ++--
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index c0754d3a41264..68c0c8551adc7 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -145,6 +145,16 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
}
}
+ // Handle float -> float bitcast
+ if (DestTy->isFloatingPointTy() && isa<ConstantFP>(C)) {
+ bool Ignored;
+ const ConstantFP *CF = cast<ConstantFP>(C);
+ APFloat Val = CF->getValueAPF();
+ Val.convert(DestTy->getScalarType()->getFltSemantics(),
+ APFloat::rmNearestTiesToEven, &Ignored);
+ return ConstantFP::get(DestTy->getContext(), Val);
+ }
+
// The code below only handles casts to vectors currently.
auto *DestVTy = dyn_cast<VectorType>(DestTy);
if (!DestVTy)
diff --git a/llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll b/llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll
index 49de798d1aa13..704c4216de43c 100644
--- a/llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll
+++ b/llvm/test/Transforms/InstSimplify/fold-float-bitcasts.ll
@@ -3,7 +3,7 @@
define bfloat @bfloat_to_half() {
; CHECK-LABEL: define bfloat @bfloat_to_half() {
-; CHECK-NEXT: ret bfloat bitcast (half 0xH7C00 to bfloat)
+; CHECK-NEXT: ret bfloat 0xR7F80
;
%val = bitcast half 0xH7C00 to bfloat
ret bfloat %val
@@ -11,7 +11,7 @@ define bfloat @bfloat_to_half() {
define half @half_to_bfloat() {
; CHECK-LABEL: define half @half_to_bfloat() {
-; CHECK-NEXT: ret half bitcast (bfloat 0xR7C00 to half)
+; CHECK-NEXT: ret half 0xH7C00
;
%val = bitcast bfloat 0xR7C00 to half
ret half %val
More information about the llvm-commits
mailing list