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 @@ -311,8 +311,9 @@ class CounterProcWithMaxHelper : public CounterProcHelper {
uint32_t start_value,
uint32_t delta_each_round) override {
BValue enable = pb.StateElement("enable", Value(UBits(1, 1)));
BValue counter = pb.StateElement(kCounterStateElementName,
Value(UBits(start_value, 32)), enable);
BValue counter =
pb.StateElement(kCounterStateElementName, Value(UBits(start_value, 32)),
enable, /*non_synthesizable=*/false);
pb.SendIf(out_channel, pb.Literal(Value::Token()), enable, counter);
pb.Next(counter, pb.Add(counter, pb.Literal(UBits(delta_each_round, 32))),
enable);
Expand Down
3 changes: 2 additions & 1 deletion xls/codegen/register_combining_pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ TEST_F(RegisterCombiningPassTest, DoesntApplyToPredicatedReads) {
auto tok = pb.InitialToken();
auto always_false = pb.Literal(UBits(0, 1));
auto st = pb.StateElement("foo", UBits(1, 32),
/*read_predicate=*/always_false);
/*read_predicate=*/always_false,
/*non_synthesizable=*/false);
auto lit_1 = pb.Literal(UBits(1, 32));
auto always_false_2 = pb.Literal(UBits(0, 1));
auto st_v = pb.Select(always_false_2, /*cases=*/{lit_1, st});
Expand Down
3 changes: 2 additions & 1 deletion xls/codegen_v_1_5/convert_to_block_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ TEST_F(ConvertToBlockTest, ProcWithExplicitStateAccessNextValueStateElement) {
pb.AddOutputChannel("out_ch", p->GetBitsType(32)));

XLS_ASSERT_OK_AND_ASSIGN(StateElement * se,
pb.UnreadStateElement("state", Value(UBits(0, 32))));
pb.UnreadStateElement("state", Value(UBits(0, 32)),
/*non_synthesizable=*/false));
BValue state_read = pb.StateRead(se);
BValue current = pb.Identity(state_read);
BValue add_val = pb.Add(current, pb.Literal(UBits(1, 32)));
Expand Down
3 changes: 2 additions & 1 deletion xls/codegen_v_1_5/pipeline_register_insertion_pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,8 @@ TEST_F(PipelineRegisterInsertionPassTest,
sbb.StartStage(sbb.Literal(UBits(1, 1)), sbb.Literal(UBits(1, 1)));
XLS_ASSERT_OK_AND_ASSIGN(
StateElement * source_se,
source->AppendUnreadStateElement("acc", Value(UBits(0, 32))));
source->AppendUnreadStateElement("acc", Value(UBits(0, 32)),
/*non_synthesizable=*/false));
XLS_ASSERT_OK_AND_ASSIGN(StateRead * source_read,
source->AddStateRead(source_se));
BValue acc = sbb.SourceNode(source_read);
Expand Down
13 changes: 8 additions & 5 deletions xls/contrib/xlscc/generate_fsm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,10 @@ NewFSMGenerator::GenerateNewFSMInvocation(
pb.Literal(xls::ZeroOfType(top_return_type), body_loc));
}

TrackedBValue next_activation_slice_index = pb.StateElement(
"__next_activation_slice",
xls::Value(xls::UBits(0, num_slice_index_bits)), body_loc);
TrackedBValue next_activation_slice_index =
pb.StateElement("__next_activation_slice",
xls::Value(xls::UBits(0, num_slice_index_bits)),
/*non_synthesizable=*/false, body_loc);

TrackedBValue first_slice_index =
pb.Literal(xls::UBits(0, num_slice_index_bits), body_loc);
Expand All @@ -1014,7 +1015,8 @@ NewFSMGenerator::GenerateNewFSMInvocation(

TrackedBValue state_element =
pb.StateElement(absl::StrFormat("__jump_state_%li", jump_slice_index),
xls::Value(xls::UBits(0, 1)), body_loc);
xls::Value(xls::UBits(0, 1)),
/*non_synthesizable=*/false, body_loc);

state_element_by_jump_slice_index[jump_slice_index] = state_element;
}
Expand Down Expand Up @@ -1055,7 +1057,8 @@ NewFSMGenerator::GenerateNewFSMInvocation(

if (state_element.existing_state_element == nullptr) {
xls_state_element = pb.StateElement(
state_element.name, xls::ZeroOfType(state_element.type), body_loc);
state_element.name, xls::ZeroOfType(state_element.type),
/*non_synthesizable=*/false, body_loc);
} else {
xls::StateRead* state_read = pb.proc()->GetStateReadByStateElement(
state_element.existing_state_element);
Expand Down
10 changes: 6 additions & 4 deletions xls/contrib/xlscc/translate_block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ Translator::GenerateOldFSMInvocation(PreparedBlock& prepared,
xls::Value initial_exited_last_activation = xls::Value(xls::UBits(1, 1));
changed_state_last_activation = pb.StateElement(
absl::StrFormat("%s_exited_last_activation", fsm_prefix),
initial_exited_last_activation, body_loc);
initial_exited_last_activation, /*non_synthesizable=*/false, body_loc);
}

// Set up state with wrap-around, if needed
Expand Down Expand Up @@ -1107,8 +1107,9 @@ Translator::GenerateOldFSMInvocation(PreparedBlock& prepared,
initial_args_val};

xls::Value initial_state = xls::Value::Tuple(initial_state_elements);
state_read = pb.StateElement(absl::StrFormat("%s_state", fsm_prefix),
initial_state, body_loc);
state_read =
pb.StateElement(absl::StrFormat("%s_state", fsm_prefix), initial_state,
/*non_synthesizable=*/false, body_loc);
state_index =
pb.TupleIndex(state_read, /*idx=*/0, body_loc,
/*name=*/absl::StrFormat("%s_state_index", fsm_prefix));
Expand Down Expand Up @@ -2092,7 +2093,8 @@ Translator::GenerateIRBlockPrepare(
decl_leaf.leaf_index = i;
}
TrackedBValue elem_bval =
pb.StateElement(decomposed_name, decomposed_value, body_loc);
pb.StateElement(decomposed_name, decomposed_value,
/*non_synthesizable=*/false, body_loc);
xls::StateElement* state_elem =
elem_bval.node()->As<xls::StateRead>()->state_element();
prepared.state_element_for_variable[decl_leaf] = state_elem;
Expand Down
5 changes: 3 additions & 2 deletions xls/contrib/xlscc/unit_tests/fsm_layout_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ class FSMLayoutTest : public XlsccTestBase {
DeclLeaf decl_leaf = {.decl = decl, .leaf_index = i};
const std::string& name = decl->getNameAsString();
std::string decomposed_name = absl::StrFormat("%s_%d", name, i);
NATIVE_BVAL state_read_bval = pb.StateElement(
decomposed_name, decomposed_value, xls::SourceInfo());
NATIVE_BVAL state_read_bval =
pb.StateElement(decomposed_name, decomposed_value,
/*non_synthesizable=*/false, xls::SourceInfo());
state_element_for_static[decl_leaf] =
state_read_bval.node()->As<xls::StateRead>()->state_element();
}
Expand Down
2 changes: 1 addition & 1 deletion xls/dev_tools/extract_state_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ absl::Status ExtractSegmentInto(ProcBuilder& pb, Proc* original,
s->predicate()
? std::make_optional(BValue(old_to_new[*s->predicate()], &pb))
: std::nullopt,
n->loc());
s->state_element()->non_synthesizable(), n->loc());

old_to_new[n] = copied_state.node();
if (send_state_values) {
Expand Down
6 changes: 4 additions & 2 deletions xls/dslx/ir_convert/function_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4306,8 +4306,10 @@ absl::Status FunctionConverter::HandleProcNextFunction(
state_name = absl::StrCat("__", p->identifier());
Value init = f->params().size() > 1 ? initial_element.elements()[i]
: initial_element;
XLS_ASSIGN_OR_RETURN(xls::StateElement * unread_state_element,
builder_ptr->UnreadStateElement(state_name, init));
XLS_ASSIGN_OR_RETURN(
xls::StateElement * unread_state_element,
builder_ptr->UnreadStateElement(state_name, init,
/*non_synthesizable=*/false));
state_name_proto->set_name(state_name);
XLS_ASSIGN_OR_RETURN(auto type, ResolveTypeToIr(p->type_annotation()));
*state_name_proto->mutable_type() = type->ToProto();
Expand Down
18 changes: 18 additions & 0 deletions xls/ir/clone_package_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,23 @@ TEST_F(ClonePackageTest, BasicBlock) {
absl_testing::IsOkAndHolds(m::OutputPort(m::InstantiationOutput("foo"))));
}

TEST_F(ClonePackageTest, CloneProcWithNonSynthesizableState) {
auto p = CreatePackage();
ProcBuilder pb("prc", p.get());
auto st_synth = pb.StateElement("synth", UBits(32, 32));
auto st_non_synth =
pb.StateElement("non_synth", UBits(32, 32), /*non_synthesizable=*/true);
pb.Next(st_synth, st_synth);
pb.Next(st_non_synth, st_non_synth);
XLS_ASSERT_OK(pb.Build());

XLS_ASSERT_OK_AND_ASSIGN(auto p2, ClonePackage(p.get(), "foobar"));
XLS_ASSERT_OK_AND_ASSIGN(Proc * cp, p2->GetProc("prc"));

ASSERT_EQ(cp->GetStateElementCount(), 2);
EXPECT_FALSE(cp->GetStateElement(0)->non_synthesizable());
EXPECT_TRUE(cp->GetStateElement(1)->non_synthesizable());
}

} // namespace
} // namespace xls
12 changes: 8 additions & 4 deletions xls/ir/function_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1507,12 +1507,13 @@ absl::StatusOr<Proc*> ProcBuilder::Build(absl::Span<const BValue> next_state) {
BValue ProcBuilder::StateElement(std::string_view name,
const Value& initial_value,
std::optional<BValue> read_predicate,
bool non_synthesizable,
const SourceInfo& loc) {
absl::StatusOr<xls::StateRead*> state_read = proc()->AppendStateElement(
name, initial_value,
read_predicate.has_value() ? std::make_optional(read_predicate->node())
: std::nullopt,
/*next_state=*/std::nullopt, loc);
/*next_state=*/std::nullopt, non_synthesizable, loc);
if (!state_read.ok()) {
return SetError(absl::StrFormat("Unable to add state element: %s",
state_read.status().message()),
Expand All @@ -1525,22 +1526,25 @@ BValue ProcBuilder::StateElement(std::string_view name,
BValue ProcBuilder::StateElement(std::string_view name,
const ValueBuilder& initial_value,
std::optional<BValue> read_predicate,
bool non_synthesizable,
const SourceInfo& loc) {
absl::StatusOr<Value> built = initial_value.Build();
if (built.ok()) {
return StateElement(name, *built, read_predicate, loc);
return StateElement(name, *built, read_predicate, non_synthesizable, loc);
}
return SetError(absl::StrFormat("Unable to create initial value due to %s",
built.status().ToString()),
loc);
}

absl::StatusOr<class StateElement*> ProcBuilder::UnreadStateElement(
std::string_view name, const Value& initial_value, const SourceInfo& loc) {
std::string_view name, const Value& initial_value, bool non_synthesizable,
const SourceInfo& loc) {
if (ErrorPending()) {
return GetError();
}
return proc()->AppendUnreadStateElement(name, initial_value, loc);
return proc()->AppendUnreadStateElement(name, initial_value,
non_synthesizable, loc);
}

BValue ProcBuilder::StateRead(class StateElement* state_element,
Expand Down
19 changes: 14 additions & 5 deletions xls/ir/function_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -893,34 +893,43 @@ class ProcBuilder : public BuilderBase {
// predicate if provided). Returns the newly added state read.
BValue StateElement(std::string_view name, const Value& initial_value,
std::optional<BValue> read_predicate,
bool non_synthesizable,
const SourceInfo& loc = SourceInfo());
BValue StateElement(std::string_view name, const ValueBuilder& initial_value,
std::optional<BValue> read_predicate,
bool non_synthesizable,
const SourceInfo& loc = SourceInfo());
BValue StateElement(std::string_view name, const Bits& initial_value,
std::optional<BValue> read_predicate,
bool non_synthesizable,
const SourceInfo& loc = SourceInfo()) {
return StateElement(name, Value(initial_value), read_predicate, loc);
return StateElement(name, Value(initial_value), read_predicate,
non_synthesizable, loc);
}
BValue StateElement(std::string_view name, const Value& initial_value,
bool non_synthesizable = false,
const SourceInfo& loc = SourceInfo()) {
return StateElement(name, initial_value, /*read_predicate=*/std::nullopt,
return StateElement(name, initial_value,
/*read_predicate=*/std::nullopt, non_synthesizable,
loc);
}
BValue StateElement(std::string_view name, const ValueBuilder& initial_value,
bool non_synthesizable = false,
const SourceInfo& loc = SourceInfo()) {
return StateElement(name, initial_value, /*read_predicate=*/std::nullopt,
loc);
non_synthesizable, loc);
}
BValue StateElement(std::string_view name, const Bits& initial_value,
bool non_synthesizable = false,
const SourceInfo& loc = SourceInfo()) {
return StateElement(name, Value(initial_value),
/*read_predicate=*/std::nullopt, loc);
/*read_predicate=*/std::nullopt, non_synthesizable,
loc);
}

// Adds a state element to the proc without creating a state read.
absl::StatusOr<class StateElement*> UnreadStateElement(
std::string_view name, const Value& initial_value,
std::string_view name, const Value& initial_value, bool non_synthesizable,
const SourceInfo& loc = SourceInfo());

// Adds a state read node for an existing state element.
Expand Down
12 changes: 8 additions & 4 deletions xls/ir/function_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ TEST(FunctionBuilderTest, UnreadStateElementAndStateRead) {

XLS_ASSERT_OK_AND_ASSIGN(
StateElement * state_element,
b.UnreadStateElement("my_state", Value(UBits(42, 32))));
b.UnreadStateElement("my_state", Value(UBits(42, 32)),
/*non_synthesizable=*/false));

BValue cond = b.Literal(UBits(1, 1));
BValue not_cond = b.Not(cond);
Expand Down Expand Up @@ -991,11 +992,14 @@ TEST(FunctionBuilderTest, StateReadIsDefinitelyEqualTo) {
ProcBuilder pb("the_proc", &p);
BValue pred = pb.Literal(UBits(1, 1));
Value v = Value(UBits(0, 32));
BValue y_pred_label0 = pb.StateElement("y_pred_label0", v, pred);
BValue y_pred_label1 = pb.StateElement("y_pred_label1", v, pred);
BValue y_pred_label0 = pb.StateElement("y_pred_label0", v, pred,
/*non_synthesizable=*/false);
BValue y_pred_label1 = pb.StateElement("y_pred_label1", v, pred,
/*non_synthesizable=*/false);
BValue y_pred_nolabel = pb.StateElement("y_nolabel_nopred", v);
BValue y_nopred_label0 = pb.StateElement("y_nopred_label0", v);
BValue y_pred_label0_copy = pb.StateElement("y_nopred_copy", v, pred);
BValue y_pred_label0_copy = pb.StateElement("y_nopred_copy", v, pred,
/*non_synthesizable=*/false);

y_pred_label0.node()->As<StateRead>()->set_label("label0");
y_pred_label1.node()->As<StateRead>()->set_label("label1");
Expand Down
12 changes: 4 additions & 8 deletions xls/ir/ir_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2246,16 +2246,12 @@ absl::StatusOr<std::unique_ptr<ProcBuilder>> Parser::ParseProcSignature(
/*should_verify=*/false);
}
for (int64_t i = 0; i < state_params.size(); ++i) {
BValue param_bvalue =
builder->StateElement(state_params[i].name, init_values[i]);
bool non_synthesizable =
non_synthesizable_states.contains(state_params[i].name);
BValue param_bvalue = builder->StateElement(
state_params[i].name, init_values[i], non_synthesizable);
(*name_to_value)[state_params[i].name] = param_bvalue;
param_bvalue.node()->SetId(state_params[i].id.value_or(kUnassignedNodeId));
if (non_synthesizable_states.contains(state_params[i].name)) {
param_bvalue.node()
->As<StateRead>()
->state_element()
->SetNonSynthesizable();
}
}

return std::move(builder);
Expand Down
28 changes: 15 additions & 13 deletions xls/ir/proc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,20 @@ absl::Status Proc::RemoveAllStateElements() {
absl::StatusOr<StateRead*> Proc::AppendStateElement(
std::string_view requested_state_name, const Value& init_value,
std::optional<Node*> read_predicate, std::optional<Node*> next_state,
const SourceInfo& loc) {
bool non_synthesizable, const SourceInfo& loc) {
return InsertStateElement(GetStateElementCount(), requested_state_name,
init_value, read_predicate, next_state, loc);
init_value, read_predicate, next_state,
non_synthesizable, loc);
}

absl::StatusOr<StateElement*> Proc::InsertUnreadStateElement(
int64_t index, std::string_view requested_state_name,
const Value& init_value) {
const Value& init_value, bool non_synthesizable) {
XLS_RET_CHECK_LE(index, GetStateElementCount());
std::string state_name = UniquifyStateName(requested_state_name);
state_elements_[state_name] = std::make_unique<StateElement>(
state_name, package()->GetTypeForValue(init_value), init_value);
state_name, package()->GetTypeForValue(init_value), init_value,
non_synthesizable);
StateElement* state_element = state_elements_.at(state_name).get();
state_vec_.insert(state_vec_.begin() + index, state_element);
return state_element;
Expand All @@ -287,10 +289,11 @@ absl::StatusOr<StateRead*> Proc::AddStateRead(StateElement* state_element,
absl::StatusOr<StateRead*> Proc::InsertStateElement(
int64_t index, std::string_view requested_state_name,
const Value& init_value, std::optional<Node*> read_predicate,
std::optional<Node*> next_state, const SourceInfo& loc) {
XLS_ASSIGN_OR_RETURN(
StateElement * state_element,
InsertUnreadStateElement(index, requested_state_name, init_value));
std::optional<Node*> next_state, bool non_synthesizable,
const SourceInfo& loc) {
XLS_ASSIGN_OR_RETURN(StateElement * state_element,
InsertUnreadStateElement(index, requested_state_name,
init_value, non_synthesizable));
XLS_ASSIGN_OR_RETURN(StateRead * state_read,
MakeNodeWithName<StateRead>(
loc, state_element, read_predicate,
Expand Down Expand Up @@ -373,7 +376,8 @@ absl::StatusOr<Proc*> Proc::Clone(
->InsertUnreadStateElement(
cloned_proc->GetStateElementCount(),
remap_name(state_name_remapping, state_element->name()),
state_element->initial_value())
state_element->initial_value(),
state_element->non_synthesizable())
.status());
}
if (is_new_style_proc()) {
Expand Down Expand Up @@ -949,12 +953,10 @@ absl::StatusOr<StateElement*> Proc::TransformStateElement(
StateRead * new_state_read,
AppendStateElement(absl::StrFormat("TEMP_NAME__%s__", orig_name),
init_value, read_predicate,
/*next_state=*/std::nullopt));
/*next_state=*/std::nullopt,
old_state_element->non_synthesizable()));
new_state_read->SetLoc(old_state_read->loc());
new_state_read->set_label(old_state_read->label());
if (old_state_read->state_element()->non_synthesizable()) {
new_state_read->state_element()->SetNonSynthesizable();
}
StateElement* new_state_element = new_state_read->state_element();
std::string temp_name = new_state_element->name();

Expand Down
Loading
Loading