Skip to content

Commit

Permalink
Refactor interpreter events.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
grebe authored and Copybara-Service committed Jul 3, 2024
1 parent 4afa182 commit 91374e4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions xls/interpreter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions xls/interpreter/block_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include "xls/interpreter/block_evaluator.h"

#include <cstdint>
#include <iterator>
#include <memory>
#include <optional>
#include <random>
#include <string>
#include <utility>
#include <vector>

#include "absl/algorithm/container.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions xls/interpreter/block_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ class ChannelSink {
struct BlockIOResults {
std::vector<absl::flat_hash_map<std::string, Value>> inputs;
std::vector<absl::flat_hash_map<std::string, Value>> outputs;
InterpreterEvents interpreter_events;
};

struct BlockIOResultsAsUint64 {
std::vector<absl::flat_hash_map<std::string, uint64_t>> inputs;
std::vector<absl::flat_hash_map<std::string, uint64_t>> outputs;
InterpreterEvents interpreter_events;
};

class BlockContinuation;
Expand Down
57 changes: 56 additions & 1 deletion xls/interpreter/block_evaluator_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -47,6 +46,7 @@
namespace xls {
namespace {

using ::testing::_;
using ::testing::ElementsAre;
using ::testing::FieldsAre;
using ::testing::HasSubstr;
Expand Down Expand Up @@ -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<ChannelSource> sources{
ChannelSource("x", "x_vld", "x_rdy", 0.5, block)};
XLS_ASSERT_OK(
sources.at(0).SetDataSequence(std::vector<uint64_t>{8, 7, 6, 5, 4}));

std::vector<ChannelSink> sinks{
ChannelSink("y", "y_vld", "y_rdy", 0.1, block),
};

std::vector<absl::flat_hash_map<std::string, uint64_t>> 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<uint64_t> 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());
Expand Down

0 comments on commit 91374e4

Please sign in to comment.