-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang][CodeGen] Use EmitLoadOfLValue instead of EmitLoadOfScalar to get LHS for complex compound assignment #166798
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
Conversation
…get LHS for complex compound assignment
|
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 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. |
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-codegen Author: Benjamin Stott (BStott6) Changes
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:
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()) {
|
efriedma-quic
left a comment
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
efriedma-quic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
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. |
🐧 Linux x64 Test Results
|
|
@efriedma-quic Should be ready to merge |
|
@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! |
ComplexExprEmitter::EmitCompoundAssignLValueis callingEmitLoadOfScalar(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 originalEmitLoadOfScalarif 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!