Skip to content

Commit

Permalink
[NFC][FIRRTL] Canonicalize away empty probes (#6617)
Browse files Browse the repository at this point in the history
  • Loading branch information
nandor authored Jan 27, 2024
1 parent 649ee55 commit bab53e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/circt/Dialect/FIRRTL/FIRRTLIntrinsics.td
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def FPGAProbeIntrinsicOp : FIRRTLOp<"int.fpga_probe", []> {
let arguments = (ins AnyType:$input, NonConstClockType:$clock);
let results = (outs);
let assemblyFormat = "$clock `,` $input attr-dict `:` type($input)";

let hasCanonicalizeMethod = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
31 changes: 31 additions & 0 deletions lib/Dialect/FIRRTL/FIRRTLFolds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3289,3 +3289,34 @@ OpFoldResult HasBeenResetIntrinsicOp::fold(FoldAdaptor adaptor) {

return {};
}

//===----------------------------------------------------------------------===//
// FPGAProbeIntrinsicOp
//===----------------------------------------------------------------------===//

static bool isTypeEmpty(FIRRTLType type) {
return FIRRTLTypeSwitch<FIRRTLType, bool>(type)
.Case<FVectorType>(
[&](auto ty) -> bool { return isTypeEmpty(ty.getElementType()); })
.Case<BundleType>([&](auto ty) -> bool {
for (auto elem : ty.getElements())
if (!isTypeEmpty(elem.type))
return false;
return true;
})
.Case<IntType>([&](auto ty) { return ty.getWidth() == 0; })
.Default([](auto) -> bool { return false; });
}

LogicalResult FPGAProbeIntrinsicOp::canonicalize(FPGAProbeIntrinsicOp op,
PatternRewriter &rewriter) {
auto firrtlTy = type_dyn_cast<FIRRTLType>(op.getInput().getType());
if (!firrtlTy)
return failure();

if (!isTypeEmpty(firrtlTy))
return failure();

rewriter.eraseOp(op);
return success();
}
9 changes: 9 additions & 0 deletions test/Dialect/FIRRTL/canonicalization.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3324,4 +3324,13 @@ firrtl.module @Whens(in %clock: !firrtl.clock, in %a: !firrtl.uint<1>, in %reset
}
}

firrtl.module @Probes(in %clock: !firrtl.clock) {
// CHECK-NOT: firrtl.int.fpga_probe %clock, %zero_width : !firrtl.uint<0>
%zero_width = firrtl.wire : !firrtl.uint<0>
firrtl.int.fpga_probe %clock, %zero_width : !firrtl.uint<0>
// CHECK-NOT: firrtl.int.fpga_probe %clock, %empty_bundle : !firrtl.bundle<a: uint<0>>
%empty_bundle = firrtl.wire : !firrtl.bundle<a: uint<0>>
firrtl.int.fpga_probe %clock, %empty_bundle : !firrtl.bundle<a: uint<0>>
}

}
10 changes: 10 additions & 0 deletions test/Dialect/FIRRTL/round-trip.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ firrtl.module @Intrinsics(in %ui : !firrtl.uint, in %clock: !firrtl.clock, in %u
%cg1 = firrtl.int.clock_gate %clock, %ui1, %ui1
}

// CHECK-LABEL: firrtl.module @FPGAProbe
firrtl.module @FPGAProbe(
in %clock: !firrtl.clock,
in %reset: !firrtl.uint<1>,
in %in: !firrtl.uint<8>
) {
// CHECK: firrtl.int.fpga_probe %clock, %in : !firrtl.uint<8>
firrtl.int.fpga_probe %clock, %in : !firrtl.uint<8>
}

// CHECK-LABEL: firrtl.option @Platform
firrtl.option @Platform {
// CHECK:firrtl.option_case @FPGA
Expand Down

0 comments on commit bab53e6

Please sign in to comment.