Skip to content

Commit

Permalink
[MSFT] Track instance hierarchy through module parameters (#3229)
Browse files Browse the repository at this point in the history
The MSFT dialect will be specializing modules based on a _per-instance_ basis. In practice, we expect this to be rather sparse. Rather than elaborating the entire design, we've chosen to select implementations based on the instance hierarchy path. So we need a way to know said path. Tracking it through module parameters fills that need.

As a first step, this PR adds a pervasive `__INST_HIER` module parameter during lowering and appends the instance name to it in every module instance.
  • Loading branch information
teqdruid authored May 28, 2022
1 parent 788d40f commit e00cf85
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/circt/Dialect/MSFT/MSFTOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def InstanceOp : MSFTOp<"instance", [
Symbol,
ParentOneOf<["::circt::hw::HWModuleOp", "MSFTModuleOp"]>,
ParentOneOf<["MSFTModuleOp"]>,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<SymbolUserOpInterface>
]> {
Expand Down
22 changes: 20 additions & 2 deletions lib/Dialect/MSFT/MSFTPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,27 @@ InstanceOpLowering::matchAndRewrite(InstanceOp msftInst, OpAdaptor adaptor,
if (!hw::isAnyModule(referencedModule))
return rewriter.notifyMatchFailure(
msftInst, "Referenced module was not an HW module");

ArrayAttr paramValues;
if (isa<hw::HWModuleExternOp>(referencedModule)) {
paramValues = msftInst.parametersAttr();
if (!paramValues)
paramValues = rewriter.getArrayAttr({});
} else {
auto instAppendParam = hw::ParamExprAttr::get(
hw::PEO::StrConcat,
{hw::ParamDeclRefAttr::get(rewriter.getStringAttr("__INST_HIER"),
rewriter.getNoneType()),
rewriter.getStringAttr("."), msftInst.sym_nameAttr()});
paramValues = rewriter.getArrayAttr(
{hw::ParamDeclAttr::get("__INST_HIER", instAppendParam)});
}

auto hwInst = rewriter.create<hw::InstanceOp>(
msftInst.getLoc(), referencedModule, msftInst.instanceNameAttr(),
SmallVector<Value>(adaptor.getOperands().begin(),
adaptor.getOperands().end()),
msftInst.parameters().getValueOr(ArrayAttr()), msftInst.sym_nameAttr());
paramValues, msftInst.sym_nameAttr());
hwInst->setDialectAttrs(msftInst->getDialectAttrs());
rewriter.replaceOp(msftInst, hwInst.getResults());
return success();
Expand Down Expand Up @@ -288,8 +304,10 @@ ModuleOpLowering::matchAndRewrite(MSFTModuleOp mod, OpAdaptor adaptor,
return success();
}

ArrayAttr params = rewriter.getArrayAttr({hw::ParamDeclAttr::get(
rewriter.getStringAttr("__INST_HIER"), rewriter.getNoneType())});
auto hwmod = rewriter.replaceOpWithNewOp<hw::HWModuleOp>(
mod, mod.getNameAttr(), mod.getPorts());
mod, mod.getNameAttr(), mod.getPorts(), params);
rewriter.eraseBlock(hwmod.getBodyBlock());
rewriter.inlineRegionBefore(mod.getBody(), hwmod.getBody(),
hwmod.getBody().end());
Expand Down
9 changes: 5 additions & 4 deletions test/Dialect/MSFT/module_instance.mlir
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// RUN: circt-opt %s -verify-diagnostics | circt-opt -verify-diagnostics | FileCheck %s
// RUN: circt-opt %s --lower-msft-to-hw -verify-diagnostics | circt-opt -verify-diagnostics | FileCheck %s --check-prefix=HWLOW

// CHECK-LABEL: hw.module @top
// CHECK-LABEL: msft.module @top
// HWLOW-LABEL: hw.module @top
hw.module @top () {
msft.module @top {} () {
msft.instance @foo @Foo() : () -> (i32)
// CHECK: %foo.x = msft.instance @foo @Foo() : () -> i32
// HWLOW: %foo.x = hw.instance "foo" sym @foo @Foo() -> (x: i32)
// HWLOW: %foo.x = hw.instance "foo" sym @foo @Foo<__INST_HIER: none = #hw.param.expr.str.concat<#hw.param.decl.ref<"__INST_HIER">, ".foo">>() -> (x: i32)

%true = hw.constant true
%extern.out = msft.instance @extern @Extern(%true)<param: i1 = false> : (i1) -> i1
// CHECK: %extern.out = msft.instance @extern @Extern(%true) <param: i1 = false> : (i1) -> i1
// HWLOW: %extern.out = hw.instance "extern" sym @extern @Extern<param: i1 = false>(in: %true: i1) -> (out: i1)
msft.output
}

// CHECK-LABEL: msft.module @B {WIDTH = 1 : i64} (%a: i4) -> (nameOfPortInSV: i4) attributes {fileName = "b.sv"} {
// HWLOW-LABEL: hw.module @B(%a: i4) -> (nameOfPortInSV: i4) attributes {output_file = #hw.output_file<"b.sv", includeReplicatedOps>} {
// HWLOW-LABEL: hw.module @B<__INST_HIER: none>(%a: i4) -> (nameOfPortInSV: i4) attributes {output_file = #hw.output_file<"b.sv", includeReplicatedOps>} {
msft.module @B { "WIDTH" = 1 } (%a: i4) -> (nameOfPortInSV: i4) attributes {fileName = "b.sv"} {
%0 = comb.add %a, %a : i4
// CHECK: comb.add %a, %a : i4
Expand Down
3 changes: 2 additions & 1 deletion test/Dialect/MSFT/opt-errors.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ module {

// -----

hw.module @M() {
msft.module @M {} () {
// expected-error @+1 {{Cannot find module definition 'Bar'}}
msft.instance @instance @Bar () : () -> ()
msft.output
}

// -----
Expand Down

0 comments on commit e00cf85

Please sign in to comment.