Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ top proc __test_module__Array_0_next<>(__state: bits[32][4], init={[0, 1, 2, 3]}
tuple.3: () = tuple(id=3)
tuple_index.20: token = tuple_index(counted_for.19, index=0, id=20)
assert.24: token = assert(__token, or.23, message="State element written before read in same activation.", id=24)
next_value.25: () = next_value(param=__state, value=new_array, predicate=literal.2, id=25)
next_value.25: () = next_value(state_element=__state, value=new_array, predicate=literal.2, id=25)
tuple.26: () = tuple(id=26)
tuple.27: () = tuple(id=27)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ top proc __test_module__Bool_0_next<>(__state: bits[1], init={0}) {
tuple.3: () = tuple(id=3)
false_val: bits[1] = identity(__state, id=5)
assert.9: token = assert(__token, or.8, message="State element written before read in same activation.", id=9)
next_value.10: () = next_value(param=__state, value=literal.6, predicate=literal.2, id=10)
next_value.10: () = next_value(state_element=__state, value=literal.6, predicate=literal.2, id=10)
tuple.11: () = tuple(id=11)
tuple.12: () = tuple(id=12)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ top proc __test_module__main_0_next<>(__state: (bits[32], bits[1]), init={(0, 0)
tuple.3: () = tuple(id=3)
literal.6: bits[32] = literal(value=0, id=6)
literal.12: bits[32] = literal(value=0, id=12)
next_value.22: () = next_value(param=__state, value=tuple.17, predicate=and.18, id=22)
next_value.22: () = next_value(state_element=__state, value=tuple.17, predicate=and.18, id=22)
literal.24: bits[32] = literal(value=0, id=24)
assert.38: token = assert(assert.34, or.37, message="State element written after write in same activation.", id=38)
or.39: bits[1] = or(and.18, and.31, id=39)
next_value.40: () = next_value(param=__state, value=tuple.29, predicate=and.31, id=40)
next_value.40: () = next_value(state_element=__state, value=tuple.29, predicate=and.31, id=40)
sel.42: () = sel(eq.11, cases=[tuple.41, tuple.23], id=42)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ top proc __test_module__main_0_next<>(__state: bits[32], init={0}) {
y: bits[32] = add(x, literal.6, id=7)
tuple.3: () = tuple(id=3)
assert.10: token = assert(__token, or.9, message="State element written before read in same activation.", id=10)
next_value.11: () = next_value(param=__state, value=y, predicate=literal.2, label="main_write", id=11)
next_value.11: () = next_value(state_element=__state, value=y, predicate=literal.2, label="main_write", id=11)
tuple.12: () = tuple(id=12)
tuple.13: () = tuple(id=13)
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ top proc __test_module__main_0_next<>(__state: bits[32], init={0}) {
tuple.36: () = tuple(id=36)
tuple.50: () = tuple(id=50)
tuple.3: () = tuple(id=3)
next_value.17: () = next_value(param=__state, value=add.11, predicate=and.13, id=17)
next_value.17: () = next_value(state_element=__state, value=add.11, predicate=and.13, id=17)
tuple.18: () = tuple(id=18)
next_value.34: () = next_value(param=__state, value=umul.23, predicate=and.25, id=34)
next_value.34: () = next_value(state_element=__state, value=umul.23, predicate=and.25, id=34)
tuple.35: () = tuple(id=35)
literal.37: bits[1] = literal(value=1, id=37)
assert.46: token = assert(assert.42, or.45, message="State element written after write in same activation.", id=46)
or.47: bits[1] = or(or.33, and.39, id=47)
next_value.48: () = next_value(param=__state, value=current, predicate=and.39, id=48)
next_value.48: () = next_value(state_element=__state, value=current, predicate=and.39, id=48)
tuple.49: () = tuple(id=49)
priority_sel.52: () = priority_sel(concat.51, cases=[tuple.19, tuple.36], default=tuple.50, id=52)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ top proc __test_module__main_0_next<>(__state: bits[1], init={1}) {
tuple.29: () = tuple(id=29)
tuple.58: () = tuple(id=58)
tuple.3: () = tuple(id=3)
next_value.14: () = next_value(param=__state, value=literal.8, predicate=and.10, id=14)
next_value.14: () = next_value(state_element=__state, value=literal.8, predicate=and.10, id=14)
tuple.15: () = tuple(id=15)
next_value.27: () = next_value(param=__state, value=literal.16, predicate=and.18, id=27)
next_value.27: () = next_value(state_element=__state, value=literal.16, predicate=and.18, id=27)
tuple.28: () = tuple(id=28)
eq.31: bits[1] = eq(literal.30, val, id=31)
next_value.43: () = next_value(param=__state, value=literal.32, predicate=and.34, id=43)
next_value.43: () = next_value(state_element=__state, value=literal.32, predicate=and.34, id=43)
tuple.44: () = tuple(id=44)
assert.54: token = assert(assert.50, or.53, message="State element written after write in same activation.", id=54)
or.55: bits[1] = or(or.42, and.47, id=55)
next_value.56: () = next_value(param=__state, value=literal.45, predicate=and.47, id=56)
next_value.56: () = next_value(state_element=__state, value=literal.45, predicate=and.47, id=56)
tuple.57: () = tuple(id=57)
priority_sel.60: () = priority_sel(concat.59, cases=[tuple.29], default=tuple.58, id=60)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ top proc __test_module__main_0_next<>(__val: bits[32], __switch: bits[1], init={
or.46: bits[1] = or(not.45, literal.2, id=46)
not.44: bits[1] = not(switch_val, id=44)
tuple.3: () = tuple(id=3)
next_value.15: () = next_value(param=__val, value=add.10, predicate=and.11, label="EvenWrite", id=15)
next_value.15: () = next_value(state_element=__val, value=add.10, predicate=and.11, label="EvenWrite", id=15)
tuple.16: () = tuple(id=16)
or.24: bits[1] = or(and.6, and.19, id=24)
or.39: bits[1] = or(and.11, and.31, id=39)
next_value.40: () = next_value(param=__val, value=add.29, predicate=and.31, label="OddWrite", id=40)
next_value.40: () = next_value(state_element=__val, value=add.29, predicate=and.31, label="OddWrite", id=40)
tuple.41: () = tuple(id=41)
sel.43: () = sel(switch_val, cases=[tuple.42, tuple.17], id=43)
assert.47: token = assert(assert.38, or.46, message="State element written before read in same activation.", id=47)
next_value.48: () = next_value(param=__switch, value=not.44, predicate=literal.2, id=48)
next_value.48: () = next_value(state_element=__switch, value=not.44, predicate=literal.2, id=48)
tuple.49: () = tuple(id=49)
tuple.50: () = tuple(id=50)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ top proc __test_module__main_0_next<>(__state_0: (bits[32], bits[32]), __state_1
tuple.3: () = tuple(id=3)
literal.13: bits[32] = literal(value=1, id=13)
literal.17: bits[32] = literal(value=0, id=17)
next_value.27: () = next_value(param=__state_0, value=new_a, predicate=literal.2, id=27)
next_value.27: () = next_value(state_element=__state_0, value=new_a, predicate=literal.2, id=27)
tuple.28: () = tuple(id=28)
next_value.32: () = next_value(param=__state_1, value=new_b, predicate=literal.2, id=32)
next_value.32: () = next_value(state_element=__state_1, value=new_b, predicate=literal.2, id=32)
tuple.33: () = tuple(id=33)
assert.38: token = assert(assert.31, or.37, message="State element written before read in same activation.", id=38)
next_value.39: () = next_value(param=__state_2, value=add.35, predicate=literal.2, id=39)
next_value.39: () = next_value(state_element=__state_2, value=add.35, predicate=literal.2, id=39)
tuple.40: () = tuple(id=40)
tuple.41: () = tuple(id=41)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ top proc __test_module__main_0_next<>(__state_0: bits[32], __state_1: bits[32],
or.21: bits[1] = or(not.19, not.20, id=21)
add.15: bits[32] = add(a, literal.14, id=15)
tuple.3: () = tuple(id=3)
next_value.12: () = next_value(param=__state_1, value=add.8, predicate=literal.2, id=12)
next_value.12: () = next_value(state_element=__state_1, value=add.8, predicate=literal.2, id=12)
tuple.13: () = tuple(id=13)
assert.22: token = assert(assert.18, or.21, message="State element written after write in same activation.", id=22)
or.23: bits[1] = or(literal.2, literal.2, id=23)
next_value.24: () = next_value(param=__state_1, value=add.15, predicate=literal.2, id=24)
next_value.24: () = next_value(state_element=__state_1, value=add.15, predicate=literal.2, id=24)
tuple.25: () = tuple(id=25)
tuple.26: () = tuple(id=26)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ top proc __test_module__main_0_next<>(__state_0: bits[32], __state_1: bits[32],
a: bits[32] = identity(__state_0, id=5)
tuple.3: () = tuple(id=3)
assert.7: token = assert(__token, not.6, message="State element written before read in same activation.", id=7)
next_value.8: () = next_value(param=__state_1, value=a, predicate=literal.2, id=8)
next_value.8: () = next_value(state_element=__state_1, value=a, predicate=literal.2, id=8)
tuple.9: () = tuple(id=9)
tuple.10: () = tuple(id=10)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ top proc __test_module__main_0_next<>(__state: bits[32], init={0}) {
or.20: bits[1] = or(not.18, not.19, id=20)
accum__1: bits[32] = add(accum, literal.13, id=14)
tuple.3: () = tuple(id=3)
next_value.11: () = next_value(param=__state, value=accum, predicate=literal.2, id=11)
next_value.11: () = next_value(state_element=__state, value=accum, predicate=literal.2, id=11)
tuple.12: () = tuple(id=12)
assert.21: token = assert(assert.17, or.20, message="State element written after write in same activation.", id=21)
or.22: bits[1] = or(literal.2, literal.2, id=22)
next_value.23: () = next_value(param=__state, value=accum__1, predicate=literal.2, id=23)
next_value.23: () = next_value(state_element=__state, value=accum__1, predicate=literal.2, id=23)
tuple.24: () = tuple(id=24)
tuple.25: () = tuple(id=25)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ top proc __test_module__main_0_next<>(__state: bits[32], init={0}) {
tuple.3: () = tuple(id=3)
or.10: bits[1] = or(literal.2, literal.2, id=10)
assert.19: token = assert(assert.9, or.18, message="State element written before read in same activation.", id=19)
next_value.20: () = next_value(param=__state, value=y, predicate=literal.2, label="main_write", id=20)
next_value.20: () = next_value(state_element=__state, value=y, predicate=literal.2, label="main_write", id=20)
tuple.21: () = tuple(id=21)
tuple.22: () = tuple(id=22)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ top proc __test_module__Counter_0_next<>(__state: bits[32], init={4294967291}) {
add.7: bits[32] = add(current, literal.6, id=7)
tuple.3: () = tuple(id=3)
assert.10: token = assert(__token, or.9, message="State element written before read in same activation.", id=10)
next_value.11: () = next_value(param=__state, value=add.7, predicate=literal.2, id=11)
next_value.11: () = next_value(state_element=__state, value=add.7, predicate=literal.2, id=11)
tuple.12: () = tuple(id=12)
tuple.13: () = tuple(id=13)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ top proc __test_module__String_0_next<>(__state: bits[8][5], init={[104, 101, 10
tuple.3: () = tuple(id=3)
current: bits[8][5] = identity(__state, id=5)
assert.9: token = assert(__token, or.8, message="State element written before read in same activation.", id=9)
next_value.10: () = next_value(param=__state, value=literal.6, predicate=literal.2, id=10)
next_value.10: () = next_value(state_element=__state, value=literal.6, predicate=literal.2, id=10)
tuple.11: () = tuple(id=11)
tuple.12: () = tuple(id=12)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ top proc __test_module__Struct_0_next<>(__state: (bits[32], bits[32]), init={(0,
shift: (bits[32], bits[32]) = tuple(add.8, add.11, id=12)
tuple.3: () = tuple(id=3)
assert.15: token = assert(__token, or.14, message="State element written before read in same activation.", id=15)
next_value.16: () = next_value(param=__state, value=shift, predicate=literal.2, id=16)
next_value.16: () = next_value(state_element=__state, value=shift, predicate=literal.2, id=16)
tuple.17: () = tuple(id=17)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ top proc __test_module__Counter_0_next<>(__state: bits[32], init={0}) {
y: bits[32] = add(x, literal.6, id=7)
tuple.3: () = tuple(id=3)
assert.10: token = assert(__token, or.9, message="State element written before read in same activation.", id=10)
next_value.11: () = next_value(param=__state, value=y, predicate=literal.2, id=11)
next_value.11: () = next_value(state_element=__state, value=y, predicate=literal.2, id=11)
tuple.12: () = tuple(id=12)
tuple.13: () = tuple(id=13)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ top proc __test_module__main_0_next<>(__state: bits[32], init={0}) {
literal.4: bits[32] = literal(value=1, id=4)
tuple.3: () = tuple(id=3)
assert.6: token = assert(__token, not.5, message="State element written before read in same activation.", id=6)
next_value.7: () = next_value(param=__state, value=literal.4, predicate=literal.2, id=7)
next_value.7: () = next_value(state_element=__state, value=literal.4, predicate=literal.2, id=7)
tuple.8: () = tuple(id=8)
tuple.9: () = tuple(id=9)
}
28 changes: 28 additions & 0 deletions xls/interpreter/proc_evaluator_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,34 @@ TEST_P(ProcEvaluatorTestBase, ConditionalNextProc) {
EXPECT_THAT(queue.Read(), Optional(Value(UBits(2, 32))));
}

TEST_P(ProcEvaluatorTestBase, DecoupledNextProc) {
Package package(TestName());
ProcBuilder pb("dec_next", &package);
XLS_ASSERT_OK_AND_ASSIGN(
StateElement * se,
pb.UnreadStateElement("state_element", Value(UBits(42, 32)),
/*non_synthesizable=*/false));
pb.StateRead(se);
pb.Next(se, pb.Literal(UBits(56, 32)));
XLS_ASSERT_OK_AND_ASSIGN(Proc * proc, pb.Build());
XLS_ASSERT_OK(package.SetTop(proc));

std::unique_ptr<ChannelQueueManager> queue_manager =
GetParam().CreateQueueManager(&package);
std::unique_ptr<ProcEvaluator> evaluator =
GetParam().CreateEvaluator(proc, queue_manager.get());

std::unique_ptr<ProcContinuation> continuation = evaluator->NewContinuation(
queue_manager->elaboration().GetUniqueInstance(proc).value());

EXPECT_THAT(continuation->GetState(), ElementsAre(Value(UBits(42, 32))));
EXPECT_THAT(
evaluator->Tick(*continuation),
IsOkAndHolds(TickResult{.execution_state = TickExecutionState::kCompleted,
.channel_instance = std::nullopt,
.progress_made = true}));
EXPECT_THAT(continuation->GetState(), ElementsAre(Value(UBits(56, 32))));
}
TEST_P(ProcEvaluatorTestBase, CollidingNextValuesProc) {
// Create an output-only proc which increments its counter value only every
// other iteration - but also tries to set the counter value to a different
Expand Down
5 changes: 4 additions & 1 deletion xls/interpreter/proc_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ class ProcIrInterpreter : public IrInterpreter {
return SetValueResult(next, Value::Tuple({}));
}
}
(*active_next_values_)[next->state_read()->As<StateRead>()->state_element()]
(*active_next_values_)[next->has_state_read() ? next->state_read()
->As<StateRead>()
->state_element()
: next->state_element()]
.push_back(next);
return SetValueResult(next, Value::Tuple({}));
}
Expand Down
33 changes: 30 additions & 3 deletions xls/ir/ir_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,10 @@ absl::StatusOr<BValue> Parser::ParseNode(
GetEffectiveProcOrError(fb, "next operations only supported in procs",
op_token.pos())
.status());
BValue* state_read = arg_parser.AddKeywordArg<BValue>("param");
std::optional<BValue>* state_read =
arg_parser.AddOptionalKeywordArg<BValue>("param");
std::optional<IdentifierString>* state_element =
arg_parser.AddOptionalKeywordArg<IdentifierString>("state_element");
BValue* value = arg_parser.AddKeywordArg<BValue>("value");
std::optional<BValue>* predicate =
arg_parser.AddOptionalKeywordArg<BValue>("predicate");
Expand All @@ -869,8 +872,32 @@ absl::StatusOr<BValue> Parser::ParseNode(
label_string = label->value().value;
}
XLS_ASSIGN_OR_RETURN(operands, arg_parser.Run(/*arity=*/0));
bvalue = fb->Next(*state_read, *value, *predicate, label_string, *loc,
node_name);
if (state_read->has_value() && state_element->has_value()) {
return absl::InvalidArgumentError(
"next_value cannot specify both 'param' (StateRead) and "
"'state_element'");
}
if (!state_read->has_value() && !state_element->has_value()) {
return absl::InvalidArgumentError(
"next_value must specify either 'param' (StateRead) or "
"'state_element'");
}
if (state_read->has_value()) {
bvalue = fb->Next(state_read->value(), *value, *predicate, label_string,
*loc, node_name);
} else {
Proc* proc = fb->function()->AsProcOrDie();
if (!proc->HasStateElement(state_element->value().value)) {
return absl::InvalidArgumentError(absl::StrFormat(
"State element '%s' not found for next_value @ %s",
state_element->value().value, op_token.pos().ToHumanString()));
}
XLS_ASSIGN_OR_RETURN(
StateElement * state_element,
proc->GetStateElementByName(state_element->value().value));
bvalue = fb->Next(state_element, *value, *predicate, label_string, *loc,
node_name);
}
break;
}
case Op::kCountedFor: {
Expand Down
21 changes: 21 additions & 0 deletions xls/ir/ir_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,27 @@ proc foo( x: bits[32], y: (), z: bits[32], init={42, (), 123}) {
EXPECT_THAT(reads.front()->predicate(), std::nullopt);
}

TEST(IrParserTest, DecoupledNextProc) {
std::string program = R"(
package test

proc decoupled_proc(__state: bits[32], init={0}) {
sr: bits[32] = state_read(state_element=__state)
one: bits[32] = literal(value=1)
add: bits[32] = add(sr, one)
next_state: () = next_value(state_element=__state, value=add)
}
)";
XLS_ASSERT_OK_AND_ASSIGN(auto package, Parser::ParsePackage(program));
XLS_ASSERT_OK_AND_ASSIGN(Proc * proc, package->GetProc("decoupled_proc"));
EXPECT_EQ(proc->GetStateElementCount(), 1);
XLS_ASSERT_OK_AND_ASSIGN(StateElement * state,
proc->GetStateElementByName("__state"));
absl::Span<StateRead* const> reads = proc->GetStateReadsByStateElement(state);
ASSERT_EQ(reads.size(), 1);
EXPECT_THAT(reads.front()->predicate(), std::nullopt);
}

TEST(IrParserTest, ProcWithPredicatedStateRead) {
std::string program = R"(
package test
Expand Down
10 changes: 6 additions & 4 deletions xls/ir/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,12 @@ std::string Node::ToStringInternal(bool include_operand_types) const {
}
case Op::kNext: {
const Next* next = As<Next>();
std::string param_name = next->has_state_read()
? next->state_read()->GetName()
: next->state_element()->name();
args = {absl::StrFormat("param=%s", param_name),
std::string target_arg =
next->has_state_read()
? absl::StrFormat("param=%s", next->state_read()->GetName())
: absl::StrFormat("state_element=%s",
next->state_element()->name());
args = {target_arg,
absl::StrFormat("value=%s", next->value()->GetName())};
std::optional<Node*> predicate = next->predicate();
if (predicate.has_value()) {
Expand Down
13 changes: 10 additions & 3 deletions xls/ir/nodes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -985,9 +985,16 @@ bool StateRead::IsDefinitelyEqualTo(const Node* other) const {

std::vector<Next*> StateRead::GetNextValues() const {
std::vector<Next*> next_values;
for (Node* user : users()) {
if (user->Is<Next>() && user->As<Next>()->state_read() == this) {
next_values.push_back(user->As<Next>());
if (function_base()->AsProcOrDie()->uses_decoupled_next()) {
const auto& next_values_se =
function_base()->AsProcOrDie()->next_values(state_element_);
next_values.insert(next_values.end(), next_values_se.begin(),
next_values_se.end());
} else {
for (Node* user : users()) {
if (user->Is<Next>() && user->As<Next>()->state_read() == this) {
next_values.push_back(user->As<Next>());
}
}
}
return next_values;
Expand Down
Loading
Loading