Implement select cases simplifications (#204)#217
Implement select cases simplifications (#204)#217mtsamis wants to merge 7 commits intop4lang:mainfrom
Conversation
|
|
||
| // CHECK-COUNT-2: p4hir.select_case | ||
|
|
||
| p4hir.state @start { |
There was a problem hiding this comment.
I would probably suggest to do CHECK-LABEL: p4hir.state @start to make checks more structured. Same for other cases. You won't need CHECK-COUNT above then.
There was a problem hiding this comment.
Thanks for pointing out, done.
(I kept the check-count because it was meant to be only for the first state, not both)
| %set_3 = p4hir.const #set_const_of_int2_i8i | ||
| %set_4 = p4hir.const #set_const_of_int1_b2i | ||
| %everything = p4hir.const #everything | ||
| p4hir.state @start { |
There was a problem hiding this comment.
See above. I'd use CHECK-LABEL to narrow the check place
| %false = p4hir.const #false | ||
| %w = p4hir.variable ["w", init] : <!p4hir.bool> | ||
| p4hir.assign %false, %w : <!p4hir.bool> | ||
| p4hir.state @start { |
| %false = p4hir.const #false | ||
| %w = p4hir.variable ["w", init] : <!p4hir.bool> | ||
| p4hir.assign %false, %w : <!p4hir.bool> | ||
| p4hir.state @start { |
lib/Transforms/SimplifySelect.cpp
Outdated
|
|
||
| // Collect operations to apply patterns. | ||
| llvm::SmallVector<mlir::Operation *, 16> ops; | ||
| getOperation()->walk<mlir::WalkOrder::PostOrder>([&](mlir::Operation *op) { |
There was a problem hiding this comment.
I think you can just do
getOperation()->walk<mlir::WalkOrder::PostOrder>([&](P4HIR::ParserTransitionSelectOp op) {
ops.push_back(op);
}What is the purpose of postorder traversal?
Even more, it seems we can explicitly apply to the ops, right? Something along https://mlir.llvm.org/docs/PassManagement/#operation-pass-static-filtering-by-op-type and corresponding tablegen bits.
There was a problem hiding this comment.
Yes, thanks for pointing out, I copied this from another pass and didn't though about it, fixed now:
- Modified pass in tablegen to only apply on
ParserOp. - Removed WalkOrder and
mlir::isabits. - Added
strictMode = mlir::GreedyRewriteStrictness::ExistingOpsconfig to avoid unnecessary iterations on other ops.
lib/Transforms/SimplifySelect.cpp
Outdated
| return false; | ||
|
|
||
| bool isShapePrimitive = (shape.size() == 1) | ||
| && !shape.front().getDefiningOp<P4HIR::TupleOp>(); |
There was a problem hiding this comment.
is this really how clang-format did? If not, will you please reformat the things? :)
There was a problem hiding this comment.
No, because I didn't use clang-format 😅
I now properly formatted all the code in the last push.
30dc9dc to
3eb26e9
Compare
| getOperation()->walk([&](P4HIR::ParserTransitionSelectOp op) { ops.push_back(op); }); | ||
|
|
||
| mlir::GreedyRewriteConfig config; | ||
| config.strictMode = mlir::GreedyRewriteStrictness::ExistingOps; |
There was a problem hiding this comment.
What if we'd have bool inside tuple as a key? Won't this prevent application of second pattern to the newly created op? Should we use ExistingAndNewOps here?
There was a problem hiding this comment.
It's always the same transition select op and we just modify the arguments / cases. So no, it doesn't prevent application of the pattern multiple times.
3eb26e9 to
97acab0
Compare
|
Latest push fixes and improves the code for Previously both In the latest code The test case was extended with the previously failing cases. |
Can you always do this? And let canonicalizer fold the extracts? |
97acab0 to
752096c
Compare
Done by also implementing TupleExtractOp::fold (same with StructExtractOp). |
|
The last force-pushed revision of this also modified the SetAttr::getConstantValue. llvm::APInt getConstantValue() {
assert(getKind() == SetKind::Constant && "Expected Constant kind set.");
assert(getMembers().size() == 1 && "Expected single member set.");
auto attr = getMembers()[0];
if (auto intAttr = mlir::dyn_cast<P4HIR::IntAttr>(attr)) {
return intAttr.getValue();
} else if (auto boolAttr = mlir::dyn_cast<P4HIR::BoolAttr>(attr)) {
return llvm::APInt(1, static_cast<uint64_t>(boolAttr.getValue()));
} else {
auto enumFieldAttr = mlir::cast<P4HIR::EnumFieldAttr>(attr);
auto enumType = mlir::cast<P4HIR::SerEnumType>(enumFieldAttr.getType());
auto field = enumFieldAttr.getField().getValue();
return enumType.valueOf<P4HIR::IntAttr>(field).getValue();
}
}This fixed a crash when we had a SerEnum type in a select during the |
Signed-off-by: Manolis Tsamis <tsamismanolis@gmail.com>
Signed-off-by: Manolis Tsamis <tsamismanolis@gmail.com>
Signed-off-by: Manolis Tsamis <tsamismanolis@gmail.com>
Signed-off-by: Manolis Tsamis <tsamismanolis@gmail.com>
752096c to
92e6a98
Compare
Signed-off-by: Manolis Tsamis <tsamismanolis@gmail.com>
Signed-off-by: Manolis Tsamis <tsamismanolis@gmail.com>
…f SimplifySelectCases Signed-off-by: Manolis Tsamis <tsamismanolis@gmail.com>
92e6a98 to
7337dfe
Compare
|
Changes in the last version:
With these changes all samples from the P4C repository transform without crashes. |
|
|
||
| private: | ||
| // Create appropriate casts to convert `keyset` to a set with element type `newType`. | ||
| mlir::Value castKeysetValue(mlir::PatternRewriter &rewriter, mlir::Type newType, |
There was a problem hiding this comment.
Can we utilize TypeConverter here? We're having bunch of helpers in p4mlir/Conversion/ConversionPatterns.h
This PR implements the various transition_select optimizations from ticket #204.
Four of the five transformations (
SimplifySelectList/ReplaceSelectRange/RemoveSelectBooleans/SingleArgumentSelect) are implemented as patterns of a newSimplifySelectoptimization pass, while the equivalent ofSimplifySelectCasesis implemented asParserTransitionSelectOpcanonicalizations.To aid implementing these optimizations and also simplify the IR, there is an initial commit that makes
p4hir.transition_selectand the corresponding yield statements have variadic arguments. This was done because having either a single argument or a tuple of arguments and set products made all passes more complicated and required code duplication to handle one vs many arguments.There is also an additional commit that refactors
CastOpconstant folding and fixes various signedness bugs around it.