-
Notifications
You must be signed in to change notification settings - Fork 168
[CIR] Emit nsw flag for unary integer operations #1485
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
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.
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()); |
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.
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.
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.
Updated.
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.
Overall approach looks good, I don't have anything to add besides @andykaylor comments
f089169
to
a49ffbb
Compare
Rebased onto the latest |
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
Resolves #1477 .