Skip to content

Commit 887923d

Browse files
hzellercopybara-github
authored andcommitted
Switch to --testvector_textproto as input to simulation tools.
Instead of the various ad-hoc serializations and different flags such as --args_file/--input_file and --channel_values, use testvector proto universally. Issues: #1645 PiperOrigin-RevId: 694676298
1 parent abc11e1 commit 887923d

File tree

5 files changed

+108
-78
lines changed

5 files changed

+108
-78
lines changed

xls/fuzzer/cpp_run_fuzz.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ absl::StatusOr<std::optional<std::filesystem::path>> MinimizeIr(
7575
{
7676
std::vector<std::string> args = {
7777
std::string{sample_runner_main_path}, "--logtostderr",
78-
"--options_file=ir_minimizer.options.pbtxt", "--args_file=args.txt",
79-
"--input_file=$1"};
78+
"--options_file=ir_minimizer.options.pbtxt",
79+
"--testvector_textproto=testvector.pbtxt", "--input_file=$1"};
8080
const std::filesystem::path test_script = run_dir / "ir_minimizer_test.sh";
8181
XLS_RETURN_IF_ERROR(SetFileContents(
8282
test_script, absl::StrCat("#!/bin/sh\n! ", absl::StrJoin(args, " "))));
@@ -131,7 +131,8 @@ absl::StatusOr<std::optional<std::filesystem::path>> MinimizeIr(
131131
run_dir / absl::StrCat(basename, ".stderr");
132132

133133
std::vector<std::string> args = {find_failing_input_main_path,
134-
"--input_file=args.txt", "sample.ir"};
134+
"--testvector_textproto=testvector.pbtxt",
135+
"sample.ir"};
135136
args.insert(args.end(), extra_args.begin(), extra_args.end());
136137
XLS_ASSIGN_OR_RETURN(
137138
find_failing_input_result,

xls/fuzzer/run_fuzz.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,11 @@ absl::Status RunSample(const Sample& smp, const std::filesystem::path& run_dir,
210210
testvector::SampleInputsProto testvector;
211211
XLS_RETURN_IF_ERROR(smp.FillSampleInputs(&testvector));
212212
XLS_RETURN_IF_ERROR(SetTextProtoFile(testvector_path, testvector));
213-
// TODO(hzeller): This is a preparation, but testvector.pbtxt is not yet
214-
// passed to tools. This is the egg, chicken follows in next change.
215-
// argv.push_back("--testvector_textproto=testvector.pbtxt");
213+
argv.push_back("--testvector_textproto=testvector.pbtxt");
216214

217215
std::filesystem::path args_file_name = run_dir / "args.txt";
218216
XLS_RETURN_IF_ERROR(
219217
SetFileContents(args_file_name, ArgsBatchToText(smp.args_batch())));
220-
argv.push_back("--args_file=args.txt");
221218

222219
std::optional<std::filesystem::path> ir_channel_names_file_name =
223220
std::nullopt;
@@ -226,7 +223,6 @@ absl::Status RunSample(const Sample& smp, const std::filesystem::path& run_dir,
226223
XLS_RETURN_IF_ERROR(
227224
SetFileContents(*ir_channel_names_file_name,
228225
IrChannelNamesToText(smp.ir_channel_names())));
229-
argv.push_back("--ir_channel_names_file=ir_channel_names.txt");
230226
}
231227

232228
argv.push_back("\"$RUNDIR\"");
@@ -246,9 +242,9 @@ cd "$RUNDIR"
246242
VLOG(1) << "Starting to run sample";
247243
VLOG(2) << smp.input_text();
248244
SampleRunner runner(run_dir);
249-
XLS_RETURN_IF_ERROR(runner.RunFromFiles(sample_file_name, options_file_name,
250-
args_file_name,
251-
ir_channel_names_file_name));
245+
XLS_RETURN_IF_ERROR(
246+
runner.RunFromFiles(sample_file_name, options_file_name, args_file_name,
247+
ir_channel_names_file_name, testvector_path));
252248

253249
fuzzer::SampleTimingProto timing = runner.timing();
254250

xls/fuzzer/sample_runner.cc

Lines changed: 80 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ absl::StatusOr<std::vector<dslx::InterpValue>> ParseValues(std::string_view s) {
321321
// Evaluate the IR file with a function as its top and return the result Values.
322322
absl::StatusOr<std::vector<dslx::InterpValue>> EvaluateIrFunction(
323323
const std::filesystem::path& ir_path,
324-
const std::filesystem::path& args_path, bool use_jit,
324+
const std::filesystem::path& testvector_path, bool use_jit,
325325
const SampleOptions& options, const std::filesystem::path& run_dir,
326326
const SampleRunner::Commands& commands) {
327327
std::optional<SampleRunner::Commands::Callable> command =
@@ -332,15 +332,16 @@ absl::StatusOr<std::vector<dslx::InterpValue>> EvaluateIrFunction(
332332

333333
XLS_ASSIGN_OR_RETURN(
334334
std::string results_text,
335-
RunCommand(absl::StrFormat("Evaluating IR file (%s): %s",
336-
(use_jit ? "JIT" : "interpreter"), ir_path),
337-
*command,
338-
{
339-
absl::StrCat("--input_file=", args_path.string()),
340-
absl::StrFormat("--%suse_llvm_jit", use_jit ? "" : "no"),
341-
ir_path,
342-
},
343-
run_dir, options));
335+
RunCommand(
336+
absl::StrFormat("Evaluating IR file (%s): %s",
337+
(use_jit ? "JIT" : "interpreter"), ir_path),
338+
*command,
339+
{
340+
absl::StrCat("--testvector_textproto=", testvector_path.string()),
341+
absl::StrFormat("--%suse_llvm_jit", use_jit ? "" : "no"),
342+
ir_path,
343+
},
344+
run_dir, options));
344345
XLS_RETURN_IF_ERROR(SetFileContents(
345346
absl::StrCat(ir_path.string(), ".results"), results_text));
346347
return ParseValues(results_text);
@@ -397,7 +398,7 @@ absl::StatusOr<std::filesystem::path> OptimizeIr(
397398
absl::StatusOr<std::vector<dslx::InterpValue>> SimulateFunction(
398399
const std::filesystem::path& verilog_path,
399400
const std::filesystem::path& module_sig_path,
400-
const std::filesystem::path& args_path, const SampleOptions& options,
401+
const std::filesystem::path& testvector_path, const SampleOptions& options,
401402
const std::filesystem::path& run_dir,
402403
const SampleRunner::Commands& commands) {
403404
std::optional<SampleRunner::Commands::Callable> command =
@@ -408,7 +409,7 @@ absl::StatusOr<std::vector<dslx::InterpValue>> SimulateFunction(
408409

409410
std::vector<std::string> simulator_args = {
410411
absl::StrCat("--signature_file=", module_sig_path.string()),
411-
absl::StrCat("--args_file=", args_path.string()),
412+
absl::StrCat("--testvector_textproto=", testvector_path.string()),
412413
};
413414
XLS_RETURN_IF_ERROR(CheckSimulator(options.simulator()));
414415
if (!options.simulator().empty()) {
@@ -706,8 +707,8 @@ absl::StatusOr<std::filesystem::path> DslxToIrProc(
706707

707708
absl::StatusOr<absl::flat_hash_map<std::string, std::vector<Value>>>
708709
EvaluateIrProc(const std::filesystem::path& ir_path, int64_t tick_count,
709-
const std::filesystem::path& ir_channel_values_path,
710-
bool use_jit, const SampleOptions& options,
710+
const std::filesystem::path& testvector_proto_path, bool use_jit,
711+
const SampleOptions& options,
711712
const std::filesystem::path& run_dir,
712713
const SampleRunner::Commands& commands) {
713714
std::optional<SampleRunner::Commands::Callable> command =
@@ -721,8 +722,7 @@ EvaluateIrProc(const std::filesystem::path& ir_path, int64_t tick_count,
721722
absl::StrFormat("Evaluating IR file (%s): %s", evaluation_type, ir_path);
722723
std::string_view backend_type = use_jit ? "serial_jit" : "ir_interpreter";
723724
std::vector<std::string> args = {
724-
absl::StrCat("--inputs_for_all_channels=",
725-
ir_channel_values_path.string()),
725+
absl::StrCat("--testvector_textproto=", testvector_proto_path.string()),
726726
absl::StrCat("--ticks=", tick_count),
727727
absl::StrCat("--backend=", backend_type),
728728
ir_path,
@@ -780,7 +780,7 @@ std::string GetOutputChannelToString(
780780
absl::StatusOr<absl::flat_hash_map<std::string, std::vector<Value>>>
781781
SimulateProc(const std::filesystem::path& verilog_path,
782782
const std::filesystem::path& module_sig_path,
783-
const std::filesystem::path& ir_channel_values_path,
783+
const std::filesystem::path& testvector_path,
784784
std::string_view output_channel_counts,
785785
const SampleOptions& options, const std::filesystem::path& run_dir,
786786
const SampleRunner::Commands& commands) {
@@ -792,7 +792,7 @@ SimulateProc(const std::filesystem::path& verilog_path,
792792

793793
std::vector<std::string> simulator_args = {
794794
absl::StrCat("--signature_file=", module_sig_path.string()),
795-
absl::StrCat("--channel_values_file=", ir_channel_values_path.string()),
795+
absl::StrCat("--testvector_textproto=", testvector_path.string()),
796796
absl::StrCat("--output_channel_counts=", output_channel_counts),
797797
};
798798
if (!options.simulator().empty()) {
@@ -853,14 +853,15 @@ absl::Status SampleRunner::Run(const Sample& sample) {
853853
}
854854

855855
return RunFromFiles(input_path, options_path, args_path,
856-
ir_channel_names_path);
856+
ir_channel_names_path, testvector_path);
857857
}
858858

859859
absl::Status SampleRunner::RunFromFiles(
860860
const std::filesystem::path& input_path,
861861
const std::filesystem::path& options_path,
862-
const std::optional<std::filesystem::path>& args_path,
863-
const std::optional<std::filesystem::path>& ir_channel_names_path) {
862+
const std::optional<std::filesystem::path>& args_path, // deprecated
863+
const std::optional<std::filesystem::path>& ir_channel_names_path, // ditto
864+
const std::optional<std::filesystem::path>& testvector_path) {
864865
VLOG(1) << "Running sample in directory " << run_dir_;
865866
VLOG(1) << "Reading sample files.";
866867

@@ -874,10 +875,11 @@ absl::Status SampleRunner::RunFromFiles(
874875
absl::Status status;
875876
switch (options.sample_type()) {
876877
case fuzzer::SampleType::SAMPLE_TYPE_FUNCTION:
877-
status = RunFunction(input_path, options, args_path);
878+
status = RunFunction(input_path, options, std::nullopt, testvector_path);
878879
break;
879880
case fuzzer::SampleType::SAMPLE_TYPE_PROC:
880-
status = RunProc(input_path, options, args_path, ir_channel_names_path);
881+
status = RunProc(input_path, options, std::nullopt, std::nullopt,
882+
testvector_path);
881883
break;
882884
default:
883885
status = absl::InvalidArgumentError(
@@ -901,13 +903,21 @@ absl::Status SampleRunner::RunFromFiles(
901903

902904
absl::Status SampleRunner::RunFunction(
903905
const std::filesystem::path& input_path, const SampleOptions& options,
904-
const std::optional<std::filesystem::path>& args_path) {
906+
const std::optional<std::filesystem::path>& args_path,
907+
const std::optional<std::filesystem::path>& testvector_path) {
905908
XLS_ASSIGN_OR_RETURN(std::string input_text, GetFileContents(input_path));
906909

907910
std::optional<ArgsBatch> args_batch = std::nullopt;
908911
if (args_path.has_value()) {
909912
XLS_ASSIGN_OR_RETURN(std::string args_text, GetFileContents(*args_path));
910913
XLS_ASSIGN_OR_RETURN(args_batch, dslx::ParseArgsBatch(args_text));
914+
} else if (testvector_path.has_value()) {
915+
testvector::SampleInputsProto sample_inputs;
916+
XLS_RETURN_IF_ERROR(ParseTextProtoFile(*testvector_path, &sample_inputs));
917+
ArgsBatch extracted;
918+
XLS_RETURN_IF_ERROR(Sample::ExtractArgsBatch(/* is_proc_samples= */ false,
919+
sample_inputs, extracted));
920+
args_batch = std::move(extracted);
911921
}
912922

913923
absl::flat_hash_map<std::string, std::vector<dslx::InterpValue>> results;
@@ -938,20 +948,20 @@ absl::Status SampleRunner::RunFunction(
938948
XLS_RETURN_IF_ERROR(SetFileContents(ir_path, input_text));
939949
}
940950

941-
if (args_path.has_value()) {
951+
if (args_batch.has_value()) {
942952
Stopwatch t;
943953

944954
// Unconditionally evaluate with the interpreter even if using the JIT. This
945955
// exercises the interpreter and serves as a reference.
946956
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (interpreter)"],
947-
EvaluateIrFunction(ir_path, *args_path, false, options,
948-
run_dir_, commands_));
957+
EvaluateIrFunction(ir_path, *testvector_path, false,
958+
options, run_dir_, commands_));
949959
timing_.set_unoptimized_interpret_ir_ns(
950960
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
951961

952962
if (options.use_jit()) {
953963
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (JIT)"],
954-
EvaluateIrFunction(ir_path, *args_path, true,
964+
EvaluateIrFunction(ir_path, *testvector_path, true,
955965
options, run_dir_, commands_));
956966
timing_.set_unoptimized_jit_ns(
957967
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
@@ -964,19 +974,21 @@ absl::Status SampleRunner::RunFunction(
964974
OptimizeIr(ir_path, options, run_dir_, commands_));
965975
timing_.set_optimize_ns(absl::ToInt64Nanoseconds(t.GetElapsedTime()));
966976

967-
if (args_path.has_value()) {
977+
if (args_batch.has_value()) {
968978
if (options.use_jit()) {
969979
t.Reset();
970-
XLS_ASSIGN_OR_RETURN(results["evaluated opt IR (JIT)"],
971-
EvaluateIrFunction(opt_ir_path, *args_path, true,
972-
options, run_dir_, commands_));
980+
XLS_ASSIGN_OR_RETURN(
981+
results["evaluated opt IR (JIT)"],
982+
EvaluateIrFunction(opt_ir_path, *testvector_path, true, options,
983+
run_dir_, commands_));
973984
timing_.set_optimized_jit_ns(
974985
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
975986
}
976987
t.Reset();
977-
XLS_ASSIGN_OR_RETURN(results["evaluated opt IR (interpreter)"],
978-
EvaluateIrFunction(opt_ir_path, *args_path, false,
979-
options, run_dir_, commands_));
988+
XLS_ASSIGN_OR_RETURN(
989+
results["evaluated opt IR (interpreter)"],
990+
EvaluateIrFunction(opt_ir_path, *testvector_path, false, options,
991+
run_dir_, commands_));
980992
timing_.set_optimized_interpret_ir_ns(
981993
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
982994
}
@@ -989,12 +1001,11 @@ absl::Status SampleRunner::RunFunction(
9891001
timing_.set_codegen_ns(absl::ToInt64Nanoseconds(t.GetElapsedTime()));
9901002

9911003
if (options.simulate()) {
992-
XLS_RET_CHECK(args_path.has_value());
9931004
t.Reset();
9941005
XLS_ASSIGN_OR_RETURN(
9951006
results["simulated"],
996-
SimulateFunction(verilog_path, "module_sig.textproto", *args_path,
997-
options, run_dir_, commands_));
1007+
SimulateFunction(verilog_path, "module_sig.textproto",
1008+
*testvector_path, options, run_dir_, commands_));
9981009
timing_.set_simulate_ns(absl::ToInt64Nanoseconds(t.GetElapsedTime()));
9991010
}
10001011
}
@@ -1009,20 +1020,31 @@ absl::Status SampleRunner::RunFunction(
10091020
absl::Status SampleRunner::RunProc(
10101021
const std::filesystem::path& input_path, const SampleOptions& options,
10111022
const std::optional<std::filesystem::path>& args_path,
1012-
const std::optional<std::filesystem::path>& ir_channel_names_path) {
1023+
const std::optional<std::filesystem::path>& ir_channel_names_path,
1024+
const std::optional<std::filesystem::path>& testvector_path) {
10131025
XLS_ASSIGN_OR_RETURN(std::string input_text, GetFileContents(input_path));
10141026

10151027
std::optional<ArgsBatch> args_batch = std::nullopt;
1028+
std::optional<std::vector<std::string>> ir_channel_names = std::nullopt;
1029+
10161030
if (args_path.has_value()) {
10171031
XLS_ASSIGN_OR_RETURN(std::string args_text, GetFileContents(*args_path));
10181032
XLS_ASSIGN_OR_RETURN(args_batch, dslx::ParseArgsBatch(args_text));
1019-
}
1020-
1021-
std::optional<std::vector<std::string>> ir_channel_names = std::nullopt;
1022-
if (ir_channel_names_path.has_value()) {
1023-
XLS_ASSIGN_OR_RETURN(std::string ir_channel_names_text,
1024-
GetFileContents(*ir_channel_names_path));
1025-
ir_channel_names = ParseIrChannelNames(ir_channel_names_text);
1033+
if (ir_channel_names_path.has_value()) {
1034+
XLS_ASSIGN_OR_RETURN(std::string ir_channel_names_text,
1035+
GetFileContents(*ir_channel_names_path));
1036+
ir_channel_names = ParseIrChannelNames(ir_channel_names_text);
1037+
}
1038+
} else if (testvector_path.has_value()) {
1039+
testvector::SampleInputsProto sample_inputs;
1040+
XLS_RETURN_IF_ERROR(ParseTextProtoFile(*testvector_path, &sample_inputs));
1041+
ArgsBatch extracted_args;
1042+
std::vector<std::string> extracted_channel_names;
1043+
XLS_RETURN_IF_ERROR(Sample::ExtractArgsBatch(/* is_proc_samples= */ true,
1044+
sample_inputs, extracted_args,
1045+
&extracted_channel_names));
1046+
args_batch = std::move(extracted_args);
1047+
ir_channel_names = std::move(extracted_channel_names);
10261048
}
10271049

10281050
std::string ir_channel_values_file_content;
@@ -1102,10 +1124,9 @@ absl::Status SampleRunner::RunProc(
11021124
// Unconditionally evaluate with the interpreter even if using the JIT. This
11031125
// exercises the interpreter and serves as a reference.
11041126
Stopwatch t;
1105-
XLS_ASSIGN_OR_RETURN(
1106-
results["evaluated unopt IR (interpreter)"],
1107-
EvaluateIrProc(ir_path, tick_count, ir_channel_values_path, false,
1108-
options, run_dir_, commands_));
1127+
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (interpreter)"],
1128+
EvaluateIrProc(ir_path, tick_count, *testvector_path,
1129+
false, options, run_dir_, commands_));
11091130
if (!reference.has_value()) {
11101131
reference = results["evaluated unopt IR (interpreter)"];
11111132
}
@@ -1114,10 +1135,9 @@ absl::Status SampleRunner::RunProc(
11141135

11151136
if (options.use_jit()) {
11161137
t.Reset();
1117-
XLS_ASSIGN_OR_RETURN(
1118-
results["evaluated unopt IR (JIT)"],
1119-
EvaluateIrProc(ir_path, tick_count, ir_channel_values_path, true,
1120-
options, run_dir_, commands_));
1138+
XLS_ASSIGN_OR_RETURN(results["evaluated unopt IR (JIT)"],
1139+
EvaluateIrProc(ir_path, tick_count, *testvector_path,
1140+
true, options, run_dir_, commands_));
11211141
timing_.set_unoptimized_jit_ns(
11221142
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
11231143
}
@@ -1135,17 +1155,17 @@ absl::Status SampleRunner::RunProc(
11351155
t.Reset();
11361156
XLS_ASSIGN_OR_RETURN(
11371157
results["evaluated opt IR (JIT)"],
1138-
EvaluateIrProc(*opt_ir_path, tick_count, ir_channel_values_path,
1139-
true, options, run_dir_, commands_));
1158+
EvaluateIrProc(*opt_ir_path, tick_count, *testvector_path, true,
1159+
options, run_dir_, commands_));
11401160
timing_.set_optimized_jit_ns(
11411161
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
11421162
}
11431163

11441164
t.Reset();
11451165
XLS_ASSIGN_OR_RETURN(
11461166
results["evaluated opt IR (interpreter)"],
1147-
EvaluateIrProc(*opt_ir_path, tick_count, ir_channel_values_path,
1148-
false, options, run_dir_, commands_));
1167+
EvaluateIrProc(*opt_ir_path, tick_count, *testvector_path, false,
1168+
options, run_dir_, commands_));
11491169
timing_.set_optimized_interpret_ir_ns(
11501170
absl::ToInt64Nanoseconds(t.GetElapsedTime()));
11511171

@@ -1166,8 +1186,8 @@ absl::Status SampleRunner::RunProc(
11661186
XLS_ASSIGN_OR_RETURN(
11671187
results["simulated"],
11681188
SimulateProc(verilog_path, "module_sig.textproto",
1169-
ir_channel_values_path, output_channel_counts_str,
1170-
options, run_dir_, commands_));
1189+
*testvector_path, output_channel_counts_str, options,
1190+
run_dir_, commands_));
11711191
timing_.set_simulate_ns(absl::ToInt64Nanoseconds(t.GetElapsedTime()));
11721192
}
11731193
}

0 commit comments

Comments
 (0)