Skip to content

Commit

Permalink
[nix] seperate verilating and linking for better caching
Browse files Browse the repository at this point in the history
  • Loading branch information
FanShupei authored and sequencer committed Jan 21, 2025
1 parent 1e24195 commit 697766b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
39 changes: 39 additions & 0 deletions nix/t1/conversion/link-verilator-with-dpi.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{ lib
, stdenv
, zlib
}:

{ mainProgram
, verilatorLib
, dpiLibs
}:

assert lib.assertMsg (builtins.typeOf dpiLibs == "list") "dpiLibs should be a list of file path";

stdenv.mkDerivation (rec {
name = mainProgram;
inherit mainProgram;

dontUnpack = true;

propagatedBuildInputs = [ zlib ];

ccArgs = [
"${verilatorLib}/lib/libV${verilatorLib.topModule}.a"
]
++ dpiLibs
++ [
"${verilatorLib}/lib/libverilated.a"
"-lz"
];

buildCommand = ''
mkdir -p $out/bin
$CXX -o $out/bin/$mainProgram ${lib.escapeShellArgs ccArgs}
'';

passthru = {
inherit (verilatorLib) enableTrace;
};
})
24 changes: 9 additions & 15 deletions nix/t1/conversion/sv-to-verilator-emulator.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
}:

{ mainProgram
, topModule
, rtl
, vsrc
, enableTrace ? false
, extraVerilatorArgs ? [ ]
, topModule ? null
, ...
}@overrides:
}:

assert lib.assertMsg (builtins.typeOf vsrc == "list") "vsrc should be a list of file path";

stdenv.mkDerivation (lib.recursiveUpdate
rec {
stdenv.mkDerivation (rec {
name = mainProgram;
inherit mainProgram;

Expand All @@ -35,7 +33,6 @@ rec {
verilatorArgs = [
"--cc"
"--main"
"--exe"
"--timescale"
"1ns/1ps"
"--timing"
Expand Down Expand Up @@ -73,24 +70,22 @@ rec {
mkdir -p $out/share
cp -r obj_dir $out/share/verilated_src
# We can't use -C here because VTestBench.mk is generated with relative path
cd obj_dir
make -j "$NIX_BUILD_CORES" -f VTestBench.mk VTestBench
make -j "$NIX_BUILD_CORES" -f V${topModule}.mk -C obj_dir
runHook postBuild
'';

passthru = {
inherit topModule;
inherit enableTrace;
};

installPhase = ''
runHook preInstall
mkdir -p $out/{include,lib,bin}
cp *.h $out/include
cp *.a $out/lib
cp VTestBench $out/bin/$mainProgram
mkdir -p $out/{include,lib}
cp obj_dir/*.h $out/include
cp obj_dir/*.a $out/lib
runHook postInstall
'';
Expand All @@ -103,5 +98,4 @@ rec {
# we'd like verilator to controll optimization flags, so disable it.
# `-O2` will make gcc build time in verilating extremely long
hardeningDisable = [ "fortify" ];
}
overrides)
})
2 changes: 2 additions & 0 deletions nix/t1/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ lib.makeScope newScope
# * verilatorArgs: Final arguments that pass to the verilator.
sv-to-verilator-emulator = lib.makeOverridable (t1Scope.callPackage ./conversion/sv-to-verilator-emulator.nix { stdenv = moldStdenv; });

link-verilator-with-dpi = lib.makeOverridable (t1Scope.callPackage ./conversion/link-verilator-with-dpi.nix { stdenv = moldStdenv; });

# sv-to-vcs-simulator :: { mainProgram :: String, rtl :: Derivation, enableTrace :: Bool, vcsLinkLibs :: List<String> } -> Derivation
#
# sv-to-vcs-simulator will compile the given rtl, link with path specified in vcsLinksLibs to produce a VCS emulator.
Expand Down
17 changes: 13 additions & 4 deletions nix/t1/t1.nix
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,25 @@ forEachTop (topName: generator: self: {
moduleType = "dpi_${topName}";
};

verilator-emu = t1Scope.sv-to-verilator-emulator {
mainProgram = "${topName}-verilated-simulator";
verilator-emu-lib = t1Scope.sv-to-verilator-emulator {
mainProgram = "${topName}-verilated-lib";
topModule = "TestBench";
rtl = self.rtl;
vsrc = lib.filesystem.listFilesRecursive self.clean-vsrc.outPath;
extraVerilatorArgs = [ "${self.verilator-dpi-lib}/lib/libdpi_${topName}.a" ];
};
verilator-emu-trace = self.verilator-emu.override {
verilator-emu-trace-lib = self.verilator-emu-lib.override {
enableTrace = true;
mainProgram = "${topName}-verilated-trace-lib";
};

verilator-emu = t1Scope.link-verilator-with-dpi {
mainProgram = "${topName}-verilated-simulator";
verilatorLib = self.verilator-emu-lib;
dpiLibs = [ "${self.verilator-dpi-lib}/lib/libdpi_${topName}.a" ];
};
verilator-emu-trace = self.verilator-emu.override {
mainProgram = "${topName}-verilated-trace-simulator";
verilatorLib = self.verilator-emu-trace-lib;
};

# ---------------------------------------------------------------------------------
Expand Down

0 comments on commit 697766b

Please sign in to comment.