Skip to content

Commit cb3174b

Browse files
authored
[clang][CodeGen] fix UB in aarch64 bfloat16 scalar conversion (#89062)
do not bitcast 16bit `bfloat16` to 32bit `int32_t` directly bitcast to `int16_t`, and then upcast to `int32_t` Fix ASAN runtime error when calling vcvtah_f32_bf16 `==21842==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x007fda1dd063 at pc 0x005c0361c234 bp 0x007fda1dd030 sp 0x007fda1dd028 ` without patch ```c __ai __attribute__((target("bf16"))) float32_t vcvtah_f32_bf16(bfloat16_t __p0) { float32_t __ret; bfloat16_t __reint = __p0; int32_t __reint1 = *(int32_t *) &__reint << 16; __ret = *(float32_t *) &__reint1; return __ret; } ``` with this patch ```c __ai __attribute__((target("bf16"))) float32_t vcvtah_f32_bf16(bfloat16_t __p0) { float32_t __ret; bfloat16_t __reint = __p0; int32_t __reint1 = (int32_t)(*(int16_t *) &__reint) << 16; __ret = *(float32_t *) &__reint1; return __ret; } ``` fix issue #61983
1 parent 179e174 commit cb3174b

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

clang/include/clang/Basic/arm_neon.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def OP_VCVT_BF16_F32_HI_A32
275275
(call "vget_low", $p0))>;
276276

277277
def OP_CVT_F32_BF16
278-
: Op<(bitcast "R", (op "<<", (bitcast "int32_t", $p0),
278+
: Op<(bitcast "R", (op "<<", (cast "int32_t", (bitcast "int16_t", $p0)),
279279
(literal "int32_t", "16")))>;
280280

281281
//===----------------------------------------------------------------------===//

clang/test/CodeGen/arm-bf16-convert-intrinsics.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,12 @@ bfloat16_t test_vcvth_bf16_f32(float32_t a) {
426426
// CHECK-NEXT: [[__REINT_I:%.*]] = alloca bfloat, align 2
427427
// CHECK-NEXT: [[__REINT1_I:%.*]] = alloca i32, align 4
428428
// CHECK-NEXT: store bfloat [[A:%.*]], ptr [[__REINT_I]], align 2
429-
// CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[__REINT_I]], align 2
430-
// CHECK-NEXT: [[SHL_I:%.*]] = shl i32 [[TMP1]], 16
429+
// CHECK-NEXT: [[TMP0:%.*]] = load i16, ptr [[__REINT_I]], align 2
430+
// CHECK-NEXT: [[CONV_I:%.*]] = sext i16 [[TMP0]] to i32
431+
// CHECK-NEXT: [[SHL_I:%.*]] = shl i32 [[CONV_I]], 16
431432
// CHECK-NEXT: store i32 [[SHL_I]], ptr [[__REINT1_I]], align 4
432-
// CHECK-NEXT: [[TMP3:%.*]] = load float, ptr [[__REINT1_I]], align 4
433-
// CHECK-NEXT: ret float [[TMP3]]
433+
// CHECK-NEXT: [[TMP1:%.*]] = load float, ptr [[__REINT1_I]], align 4
434+
// CHECK-NEXT: ret float [[TMP1]]
434435
//
435436
float32_t test_vcvtah_f32_bf16(bfloat16_t a) {
436437
return vcvtah_f32_bf16(a);

0 commit comments

Comments
 (0)