Skip to content

Commit

Permalink
[nix] refactor whole test attribute under emulator
Browse files Browse the repository at this point in the history
Signed-off-by: Avimitin <[email protected]>
  • Loading branch information
Avimitin committed Aug 13, 2024
1 parent 9e98814 commit e2c1bed
Show file tree
Hide file tree
Showing 22 changed files with 602 additions and 451 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/vcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
nix build '.#t1.${{ matrix.config }}.ip.vcs-emu' --impure --no-link --cores 64
- name: "Build all testcases"
run: |
nix build ".#t1.${{ matrix.config }}.ip.cases._all" --max-jobs auto --no-link --cores 64
nix build ".#t1.${{ matrix.config }}.ip.vcs-emu.cases._all" --max-jobs auto --no-link --cores 64
gen-matrix:
name: "Prepare for running testcases"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/verilator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: "Build all testcases"
run: |
# Build testcases with vlen 1024 and vlen 4096
nix build ".#t1.${{ matrix.config }}.ip.cases._all" --max-jobs auto -L --no-link --cores 64
nix build ".#t1.${{ matrix.config }}.ip.verilator-emu.cases._all" --max-jobs auto -L --no-link --cores 64
# In the future, we may choose Verdi for trace, and left verilator trace only for performance evaluation
build-verilator-trace-emulators:
Expand Down
79 changes: 79 additions & 0 deletions difftest/run-emulator.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{ lib, runCommand, zstd, jq }:
emulator:
testCase:

runCommand "run-${emulator.name}-for-${testCase.pname}"
{
name = "${testCase.pname}-emu-result" + (lib.optionalString emulator.enable-trace "-trace");
nativeBuildInputs = [ zstd jq ];
} ''
mkdir -p "$out"
emuDriverArgsArray=(
"--elf-file"
"${testCase}/bin/${testCase.pname}.elf"
"--log-file"
"$out/emu.log"
"--log-level"
"ERROR"
${lib.optionalString emulator.enable-trace "--wave-path"}
${lib.optionalString emulator.enable-trace "$out/wave.fst"}
)
emuDriverArgs="''${emuDriverArgsArray[@]}"
emuDriver="${emulator}/bin/online_drive"
rtlEventOutPath="$out/${testCase.pname}-rtl-event.jsonl"
echo "[nix] Running test case ${testCase.pname} with args $emuDriverArgs"
export RUST_BACKTRACE=full
if ! "$emuDriver" $emuDriverArgs 2> "$rtlEventOutPath"; then
echo -e "\033[0;31m[nix]\033[0m: online driver run failed"
cat $rtlEventOutPath
echo -e "\033[0;31m[nix]\033[0m: Try rerun with '\033[0;34m$emuDriver $emuDriverArgs\033[0m'"
exit 1
fi
echo "[nix] online driver run done"
if [ ! -r "$rtlEventOutPath" ]; then
echo -e "[nix] \033[0;31mInternal Error\033[0m: no $rtlEventOutPath found in output"
exit 1
fi
if ! jq --stream -c -e '.[]' "$rtlEventOutPath" >/dev/null 2>&1; then
echo -e "[nix] \033[0;31mInternal Error\033[0m: invalid JSON file $rtlEventOutPath, showing original file:"
echo "--------------------------------------------"
cat $rtlEventOutPath
echo "--------------------------------------------"
exit 1
fi
set +e
"${emulator}/bin/offline" \
--elf-file ${testCase}/bin/${testCase.pname}.elf \
--log-file $rtlEventOutPath \
--log-level ERROR &> $out/offline-check-journal
printf "$?" > $out/offline-check-status
if [ "$(cat $out/offline-check-status)" != "0" ]; then
echo "[nix] Offline check FAIL"
else
echo "[nix] Offline check PASS"
fi
set -e
echo "[nix] compressing event log"
zstd $rtlEventOutPath -o $rtlEventOutPath.zstd
rm $rtlEventOutPath
if [ -r perf.txt ]; then
mv perf.txt $out/
fi
${lib.optionalString emulator.enable-trace ''
if [ ! -r "$out/wave.fst" ]; then
echo -e "[nix] \033[0;31mInternal Error\033[0m: waveform not found in output"
exit 1
fi
''}
''
18 changes: 17 additions & 1 deletion difftest/verilator.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
, verilated
, cmake
, clang-tools

, rtlDesignMetadata
}:

let
Expand Down Expand Up @@ -73,7 +75,9 @@ let
clang-tools
];
});
inherit libspike_interfaces;

inherit libspike_interfaces rtlDesignMetadata;
inherit (verilated) enable-trace;

# enable debug info for difftest itself and libspike
withDebug = self.overrideAttrs (old: {
Expand All @@ -84,6 +88,18 @@ let
};
dontStrip = true;
});

# Here is an curry function:
#
# * run-emulator.nix return type
# run-emulator :: { callPackage Args... } -> emulatorDerivation -> testCase -> runCommandDerivation
#
# * runEmulation attribute type:
# runEmulation :: testCase -> runCommandDerivation
#
runEmulation = (callPackage ./run-emulator.nix { }) self;

cases = callPackage ../tests { emulator = self; };
};
};
in
Expand Down
1 change: 1 addition & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@ rec {
};

t1 = final.callPackage ./t1 { };
t1-rocket = final.callPackage ../t1rocketemu { };
}
24 changes: 8 additions & 16 deletions nix/t1/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ lib.makeScope newScope

rocketv = self.callPackage ../../rocketemu { };

t1rocketemu = self.callPackage ../../t1rocketemu { };
t1rocket = self.callPackage ../../t1rocketemu { };

omreader-unwrapped = self.callPackage ./omreader.nix { };
submodules = self.callPackage ./submodules.nix { };
Expand Down Expand Up @@ -84,23 +84,14 @@ lib.makeScope newScope
omGet = args: lib.fileContents (runCommand "get-${args}" { } ''
${emu-omreader}/bin/omreader ${args} > $out
'');
rtlDesignMetadata = {
rtlDesignMetadata = rec {
march = omGet "march";
extensions = builtins.fromJSON (omGet "extensionsJson");
vlen = omGet "vlen";
dlen = omGet "dlen";
xlen = if (lib.hasPrefix "rv32" march) then 32 else 64;
};

cases = innerSelf.callPackage ../../tests {
inherit (ip) verilator-emu verilator-emu-trace vcs-emu vcs-emu-trace rtlDesignMetadata;
};

# for the convenience to use x86 cases on non-x86 machines, avoiding the extra build time
cases-x86 =
if system == "x86-64-linux"
then self.cases
else pkgsX86.t1."${configName}".cases;

emu-elaborate = innerSelf.callPackage ./elaborate.nix { target = "ipemu"; };
emu-mlirbc = innerSelf.callPackage ./mlirbc.nix { elaborate = emu-elaborate; };

Expand All @@ -119,8 +110,9 @@ lib.makeScope newScope
};
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/verilator.nix { verilated = verilator-emu-rtl-verilated; };
verilator-emu-trace = innerSelf.callPackage ../../difftest/verilator.nix { verilated = verilator-emu-rtl-verilated-trace; };

verilator-emu = innerSelf.callPackage ../../difftest/verilator.nix { inherit rtlDesignMetadata; verilated = verilator-emu-rtl-verilated; };
verilator-emu-trace = innerSelf.callPackage ../../difftest/verilator.nix { inherit rtlDesignMetadata; verilated = verilator-emu-rtl-verilated-trace; };

# T1 VCS Emulator
vcs-emu-omreader = self.omreader-unwrapped.mkWrapper { mlirbc = emu-mlirbc; };
Expand All @@ -137,8 +129,8 @@ lib.makeScope newScope
};
vcs-dpi-lib = innerSelf.callPackage ../../difftest/online_vcs { };
vcs-dpi-lib-trace = vcs-dpi-lib.override { enable-trace = true; };
vcs-emu = innerSelf.callPackage ./vcs.nix { inherit vcs-dpi-lib; rtl = vcs-emu-rtl; };
vcs-emu-trace = innerSelf.callPackage ./vcs.nix { vcs-dpi-lib = vcs-dpi-lib-trace; rtl = vcs-emu-rtl; };
vcs-emu = innerSelf.callPackage ./vcs.nix { inherit vcs-dpi-lib rtlDesignMetadata; rtl = vcs-emu-rtl; };
vcs-emu-trace = innerSelf.callPackage ./vcs.nix { inherit rtlDesignMetadata; vcs-dpi-lib = vcs-dpi-lib-trace; rtl = vcs-emu-rtl; };
};

subsystem = rec {
Expand Down
74 changes: 74 additions & 0 deletions nix/t1/run-vcs-emulation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{ lib, runCommand, zstd, jq }:
emulator:
testCase:

runCommand "run-${emulator.name}-for-${testCase.pname}"
{
name = "${testCase.pname}-vcs-result" + (lib.optionalString emulator.enable-trace "-trace");
nativeBuildInputs = [ zstd jq ];
__noChroot = true;
} ''
mkdir -p "$out"
emuDriverArgsArray=(
"--elf-file"
"${testCase}/bin/${testCase.pname}.elf"
${lib.optionalString emulator.enable-trace "--wave-path"}
${lib.optionalString emulator.enable-trace "${testCase.pname}.fsdb"}
)
emuDriverArgs="''${emuDriverArgsArray[@]}"
emuDriver="${emulator}/bin/t1-vcs-simulator"
rtlEventOutPath="$out/${testCase.pname}-rtl-event.jsonl"
echo "[nix] Running VCS ${testCase.pname} with args $emuDriverArgs"
export RUST_BACKTRACE=full
if ! "$emuDriver" $emuDriverArgs >/dev/null 2>"$rtlEventOutPath"; then
echo -e "\033[0;31m[nix]\033[0m: online driver run failed"
cat $rtlEventOutPath
echo -e "\033[0;31m[nix]\033[0m: Try rerun with '\033[0;34m$emuDriver $emuDriverArgs\033[0m'"
exit 1
fi
echo "[nix] VCS run done"
if [ ! -r "$rtlEventOutPath" ]; then
echo -e "[nix] \033[0;31mInternal Error\033[0m: no $rtlEventOutPath found in output"
exit 1
fi
if ! jq --stream -c -e '.[]' "$rtlEventOutPath" >/dev/null 2>&1; then
echo -e "[nix] \033[0;31mInternal Error\033[0m: invalid JSON file $rtlEventOutPath, showing original file:"
echo "--------------------------------------------"
cat $rtlEventOutPath
echo "--------------------------------------------"
exit 1
fi
set +e
"${emulator}/bin/offline" \
--elf-file ${testCase}/bin/${testCase.pname}.elf \
--log-file $rtlEventOutPath \
--log-level ERROR &> $out/offline-check-journal
printf "$?" > $out/offline-check-status
if [ "$(cat $out/offline-check-status)" != "0" ]; then
echo "[nix] Offline check FAIL"
else
echo "[nix] Offline check PASS"
fi
set -e
echo "[nix] compressing event log"
zstd $rtlEventOutPath -o $rtlEventOutPath.zstd
rm $rtlEventOutPath
if [ -r perf.txt ]; then
mv perf.txt $out/
fi
${lib.optionalString emulator.enable-trace ''
cp -v ${testCase.pname}.fsdb "$out"
cp -vr ${emulator}/lib/t1-vcs-simulator.daidir "$out"
''}
''
Loading

0 comments on commit e2c1bed

Please sign in to comment.