From 91374e4c8742b46487ea270efca40517302f82f4 Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Wed, 3 Jul 2024 15:15:50 -0700 Subject: [PATCH] Refactor interpreter events. Add interpreter events to `BlockIOResults`, the data structure returned by the channelized interface to block evaluators. The channelized interface accumulates events from the underlying continuation. PiperOrigin-RevId: 649213186 --- xls/interpreter/BUILD | 1 + xls/interpreter/block_evaluator.cc | 10 ++++ xls/interpreter/block_evaluator.h | 2 + xls/interpreter/block_evaluator_test_base.cc | 57 +++++++++++++++++++- 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/xls/interpreter/BUILD b/xls/interpreter/BUILD index c1758ee6cd..764d720894 100644 --- a/xls/interpreter/BUILD +++ b/xls/interpreter/BUILD @@ -71,6 +71,7 @@ cc_library( srcs = ["block_evaluator.cc"], hdrs = ["block_evaluator.h"], deps = [ + "@com_google_absl//absl/algorithm:container", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/log", "@com_google_absl//absl/log:check", diff --git a/xls/interpreter/block_evaluator.cc b/xls/interpreter/block_evaluator.cc index 3fe5195093..585782ef94 100644 --- a/xls/interpreter/block_evaluator.cc +++ b/xls/interpreter/block_evaluator.cc @@ -15,6 +15,7 @@ #include "xls/interpreter/block_evaluator.h" #include +#include #include #include #include @@ -22,6 +23,7 @@ #include #include +#include "absl/algorithm/container.h" #include "absl/container/flat_hash_map.h" #include "absl/log/check.h" #include "absl/log/log.h" @@ -441,6 +443,12 @@ BlockEvaluator::EvaluateChannelizedSequentialBlock( block_io_results.inputs.push_back(std::move(input_set)); block_io_results.outputs.push_back(continuation->output_ports()); + absl::c_copy( + continuation->events().assert_msgs, + std::back_inserter(block_io_results.interpreter_events.assert_msgs)); + absl::c_copy( + continuation->events().trace_msgs, + std::back_inserter(block_io_results.interpreter_events.trace_msgs)); } return block_io_results; @@ -488,6 +496,8 @@ BlockEvaluator::EvaluateChannelizedSequentialBlockWithUint64( ConvertInputsToUint64(input_value_set, block)); block_io_result_as_uint64.inputs.push_back(std::move(input_set)); } + block_io_result_as_uint64.interpreter_events = + std::move(block_io_result.interpreter_events); return block_io_result_as_uint64; } diff --git a/xls/interpreter/block_evaluator.h b/xls/interpreter/block_evaluator.h index 651d31141f..fca6aa1587 100644 --- a/xls/interpreter/block_evaluator.h +++ b/xls/interpreter/block_evaluator.h @@ -200,11 +200,13 @@ class ChannelSink { struct BlockIOResults { std::vector> inputs; std::vector> outputs; + InterpreterEvents interpreter_events; }; struct BlockIOResultsAsUint64 { std::vector> inputs; std::vector> outputs; + InterpreterEvents interpreter_events; }; class BlockContinuation; diff --git a/xls/interpreter/block_evaluator_test_base.cc b/xls/interpreter/block_evaluator_test_base.cc index d26e6e9989..5d2dacf3ff 100644 --- a/xls/interpreter/block_evaluator_test_base.cc +++ b/xls/interpreter/block_evaluator_test_base.cc @@ -36,7 +36,6 @@ #include "xls/ir/bits.h" #include "xls/ir/block.h" #include "xls/ir/channel.h" -#include "xls/ir/elaboration.h" #include "xls/ir/format_preference.h" #include "xls/ir/function_builder.h" #include "xls/ir/instantiation.h" @@ -47,6 +46,7 @@ namespace xls { namespace { +using ::testing::_; using ::testing::ElementsAre; using ::testing::FieldsAre; using ::testing::HasSubstr; @@ -1270,6 +1270,61 @@ TEST_P(BlockEvaluatorTest, InterpreterEventsCaptured) { } } +TEST_P(BlockEvaluatorTest, InterpreterEventsCapturedByChannelizedInterface) { + auto package = CreatePackage(); + BlockBuilder b(TestName(), package.get()); + XLS_ASSERT_OK(b.block()->AddClockPort("clk")); + + BValue x = b.InputPort("x", package->GetBitsType(32)); + BValue x_vld = b.InputPort("x_vld", package->GetBitsType(1)); + BValue y_rdy = b.InputPort("y_rdy", package->GetBitsType(1)); + b.OutputPort("y", x); + b.OutputPort("x_rdy", y_rdy); + b.OutputPort("y_vld", x_vld); + + BValue tkn = b.Literal(Value::Token()); + BValue fire = b.And({x_vld, y_rdy}); + BValue assert_cond = b.UGt(x, b.Literal(Value(UBits(5, 32)))); + BValue fire_implies_cond = b.Or(b.Not(fire), assert_cond); + BValue assertion = b.Assert(tkn, fire_implies_cond, "foo"); + b.Trace(assertion, fire, {x}, + {"I'm emphasizing that x is ", FormatPreference::kDefault}, + /*verbosity=*/3); + + XLS_ASSERT_OK_AND_ASSIGN(Block * block, b.Build()); + + { + std::vector sources{ + ChannelSource("x", "x_vld", "x_rdy", 0.5, block)}; + XLS_ASSERT_OK( + sources.at(0).SetDataSequence(std::vector{8, 7, 6, 5, 4})); + + std::vector sinks{ + ChannelSink("y", "y_vld", "y_rdy", 0.1, block), + }; + + std::vector> inputs; + inputs.resize(100); + + BlockIOResultsAsUint64 block_io; + XLS_ASSERT_OK_AND_ASSIGN( + block_io, + evaluator().EvaluateChannelizedSequentialBlockWithUint64( + block, absl::MakeSpan(sources), absl::MakeSpan(sinks), inputs)); + + XLS_ASSERT_OK_AND_ASSIGN(std::vector output_sequence, + sinks.at(0).GetOutputSequenceAsUint64()); + EXPECT_GT(block_io.outputs.size(), output_sequence.size()); + EXPECT_THAT(output_sequence, ElementsAre(8, 7, 6, 5, 4)); + EXPECT_THAT(block_io.interpreter_events.assert_msgs, + // Assertion fails for inputs 5 and 4. + ElementsAre("foo", "foo")); + EXPECT_THAT(block_io.interpreter_events.trace_msgs, + Contains(FieldsAre(HasSubstr("I'm emphasizing that x is "), _)) + .Times(5)); + } +} + TEST_P(BlockEvaluatorTest, TupleInputOutput) { auto package = CreatePackage(); BlockBuilder b(TestName(), package.get());