-
Notifications
You must be signed in to change notification settings - Fork 444
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
Add string literal support in constant folding. #4837
Conversation
Signed-off-by: fruffy <fruffy@nyu.edu>
frontends/common/constantFolding.cpp
Outdated
@@ -440,6 +441,15 @@ const IR::Node *DoConstantFolding::compare(const IR::Operation_Binary *e) { | |||
} | |||
bool bresult = (left->value == right->value) == eqTest; | |||
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), bresult); | |||
} else if (eleft->is<IR::StringLiteral>()) { |
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.
Can this be simplified to:
} else if (const auto *left = eleft->to<IR::StringLiteral>()) {
..
This would skip double cast. The code for BoolLiteral
likely would need to be fixed as well :)
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.
Also, maybe it would help to add comment to bool eqTest
above to clarify that compare
is called only for IR::Equ
and IR::Neq
, so other cases are not possible. It tool me a while to figure out why other IR::Operation_Binary
cases are not handled...
Signed-off-by: fruffy <fruffy@nyu.edu>
Fun thing, P4 spec says there are no operations on string literals... What is the use case for comparing strings btw? |
Hmm, in many cases the compiler optimization passes have more flexibility than what the spec permits, under the assumption that user-facing behavior is not affected. However, in this case, because
This is quite useful when working with IR expressions (often generated for static/dynamic analysis). For example, simplifying an expression of the from:
Maybe it makes sense to split |
Oh, I see, this is for P4Tools etc. not for actually using it in user-written P4 code.
Maybe... the difference will be really probably (hopefully) just this. I'd say that ideally, we would want to type-check the program as one of the very first things, before constant folding. That would require ability to constant-evaluate expressions from the type checker (but I'd guess they would/could be type-checked at the point when they are evaluated!). But rewriting the type checker would be a huge thing. That would even allow constant-folding to be deferred to midend, where it belongs. |
String literals can also be compared and folded if they are equal or unequal. There might be more cases but I think the changes include the major ones.