Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][CodeGen] fix UB in aarch64 bfloat16 scalar conversion #89062

Merged
merged 2 commits into from
Apr 29, 2024

Conversation

nihui
Copy link
Contributor

@nihui nihui commented Apr 17, 2024

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

__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

__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

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 17, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 17, 2024

@llvm/pr-subscribers-clang

Author: None (nihui)

Changes

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

__ai __attribute__((target("bf16"))) float32_t vcvtah_f32_bf16(bfloat16_t __p0) {
  float32_t __ret;
bfloat16_t __reint = __p0;
int32_t __reint1 = *(int32_t *) &amp;__reint &lt;&lt; 16;
  __ret = *(float32_t *) &amp;__reint1;
  return __ret;
}

with this patch

__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 *) &amp;__reint) &lt;&lt; 16;
  __ret = *(float32_t *) &amp;__reint1;
  return __ret;
}

fix issue #61983


Full diff: https://github.com/llvm/llvm-project/pull/89062.diff

1 Files Affected:

  • (modified) clang/include/clang/Basic/arm_neon.td (+1-1)
diff --git a/clang/include/clang/Basic/arm_neon.td b/clang/include/clang/Basic/arm_neon.td
index 6d655c39360d3b..6390ba3f9fe5e5 100644
--- a/clang/include/clang/Basic/arm_neon.td
+++ b/clang/include/clang/Basic/arm_neon.td
@@ -275,7 +275,7 @@ def OP_VCVT_BF16_F32_HI_A32
                            (call "vget_low", $p0))>;
 
 def OP_CVT_F32_BF16
-    : Op<(bitcast "R", (op "<<", (bitcast "int32_t", $p0),
+    : Op<(bitcast "R", (op "<<", (cast "int32_t", (bitcast "int16_t", $p0)),
                                  (literal "int32_t", "16")))>;
 
 //===----------------------------------------------------------------------===//

@nihui nihui force-pushed the arm-neon-vcvtah_f32 branch 2 times, most recently from c9e664b to 0546189 Compare April 18, 2024 02:17
@nihui
Copy link
Contributor Author

nihui commented Apr 26, 2024

@ostannard hi, would you please review my fix :)

Copy link
Collaborator

@ostannard ostannard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@nihui
Copy link
Contributor Author

nihui commented Apr 29, 2024

LGTM, thanks.

And please merge this fix for me, I have no commit access :)

@ostannard ostannard merged commit cb3174b into llvm:main Apr 29, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants