Skip to content

[clang codegen] fix crash emitting __array_rank #113186

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

Merged
merged 3 commits into from
Oct 22, 2024

Conversation

HerrCai0907
Copy link
Contributor

@HerrCai0907 HerrCai0907 commented Oct 21, 2024

Fixed: #113044
the type of ArrayTypeTraitExpr can be changed, use i32 directly is incorrect.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Oct 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2024

@llvm/pr-subscribers-clang

Author: Congcong Cai (HerrCai0907)

Changes

Fixed: #113044


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+1-1)
  • (added) clang/test/CodeGen/builtins-array-rank.cpp (+6)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 35c31452cef411..550c5f1b2c2126 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -374,6 +374,7 @@ Bug Fixes in This Version
 - Fixed a crash when trying to transform a dependent address space type. Fixes #GH101685.
 - Fixed a crash when diagnosing format strings and encountering an empty
   delimited escape sequence (e.g., ``"\o{}"``). #GH102218
+- Fixed a crash when the result of ``__array_rank`` is used as operand for bit operation. (#GH113044).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index b7f5b932c56b6f..0ea757fc0befdb 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -718,7 +718,7 @@ class ScalarExprEmitter
   }
 
   Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
-    return llvm::ConstantInt::get(Builder.getInt32Ty(), E->getValue());
+    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
   }
 
   Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
diff --git a/clang/test/CodeGen/builtins-array-rank.cpp b/clang/test/CodeGen/builtins-array-rank.cpp
new file mode 100644
index 00000000000000..e6f0a55245aad9
--- /dev/null
+++ b/clang/test/CodeGen/builtins-array-rank.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple aarch64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
+
+unsigned long array_rank_binary_operator(void) {
+  // CHECK: ret i64 3
+  return __array_rank(int[10]) | 2;
+}

@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2024

@llvm/pr-subscribers-clang-codegen

Author: Congcong Cai (HerrCai0907)

Changes

Fixed: #113044


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+1-1)
  • (added) clang/test/CodeGen/builtins-array-rank.cpp (+6)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 35c31452cef411..550c5f1b2c2126 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -374,6 +374,7 @@ Bug Fixes in This Version
 - Fixed a crash when trying to transform a dependent address space type. Fixes #GH101685.
 - Fixed a crash when diagnosing format strings and encountering an empty
   delimited escape sequence (e.g., ``"\o{}"``). #GH102218
+- Fixed a crash when the result of ``__array_rank`` is used as operand for bit operation. (#GH113044).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index b7f5b932c56b6f..0ea757fc0befdb 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -718,7 +718,7 @@ class ScalarExprEmitter
   }
 
   Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
-    return llvm::ConstantInt::get(Builder.getInt32Ty(), E->getValue());
+    return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
   }
 
   Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
diff --git a/clang/test/CodeGen/builtins-array-rank.cpp b/clang/test/CodeGen/builtins-array-rank.cpp
new file mode 100644
index 00000000000000..e6f0a55245aad9
--- /dev/null
+++ b/clang/test/CodeGen/builtins-array-rank.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple aarch64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
+
+unsigned long array_rank_binary_operator(void) {
+  // CHECK: ret i64 3
+  return __array_rank(int[10]) | 2;
+}

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

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

Please make the title of the pull request something more useful, like "[clang codegen] fix crash emitting __array_rank". The current title is utterly useless for someone looking through the commit history.

LGTM with comments addressed.

@HerrCai0907 HerrCai0907 changed the title [clang] use ArrayTypeTraitExpr::getType as value type [clang codegen] fix crash emitting __array_rank Oct 21, 2024
@HerrCai0907 HerrCai0907 merged commit c0c36aa into llvm:main Oct 22, 2024
9 checks passed
@HerrCai0907 HerrCai0907 deleted the fix/113044 branch October 22, 2024 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] Assertion `BitWidth == RHS.BitWidth && "Bit widths must be the same"' failed.
3 participants