Skip to content

Commit

Permalink
[SimToSV] Add include guards to DPI import (#7459)
Browse files Browse the repository at this point in the history
This adds include guards `__CIRCT_DPI_IMPORT_*` to DPI import statements generated in SimToSV. 

Fix #7458.
  • Loading branch information
uenoku authored Aug 8, 2024
1 parent a943626 commit 12822ad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/Conversion/SimToSV/SimToSV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,17 @@ void LowerDPIFunc::lower(sim::DPIFuncOp func) {
auto name = builder.getStringAttr(nameSpace.newName(
func.getSymNameAttr().getValue(), "dpi_import_fragument"));

// Add include guards to avoid duplicate declarations. See Issue 7458.
auto macroDecl = builder.create<sv::MacroDeclOp>(nameSpace.newName(
"__CIRCT_DPI_IMPORT", func.getSymNameAttr().getValue().upper()));
builder.create<emit::FragmentOp>(name, [&]() {
builder.create<sv::FuncDPIImportOp>(func.getSymNameAttr(), StringAttr());
builder.create<sv::IfDefOp>(
macroDecl.getSymNameAttr(), []() {},
[&]() {
builder.create<sv::FuncDPIImportOp>(func.getSymNameAttr(),
StringAttr());
builder.create<sv::MacroDefOp>(macroDecl.getSymNameAttr(), "");
});
});

symbolToFragment.insert({func.getSymNameAttr(), name});
Expand Down
7 changes: 6 additions & 1 deletion test/Conversion/SimToSV/dpi.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

sim.func.dpi @dpi(out arg0: i1, in %arg1: i1, out arg2: i1)
// CHECK: sv.func private @dpi(out arg0 : i1, in %arg1 : i1, out arg2 : i1)
// CHECK-NEXT: sv.macro.decl @__CIRCT_DPI_IMPORT_DPI
// CHECK-NEXT: emit.fragment @dpi_dpi_import_fragument {
// CHECK-NEXT: sv.func.dpi.import @dpi
// CHECK-NEXT: sv.ifdef @__CIRCT_DPI_IMPORT_DPI {
// CHECK-NEXT: } else {
// CHECK-NEXT: sv.func.dpi.import @dpi
// CHECK-NEXT: sv.macro.def @__CIRCT_DPI_IMPORT_DPI ""
// CHECK-NEXT: }
// CHECK-NEXT: }

// VERILOG: import "DPI-C" context function void dpi(
Expand Down
16 changes: 13 additions & 3 deletions test/firtool/dpi.fir
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@

FIRRTL version 4.0.0
circuit DPI:
; CHECK-LABEL: import "DPI-C" context function void clocked_result(
; CHECK-LABEL: `ifndef __CIRCT_DPI_IMPORT_CLOCKED_RESULT
; CHECK-NEXT: import "DPI-C" context function void clocked_result(
; CHECK-NEXT: input byte foo,
; CHECK-NEXT: bar,
; CHECK-NEXT: output byte baz
; CHECK-NEXT: );
; CHECK: `define __CIRCT_DPI_IMPORT_CLOCKED_RESULT
; CHECK-NEXT: `endif

; CHECK-LABEL: import "DPI-C" context function void clocked_void(
; CHECK-LABEL: `ifndef __CIRCT_DPI_IMPORT_CLOCKED_VOID
; CHECK-NEXT: import "DPI-C" context function void clocked_void(
; CHECK-NEXT: input byte in_0,
; CHECK-NEXT: in_1,
; CHECK-NEXT: in_2[]
; CHECK-NEXT: );
; CHECK: `define __CIRCT_DPI_IMPORT_CLOCKED_VOID
; CHECK-NEXT: `endif

; CHECK-LABEL: import "DPI-C" context function void unclocked_result(

; CHECK-LABEL: `ifndef __CIRCT_DPI_IMPORT_UNCLOCKED_RESULT
; CHECK-NEXT: import "DPI-C" context function void unclocked_result(
; CHECK-NEXT: input byte in_0,
; CHECK-NEXT: in_1,
; CHECK-NEXT: output byte out_0
; CHECK-NEXT: );
; CHECK: `define __CIRCT_DPI_IMPORT_UNCLOCKED_RESULT
; CHECK-NEXT: `endif

; CHECK-LABEL: module DPI(
; CHECK: logic [7:0] [[TMP:_.+]];
Expand Down

0 comments on commit 12822ad

Please sign in to comment.