[llvm] 4bac9d4 - [RISCV] Add isel for bitcasting between bfloat and half types (#158828)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 17 12:10:51 PDT 2025
Author: Ying Wang
Date: 2025-09-17T12:10:47-07:00
New Revision: 4bac9d4911a14c5d444f8a6b94c449e5a2c4a332
URL: https://github.com/llvm/llvm-project/commit/4bac9d4911a14c5d444f8a6b94c449e5a2c4a332
DIFF: https://github.com/llvm/llvm-project/commit/4bac9d4911a14c5d444f8a6b94c449e5a2c4a332.diff
LOG: [RISCV] Add isel for bitcasting between bfloat and half types (#158828)
There is no RISCV isel for bitcast between f16 and bf16 which will
trigger "cannot select" fatal error.
Co-authored-by: Ying Wang <wy446777 at alibaba-inc.com>
Added:
llvm/test/CodeGen/RISCV/bfloat-convert-half.ll
Modified:
llvm/lib/Target/RISCV/RISCVInstrInfoZfbfmin.td
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoZfbfmin.td b/llvm/lib/Target/RISCV/RISCVInstrInfoZfbfmin.td
index 8f0768f91c370..cdb43b11dd922 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoZfbfmin.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoZfbfmin.td
@@ -70,3 +70,10 @@ def : Pat<(bf16 (fpround FPR64:$rs1)),
def : Pat<(fpextend (bf16 FPR16:$rs1)),
(FCVT_D_S (FCVT_S_BF16 FPR16:$rs1, FRM_DYN), FRM_RNE)>;
}
+
+let Predicates = [HasStdExtZfhmin, HasStdExtZfbfmin] in {
+// bf16 -> f16
+def : Pat<(f16 (bitconvert (bf16 FPR16:$src))), (f16 FPR16:$src)>;
+// f16 -> bf16
+def : Pat<(bf16 (bitconvert (f16 FPR16:$src))), (bf16 FPR16:$src)>;
+}
diff --git a/llvm/test/CodeGen/RISCV/bfloat-convert-half.ll b/llvm/test/CodeGen/RISCV/bfloat-convert-half.ll
new file mode 100644
index 0000000000000..79f12ed62f435
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/bfloat-convert-half.ll
@@ -0,0 +1,29 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 -mattr=+zfhmin,+zfbfmin \
+; RUN: -verify-machineinstrs | FileCheck %s --check-prefixes=RV32
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -mattr=+zfhmin,+zfbfmin \
+; RUN: -verify-machineinstrs | FileCheck %s --check-prefixes=RV64
+
+define bfloat @fcvt_bf16_h(half %a) nounwind {
+; RV32-LABEL: fcvt_bf16_h:
+; RV32: # %bb.0:
+; RV32-NEXT: ret
+;
+; RV64-LABEL: fcvt_bf16_h:
+; RV64: # %bb.0:
+; RV64-NEXT: ret
+ %r = bitcast half %a to bfloat
+ ret bfloat %r
+}
+
+define half @test_h_bf16(bfloat %a) nounwind {
+; RV32-LABEL: test_h_bf16:
+; RV32: # %bb.0:
+; RV32-NEXT: ret
+;
+; RV64-LABEL: test_h_bf16:
+; RV64: # %bb.0:
+; RV64-NEXT: ret
+ %r = bitcast bfloat %a to half
+ ret half %r
+}
More information about the llvm-commits
mailing list