Skip to content

Commit

Permalink
[FIRRTL] Erase empty layerblocks
Browse files Browse the repository at this point in the history
Add a canonicalizer that will erase empty `firrtl.layerblock` ops.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
  • Loading branch information
seldridge committed Dec 2, 2024
1 parent dcd7c47 commit 94a562c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/circt/Dialect/FIRRTL/FIRRTLStatements.td
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def LayerBlockOp : FIRRTLOp<
$layerName $region attr-dict
}];
let hasVerifier = 1;
let hasCanonicalizeMethod = 1;
}

#endif // CIRCT_DIALECT_FIRRTL_FIRRTLSTATEMENTS_TD
16 changes: 16 additions & 0 deletions lib/Dialect/FIRRTL/FIRRTLFolds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3410,3 +3410,19 @@ LogicalResult FPGAProbeIntrinsicOp::canonicalize(FPGAProbeIntrinsicOp op,
rewriter.eraseOp(op);
return success();
}

//===----------------------------------------------------------------------===//
// Layer Block Op
//===----------------------------------------------------------------------===//

LogicalResult LayerBlockOp::canonicalize(LayerBlockOp op,
PatternRewriter &rewriter) {

// If the layerblock is empty, erase it.
if (op.getBody()->empty()) {
rewriter.eraseOp(op);
return success();
}

return failure();
}
8 changes: 8 additions & 0 deletions test/Dialect/FIRRTL/canonicalization.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

firrtl.circuit "Casts" {

firrtl.layer @A bind {}

// CHECK-LABEL: firrtl.module @Casts
firrtl.module @Casts(in %ui1 : !firrtl.uint<1>, in %si1 : !firrtl.sint<1>,
in %clock : !firrtl.clock, in %asyncreset : !firrtl.asyncreset,
Expand Down Expand Up @@ -3588,4 +3590,10 @@ firrtl.class @PropertyArithmetic(in %in: !firrtl.integer, out %out0: !firrtl.int
firrtl.propassign %out0, %3 : !firrtl.integer
firrtl.propassign %out1, %4 : !firrtl.integer
}

// CHECK-LABEL: firrtl.module private @LayerBlocks
firrtl.module private @LayerBlocks() {
// CHECK-NOT: firrtl.layerblock @A
firrtl.layerblock @A {}
}
}

0 comments on commit 94a562c

Please sign in to comment.