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

[HW] Enable parametric polymorphism for module parameters #8040

Open
wants to merge 4 commits 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
1 change: 1 addition & 0 deletions include/circt/Dialect/HW/HWInstanceImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ LogicalResult verifyOutputs(ArrayAttr resultNames, ArrayAttr moduleResultNames,
/// Verify that the parameter lists of the instance and the module match in
/// terms of length, names, and types.
LogicalResult verifyParameters(ArrayAttr parameters, ArrayAttr moduleParameters,
ArrayRef<Type> resolvedModParametersRefs,
const EmitErrorFn &emitError);

/// Combines verifyReferencedModule, verifyInputs, verifyOutputs, and
Expand Down
25 changes: 22 additions & 3 deletions lib/Dialect/HW/HWInstanceImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ LogicalResult instance_like_impl::verifyOutputs(
LogicalResult
instance_like_impl::verifyParameters(ArrayAttr parameters,
ArrayAttr moduleParameters,
ArrayRef<Type> resolvedModParametersRefs,
const EmitErrorFn &emitError) {
// Check parameters match up.
auto numParameters = parameters.size();
Expand All @@ -164,6 +165,7 @@ instance_like_impl::verifyParameters(ArrayAttr parameters,
for (size_t i = 0; i != numParameters; ++i) {
auto param = cast<ParamDeclAttr>(parameters[i]);
auto modParam = cast<ParamDeclAttr>(moduleParameters[i]);
auto resolvedModParamType = resolvedModParametersRefs[i];

auto paramName = param.getName();
if (paramName != modParam.getName()) {
Expand All @@ -175,7 +177,7 @@ instance_like_impl::verifyParameters(ArrayAttr parameters,
return failure();
}

if (param.getType() != modParam.getType()) {
if (param.getType() != resolvedModParamType) {
emitError([&](auto &diag) {
diag << "parameter " << paramName << " should have type "
<< modParam.getType() << " but has type " << param.getType();
Expand Down Expand Up @@ -227,6 +229,7 @@ LogicalResult instance_like_impl::verifyInstanceOfHWModule(
ArrayAttr::get(instance->getContext(), mod.getOutputNames());

ArrayRef<Type> resolvedModInputTypesRef = getModuleType(module).getInputs();

SmallVector<Type> resolvedModInputTypes;
if (parameters) {
if (failed(instance_like_impl::resolveParametricTypes(
Expand All @@ -235,6 +238,7 @@ LogicalResult instance_like_impl::verifyInstanceOfHWModule(
return failure();
resolvedModInputTypesRef = resolvedModInputTypes;
}

if (failed(instance_like_impl::verifyInputs(
argNames, modArgNames, inputs.getTypes(), resolvedModInputTypesRef,
emitError)))
Expand All @@ -256,9 +260,24 @@ LogicalResult instance_like_impl::verifyInstanceOfHWModule(
return failure();

if (parameters) {
SmallVector<Type> rawModParameters;
SmallVector<Type> resolvedModParameters;

auto modParameters = module->getAttrOfType<ArrayAttr>("parameters");
for (auto param : modParameters) {
auto paramDecl = cast<ParamDeclAttr>(param);
rawModParameters.push_back(paramDecl.getType());
}

// resolve parameters
if (failed(instance_like_impl::resolveParametricTypes(
instance->getLoc(), parameters, rawModParameters,
resolvedModParameters, emitError)))
return failure();

// Check that the parameters are consistent with the referenced module.
ArrayAttr modParameters = module->getAttrOfType<ArrayAttr>("parameters");
if (failed(instance_like_impl::verifyParameters(parameters, modParameters,
if (failed(instance_like_impl::verifyParameters(parameters, modParameters,
resolvedModParameters,
emitError)))
return failure();
}
Expand Down
10 changes: 10 additions & 0 deletions test/Dialect/HW/parameters.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,13 @@ hw.module @NoneTypeParam<p1: none>() {}
hw.module @ParamConcatInst() {
hw.instance "inst" @NoneTypeParam<p1: none = #hw.param.expr.str.concat<"top", ".", "child">>() -> ()
}

// CHECK-LABEL: @param_in_param<
hw.module.extern @param_in_param<RESET_VALUE: !hw.int<#hw.param.decl.ref<"DATA_WIDTH">>, DATA_WIDTH: i32 = 32>(in %input : !hw.int<#hw.param.decl.ref<"DATA_WIDTH">>, out output : !hw.int<#hw.param.decl.ref<"DATA_WIDTH">>)

// CHECK-LABEL: @param_in_param_inst(
// CHECK: %a.output = hw.instance "a" @param_in_param<RESET_VALUE: i96 = 0, DATA_WIDTH: i32 = 96>(input: %input: i96) -> (output: i96)
hw.module @param_in_param_inst(in %input : i96, in %clk : i1, in %rst : i1, out output : i96) {
%a.output = hw.instance "a" @param_in_param<RESET_VALUE: i96 = 0, DATA_WIDTH: i32 = 96>(input: %input: i96) -> (output: i96)
hw.output %a.output : i96
}