Skip to content

Commit

Permalink
Fix connection of interface arrays to ports with different declared d…
Browse files Browse the repository at this point in the history
…imensions
  • Loading branch information
MikePopoloski committed Oct 19, 2024
1 parent 34148f2 commit c92f1a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
23 changes: 21 additions & 2 deletions source/ast/symbols/PortSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,25 @@ class PortConnectionBuilder {
return true;
}

const Symbol* rewireIfaceArrayIndices(const Symbol* sym, std::string_view name,
SourceLocation loc,
std::span<const ConstantRange> portDims) {
if (!sym || sym->kind != SymbolKind::InstanceArray)
return sym;

// The port dimensions are guaranteed to have the same count and width as
// the instance array dimensions but their indices may differ, so this
// function creates a new array with the correct range.
SLANG_ASSERT(!portDims.empty());
auto subPortDims = portDims.subspan(1);

SmallVector<const Symbol*> newElems;
for (auto elem : sym->as<InstanceArraySymbol>().elements)
newElems.push_back(rewireIfaceArrayIndices(elem, ""sv, loc, subPortDims));

return comp.emplace<InstanceArraySymbol>(comp, name, loc, newElems.copy(comp), portDims[0]);
}

PortConnection::IfaceConn getInterfaceConn(ASTContext& context, const InterfacePortSymbol& port,
const ExpressionSyntax& syntax) {
SLANG_ASSERT(!port.isInvalid());
Expand Down Expand Up @@ -1169,7 +1188,7 @@ class PortConnectionBuilder {
// Make the connection if the dimensions match exactly what the port is expecting.
const Symbol* symbol = expr->as<ArbitrarySymbolExpression>().symbol;
if (areDimSizesEqual(*portDims, dims))
return {symbol, modport};
return {rewireIfaceArrayIndices(symbol, port.name, port.location, *portDims), modport};

// Otherwise, if the instance being instantiated is part of an array of instances *and*
// the symbol we're connecting to is an array of interfaces, we need to check to see whether
Expand Down Expand Up @@ -1198,7 +1217,7 @@ class PortConnectionBuilder {
symbol = array.elements[size_t(index)];
}

return {symbol, modport};
return {rewireIfaceArrayIndices(symbol, port.name, port.location, *portDims), modport};
}

auto& diag = scope.addDiag(diag::PortConnDimensionsMismatch, syntax.sourceRange())
Expand Down
22 changes: 22 additions & 0 deletions tests/unittests/ast/InterfaceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,25 @@ endmodule
REQUIRE(diags.size() == 1);
CHECK(diags[0].code == diag::UndeclaredIdentifier);
}

TEST_CASE("Iface array with different declared indices regress -- GH #1152") {
auto tree = SyntaxTree::fromText(R"(
interface bus();
logic a;
logic b;
endinterface
module submodule(bus iface [3:2]);
assign iface[2].a = iface[3].b;
endmodule
module top();
bus iface[1:0]();
submodule inst(iface);
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
}

0 comments on commit c92f1a3

Please sign in to comment.