-
Notifications
You must be signed in to change notification settings - Fork 13
Implement new remove-parser-control-flow optimization pass (#99) #221
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
base: main
Are you sure you want to change the base?
Conversation
ceda822 to
f2e7092
Compare
f2e7092 to
cab6581
Compare
asl
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.
See comments (mostly minor) here
include/p4mlir/Transforms/IRUtils.h
Outdated
|
|
||
| // Helper to create a new empty sub-state for `state`. | ||
| P4HIR::ParserStateOp createSubState(mlir::RewriterBase &rewriter, P4HIR::ParserStateOp state, | ||
| llvm::StringRef suffix); |
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.
Use Twine for suffix.
| auto getNextTransition() { return getBody().back().getTerminator(); } | ||
| auto getNextTransition() { return getBlock()->getTerminator(); } | ||
|
|
||
| /// Get a SymbolRefAttr for this parser state. |
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 we be a bit more specific here. Essentially it is a fully qualified state name, right? So this should be explained here :)
include/p4mlir/Transforms/IRUtils.h
Outdated
| // Inline `scopeOp`'s body to its parent. | ||
| void inlineScope(mlir::RewriterBase &rewriter, P4HIR::ScopeOp scopeOp); | ||
|
|
||
| // Return true if it's valid to call `splitBlockAt` for the given arguments. |
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.
It would help to explain better the invariants / conditions here.
lib/Transforms/IRUtils.cpp
Outdated
| // Split resulting block. | ||
| mlir::Block *before = op->getBlock(); | ||
| mlir::Block *middle = rewriter.splitBlock(before, op->getIterator()); | ||
| mlir::Block *after = rewriter.splitBlock(middle, ++op->getIterator()); |
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.
| mlir::Block *after = rewriter.splitBlock(middle, ++op->getIterator()); | |
| mlir::Block *after = rewriter.splitBlock(middle, std::next(op->getIterator())); |
lib/Transforms/IRUtils.cpp
Outdated
| // This is needed to properly handle operands after `copyOp` calls. | ||
| for (mlir::Operation &op : llvm::make_early_inc_range(llvm::reverse(*block))) { | ||
| if (mlir::isa<P4HIR::VariableOp>(op)) { | ||
| // Promote variables to a parser locals. |
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.
| // Promote variables to a parser locals. | |
| // Move variables to top-level. Rewriter position is expected to point where we can create such locals (e.g. parser or control locals |
Note that for control locals we'd need to be a bit more elaborated as actions are isolated from above, so they are exported via "symbol references"
lib/Transforms/IRUtils.cpp
Outdated
| std::string name = basename; | ||
|
|
||
| size_t counter = 0; | ||
| while (parser.lookupSymbol(name)) name = basename + "_" + std::to_string(counter++); |
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 you ask symbol table to generate unique symbol for you?
E.g.:
auto getUniqueName = [&](mlir::StringAttr toRename) {
unsigned counter = 0;
return mlir::SymbolTable::generateSymbolName<256>(
toRename,
[&](llvm::StringRef candidate) {
return parser.lookupSymbol(StringAttr::get(candidate)) != nullptr;
},
counter);
};
auto nameAttr = getUniqueName(StringAttr::get(llvm::Twine(state.getSymName()) + "_" + suffix)));You might want to tune strings types here though :)
include/p4mlir/Transforms/IRUtils.h
Outdated
|
|
||
| // Create new intermediate state that transitions to `transitionTo` and move `ops` in it. | ||
| // If `ops` is non-null the terminator of the block is erased once moved. | ||
| P4HIR::ParserStateOp createSubState(llvm::StringRef suffix, |
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.
Use twines here and below
cab6581 to
e6c0ed9
Compare
|
Thanks for the review! I have addressed your comments in the latest version. In addition, I made
Indeed, the current version probably won't work for control locals. It looks like it would need some work to get them right, but we don't currently need that somewhere. Should we add an assertion for now and implement in the future if needed? |
Yeah, let's do an assert for now. |
e6c0ed9 to
55a6e4e
Compare
|
Added assert for |
include/p4mlir/Transforms/IRUtils.h
Outdated
|
|
||
| // Create new intermediate state that transitions to `transitionTo` and move `ops` in it. | ||
| // If `ops` is non-null the terminator of the block is erased once moved. | ||
| P4HIR::ParserStateOp createSubState(llvm::Twine suffix, P4HIR::ParserStateOp transitionTo = {}, |
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 is a common problem :)
Twines should only be used as const references in arguments
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 see, thanks for pointing out. I force pushed the fix.
55a6e4e to
9f491a8
Compare
0b4b930 to
596ac64
Compare
|
@mtsamis Looks like it does not compile anymore |
Thanks for reporting, I'll have a look. |
Signed-off-by: Manolis Tsamis <[email protected]>
Signed-off-by: Manolis Tsamis <[email protected]>
Signed-off-by: Manolis Tsamis <[email protected]>
Signed-off-by: Manolis Tsamis <[email protected]>
Signed-off-by: Manolis Tsamis <[email protected]>
2671cb6 to
e29f8c5
Compare
This PR implements the equivalent of P4C's RemoveParserControlFlow pass. It replaces if and switch statements inside parser states with newly introduced states and transitions.
Some infrastructure for this pass is generally useful and will be helpful in implementing subparser/control inlining. As such, it has been placed in a separate IRUtils namespace.
(This is very similar to #220, which I accidentally closed by deleting the branch. This new branch contains few more helper functions and a different
TransitionSelectBuilder::addCasesimplementation).