Skip to content

Conversation

Lancern
Copy link
Member

@Lancern Lancern commented Mar 15, 2025

Resolves #1477 .

Copy link
Collaborator

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

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

Thanks for the patch! It looks good except for one issue.

@@ -467,7 +467,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
auto Kind =
E->isIncrementOp() ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec;
// NOTE(CIR): clang calls CreateAdd but folds this to a unary op
value = emitUnaryOp(E, Kind, input);
value =
emitUnaryOp(E, Kind, input, /*nsw=*/type->isSignedIntegerType());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Classic codegen doesn't add nsw here. Based on the checks above, we should only get here if E->canOverflow() is false. For instance, I think we hit this code with this:

short f(short s) {
  s++;
  return s;
}

In this case, s is promoted to int before the operation, so it can't overflow, and classic codegen doesn't set the nsw flag.

https://godbolt.org/z/P4a11n4v5

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Overall approach looks good, I don't have anything to add besides @andykaylor comments

@Lancern Lancern force-pushed the unary-op-overflow-flags branch from f089169 to a49ffbb Compare March 18, 2025 16:45
@Lancern
Copy link
Member Author

Lancern commented Mar 18, 2025

Rebased onto the latest main.

Copy link
Collaborator

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

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

lgtm

@bcardosolopes bcardosolopes merged commit aec5941 into llvm:main Mar 19, 2025
9 checks passed
@Lancern Lancern deleted the unary-op-overflow-flags branch March 19, 2025 06:43
el-ev pushed a commit to el-ev/clangir that referenced this pull request Mar 21, 2025
el-ev pushed a commit to el-ev/clangir that referenced this pull request Mar 24, 2025
terapines-osc-cir pushed a commit to Terapines/clangir that referenced this pull request Sep 2, 2025
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.

Unary operators don't produce overflow flags for integer operations
3 participants