Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIRRTL] Verify enabled layers of module, extmodule. #7557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions lib/Dialect/FIRRTL/FIRRTLOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,23 +1694,51 @@ static LogicalResult verifyPortSymbolUses(FModuleLike module,
return success();
}

static LogicalResult verifyEnabledLayers(FModuleLike module,
SymbolTableCollection &symbolTable) {
auto circuitOp = module->getParentOfType<CircuitOp>();
for (auto layer : module.getLayers()) {
auto *op =
symbolTable.lookupSymbolIn(circuitOp, cast<SymbolRefAttr>(layer));
if (!op)
return module->emitOpError() << "enables unknown layer '" << layer << "'";
if (!isa<LayerOp>(op))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks the final reference is a layer, but structural requirements on layers ensure all parents will also be layerop's.

return module->emitOpError()
.append("enables non-layer '", layer, "'")
.attachNote(op->getLoc())
.append("expected layer");
}

// Check public modules don't enable circuit-disabled layers.
if (auto disabledLayers = circuitOp.getDisableLayersAttr()) {
auto disabledLayersSymRefs = disabledLayers.getAsRange<SymbolRefAttr>();
if (module.isPublic()) {
for (auto layer : module.getLayers())
if (llvm::is_contained(disabledLayersSymRefs, layer))
return module->emitOpError(
"public module has circuit-disabled layer ")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is set membership sufficient, or do we need to walk and check if parent layer disabled....?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're going to have to check if a parent layer is disabled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

<< layer << " enabled";
}
}

return success();
}

LogicalResult FModuleOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
if (failed(
verifyPortSymbolUses(cast<FModuleLike>(getOperation()), symbolTable)))
return failure();

auto circuitOp = (*this)->getParentOfType<CircuitOp>();
for (auto layer : getLayers()) {
if (!symbolTable.lookupSymbolIn(circuitOp, cast<SymbolRefAttr>(layer)))
return emitOpError() << "enables unknown layer '" << layer << "'";
}

return success();
return verifyEnabledLayers(cast<FModuleLike>(getOperation()), symbolTable);
}

LogicalResult
FExtModuleOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
return verifyPortSymbolUses(cast<FModuleLike>(getOperation()), symbolTable);
if (failed(
verifyPortSymbolUses(cast<FModuleLike>(getOperation()), symbolTable)))
return failure();

return verifyEnabledLayers(cast<FModuleLike>(getOperation()), symbolTable);
}

LogicalResult
Expand Down
34 changes: 34 additions & 0 deletions test/Dialect/FIRRTL/errors.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ firrtl.circuit "InstanceCannotHavePortSymbols" {
// -----

firrtl.circuit "InstanceMissingLayers" {
firrtl.layer @A bind {}
// expected-note @below {{original module declared here}}
firrtl.extmodule @Ext(in in : !firrtl.uint<1>) attributes {layers = [@A]}
firrtl.module @InstanceMissingLayers() {
Expand Down Expand Up @@ -1977,6 +1978,38 @@ firrtl.circuit "UnknownEnabledLayer" {

// -----

// https://github.com/llvm/circt/issues/7448

firrtl.circuit "NonLayerEnabledLayer" {
// expected-note @below {{expected layer}}
firrtl.extmodule @A()
// expected-error @below {{'firrtl.module' op enables non-layer '@A'}}
firrtl.module @NonLayerEnabledLayer() attributes {layers = [@A]} {}
}

// -----

// Disable layer enabled by main.

firrtl.circuit "MainEnablesDisabledLayer" attributes {disable_layers = [@A]} {
firrtl.layer @A bind { }
// expected-error @below {{public module has circuit-disabled layer @A enabled}}
firrtl.module @MainEnablesDisabledLayer() attributes {layers = [@A]} { }
}

// -----

// Disable layer enabled by non-main public.

firrtl.circuit "PublicEnablesDisabledLayer" attributes {disable_layers = [@A]} {
firrtl.layer @A bind { }
// expected-error @below {{public module has circuit-disabled layer @A enabled}}
firrtl.module @M() attributes {layers = [@A]} { }
firrtl.module @PublicEnablesDisabledLayer() { }
}

// -----

firrtl.circuit "RWProbeRemote" {
firrtl.module @Other() {
%w = firrtl.wire sym @x : !firrtl.uint<1>
Expand Down Expand Up @@ -2561,3 +2594,4 @@ firrtl.circuit "MainNotModule" {
return
}
}

30 changes: 15 additions & 15 deletions test/Dialect/FIRRTL/specialize-layers.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,21 @@ attributes {
}
}

// CHECK: firrtl.extmodule @ExtModule() attributes {layers = [@A, @A, @A::@B_C]}
firrtl.extmodule @ExtModule() attributes {layers = [@A, @A::@B, @A::@B::@C]}
// CHECK: firrtl.extmodule private @ExtModule() attributes {layers = [@A, @A, @A::@B_C]}
firrtl.extmodule private @ExtModule() attributes {layers = [@A, @A::@B, @A::@B::@C]}
}

// CHECK-LABEL: firrtl.circuit "DisableLayerA"
firrtl.circuit "DisableLayerA" attributes {
disable_layers = [@A]
} {
firrtl.layer @A bind { }
// CHECK-NOT: firrtl.extmodule @Test0()
firrtl.extmodule @Test0() attributes {layers = [@A]}
// CHECK-NOT: firrtl.extmodule @Test1() attributes {layers = [@A]}
firrtl.extmodule @Test1() attributes {layers = [@A]}
// CHECK-NOT: firrtl.extmodule @Test2() attributes {layers = [@A]}
firrtl.extmodule @Test2() attributes {layers = [@A]}
// CHECK-NOT: firrtl.extmodule private @Test0()
firrtl.extmodule private @Test0() attributes {layers = [@A]}
// CHECK-NOT: firrtl.extmodule private @Test1() attributes {layers = [@A]}
firrtl.extmodule private @Test1() attributes {layers = [@A]}
// CHECK-NOT: firrtl.extmodule private @Test2() attributes {layers = [@A]}
firrtl.extmodule private @Test2() attributes {layers = [@A]}

// Top level module, which can't be deleted by the pass.
firrtl.extmodule @DisableLayerA()
Expand Down Expand Up @@ -368,7 +368,7 @@ firrtl.circuit "ProbeOpsEnableA" attributes {
(out out : !firrtl.probe<uint<1>, @A>)
}

firrtl.extmodule @ExtModule(out out : !firrtl.probe<uint<1>, @A>)
firrtl.extmodule private @ExtModule(out out : !firrtl.probe<uint<1>, @A>)
}

// CHECK-LABEL: firrtl.circuit "ProbeOpsDisableA"
Expand Down Expand Up @@ -416,8 +416,8 @@ firrtl.circuit "ProbeOpsDisableA" attributes {
(out out : !firrtl.probe<uint<1>, @A>)
}

// CHECK: firrtl.extmodule @ExtModule()
firrtl.extmodule @ExtModule(out out : !firrtl.probe<uint<1>, @A>)
// CHECK: firrtl.extmodule private @ExtModule()
firrtl.extmodule private @ExtModule(out out : !firrtl.probe<uint<1>, @A>)
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -451,11 +451,11 @@ firrtl.circuit "HierPathDelete" attributes {
}
}

firrtl.extmodule @Leaf()
firrtl.extmodule private @Leaf()

// CHECK-NOT: hw.hierpath private @DeletedPath [@Deleted]
hw.hierpath private @DeletedPath [@Deleted]
firrtl.extmodule @Deleted() attributes {layers = [@Layer]}
firrtl.extmodule private @Deleted() attributes {layers = [@Layer]}
}

// CHECK-LABEL: firrtl.circuit "HierPathDelete2"
Expand Down Expand Up @@ -485,11 +485,11 @@ firrtl.circuit "HierPathDelete2" attributes {
}
}

firrtl.extmodule @Leaf()
firrtl.extmodule private @Leaf()

// CHECK-NOT: hw.hierpath private @DeletedPath [@Deleted]
hw.hierpath private @DeletedPath [@Deleted]
firrtl.extmodule @Deleted() attributes {layers = [@Layer]}
firrtl.extmodule private @Deleted() attributes {layers = [@Layer]}
}


Expand Down
Loading