Skip to content

Commit

Permalink
[FIRRTL] Fix hasDontTouch crash on non-module block args
Browse files Browse the repository at this point in the history
Fix an issue in `firrtl::hasDontTouch` where the cast to `FModuleOp`
would crash if the function was called on a block argument of an
operation that is not a module, for example, a `ContractOp`. Such block
arguments are considered to not be marked as "don't touch".
  • Loading branch information
fabianschuiki committed Feb 5, 2025
1 parent f292e2a commit a47f7f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Dialect/FIRRTL/FIRRTLOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ bool firrtl::hasDontTouch(Value value) {
if (auto *op = value.getDefiningOp())
return hasDontTouch(op);
auto arg = dyn_cast<BlockArgument>(value);
auto module = cast<FModuleOp>(arg.getOwner()->getParentOp());
auto module = dyn_cast<FModuleOp>(arg.getOwner()->getParentOp());
if (!module)
return false;
return (module.getPortSymbolAttr(arg.getArgNumber())) ||
AnnotationSet::forPort(module, arg.getArgNumber()).hasDontTouch();
}
Expand Down
14 changes: 14 additions & 0 deletions test/Dialect/FIRRTL/imdce.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,17 @@ firrtl.circuit "FormalMarkerIsUse" {
firrtl.formal @Test, @Foo {}
"some_unknown_dialect.op"() { magic = @Bar, other = @Uninstantiated } : () -> ()
}

// -----

// Block arguments on operations that aren't modules, e.g. contracts, must not
// crash the `firrtl::hasDontTouch` query.
// CHECK-LABEL: firrtl.circuit "NonModuleBlockArgsMustNotCrash"
firrtl.circuit "NonModuleBlockArgsMustNotCrash" {
firrtl.module @NonModuleBlockArgsMustNotCrash(in %a: !firrtl.uint<42>, out %b: !firrtl.uint<42>) {
%0 = firrtl.contract %a : !firrtl.uint<42> {
^bb0(%arg0: !firrtl.uint<42>):
}
firrtl.matchingconnect %b, %0 : !firrtl.uint<42>
}
}

0 comments on commit a47f7f0

Please sign in to comment.