File tree Expand file tree Collapse file tree 4 files changed +25
-5
lines changed
include/p4mlir/Dialect/P4HIR Expand file tree Collapse file tree 4 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -540,7 +540,8 @@ def CmpOp : P4HIR_Op<"cmp",
540540def ScopeOp : P4HIR_Op<"scope", [
541541 DeclareOpInterfaceMethods<RegionBranchOpInterface>,
542542 RecursivelySpeculatable,
543- RecursiveMemoryEffects, AutomaticAllocationScope,
543+ DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
544+ AutomaticAllocationScope,
544545 NoRegionArguments, Annotated]> {
545546 let summary = "Represents a P4 scope";
546547 let description = [{
Original file line number Diff line number Diff line change @@ -926,6 +926,24 @@ LogicalResult P4HIR::ScopeOp::verify() {
926926 return success ();
927927}
928928
929+ void P4HIR::ScopeOp::getEffects (
930+ SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> &effects) {
931+ if (getAnnotations ()) {
932+ effects.emplace_back (MemoryEffects::Write::get (), ExternResource::get ());
933+ return ;
934+ }
935+
936+ for (Region ®ion : getOperation ()->getRegions ()) {
937+ for (Block &block : region) {
938+ for (Operation &op : block) {
939+ if (auto opEffects = mlir::getEffectsRecursively (&op)) {
940+ effects.append (opEffects->begin (), opEffects->end ());
941+ }
942+ }
943+ }
944+ }
945+ }
946+
929947LogicalResult P4HIR::ScopeOp::canonicalize (P4HIR::ScopeOp op, PatternRewriter &rewriter) {
930948 // Canonicalize scope: one without variables could be inlined
931949 if (op.getOps <VariableOp>().empty () && op.getScopeRegion ().hasOneBlock () &&
Original file line number Diff line number Diff line change @@ -86,9 +86,7 @@ class ScopeOpFlattening : public mlir::OpRewritePattern<P4HIR::ScopeOp> {
8686 mlir::OpBuilder::InsertionGuard guard (rewriter);
8787 auto loc = scopeOp.getLoc ();
8888
89- // Empty scope: just remove it.
90- // TODO: Decide if we'd need to do something with annotated scopes
91- if (scopeOp.isEmpty ()) {
89+ if (scopeOp.isEmpty () && !scopeOp.getAnnotations ()) {
9290 rewriter.eraseOp (scopeOp);
9391 return mlir::success ();
9492 }
Original file line number Diff line number Diff line change @@ -68,7 +68,10 @@ module {
6868
6969 // CHECK-LABEL: @empty_scope_with_annotations_preserved
7070 p4hir.func @empty_scope_with_annotations_preserved (%arg0: !ref_i32i ) {
71- // TODO: Doesn't work(but should)
71+ // Empty scopes with annotations should be preserved
72+ // CHECK: p4hir.scope annotations {name = "test"} {
73+ // CHECK-NEXT: }
74+ // CHECK-NEXT: p4hir.return
7275 p4hir.scope annotations {name = " test" } {
7376 }
7477 p4hir.return
You can’t perform that action at this time.
0 commit comments