8000 [xlscc] Fixes for static variables by copybara-service[bot] · Pull Request #5520 · google/xls · GitHub
[go: up one dir, main page]

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
37 changes: 35 additions & 2 deletions xls/contrib/xlscc/generate_fsm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,21 @@ absl::Status NewFSMGenerator::LayoutNewFSMStateElements(
return a_name < b_name;
});

absl::flat_hash_map<const clang::NamedDecl*, int64_t>
static_read_slice_by_decl;
int64_t static_slice_idx = 0;
for (const GeneratedFunctionSlice& slice : func.slices) {
for (const clang::NamedDecl* decl : slice.static_values) {
auto [it, inserted] =
static_read_slice_by_decl.emplace(decl, static_slice_idx);
if (!inserted) {
it->second = std::max(it->second, static_slice_idx);
}
}
++static_slice_idx;
}

absl::flat_hash_map<int64_t, int64_t> static_live_until_slice_by_elem;
for (const DeclLeaf& decl_leaf : static_decls) {
xls::StateElement* existing_state_element =
state_element_for_static.at(decl_leaf);
Expand All @@ -539,8 +554,13 @@ absl::Status NewFSMGenerator::LayoutNewFSMStateElements(
.existing_state_element = existing_state_element,
};
layout.state_elements.push_back(state_element);
state_element_indices_by_decl[decl_leaf].push_back(
layout.state_elements.size() - 1);
const int64_t elem_idx =
static_cast<int64_t>(layout.state_elements.size()) - 1;
state_element_indices_by_decl[decl_leaf].push_back(elem_idx);
if (auto it = static_read_slice_by_decl.find(decl_leaf.decl);
it != static_read_slice_by_decl.end() && it->second > 0) {
static_live_until_slice_by_elem[elem_idx] = it->second;
}
}

absl::flat_hash_map<const ContinuationValue*, absl::flat_hash_set<int64_t>>
Expand All @@ -566,6 +586,19 @@ absl::Status NewFSMGenerator::LayoutNewFSMStateElements(
// A state element can only be used once in a given transition
absl::flat_hash_set<int64_t> used_state_element_indices;

// Static state elements hold live state from the start of each activation
// until the slice that reads them runs, so earlier slices cannot overwrite
// them with unrelated continuation values.
for (const auto& [elem_idx, read_slice_idx] :
static_live_until_slice_by_elem) {
if (state.slice_index < read_slice_idx) {
used_state_element_indices.insert(elem_idx);
for (const ContinuationValue* value : state.values_to_save) {
disallowed_elements_by_value[value].insert(elem_idx);
}
}
}

// Mark reserved elements
for (const ContinuationValue* value : state.values_to_save) {
if (!layout.state_element_by_continuation_value.contains(value)) {
Expand Down
27 changes: 20 additions & 7 deletions xls/contrib/xlscc/translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,7 @@ absl::Status Translator::PropagateVariables(TranslationContext& from,
// Prevent extra selects from being generated in future propagations
from.variables.at(name) = prepared;
}
} else if (to.sf->static_values.contains(name) ||
from.propagate_declarations) {
} else if (from.propagate_declarations) {
to.variables[name] = from.variables.at(name);
}
}
Expand Down Expand Up @@ -2760,14 +2759,21 @@ absl::Status Translator::Assign(const clang::Expr* lvalue, const CValue& rvalue,
absl::Status Translator::DeclareVariable(const clang::NamedDecl* lvalue,
const CValue& rvalue,
const xls::SourceInfo& loc,
bool check_unique_ids) {
bool check_unique_ids,
bool declare_up_stack) {
unique_decl_ids_.insert(lvalue);
context().sf->declaration_order_by_name_[lvalue] =
++context().sf->declaration_count;

XLS_RETURN_IF_ERROR(ValidateLValue(rvalue.lvalue(), loc));

context().variables[lvalue] = rvalue;
for (TranslationContext& context : context_stack_) {
context.variables[lvalue] = rvalue;

if (!context.propagate_up || !declare_up_stack) {
break;
}
}
return absl::OkStatus();
}

Expand All @@ -2779,12 +2785,17 @@ absl::Status Translator::DeclareStatic(
context().sf->static_values.at(lvalue) == init,
loc);

// Avoid multiple declaration in unrolled loops
if (context().sf->static_values.contains(lvalue)) {
return absl::OkStatus();
}

// Static declarations containing only LValues
if (init.rvalue().kind() == xls::ValueKind::kInvalid) {
return DeclareVariable(lvalue,
CValue(TrackedBValue(), init.type(),
/*disable_type_check=*/false, init_lvalue),
loc, check_unique_ids);
loc, check_unique_ids, /*declare_up_stack=*/true);
}

context().sf->declaration_order_by_name_[lvalue] =
Expand Down Expand Up @@ -2821,7 +2832,8 @@ absl::Status Translator::DeclareStatic(
const CValue init_cval(bval, init.type(), /*disable_type_check=*/false,
init_lvalue);

return DeclareVariable(lvalue, init_cval, loc, check_unique_ids);
return DeclareVariable(lvalue, init_cval, loc, check_unique_ids,
/*declare_up_stack=*/true);
}

absl::StatusOr<CValue> Translator::Generate_Synthetic_ByOne(
Expand Down Expand Up @@ -4320,7 +4332,8 @@ absl::StatusOr<CValue> Translator::GenerateIR_Call(
}

// Sorting order needs to include callee declarations
for (const auto [decl, _] : func->declaration_order_by_name_) {
for (const clang::NamedDecl* decl :
func->DeterministicKeyNames(func->declaration_order_by_name_)) {
context().sf->declaration_order_by_name_[decl] =
++context().sf->declaration_count;
}
Expand Down
3 changes: 2 additions & 1 deletion xls/contrib/xlscc/translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,8 @@ class Translator final : public GeneratorBase,

absl::Status DeclareVariable(const clang::NamedDecl* lvalue,
const CValue& rvalue, const xls::SourceInfo& loc,
bool check_unique_ids = true);
bool check_unique_ids = true,
bool declare_up_stack = false);

absl::Status DeclareStatic(const clang::NamedDecl* lvalue,
const ConstValue& init,
Expand Down
Loading
Loading
0