Skip to content

[AVR] Fix codegen after getConstant assertions got enabled #152269

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 1 commit into from
Aug 7, 2025

Conversation

aykevl
Copy link
Contributor

@aykevl aykevl commented Aug 6, 2025

This fixes #152097

This commit fixes two instances of a (somewhat) recently enabled assertion. One with a test, the other I can't reproduce (might be dead code) but certainly looks like an instance of the same problem.

The PR that introduced the regression: #117558

With this patch, the AVR backend is usable again for TinyGo.

@aykevl aykevl requested a review from benshi001 August 6, 2025 08:11
Copy link
Member

@benshi001 benshi001 left a comment

Choose a reason for hiding this comment

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

LGTM

@aykevl
Copy link
Contributor Author

aykevl commented Aug 6, 2025

Thanks for the quick review @benshi001!

@RKSimon can you also take a look, to be sure?

@@ -713,7 +713,7 @@ SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
// Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
// fold the constant into the cmp instruction.
if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
RHS = DAG.getSignedConstant(C->getSExtValue() + 1, DL, VT);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be DAG.getConstant(C->getZExtValue() + 1, DL, VT); its an unsigned compare

Copy link
Contributor Author

@aykevl aykevl Aug 7, 2025

Choose a reason for hiding this comment

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

Looks like you are right. I changed it to getConstant and getZExtValue and it works (both the test I added and all TinyGo AVR tests pass with this patch applied).

I also added an assert to make sure the addition won't unintentionally overflow.

@aykevl aykevl force-pushed the avr-fix-getConstant-crash branch from 77d8426 to 25b860b Compare August 7, 2025 08:10
Copy link

github-actions bot commented Aug 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

This fixes llvm#152097

This commit fixes two instances of a (somewhat) recently enabled
assertion. One with a test, the other I can't reproduce (might be dead
code) but certainly looks like an instance of the same problem.

The PR that introduced the regression:
llvm#117558
@aykevl aykevl force-pushed the avr-fix-getConstant-crash branch from 25b860b to 9391850 Compare August 7, 2025 08:14
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM

@aykevl aykevl merged commit aeeb9b5 into llvm:main Aug 7, 2025
9 checks passed
@aykevl aykevl deleted the avr-fix-getConstant-crash branch August 7, 2025 09:40
@aykevl
Copy link
Contributor Author

aykevl commented Aug 8, 2025

Requested a backport for 21.x in #152679

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AVR] crash: "Value is not an N-bit unsigned value" when enabling asserts
3 participants