[clang] 56905da - [clang] fix half && bfloat16 convert node expr codegen (#89051)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 9 19:47:36 PDT 2024
Author: JinjinLi868
Date: 2024-09-10T10:47:33+08:00
New Revision: 56905dab7da50bccfcceaeb496b206ff476127e1
URL: https://github.com/llvm/llvm-project/commit/56905dab7da50bccfcceaeb496b206ff476127e1
DIFF: https://github.com/llvm/llvm-project/commit/56905dab7da50bccfcceaeb496b206ff476127e1.diff
LOG: [clang] fix half && bfloat16 convert node expr codegen (#89051)
Data type conversion between fp16 and bf16 will generate fptrunc and
fpextend nodes, but they are actually bitcast nodes.
Added:
clang/test/CodeGen/X86/bfloat16-convert-half.c
Modified:
clang/lib/CodeGen/CGExprScalar.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 88fbbe6c4c965f..9027bab6b680d4 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1455,6 +1455,10 @@ Value *ScalarExprEmitter::EmitScalarCast(Value *Src, QualType SrcType,
return Builder.CreateFPToUI(Src, DstTy, "conv");
}
+ if ((DstElementTy->is16bitFPTy() && SrcElementTy->is16bitFPTy())) {
+ Value *FloatVal = Builder.CreateFPExt(Src, Builder.getFloatTy(), "fpext");
+ return Builder.CreateFPTrunc(FloatVal, DstTy, "fptrunc");
+ }
if (DstElementTy->getTypeID() < SrcElementTy->getTypeID())
return Builder.CreateFPTrunc(Src, DstTy, "conv");
return Builder.CreateFPExt(Src, DstTy, "conv");
diff --git a/clang/test/CodeGen/X86/bfloat16-convert-half.c b/clang/test/CodeGen/X86/bfloat16-convert-half.c
new file mode 100644
index 00000000000000..55451dc6f092cd
--- /dev/null
+++ b/clang/test/CodeGen/X86/bfloat16-convert-half.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -disable-O0-optnone -emit-llvm \
+// RUN: %s -o - | opt -S -passes=mem2reg | FileCheck %s
+
+// CHECK-LABEL: define dso_local half @test_convert_from_bf16_to_fp16(
+// CHECK-SAME: bfloat noundef [[A:%.*]]) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[FPEXT:%.*]] = fpext bfloat [[A]] to float
+// CHECK-NEXT: [[FPTRUNC:%.*]] = fptrunc float [[FPEXT]] to half
+// CHECK-NEXT: ret half [[FPTRUNC]]
+//
+_Float16 test_convert_from_bf16_to_fp16(__bf16 a) {
+ return (_Float16)a;
+}
+
+// CHECK-LABEL: define dso_local bfloat @test_convert_from_fp16_to_bf16(
+// CHECK-SAME: half noundef [[A:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[FPEXT:%.*]] = fpext half [[A]] to float
+// CHECK-NEXT: [[FPTRUNC:%.*]] = fptrunc float [[FPEXT]] to bfloat
+// CHECK-NEXT: ret bfloat [[FPTRUNC]]
+//
+__bf16 test_convert_from_fp16_to_bf16(_Float16 a) {
+ return (__bf16)a;
+}
+
More information about the cfe-commits
mailing list