Skip to content

Commit 06293d7

Browse files
Sean Purser-Haskellcopybara-github
authored andcommitted
Roll back continuation changes.
Future function slice and BValue tracking approach will be very different. PiperOrigin-RevId: 601817637
1 parent 580a8b4 commit 06293d7

12 files changed

+91
-640
lines changed

xls/contrib/xlscc/translate_block.cc

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -949,16 +949,10 @@ absl::StatusOr<Translator::SubFSMReturn> Translator::GenerateSubFSM(
949949
const int64_t context_out_ret_idx =
950950
outer_prepared.return_index_for_op.at(&invoke_context_out.op);
951951

952-
// Extract continuation from first_ret_val
953-
xls::BValue ret_value_with_continuation =
952+
xls::BValue ret_io_value =
954953
GetFlexTupleField(first_ret_val, context_out_ret_idx,
955954
outer_prepared.xls_func->return_value_count, body_loc);
956955

957-
XLS_ASSIGN_OR_RETURN(xls::BValue ret_io_value,
958-
UnpackAndApplyContinuationIn(
959-
invoke_context_out.op, ret_value_with_continuation,
960-
/*includes_io_value=*/true, body_loc));
961-
962956
xls::BValue context_out = pb.TupleIndex(ret_io_value, /*idx=*/0, body_loc);
963957

964958
// Generate inner FSM
@@ -971,10 +965,7 @@ absl::StatusOr<Translator::SubFSMReturn> Translator::GenerateSubFSM(
971965

972966
outer_prepared.token = contents_ret.token_out;
973967

974-
XLS_ASSIGN_OR_RETURN(
975-
xls::BValue context_in,
976-
AddContinuationToIOReturn(invoke_context_in.op, contents_ret.out_tuple,
977-
body_loc));
968+
xls::BValue context_in = contents_ret.out_tuple;
978969

979970
// Get context in from inner FSM
980971
const int64_t context_in_arg_idx =
@@ -1020,16 +1011,10 @@ absl::StatusOr<xls::BValue> Translator::GenerateIOInvoke(
10201011
xls::SourceInfo op_loc = op.op_location;
10211012
const int64_t return_index = prepared.return_index_for_op.at(&op);
10221013

1023-
// Extract continuation from last_ret_val
1024-
xls::BValue ret_value_with_continuation = GetFlexTupleField(
1014+
xls::BValue ret_io_value = GetFlexTupleField(
10251015
last_ret_val, return_index, prepared.xls_func->return_value_count, op_loc,
10261016
/*name=*/
1027-
absl::StrFormat("%s_ret_value_with_continuation", op.final_param_name));
1028-
1029-
XLS_ASSIGN_OR_RETURN(
1030-
xls::BValue ret_io_value,
1031-
UnpackAndApplyContinuationIn(op, ret_value_with_continuation,
1032-
/*includes_io_value=*/true, op_loc));
1017+
absl::StrFormat("%s_ret_io_value", op.final_param_name));
10331018

10341019
xls::BValue arg_io_val;
10351020

@@ -1143,11 +1128,11 @@ absl::StatusOr<xls::BValue> Translator::GenerateIOInvoke(
11431128
XLS_CHECK("Unknown IOOp type" == nullptr);
11441129
}
11451130

1146-
// Add the continuation to the IO argument and store
1147-
const int64_t arg_index = prepared.arg_index_for_op.at(&op);
1148-
XLS_CHECK(arg_index >= 0 && arg_index < prepared.args.size());
1149-
XLS_ASSIGN_OR_RETURN(prepared.args[arg_index],
1150-
AddContinuationToIOReturn(op, arg_io_val, op_loc));
1131+
if (prepared.arg_index_for_op.contains(&op)) {
1132+
const int64_t arg_index = prepared.arg_index_for_op.at(&op);
1133+
XLS_CHECK(arg_index >= 0 && arg_index < prepared.args.size());
1134+
prepared.args[arg_index] = arg_io_val;
1135+
}
11511136

11521137
// The function is invoked again with the value received from the channel
11531138
// for each read() Op. The final invocation will produce all complete
@@ -1525,10 +1510,12 @@ Translator::GenerateIRBlockPrepare(
15251510
switch (param.type) {
15261511
case xlscc::SideEffectingParameterType::kIOOp: {
15271512
const IOOp& op = *param.io_op;
1528-
xls::BValue val =
1529-
pb.Literal(xls::ZeroOfType(param.xls_io_param_type), body_loc);
1530-
prepared.arg_index_for_op[&op] = prepared.args.size();
1531-
prepared.args.push_back(val);
1513+
if (op.op == OpType::kRead || op.op == OpType::kRecv) {
1514+
xls::BValue val =
1515+
pb.Literal(xls::ZeroOfType(param.xls_io_param_type), body_loc);
1516+
prepared.arg_index_for_op[&op] = prepared.args.size();
1517+
prepared.args.push_back(val);
1518+
}
15321519
break;
15331520
}
15341521
case xlscc::SideEffectingParameterType::kStatic: {

xls/contrib/xlscc/translate_io.cc

Lines changed: 41 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -41,145 +41,11 @@
4141
#include "xls/ir/type.h"
4242
#include "xls/ir/value_helpers.h"
4343

44-
// TODO(seanhaskell): Remove when continuations are finished
45-
constexpr bool kDebugUseContinuations = false;
46-
4744
namespace xlscc {
4845

49-
absl::Status Translator::AddToContinuationNonDestructively(
50-
Continuation& continuation, const xls::SourceInfo& loc) {
51-
absl::flat_hash_set<const clang::NamedDecl*> decls_to_pass;
52-
53-
// Get variables to pass
54-
std::vector<const clang::NamedDecl*> decls_sorted;
55-
decls_sorted.reserve(context().variables.size());
56-
for (const auto& [decl, _] : context().variables) {
57-
decls_sorted.push_back(decl);
58-
}
59-
// Sort for determinism
60-
context().sf->SortNamesDeterministically(decls_sorted);
61-
62-
for (const clang::NamedDecl* decl : decls_sorted) {
63-
const CValue& cvar = context().variables.at(decl);
64-
// Pure syntactic sugar values (eg channels) don't need to be passed
65-
if (!cvar.rvalue().valid()) {
66-
continue;
67-
}
68-
continuation.vars_to_pass.push_back(
69-
ContinuationItem{.decl = decl, .ctype = cvar.type()});
70-
decls_to_pass.insert(decl);
71-
}
72-
73-
// Calculate variables accessed using reference counts
74-
const absl::flat_hash_map<const clang::NamedDecl*, int64_t>&
75-
vars_accessed_last = context().variables_accessed_at_last_continuation;
76-
77-
for (const auto& [decl, count] : context().variables_accessed) {
78-
if (!decls_to_pass.contains(decl)) {
79-
continue;
80-
}
81-
auto found_last = vars_accessed_last.find(decl);
82-
if (found_last != vars_accessed_last.end() && found_last->second == count) {
83-
continue;
84-
}
85-
XLSCC_CHECK(
86-
found_last == vars_accessed_last.end() || found_last->second < count,
87-
loc);
88-
continuation.vars_accessed_since_last_continuation.push_back(decl);
89-
}
90-
91-
context().variables_accessed_at_last_continuation =
92-
context().variables_accessed;
93-
context().variables_masked_by_assignment.clear();
94-
95-
// Build data types
96-
auto active_type = std::make_shared<CBoolType>();
97-
std::vector<std::shared_ptr<CType>> tuple_ctypes;
98-
tuple_ctypes.reserve(continuation.vars_to_pass.size() + 1);
99-
tuple_ctypes.push_back(active_type);
100-
101-
for (const ContinuationItem& item : continuation.vars_to_pass) {
102-
tuple_ctypes.push_back(item.ctype);
103-
}
104-
105-
if (!kDebugUseContinuations) {
106-
tuple_ctypes.clear();
107-
}
108-
109-
continuation.param_part_ctype = std::make_shared<CInternalTuple>(
110-
std::vector<std::shared_ptr<CType>>(tuple_ctypes));
111-
return absl::OkStatus();
112-
}
113-
114-
absl::StatusOr<xls::BValue> Translator::UnpackAndApplyContinuationIn(
115-
const IOOp& op, xls::BValue param_in_bval, bool includes_io_value,
116-
const xls::SourceInfo& loc) {
117-
std::string node_name_prefix = absl::StrFormat(
118-
"__%s_%i_continuation_in", op.final_param_name, op.channel_op_index);
119-
120-
xls::BValue continuation_in_bval = param_in_bval;
121-
xls::BValue input_io_value;
122-
123-
// Extract the IO input from the tuple, if present
124-
if (includes_io_value) {
125-
// Extract the IO input part from the continuation part
126-
input_io_value = context().fb->TupleIndex(
127-
param_in_bval, /*idx=*/0, loc,
128-
/*name=*/
129-
absl::StrFormat("__%s_%i_io_in", op.final_param_name,
130-
op.channel_op_index));
131-
132-
continuation_in_bval = context().fb->TupleIndex(
133-
param_in_bval, /*idx=*/1, loc,
134-
/*name=*/
135-
absl::StrFormat("__%s_%i_continuation_in", op.final_param_name,
136-
op.channel_op_index));
137-
}
138-
139-
// TODO(seanhaskell): Apply continuation_in_bval.
140-
// TODO(seanhaskell): Side-channel values that aren't in this scope / context
141-
142-
return input_io_value;
143-
}
144-
145-
absl::StatusOr<xls::BValue> Translator::GetContinuationOut(
146-
const Continuation& continuation, const xls::SourceInfo& loc,
147-
std::string_view node_name_prefix) {
148-
XLS_ASSIGN_OR_RETURN(xls::Type * param_part_xls_type,
149-
TranslateTypeToXLS(continuation.param_part_ctype, loc));
150-
151-
// TODO(seanhaskell): Get continuation values from context
152-
xls::BValue ret = context().fb->Literal(
153-
xls::ZeroOfType(param_part_xls_type), loc,
154-
/*name=*/absl::StrFormat("%s_default", node_name_prefix));
155-
return ret;
156-
}
157-
158-
absl::StatusOr<xls::BValue> Translator::AddContinuationToIOReturn(
159-
const IOOp& op, xls::BValue retval, const xls::SourceInfo& loc) {
160-
XLS_ASSIGN_OR_RETURN(
161-
xls::BValue continuation_out_bval,
162-
GetContinuationOut(
163-
op.continuation, loc, /*node_name_prefix=*/
164-
absl::StrFormat("__%s_%i_continuation_out", op.final_param_name,
165-
op.channel_op_index)));
166-
167-
// This function is also used for formatting parameters, so it must handle
168-
// the empty case
169-
if (!retval.valid()) {
170-
return continuation_out_bval;
171-
}
172-
173-
return context().fb->Tuple(
174-
{retval, continuation_out_bval}, loc,
175-
/*name=*/
176-
absl::StrFormat("__%s_%i_io_plus_continuation", op.final_param_name,
177-
op.channel_op_index));
178-
}
179-
180-
absl::StatusOr<IOOp*> Translator::AddOpToChannel(
181-
IOOp& op, IOChannel* channel, const xls::SourceInfo& loc, bool mask,
182-
bool add_continuation_to_return) {
46+
absl::StatusOr<IOOp*> Translator::AddOpToChannel(IOOp& op, IOChannel* channel,
47+
const xls::SourceInfo& loc,
48+
bool mask) {
18349
context().any_side_effects_requested = true;
18450
context().any_io_ops_requested = true;
18551

@@ -215,9 +81,6 @@ absl::StatusOr<IOOp*> Translator::AddOpToChannel(
21581
op.channel_op_index = context().sf->trace_count++;
21682
}
21783

218-
// Add continuation non-destructively (may be aggregating from callee op)
219-
XLS_RETURN_IF_ERROR(AddToContinuationNonDestructively(op.continuation, loc));
220-
22184
std::shared_ptr<CType> channel_item_type;
22285

22386
// Channel must be inserted first by AddOpToChannel
@@ -231,58 +94,42 @@ absl::StatusOr<IOOp*> Translator::AddOpToChannel(
23194
}
23295
}
23396

234-
XLSCC_CHECK_NE(op.continuation.param_part_ctype, nullptr, loc);
235-
std::shared_ptr<CType> param_type = op.continuation.param_part_ctype;
236-
237-
// Add the continuation via a tuple if there is an IO input, (io_item,
238-
// continuation)
239-
if (channel_item_type) {
240-
param_type =
241-
std::make_shared<CInternalTuple>(std::vector<std::shared_ptr<CType>>(
242-
{channel_item_type, op.continuation.param_part_ctype}));
243-
}
97+
std::shared_ptr<CType> param_type = channel_item_type;
24498

24599
xls::Type* xls_param_type = nullptr;
246-
XLS_ASSIGN_OR_RETURN(xls_param_type, TranslateTypeToXLS(param_type, loc));
247-
std::string safe_param_name;
248-
if (op.channel != nullptr) {
249-
const std::string channel_name = op.channel->unique_name;
250-
safe_param_name =
251-
absl::StrFormat("%s_op%i", channel_name, op.channel_op_index);
252-
} else {
253-
safe_param_name = absl::StrFormat("default_op%i", op.channel_op_index);
254-
}
255-
256-
xls::BValue pbval = context().fb->Param(safe_param_name, xls_param_type, loc);
100+
if (param_type != nullptr) {
101+
XLS_ASSIGN_OR_RETURN(xls_param_type, TranslateTypeToXLS(param_type, loc));
102+
std::string safe_param_name;
103+
if (op.channel != nullptr) {
104+
const std::string channel_name = op.channel->unique_name;
105+
safe_param_name =
106+
absl::StrFormat("%s_op%i", channel_name, op.channel_op_index);
107+
} else {
108+
safe_param_name = absl::StrFormat("default_op%i", op.channel_op_index);
109+
}
257110

258-
// Check for duplicate params
259-
if (!pbval.valid()) {
260-
return absl::InternalError(ErrorMessage(
261-
loc,
262-
"Failed to create implicit parameter %s, duplicate? See b/239861050",
263-
safe_param_name.c_str()));
264-
}
111+
xls::BValue pbval =
112+
context().fb->Param(safe_param_name, xls_param_type, loc);
265113

266-
const std::string final_param_name =
267-
pbval.node()->As<xls::Param>()->GetName();
114+
// Check for duplicate params
115+
if (!pbval.valid()) {
116+
return absl::InternalError(ErrorMessage(
117+
loc,
118+
"Failed to create implicit parameter %s, duplicate? See b/239861050",
119+
safe_param_name.c_str()));
120+
}
268121

269-
op.final_param_name = final_param_name;
122+
const std::string final_param_name =
123+
pbval.node()->As<xls::Param>()->GetName();
270124

271-
XLS_ASSIGN_OR_RETURN(
272-
xls::BValue input_io_value,
273-
UnpackAndApplyContinuationIn(
274-
op, pbval,
275-
/*includes_io_value=*/channel_item_type != nullptr, loc));
125+
op.final_param_name = final_param_name;
276126

277-
if (channel_item_type) {
278-
XLSCC_CHECK(input_io_value.valid(), loc);
279-
op.input_value = CValue(input_io_value, channel_item_type);
280-
}
127+
xls::BValue input_io_value = pbval;
281128

282-
// Add continuation to return value.
283-
if (add_continuation_to_return) {
284-
XLS_ASSIGN_OR_RETURN(op.ret_value,
285-
AddContinuationToIOReturn(op, op.ret_value, loc));
129+
if (channel_item_type) {
130+
XLSCC_CHECK(input_io_value.valid(), loc);
131+
op.input_value = CValue(input_io_value, channel_item_type);
132+
}
286133
}
287134

288135
XLS_ASSIGN_OR_RETURN(std::optional<const IOOp*> last_op,
@@ -297,14 +144,16 @@ absl::StatusOr<IOOp*> Translator::AddOpToChannel(
297144
// once "token phi" type features are available.
298145
context().sf->io_ops.push_back(op);
299146

300-
// Due to the continuations, there is always a parameter
301-
SideEffectingParameter side_effecting_param;
302-
side_effecting_param.type = SideEffectingParameterType::kIOOp;
303-
XLSCC_CHECK(!final_param_name.empty(), loc);
304-
side_effecting_param.param_name = final_param_name;
305-
side_effecting_param.xls_io_param_type = xls_param_type;
306-
side_effecting_param.io_op = &context().sf->io_ops.back();
307-
context().sf->side_effecting_parameters.push_back(side_effecting_param);
147+
if (param_type != nullptr) {
148+
XLSCC_CHECK_NE(xls_param_type, nullptr, loc);
149+
SideEffectingParameter side_effecting_param;
150+
side_effecting_param.type = SideEffectingParameterType::kIOOp;
151+
XLSCC_CHECK(!op.final_param_name.empty(), loc);
152+
side_effecting_param.param_name = op.final_param_name;
153+
side_effecting_param.xls_io_param_type = xls_param_type;
154+
side_effecting_param.io_op = &context().sf->io_ops.back();
155+
context().sf->side_effecting_parameters.push_back(side_effecting_param);
156+
}
308157

309158
return &context().sf->io_ops.back();
310159
}

0 commit comments

Comments
 (0)