Skip to content

Commit

Permalink
[FIRRTL] Fix check for multiple instances in Grand Central (llvm#2034)
Browse files Browse the repository at this point in the history
Fix the assumption in `GrandCentral` that only `InstanceOp` is a user of
 `firrtl.module` symbol, because `NonLocalAnchor` can also be a user.
  • Loading branch information
prithayan committed Oct 25, 2021
1 parent 40457b3 commit 08a931e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/Dialect/FIRRTL/Transforms/GrandCentral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,19 +627,24 @@ void GrandCentralPass::runOnOperation() {
// the module is not instantiated or is multiply instantiated.
auto exactlyOneInstance = [&](FModuleOp op,
StringRef msg) -> Optional<InstanceOp> {
auto instances = getSymbolTable().getSymbolUses(op, circuitOp);
if (!instances.hasValue()) {
auto uses = getSymbolTable().getSymbolUses(op, circuitOp);

auto instances = llvm::make_filter_range(uses.getValue(), [&](auto u) {
return (isa<InstanceOp>(u.getUser()));
});

if (instances.empty()) {
op->emitOpError() << "is marked as a GrandCentral '" << msg
<< "', but is never instantiated";
return None;
}

if (llvm::hasSingleElement(instances.getValue()))
return cast<InstanceOp>((*(instances.getValue().begin())).getUser());
if (llvm::hasSingleElement(instances))
return cast<InstanceOp>((*(instances.begin())).getUser());

auto diag = op->emitOpError() << "is marked as a GrandCentral '" << msg
<< "', but it is instantiated more than once";
for (auto instance : instances.getValue())
for (auto instance : instances)
diag.attachNote(instance.getUser()->getLoc())
<< "parent is instantiated here";
return None;
Expand Down
39 changes: 39 additions & 0 deletions test/Dialect/FIRRTL/grand-central-errors.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,42 @@ firrtl.circuit "Foo" attributes {
firrtl.instance dut @DUT()
}
}


// -----
// expected-error @+1 {{'firrtl.circuit' op contains a 'companion' with id '0', but does not contain a GrandCentral 'parent' with the same id}}
firrtl.circuit "multiInstance2" attributes {
annotations = [
{class = "sifive.enterprise.grandcentral.AugmentedBundleType",
defName = "Foo",
id = 0 : i64,
name = "View"},
{class = "sifive.enterprise.grandcentral.ExtractGrandCentralAnnotation",
directory = "gct-dir",
filename = "gct-dir/bindings.sv"}]} {
firrtl.module @View_companion() attributes {
annotations = [
{class = "sifive.enterprise.grandcentral.ViewAnnotation",
defName = "Foo",
id = 0 : i64,
name = "View",
type = "companion"}]} {}
// expected-error @+1 {{'firrtl.module' op is marked as a GrandCentral 'parent', but it is instantiated more than once}}
firrtl.module @DUTE() attributes {
annotations = [
{class = "sifive.enterprise.grandcentral.ViewAnnotation",
id = 0 : i64,
name = "view",
type = "parent"}
]} {
%a = firrtl.wire : !firrtl.uint<2>
%b = firrtl.wire : !firrtl.uint<4>
firrtl.instance View_companion @View_companion()
}
firrtl.module @multiInstance2() {
firrtl.instance dut @DUTE() // expected-note {{parent is instantiated here}}
firrtl.instance dut1 @DUTE() // expected-note {{parent is instantiated here}}
}
firrtl.nla @nla1 [@multiInstance1] ["dut"]
firrtl.nla @nla2 [@multiInstance1] ["dut1"]
}

0 comments on commit 08a931e

Please sign in to comment.