Skip to content

Commit

Permalink
[nix] refactor nix to prepare support vcs emulation
Browse files Browse the repository at this point in the history
- add vcs-fhs-env for vcs simulation
- clean up ipemu
- use `nix build --impure .#t1.blastoise.ip.vcs-emu-compiled` to elaborate .so

run with

- module load synopsys/vc_static-V-2023.12
- nix develop --impure .#t1.blastoise.ip.vcs-emu
- pushd difftest && cargo build --release -p online_vcs && popd
- C-c exit build env

- nix run --impure .#vcs-fhs-env
- LD_PRELOAD=/nix/store/m71p7f0nymb19yn1dascklyya2i96jfw-glibc-2.39-52/lib/librt.so.1 LD_LIBRARY_PATH=/opt/synopsys/vc_static/V-2023.12/vcs-mx/linux64/lib/:$LD_LIBRARY_PATH ./difftest/target/release/online_vcs -daidir=/nix/store/b05gwv4l299k0ajvzvvkvc7fj8lkqzh8-blastoise-vcs/share/daidir
  • Loading branch information
sequencer committed Jul 22, 2024
1 parent a036b57 commit 118add7
Show file tree
Hide file tree
Showing 17 changed files with 337 additions and 93 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
ref: ${{ github.event.pull_request.head.sha }}
- name: "Build verilator emulator"
run: |
nix build '.#t1.${{ matrix.config }}.ip.difftest' -L --no-link --cores 64
nix build '.#t1.${{ matrix.config }}.ip.verilator-emu' -L --no-link --cores 64
- name: "Build all testcases"
run: |
# Build testcases with vlen 1024 and vlen 4096
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: "Build verilator emulator with trace"
run: nix build '.#t1.${{ matrix.config }}.ip.difftest-trace' -L --no-link --cores 64
run: nix build '.#t1.${{ matrix.config }}.ip.verilator-emu-trace' -L --no-link --cores 64

test-emit:
if: '! github.event.pull_request.draft'
Expand Down
1 change: 1 addition & 0 deletions difftest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"offline",
"online_dpi",
"online_drive",
"online_vcs",
]
exclude = [
"spike_interfaces"
Expand Down
2 changes: 2 additions & 0 deletions difftest/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let
./offline
./online_dpi
./online_drive
./online_vcs
./test_common
./Cargo.lock
./Cargo.toml
Expand All @@ -43,6 +44,7 @@ let
];

buildFeatures = lib.optionals verilated.enable-trace [ "trace" ];
buildAndTestSubdir = "./online_drive";

env = {
VERILATED_INC_DIR = "${verilated}/include";
Expand Down
10 changes: 10 additions & 0 deletions difftest/online_vcs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "online_vcs"
edition = "2021"
version.workspace = true

[dependencies]
online_dpi = { path = "../online_dpi", features = ["svvpi"] }

[features]
trace = ["online_dpi/trace"]
14 changes: 14 additions & 0 deletions difftest/online_vcs/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::path::Path;
use std::env;

fn main() {
let vcs_lib_dir = env::var("VCS_LIB_DIR").unwrap();
println!("cargo:rustc-link-search=native={}", Path::new(&vcs_lib_dir).display());
let vcs_compiled_lib_dir = env::var("VCS_COMPILED_LIB_DIR").unwrap();
println!("cargo:rustc-env=LD_LIBRARY_PATH={}", Path::new(&vcs_lib_dir).display());
println!("cargo:rustc-link-search=native={}", Path::new(&vcs_compiled_lib_dir).display());
println!("cargo::rustc-link-lib=TestBench");
println!("cargo::rustc-link-lib=vcsnew64");
println!("cargo::rustc-link-lib=vcs_tls");
println!("cargo::rustc-link-lib=vfs");
}
30 changes: 30 additions & 0 deletions difftest/online_vcs/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// force link with online_dpi
extern crate online_dpi;

use std::{
ffi::{c_char, c_int, c_void, CString},
ptr,
};

fn main() {
let c_args: Vec<CString> = std::env::args().map(|arg| CString::new(arg).unwrap()).collect();

let mut c_args_ptr: Vec<*const c_char> = c_args.iter().map(|arg| arg.as_ptr()).collect();
c_args_ptr.push(ptr::null());

let argc = c_args.len() as c_int;
let argv = c_args_ptr.as_ptr() as *mut *mut c_char;

unsafe {
vcs_main(argc, argv);
VcsInit();
VcsSimUntil(1<<31);
}
}

extern "C" {
fn vcs_main(argc: c_int, argv: *mut *mut c_char) -> c_int;
fn VcsInit() -> c_void;
fn VcsSimUntil(c: c_int) -> c_void;
}

69 changes: 69 additions & 0 deletions difftest/vcs-emu.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{ lib
, callPackage
, elaborateConfig

, rustPlatform

, rust-analyzer
, rust-bindgen

, vcStaticInstallPath
, vcs-lib

, cmake
, clang-tools
}:

let
spike_interfaces = callPackage ./spike_interfaces { };

self = rustPlatform.buildRustPackage {
name = "t1-vcs-emu";
src = with lib.fileset; toSource {
root = ./.;
fileset = unions [
./spike_rs
./offline
./online_dpi
./online_drive
./online_vcs
./test_common
./Cargo.lock
./Cargo.toml
];
};

buildInputs = [
spike_interfaces
vcs-lib
];

buildFeatures = lib.optionals vcs-lib.enable-trace [ "trace" ];
buildAndTestSubdir = "./online_vcs";

env = {
VCS_LIB_DIR = "${vcStaticInstallPath}/vcs-mx/linux64/lib";
VCS_COMPILED_LIB_DIR = "${vcs-lib}/lib";
DESIGN_VLEN = elaborateConfig.parameter.vLen;
DESIGN_DLEN = elaborateConfig.parameter.dLen;
};

cargoLock = {
lockFile = ./Cargo.lock;
};

dontUseCmakeConfigure = true;

passthru = {
devShell = self.overrideAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs ++ [
rust-analyzer
rust-bindgen
clang-tools
];
});
inherit spike_interfaces;
};
};
in
self
5 changes: 0 additions & 5 deletions ipemu/src/TestBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ class TestBench(generator: SerializableModuleGenerator[T1, T1Parameter]) extends
| export "DPI-C" function dump_wave;
| function dump_wave(input string file);
|`ifdef VCS
| $$fsdbDumpfile(file);
| $$fsdbDumpvars("+all");
| $$fsdbDumpSVA();
| $$fsdbDumpvars(0);
| $$fsdbDumpon;
|`endif
|`ifdef VERILATOR
| $$dumpfile(file);
Expand Down
5 changes: 5 additions & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ rec {
circt-full = final.callPackage ./pkgs/circt-full.nix { };
rvv-codegen = final.callPackage ./pkgs/rvv-codegen.nix { };
add-determinism = final.callPackage ./pkgs/add-determinism { }; # faster strip-undetereminism
# Using VCS need to set VC_STATIC_HOME and SNPSLMD_LICENSE_FILE to impure env, and add sandbox dir to VC_STATIC_HOME
vcs-fhs-env = final.callPackage ./pkgs/vcs-fhs-env.nix {
vcStaticInstallPath = builtins.getEnv "VC_STATIC_HOME";
snpsLicenseFile = builtins.getEnv "SNPSLMD_LICENSE_FILE";
};

mill = let jre = final.jdk21; in
(prev.mill.override { inherit jre; }).overrideAttrs (_: {
Expand Down
87 changes: 87 additions & 0 deletions nix/pkgs/vcs-fhs-env.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{ buildFHSEnv
# e.g. /opt/synopsys/vc_static/V-2023.12
, vcStaticInstallPath
# e.g. port@addr
, snpsLicenseFile
}:
buildFHSEnv {
name = "vcs-fhs-env";
profile = ''
export TCL_TZ=UTC
export VC_STATIC_HOME=${vcStaticInstallPath}
export VCS_HOME=${vcStaticInstallPath}/vcs-mx
export VCS_TARGET_ARCH=amd64
export VCS_ARCH_OVERRIDE=linux
export VERDI_HOME=${vcStaticInstallPath}/verdi
export NOVAS_HOME=${vcStaticInstallPath}/verdi
export SPYGLASS_HOME=${vcStaticInstallPath}/SG_COMPAT/SPYGLASS_HOME
export SNPS_VERDI_CBUG_LCA=1
export SNPSLMD_LICENSE_FILE=${snpsLicenseFile}
export PATH=${vcStaticInstallPath}/bin:$PATH
export PATH=${vcStaticInstallPath}/verdi/bin:$PATH
export PATH=${vcStaticInstallPath}/vcs-mx/bin:$PATH
export PATH=${vcStaticInstallPath}/SG_COMPAT/SPYGLASS_HOME/bin:$PATH
export LD_LIBRARY_PATH=/usr/lib64/
export LD_LIBRARY_PATH=${vcStaticInstallPath}/verdi/share/PLI/lib/LINUX64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=${vcStaticInstallPath}/verdi/share/NPI/lib/LINUX64:$LD_LIBRARY_PATH
'';
targetPkgs = (ps: with ps; [
libGL
util-linux
libxcrypt-legacy
coreutils-full
ncurses5
gmp5
bzip2
glib
bc
time
elfutils
ncurses5
e2fsprogs
cyrus_sasl
expat
sqlite
(nssmdns.overrideAttrs rec {
version = "0.14.1";
src = fetchFromGitHub {
owner = "avahi";
repo = "nss-mdns";
rev = "v${version}";
hash = "sha256-7RqV0hyfcZ168QfeHVtCJpyP4pI6cMeekJ2hDHNurdA=";
};
})
(libkrb5.overrideAttrs rec {
version = "1.18.2";
src = fetchurl {
url = "https://kerberos.org/dist/krb5/${lib.versions.majorMinor version}/krb5-${version}.tar.gz";
hash = "sha256-xuTJ7BqYFBw/XWbd8aE1VJBQyfq06aRiDumyIIWHOuA=";
};
sourceRoot = "krb5-${version}/src";
})
(gnugrep.overrideAttrs rec {
version = "3.1";
doCheck = false;
src = fetchurl {
url = "mirror://gnu/grep/grep-${version}.tar.xz";
hash = "sha256-22JcerO7PudXs5JqXPqNnhw5ka0kcHqD3eil7yv3oH4=";
};
})
keyutils
graphite2
libpulseaudio
gcc
gnumake
xorg.libX11
xorg.libXft
xorg.libXScrnSaver
xorg.libXext
xorg.libxcb
xorg.libXau
xorg.libXrender
xorg.libXcomposite
xorg.libXi
]);
}
66 changes: 51 additions & 15 deletions nix/t1/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,63 @@ lib.makeScope newScope
ip = rec {
recurseForDerivations = true;

elaborate = innerSelf.callPackage ./elaborate.nix { target = "ip"; /* use-binder = true; */ };
# T1 RTL.
elaborate = innerSelf.callPackage ./elaborate.nix { target = "ip"; };
mlirbc = innerSelf.callPackage ./mlirbc.nix { inherit elaborate; };
rtl = innerSelf.callPackage ./rtl.nix { inherit mlirbc; };

rtl = innerSelf.callPackage ./rtl.nix {
inherit mlirbc;
mfcArgs = lib.escapeShellArgs [
"-O=release"
"--disable-all-randomization"
"--split-verilog"
"--preserve-values=all"
"--strip-debug-info"
"--strip-fir-debug-info"
"--verification-flavor=sva"
"--lowering-options=verifLabels,omitVersionComment,emittedLineLength=240,locationInfoStyle=none"
];
};
omreader = self.omreader-unwrapped.mkWrapper { inherit mlirbc; };

om = innerSelf.callPackage ./om.nix { inherit mlirbc; };

emu-elaborate = innerSelf.callPackage ./elaborate.nix { target = "ipemu"; /* use-binder = true; */ };
emu-elaborate = innerSelf.callPackage ./elaborate.nix { target = "ipemu"; };
emu-mlirbc = innerSelf.callPackage ./mlirbc.nix { elaborate = emu-elaborate; };
emu-omreader = self.omreader-unwrapped.mkWrapper { mlirbc = emu-mlirbc; };
emu-rtl = innerSelf.callPackage ./rtl.nix { mlirbc = emu-mlirbc; };

verilated = innerSelf.callPackage ./verilated.nix { rtl = emu-rtl; };
verilated-trace = innerSelf.callPackage ./verilated.nix { rtl = emu-rtl; enable-trace = true; };

emu = innerSelf.callPackage ./ipemu.nix { rtl = ip.emu-rtl; stdenv = moldStdenv; };
emu-trace = innerSelf.callPackage ./ipemu.nix { rtl = emu-rtl; stdenv = moldStdenv; do-trace = true; };

difftest = innerSelf.callPackage ../../difftest/default.nix { inherit verilated; };
difftest-trace = innerSelf.callPackage ../../difftest/default.nix { verilated = verilated-trace; };
# T1 Verilator Emulator
verilator-emu-omreader = self.omreader-unwrapped.mkWrapper { mlirbc = emu-mlirbc; };
verilator-emu-rtl = innerSelf.callPackage ./rtl.nix {
mlirbc = emu-mlirbc;
mfcArgs = lib.escapeShellArgs [
"-O=release"
"--split-verilog"
"--preserve-values=all"
"--verification-flavor=if-else-fatal"
"--lowering-options=verifLabels,omitVersionComment"
"--strip-debug-info"
];
};
verilator-emu-rtl-verilated = innerSelf.callPackage ./verilated.nix { rtl = verilator-emu-rtl; stdenv = moldStdenv; };
verilator-emu-rtl-verilated-trace = innerSelf.callPackage ./verilated.nix { rtl = verilator-emu-rtl; stdenv = moldStdenv; enable-trace = true; };
verilator-emu = innerSelf.callPackage ../../difftest/default.nix { verilated = verilator-emu-rtl-verilated; };
verilator-emu-trace = innerSelf.callPackage ../../difftest/default.nix { verilated = verilator-emu-rtl-verilated-trace; };

# T1 VCS Emulator
vcs-emu-omreader = self.omreader-unwrapped.mkWrapper { mlirbc = emu-mlirbc; };
vcs-emu-rtl = innerSelf.callPackage ./rtl.nix {
mlirbc = emu-mlirbc;
mfcArgs = lib.escapeShellArgs [
"-O=release"
"--split-verilog"
"--preserve-values=all"
"--verification-flavor=sva"
"--lowering-options=verifLabels,omitVersionComment"
"--strip-debug-info"
];
};
vcs-emu-compiled = innerSelf.callPackage ./vcs.nix { rtl = vcs-emu-rtl; };
vcs-emu-compiled-trace = innerSelf.callPackage ./vcs.nix { rtl = vcs-emu-rtl; enable-trace = true; };
vcs-emu = innerSelf.callPackage ../../difftest/vcs-emu.nix { vcs-lib = vcs-emu-compiled; vcStaticInstallPath = builtins.getEnv "VC_STATIC_HOME"; };
vcs-emu-trace = innerSelf.callPackage ../../difftest/vcs-emu.nix { vcs-lib = vcs-emu-compiled-trace; vcStaticInstallPath = builtins.getEnv "VC_STATIC_HOME"; };
};

subsystem = rec {
Expand Down
2 changes: 1 addition & 1 deletion nix/t1/elaborate.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}:

assert lib.assertMsg
(lib.elem target [ "ip" "ipemu" "subsystem" "subsystememu" ])
(lib.elem target [ "ip" "ipemu" ])
"Unknown elaborate target ${target}";

let
Expand Down
Loading

0 comments on commit 118add7

Please sign in to comment.