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

[ImportVerilog]Dedup module Op #7245

Merged
merged 26 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
46b542b
[ImportVerilog]Dedup module Op
mingzheTerapines Jun 27, 2024
799c179
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jun 28, 2024
5e31646
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 3, 2024
eb3440d
Avoid duplicate mapping
mingzheTerapines Jul 3, 2024
ce1cd2a
clang-tidy
mingzheTerapines Jul 3, 2024
30bf53c
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 4, 2024
c2bd781
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 8, 2024
e833e47
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 9, 2024
86f2a00
Merge remote-tracking branch 'upstream/main' into mingzhe-DedupOpt
mingzheTerapines Jul 10, 2024
3624ef7
Fix tests
mingzheTerapines Jul 10, 2024
47a5f64
Fix tests
mingzheTerapines Jul 10, 2024
ffa0759
Merge branch 'mingzhe-DedupOpt' of https://github.com/Terapines/circt…
mingzheTerapines Jul 10, 2024
8b83371
populate the portsBySyntaxNode once initially when lowering the module
mingzheTerapines Jul 10, 2024
f50714e
populate the portsBySyntaxNode once initially when lowering the module
mingzheTerapines Jul 10, 2024
6b0d974
Merge branch 'mingzhe-DedupOpt' of https://github.com/Terapines/circt…
mingzheTerapines Jul 10, 2024
cef04c7
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 12, 2024
6ce87ea
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 15, 2024
0ef43fc
Update lib/Conversion/ImportVerilog/Structure.cpp
mingzheTerapines Jul 15, 2024
d6959a9
Imporve
mingzheTerapines Jul 15, 2024
4288cdb
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 16, 2024
058b88f
Imporve logic
mingzheTerapines Jul 16, 2024
591c609
Imporve logic
mingzheTerapines Jul 16, 2024
7638e39
Merge branch 'mingzhe-DedupOpt' of https://github.com/Terapines/circt…
mingzheTerapines Jul 16, 2024
b26888f
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 17, 2024
0c08a97
Merge branch 'llvm:main' into mingzhe-DedupOpt
mingzheTerapines Jul 18, 2024
5c7d0c1
Set test unnested
mingzheTerapines Jul 18, 2024
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
5 changes: 5 additions & 0 deletions lib/Conversion/ImportVerilog/ImportVerilogInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ struct Context {
LogicalResult
convertTimingControl(const slang::ast::TimingControl &timingControl);

// Update duplicate port symbol
const slang::ast::PortSymbol *
updatePortsSymbol(ModuleLowering *moduleLowering,
const slang::ast::PortSymbol *port);

mlir::ModuleOp intoModuleOp;
const slang::SourceManager &sourceManager;
SmallDenseMap<slang::BufferID, StringRef> &bufferFilePaths;
Expand Down
65 changes: 65 additions & 0 deletions lib/Conversion/ImportVerilog/Structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ struct MemberVisitor {
// connection for the port.
if (!expr) {
auto *port = con->port.as_if<PortSymbol>();
port = context.updatePortsSymbol(moduleLowering, port);
mingzheTerapines marked this conversation as resolved.
Show resolved Hide resolved
switch (port->direction) {
case slang::ast::ArgumentDirection::In: {
auto refType = moore::RefType::get(
Expand Down Expand Up @@ -188,6 +189,7 @@ struct MemberVisitor {
: context.convertLvalueExpression(*expr);
if (!value)
return failure();
port = context.updatePortsSymbol(moduleLowering, port);
portValues.insert({port, value});
continue;
}
Expand All @@ -203,6 +205,7 @@ struct MemberVisitor {
unsigned offset = 0;
auto i32 = moore::IntType::getInt(context.getContext(), 32);
for (const auto *port : llvm::reverse(multiPort->ports)) {
port = context.updatePortsSymbol(moduleLowering, port);
unsigned width = port->getType().getBitWidth();
auto index = builder.create<moore::ConstantOp>(loc, i32, offset);
auto sliceType = context.convertType(port->getType());
Expand Down Expand Up @@ -460,7 +463,56 @@ ModuleLowering *
Context::convertModuleHeader(const slang::ast::InstanceBodySymbol *module) {
using slang::ast::ArgumentDirection;
using slang::ast::MultiPortSymbol;
using slang::ast::ParameterSymbol;
using slang::ast::PortSymbol;
using slang::ast::TypeParameterSymbol;

auto parameters = module->parameters;
bool hasModuleSame = false;
// If there is already exist a module that has the same name with this
// module ,has the same parent scope and has the same parameters we can
// define this module is a duplicate module
for (auto const &existModule : modules) {
if (module->getDeclaringDefinition() ==
existModule.getFirst()->getDeclaringDefinition()) {
auto moduleParameters = existModule.getFirst()->parameters;
hasModuleSame = true;
for (auto it1 = parameters.begin(), it2 = moduleParameters.begin();
it1 != parameters.end() && it2 != moduleParameters.end();
it1++, it2++) {
// Parameters size different
if (it1 == parameters.end() || it2 == moduleParameters.end()) {
hasModuleSame = false;
break;
}
const auto *para1 = (*it1)->symbol.as_if<ParameterSymbol>();
const auto *para2 = (*it2)->symbol.as_if<ParameterSymbol>();
// Parameters kind different
if ((para1 == nullptr) ^ (para2 == nullptr)) {
hasModuleSame = false;
break;
}
// Compare ParameterSymbol
if (para1 != nullptr) {
hasModuleSame = para1->getValue() == para2->getValue();
}
// Compare TypeParameterSymbol
if (para1 == nullptr) {
auto para1Type = convertType(
(*it1)->symbol.as<TypeParameterSymbol>().getTypeAlias());
auto para2Type = convertType(
(*it2)->symbol.as<TypeParameterSymbol>().getTypeAlias());
hasModuleSame = para1Type == para2Type;
}
if (!hasModuleSame)
break;
}
if (hasModuleSame) {
module = existModule.first;
break;
}
}
}

auto &slot = modules[module];
if (slot)
Expand Down Expand Up @@ -603,3 +655,16 @@ Context::convertModuleBody(const slang::ast::InstanceBodySymbol *module) {

return success();
}

/// Let duplicate port symbol be existing port symbol
const slang::ast::PortSymbol *
Context::updatePortsSymbol(ModuleLowering *moduleLowering,
const slang::ast::PortSymbol *port) {
if (!moduleLowering || !port)
return port;
for (const auto &existPort : moduleLowering->ports) {
if (existPort.ast.name == port->name)
return &existPort.ast;
}
return port;
}
33 changes: 31 additions & 2 deletions test/Conversion/ImportVerilog/basic.sv
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,39 @@ module Empty;
; // empty member
endmodule

// CHECK-LABEL: moore.module @Dedup
module Dedup;
module NestedA(input wire a,
input wire b,
output wire [3:0] c);
endmodule
module NestedB #(parameter p = 32)
(input wire a,
input wire b,
output wire [3:0] c);
endmodule
mingzheTerapines marked this conversation as resolved.
Show resolved Hide resolved
wire [3:0] a;
wire [3:0] b;
wire [3:0] c;
// CHECK-LABEL: moore.instance "insA" @NestedA_0
NestedA insA(.a(a), .c(c));
// CHECK-LABEL: moore.instance "insB" @NestedA_0
NestedA insB(.b(b), .c(c));
// CHECK-LABEL: moore.instance "insC" @NestedB
NestedB insC(.c(c));
// CHECK-LABEL: moore.instance "insD" @NestedB
NestedB insD(.a(a), .b(b), .c(c));
// CHECK-LABEL: moore.instance "insE" @NestedB_1
NestedB #(8) insE(.c(c));
// CHECK-LABEL: moore.module @NestedA_0(in %a : !moore.l1, in %b : !moore.l1, out c : !moore.l4) {
// CHECK-LABEL: moore.module @NestedB_1(in %a : !moore.l1, in %b : !moore.l1, out c : !moore.l4) {
// CHECK-LABEL: moore.module @NestedB(in %a : !moore.l1, in %b : !moore.l1, out c : !moore.l4) {
endmodule

// CHECK-LABEL: moore.module @NestedA() {
// CHECK: moore.instance "NestedB" @NestedB
// CHECK: moore.instance "NestedB" @NestedB_2
// CHECK: }
// CHECK-LABEL: moore.module @NestedB() {
// CHECK-LABEL: moore.module @NestedB_2() {
// CHECK: moore.instance "NestedC" @NestedC
// CHECK: }
// CHECK-LABEL: moore.module @NestedC() {
Expand Down
Loading