Skip to content

Commit f8f0e39

Browse files
dplassgitcopybara-github
authored andcommitted
Extract ConversionRecord to its own file, since it is no longer associated with just extract conversion order.
PiperOrigin-RevId: 808557090
1 parent 65aa30d commit f8f0e39

File tree

7 files changed

+234
-131
lines changed

7 files changed

+234
-131
lines changed

xls/dslx/ir_convert/BUILD

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ cc_library(
4848
],
4949
)
5050

51+
cc_library(
52+
name = "conversion_record",
53+
srcs = ["conversion_record.cc"],
54+
hdrs = ["conversion_record.h"],
55+
deps = [
56+
"//xls/common/status:status_macros",
57+
"//xls/dslx/frontend:ast",
58+
"//xls/dslx/frontend:ast_node",
59+
"//xls/dslx/frontend:module",
60+
"//xls/dslx/frontend:proc_id",
61+
"//xls/dslx/type_system:parametric_env",
62+
"//xls/dslx/type_system:type_info",
63+
"@com_google_absl//absl/container:btree",
64+
"@com_google_absl//absl/log",
65+
"@com_google_absl//absl/log:check",
66+
"@com_google_absl//absl/status",
67+
"@com_google_absl//absl/status:statusor",
68+
"@com_google_absl//absl/strings",
69+
"@com_google_absl//absl/strings:str_format",
70+
"@com_google_absl//absl/types:span",
71+
],
72+
)
73+
5174
cc_library(
5275
name = "channel_scope",
5376
srcs = ["channel_scope.cc"],
@@ -386,6 +409,7 @@ cc_library(
386409
deps = [
387410
":channel_scope",
388411
":conversion_info",
412+
":conversion_record",
389413
":convert_options",
390414
":extract_conversion_order",
391415
":function_converter",
@@ -437,6 +461,7 @@ cc_library(
437461
srcs = ["extract_conversion_order.cc"],
438462
hdrs = ["extract_conversion_order.h"],
439463
deps = [
464+
":conversion_record",
440465
"//xls/common:casts",
441466
"//xls/common:visitor",
442467
"//xls/common/status:ret_check",
@@ -452,7 +477,6 @@ cc_library(
452477
"//xls/dslx/type_system:parametric_env",
453478
"//xls/dslx/type_system:type_info",
454479
"@com_google_absl//absl/algorithm:container",
455-
"@com_google_absl//absl/container:btree",
456480
"@com_google_absl//absl/container:flat_hash_set",
457481
"@com_google_absl//absl/log",
458482
"@com_google_absl//absl/log:check",
@@ -488,6 +512,7 @@ cc_library(
488512
srcs = ["get_conversion_records.cc"],
489513
hdrs = ["get_conversion_records.h"],
490514
deps = [
515+
":conversion_record",
491516
":extract_conversion_order",
492517
"//xls/common/status:ret_check",
493518
"//xls/common/status:status_macros",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2025 The XLS Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "xls/dslx/ir_convert/conversion_record.h"
16+
17+
#include <optional>
18+
#include <string>
19+
#include <utility>
20+
21+
#include "absl/container/btree_set.h"
22+
#include "absl/log/check.h"
23+
#include "absl/log/log.h"
24+
#include "absl/status/status.h"
25+
#include "absl/status/statusor.h"
26+
#include "absl/strings/str_cat.h"
27+
#include "absl/strings/str_format.h"
28+
#include "absl/strings/str_join.h"
29+
#include "absl/types/span.h"
30+
#include "xls/common/status/status_macros.h"
31+
#include "xls/dslx/frontend/ast.h"
32+
#include "xls/dslx/frontend/ast_node.h"
33+
#include "xls/dslx/frontend/module.h"
34+
#include "xls/dslx/frontend/proc_id.h"
35+
#include "xls/dslx/type_system/parametric_env.h"
36+
#include "xls/dslx/type_system/type_info.h"
37+
38+
namespace xls::dslx {
39+
40+
std::string ConversionRecordsToString(
41+
absl::Span<const ConversionRecord> records) {
42+
return absl::StrCat(
43+
"[",
44+
absl::StrJoin(records, ",\n ",
45+
[](std::string* out, const ConversionRecord& record) {
46+
absl::StrAppend(out, record.ToString());
47+
}),
48+
"]");
49+
}
50+
51+
// -- class ConversionRecord
52+
53+
/* static */ absl::Status ConversionRecord::ValidateParametrics(
54+
Function* f, const ParametricEnv& parametric_env) {
55+
absl::btree_set<std::string> symbolic_binding_keys =
56+
parametric_env.GetKeySet();
57+
58+
auto set_to_string = [](const absl::btree_set<std::string>& s) {
59+
return absl::StrCat("{", absl::StrJoin(s, ", "), "}");
60+
};
61+
// TODO(leary): 2020-11-19 We use btrees in particular so this could use dual
62+
// iterators via the sorted property for O(n) superset comparison, but this
63+
// was easier to write and know it was correct on a first cut (couldn't find a
64+
// superset helper in absl's container algorithms at a first pass).
65+
auto is_superset = [](absl::btree_set<std::string> lhs,
66+
const absl::btree_set<std::string>& rhs) {
67+
for (const auto& item : rhs) {
68+
lhs.erase(item);
69+
}
70+
return !lhs.empty();
71+
};
72+
73+
if (is_superset(f->GetFreeParametricKeySet(), symbolic_binding_keys)) {
74+
return absl::InternalError(absl::StrFormat(
75+
"Not enough symbolic bindings to convert function: %s; need %s got %s",
76+
f->identifier(), set_to_string(f->GetFreeParametricKeySet()),
77+
set_to_string(symbolic_binding_keys)));
78+
}
79+
return absl::OkStatus();
80+
}
81+
82+
/* static */ absl::StatusOr<ConversionRecord> ConversionRecord::Make(
83+
Function* f, const Invocation* invocation, Module* module,
84+
TypeInfo* type_info, ParametricEnv parametric_env,
85+
std::optional<ProcId> proc_id, bool is_top) {
86+
XLS_RETURN_IF_ERROR(ConversionRecord::ValidateParametrics(f, parametric_env));
87+
88+
return ConversionRecord(f, invocation, module, type_info,
89+
std::move(parametric_env), std::move(proc_id),
90+
is_top);
91+
}
92+
93+
std::string ConversionRecord::ToString() const {
94+
std::string proc_id = "<none>";
95+
if (proc_id_.has_value()) {
96+
proc_id = proc_id_.value().ToString();
97+
}
98+
return absl::StrFormat(
99+
"ConversionRecord{m=%s, f=%s, top=%s, pid=%s, parametric_env=%s}",
100+
module_->name(), f_->identifier(), is_top_ ? "true" : "false", proc_id,
101+
parametric_env_.ToString());
102+
}
103+
104+
} // namespace xls::dslx
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2025 The XLS Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef XLS_DSLX_IR_CONVERSION_RECORD_H_
16+
#define XLS_DSLX_IR_CONVERSION_RECORD_H_
17+
18+
#include <optional>
19+
#include <string>
20+
#include <utility>
21+
22+
#include "absl/log/check.h"
23+
#include "absl/status/status.h"
24+
#include "absl/status/statusor.h"
25+
#include "absl/types/span.h"
26+
#include "xls/dslx/frontend/ast.h"
27+
#include "xls/dslx/frontend/proc_id.h"
28+
#include "xls/dslx/type_system/parametric_env.h"
29+
#include "xls/dslx/type_system/type_info.h"
30+
31+
namespace xls::dslx {
32+
33+
// Record used in sequence, noting order functions should be converted in.
34+
//
35+
// Describes a function instance that should be emitted (in an order determined
36+
// by an encapsulating sequence). Annotated with metadata that describes the
37+
// call graph instance.
38+
//
39+
// Attributes:
40+
// f: Function AST node to convert.
41+
// module: Module that f resides in.
42+
// type_info: Node to type mapping for use in converting this
43+
// function instance.
44+
// callees: Function names that 'f' calls.
45+
// parametric_env: Parametric bindings for this function instance.
46+
// callees: Functions that this instance calls.
47+
class ConversionRecord {
48+
public:
49+
// Note: performs ValidateParametrics() to potentially return an error status.
50+
static absl::StatusOr<ConversionRecord> Make(
51+
Function* f, const Invocation* invocation, Module* module,
52+
TypeInfo* type_info, ParametricEnv parametric_env,
53+
std::optional<ProcId> proc_id, bool is_top);
54+
55+
// Integrity-checks that the parametric_env provided are sufficient to
56+
// instantiate f (i.e. if it is parametric). Returns an internal error status
57+
// if they are not sufficient.
58+
static absl::Status ValidateParametrics(Function* f,
59+
const ParametricEnv& parametric_env);
60+
61+
Function* f() const { return f_; }
62+
const Invocation* invocation() const { return invocation_; }
63+
Module* module() const { return module_; }
64+
TypeInfo* type_info() const { return type_info_; }
65+
const ParametricEnv& parametric_env() const { return parametric_env_; }
66+
std::optional<ProcId> proc_id() const { return proc_id_; }
67+
bool IsTop() const { return is_top_; }
68+
69+
std::string ToString() const;
70+
71+
private:
72+
ConversionRecord(Function* f, const Invocation* invocation, Module* module,
73+
TypeInfo* type_info, ParametricEnv parametric_env,
74+
std::optional<ProcId> proc_id, bool is_top)
75+
: f_(f),
76+
invocation_(invocation),
77+
module_(module),
78+
type_info_(type_info),
79+
parametric_env_(std::move(parametric_env)),
80+
proc_id_(std::move(proc_id)),
81+
is_top_(is_top) {}
82+
83+
Function* f_;
84+
const Invocation* invocation_;
85+
Module* module_;
86+
TypeInfo* type_info_;
87+
ParametricEnv parametric_env_;
88+
std::optional<ProcId> proc_id_;
89+
bool is_top_;
90+
};
91+
92+
std::string ConversionRecordsToString(
93+
absl::Span<const ConversionRecord> records);
94+
95+
} // namespace xls::dslx
96+
97+
#endif // XLS_DSLX_IR_CONVERSION_RECORD_H_

xls/dslx/ir_convert/extract_conversion_order.cc

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <vector>
2323

2424
#include "absl/algorithm/container.h"
25-
#include "absl/container/btree_set.h"
2625
#include "absl/container/flat_hash_set.h"
2726
#include "absl/log/check.h"
2827
#include "absl/log/log.h"
@@ -44,6 +43,7 @@
4443
#include "xls/dslx/frontend/module.h"
4544
#include "xls/dslx/frontend/pos.h"
4645
#include "xls/dslx/frontend/proc_id.h"
46+
#include "xls/dslx/ir_convert/conversion_record.h"
4747
#include "xls/dslx/type_system/parametric_env.h"
4848
#include "xls/dslx/type_system/type_info.h"
4949

@@ -59,17 +59,6 @@ std::string CalleesToString(absl::Span<const Callee> callees) {
5959
"]");
6060
}
6161

62-
std::string ConversionRecordsToString(
63-
absl::Span<const ConversionRecord> records) {
64-
return absl::StrCat(
65-
"[",
66-
absl::StrJoin(records, ",\n ",
67-
[](std::string* out, const ConversionRecord& record) {
68-
absl::StrAppend(out, record.ToString());
69-
}),
70-
"]");
71-
}
72-
7362
} // namespace
7463

7564
// -- class Callee
@@ -109,59 +98,6 @@ std::string Callee::ToString() const {
10998
parametric_env_.ToString());
11099
}
111100

112-
// -- class ConversionRecord
113-
114-
/* static */ absl::Status ConversionRecord::ValidateParametrics(
115-
Function* f, const ParametricEnv& parametric_env) {
116-
absl::btree_set<std::string> symbolic_binding_keys =
117-
parametric_env.GetKeySet();
118-
119-
auto set_to_string = [](const absl::btree_set<std::string>& s) {
120-
return absl::StrCat("{", absl::StrJoin(s, ", "), "}");
121-
};
122-
// TODO(leary): 2020-11-19 We use btrees in particular so this could use dual
123-
// iterators via the sorted property for O(n) superset comparison, but this
124-
// was easier to write and know it was correct on a first cut (couldn't find a
125-
// superset helper in absl's container algorithms at a first pass).
126-
auto is_superset = [](absl::btree_set<std::string> lhs,
127-
const absl::btree_set<std::string>& rhs) {
128-
for (const auto& item : rhs) {
129-
lhs.erase(item);
130-
}
131-
return !lhs.empty();
132-
};
133-
134-
if (is_superset(f->GetFreeParametricKeySet(), symbolic_binding_keys)) {
135-
return absl::InternalError(absl::StrFormat(
136-
"Not enough symbolic bindings to convert function: %s; need %s got %s",
137-
f->identifier(), set_to_string(f->GetFreeParametricKeySet()),
138-
set_to_string(symbolic_binding_keys)));
139-
}
140-
return absl::OkStatus();
141-
}
142-
143-
/* static */ absl::StatusOr<ConversionRecord> ConversionRecord::Make(
144-
Function* f, const Invocation* invocation, Module* module,
145-
TypeInfo* type_info, ParametricEnv parametric_env,
146-
std::optional<ProcId> proc_id, bool is_top) {
147-
XLS_RETURN_IF_ERROR(ConversionRecord::ValidateParametrics(f, parametric_env));
148-
149-
return ConversionRecord(f, invocation, module, type_info,
150-
std::move(parametric_env), std::move(proc_id),
151-
is_top);
152-
}
153-
154-
std::string ConversionRecord::ToString() const {
155-
std::string proc_id = "<none>";
156-
if (proc_id_.has_value()) {
157-
proc_id = proc_id_.value().ToString();
158-
}
159-
return absl::StrFormat(
160-
"ConversionRecord{m=%s, f=%s, top=%s, pid=%s, parametric_env=%s}",
161-
module_->name(), f_->identifier(), is_top_ ? "true" : "false", proc_id,
162-
parametric_env_.ToString());
163-
}
164-
165101
// Collects all Invocation nodes below the visited node.
166102
class InvocationVisitor : public ExprVisitor {
167103
public:

0 commit comments

Comments
 (0)