Skip to content

Commit 71f044c

Browse files
committed
Fix annotations
1 parent ff53252 commit 71f044c

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ def CmpOp : P4HIR_Op<"cmp",
540540
def 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 = [{

lib/Dialect/P4HIR/P4HIR_Ops.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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 &region : 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+
929947
LogicalResult 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() &&

lib/Transforms/FlattenCFG.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff 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
}

test/Transforms/Folds/scope_simplification.mlir

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)