[clang] [clang] fix half && bfloat16 convert node expr codegen (PR #89051)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 17 06:25:16 PDT 2024
https://github.com/JinjinLi868 updated https://github.com/llvm/llvm-project/pull/89051
>From 69a584119d8978d0ea3177c59d8772f00df3a68e Mon Sep 17 00:00:00 2001
From: Jinjin Li <lijinjin.868 at bytedance.com>
Date: Wed, 17 Apr 2024 16:44:50 +0800
Subject: [PATCH] [clang] Fix half && bfloat16 convert node expr codegen
Data type conversion between fp16 and bf16 will generate fptrunc and fpextend nodes, but they are actually bitcast nodes.
---
clang/lib/CodeGen/CGExprScalar.cpp | 11 ++++++++---
clang/test/CodeGen/X86/bfloat16-convert-half.c | 14 ++++++++++++++
2 files changed, 22 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CodeGen/X86/bfloat16-convert-half.c
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 1f18e0d5ba409a..da5a410f040d1b 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1431,9 +1431,12 @@ Value *ScalarExprEmitter::EmitScalarCast(Value *Src, QualType SrcType,
return Builder.CreateFPToUI(Src, DstTy, "conv");
}
- if (DstElementTy->getTypeID() < SrcElementTy->getTypeID())
+ if ((DstElementTy->is16bitFPTy() && SrcElementTy->is16bitFPTy()))
+ return Builder.CreateBitCast(Src, DstTy, "conv");
+ else if (DstElementTy->getTypeID() < SrcElementTy->getTypeID())
return Builder.CreateFPTrunc(Src, DstTy, "conv");
- return Builder.CreateFPExt(Src, DstTy, "conv");
+ else
+ return Builder.CreateFPExt(Src, DstTy, "conv");
}
/// Emit a conversion from the specified type to the specified destination type,
@@ -1906,7 +1909,9 @@ Value *ScalarExprEmitter::VisitConvertVectorExpr(ConvertVectorExpr *E) {
} else {
assert(SrcEltTy->isFloatingPointTy() && DstEltTy->isFloatingPointTy() &&
"Unknown real conversion");
- if (DstEltTy->getTypeID() < SrcEltTy->getTypeID())
+ if ((DstEltTy->is16bitFPTy() && SrcEltTy->is16bitFPTy()))
+ Res = Builder.CreateBitCast(Src, DstTy, "conv");
+ else if (DstEltTy->getTypeID() < SrcEltTy->getTypeID())
Res = Builder.CreateFPTrunc(Src, DstTy, "conv");
else
Res = 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..4a13e2c33c8149
--- /dev/null
+++ b/clang/test/CodeGen/X86/bfloat16-convert-half.c
@@ -0,0 +1,14 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 4
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +fullbf16 -S -emit-llvm %s -o - | FileCheck %s
+// CHECK-LABEL: define dso_local half @test_convert(
+// CHECK-SAME: bfloat noundef [[A:%.*]]) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[A_ADDR:%.*]] = alloca bfloat, align 2
+// CHECK-NEXT: store bfloat [[A]], ptr [[A_ADDR]], align 2
+// CHECK-NEXT: [[TMP0:%.*]] = load bfloat, ptr [[A_ADDR]], align 2
+// CHECK-NEXT: [[CONV:%.*]] = bitcast bfloat [[TMP0]] to half
+// CHECK-NEXT: ret half [[CONV]]
+//
+_Float16 test_convert(__bf16 a) {
+ return (_Float16)a;
+}
More information about the cfe-commits
mailing list