Skip to content

Conversation

@BStott6
Copy link
Contributor

@BStott6 BStott6 commented Nov 6, 2025

  • Fixes Code with complex and bool crashed compiler #166512
  • ComplexExprEmitter::EmitCompoundAssignLValue is calling EmitLoadOfScalar(LValue, SourceLocation) to load the LHS value in the case that it's non-complex, however this function requires that the value is a simple LValue - issue occurred because the LValue in question was a bitfield LValue. I changed it to use this function which seems to handle all of the different cases (deferring to the original EmitLoadOfScalar if it's a simple LValue)

Note that I am new to Clang and I'm not confident this is the right change; it makes sense to me, fixes the crash and passes all tests but please check carefully!

@github-actions
Copy link

github-actions bot commented Nov 6, 2025

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 IR generation bugs: mangling, exceptions, etc. labels Nov 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 6, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Benjamin Stott (BStott6)

Changes
  • Fixes Code with complex and bool crashed compiler #166512
  • ComplexExprEmitter::EmitCompoundAssignLValue is calling EmitLoadOfScalar(LValue, SourceLocation) to load the LHS value in the case that it's non-complex, however this function requires that the value is a simple LValue - issue occurred because the LValue in question was a bitfield LValue. I changed it to use this function which seems to handle all of the different cases (deferring to the original EmitLoadOfScalar if it's a simple LValue)

Note that I am new to Clang and I'm not confident this is the right change; it makes sense to me, fixes the crash and passes all tests but please check carefully!


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

1 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprComplex.cpp (+1-1)
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index f8a946a76554a..47435758fcde7 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -1283,7 +1283,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
     else
       OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
   } else {
-    llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
+    llvm::Value *LHSVal = CGF.EmitLoadOfLValue(LHS, Loc).getScalarVal();
     // For floating point real operands we can directly pass the scalar form
     // to the binary operator emission and potentially get more efficient code.
     if (LHSTy->isRealFloatingType()) {

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.

Needs testcase in clang/test/CodeGen/. Needs release note (clang/docs/ReleaseNotes.rst).

OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
} else {
llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
llvm::Value *LHSVal = CGF.EmitLoadOfLValue(LHS, Loc).getScalarVal();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we also need to change the call to EmitStoreOfScalar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look into it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it does make sense to replace it with EmitStoreThroughLValue, what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That seems fine.

@@ -0,0 +1,10 @@
// Reduced from https://github.com/llvm/llvm-project/issues/166512
// RUN: %clang_cc1 %s -emit-obj -std=c23 -fsanitize=bool -o %t
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to have a test that actually checks that we generate correct code, not just that we generate code without crashing.

update_cc_test_checks.py is an easy way to write checks.

Copy link
Contributor Author

@BStott6 BStott6 Nov 11, 2025

Choose a reason for hiding this comment

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

You're right. I've added a test to verify that the bitfield value is loaded and stored correctly (base commit fails this test)

int main(void) {
// CHECK-LABEL: define dso_local i32 @main() #0 {
struct Bits b;
b.b = b.b += __builtin_complex(-1.0f, 0.0f);
Copy link
Collaborator

Choose a reason for hiding this comment

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

b.b = b.b += is confusing: you can't tell which store the CHECK lines are checking. Can you make the two variables different?

@@ -0,0 +1,10 @@
// Reduced from https://github.com/llvm/llvm-project/issues/166512
// RUN: %clang_cc1 %s -emit-obj -std=c23 -fsanitize=bool -o %t
Copy link
Collaborator

Choose a reason for hiding this comment

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

This RUN line is probably going to cause issues in some build configurations, because it's using a backend without specifying a backend and using REQUIRES to check that backend is enabled. Please use -emit-llvm unless there's some reason to check the backend output.

A few CHECK lines here might also be useful, just to confirm the output is what we expect.

Copy link
Contributor Author

@BStott6 BStott6 Nov 12, 2025

Choose a reason for hiding this comment

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

This test was meant specifically to ensure that the crash in the issue doesn't happen. The crash doesn't happen with -emit-llvm.

Edit: I think this is because the instrumentation pass wasn't being run with clang -emit-llvm, so I can just make sure that is run without invoking the backend.

Copy link
Collaborator

Choose a reason for hiding this comment

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

With emit-llvm, it hits an assertion which is essentially fatal; I don't really care whether it crashes with assertions disabled. We can add appropriate CHECK lines which should catch the bad output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we're not testing for the assertion failure, this test might not be needed as the code generation is already checked by complex-compound-assign-bitfield.c. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd be okay with just deleting it, sure.

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.

LGTM

@efriedma-quic
Copy link
Collaborator

There's a merge conflict in the release notes. Let me know when the patch is ready to merge, and I'll merge for you.

@github-actions
Copy link

🐧 Linux x64 Test Results

  • 111335 tests passed
  • 4417 tests skipped

@BStott6
Copy link
Contributor Author

BStott6 commented Nov 19, 2025

@efriedma-quic Should be ready to merge

@efriedma-quic efriedma-quic merged commit 60a2795 into llvm:main Nov 19, 2025
11 checks passed
@github-actions
Copy link

@BStott6 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

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.

Code with complex and bool crashed compiler

3 participants