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-
4744namespace 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