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 crash when using bool vector in compound assignment #75435

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cygao90
Copy link
Contributor

@cygao90 cygao90 commented Dec 14, 2023

Fixes: #72468.
The left side bool vectorvec[i] did not perform a specific conversion(CodeGenFunction::emitBoolVecConversion) in compound assignment.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen labels Dec 14, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 14, 2023

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Chenyang Gao (cygao90)

Changes

Fixes: #72468.
The left side bool vector did not perform a specific conversion(CodeGenFunction::emitBoolVecConversion) in compound assignment


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

3 Files Affected:

  • (modified) clang/lib/CodeGen/CGExpr.cpp (+9-4)
  • (added) clang/test/CodeGen/gh72468.c (+9)
  • (modified) clang/test/SemaCXX/vector-bool.cpp (+4-4)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index ed9aaa28c25733..5a024761d83e3c 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -2085,10 +2085,15 @@ RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
   }
 
   if (LV.isVectorElt()) {
-    llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddress(),
-                                              LV.isVolatileQualified());
-    return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
-                                                    "vecext"));
+    llvm::Value *Load = nullptr;
+    if (LV.getType()->isExtVectorBoolType())
+      Load = EmitLoadOfScalar(LV.getVectorAddress(), LV.isVolatileQualified(),
+                              LV.getType(), Loc);
+    else
+      Load =
+          Builder.CreateLoad(LV.getVectorAddress(), LV.isVolatileQualified());
+    return RValue::get(
+        Builder.CreateExtractElement(Load, LV.getVectorIdx(), "vecext"));
   }
 
   // If this is a reference to a subset of the elements of a vector, either
diff --git a/clang/test/CodeGen/gh72468.c b/clang/test/CodeGen/gh72468.c
new file mode 100644
index 00000000000000..7a602d4982803e
--- /dev/null
+++ b/clang/test/CodeGen/gh72468.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -S -emit-llvm -o - %s
+
+typedef __attribute__((__ext_vector_type__(4))) _Bool BoolVector;
+
+BoolVector vec;
+
+void f(int i, int j) {
+  vec[i] |= vec[j];
+}
\ No newline at end of file
diff --git a/clang/test/SemaCXX/vector-bool.cpp b/clang/test/SemaCXX/vector-bool.cpp
index e99d420e73fab2..5ec87beb4ed055 100644
--- a/clang/test/SemaCXX/vector-bool.cpp
+++ b/clang/test/SemaCXX/vector-bool.cpp
@@ -38,10 +38,10 @@ void Operations() {
   // (void)(eight_bools > other_eight_bools);
   // (void)(eight_bools >= other_eight_bools);
 
-  // // Legal assignments
-  // (void)(eight_bools |= other_eight_bools);
-  // (void)(eight_bools &= other_eight_bools);
-  // (void)(eight_bools ^= other_eight_bools);
+  // Legal assignments
+  (void)(eight_bools |= other_eight_bools);
+  (void)(eight_bools &= other_eight_bools);
+  (void)(eight_bools ^= other_eight_bools);
 
   // Illegal operators
   (void)(eight_bools || other_eight_bools); // expected-error@47 {{invalid operands to binary expression ('EightBools' (vector of 8 'bool' values) and 'EightBools')}}

@cygao90
Copy link
Contributor Author

cygao90 commented Dec 18, 2023

Ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang crashes in CodeGen when a compound assignment operator's operands are elements of bool vectors
2 participants