Skip to content

Commit b64c970

Browse files
richmckeevercopybara-github
authored andcommitted
When converting a proc alias to IR, use the alias name in the IR.
PiperOrigin-RevId: 820685674
1 parent 9302c31 commit b64c970

File tree

8 files changed

+59
-34
lines changed

8 files changed

+59
-34
lines changed

xls/dslx/frontend/proc_id.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,18 @@ struct ProcId {
5353
// [{A, 0}, {E, 0}, {B, 0}, {C, 0}]
5454
std::vector<std::pair<Proc*, int>> proc_instance_stack;
5555

56+
// The name of the proc alias, in a situation where we are converting a proc
57+
// to IR via an alias to it.
58+
std::optional<std::string> alias_name;
59+
5660
std::string ToString() const {
5761
if (proc_instance_stack.empty()) {
5862
return "";
5963
}
64+
if (proc_instance_stack.size() == 1 && alias_name.has_value()) {
65+
return *alias_name;
66+
}
67+
6068
// The first proc in a chain never needs an instance count. Leaving it out
6169
// specifically when the chain length is more than 1 gets us the historical
6270
// output in most cases (where there was only an instance count at the end

xls/dslx/ir_convert/extract_conversion_order.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -649,22 +649,25 @@ static absl::StatusOr<std::vector<ConversionRecord>> GetOrderForProc(
649649
TypeInfo* config_ti = type_info;
650650
TypeInfo* next_ti = type_info;
651651
ParametricEnv env;
652+
std::optional<std::string> alias_name;
652653
if (resolved_proc_alias.has_value()) {
653654
config_ti = resolved_proc_alias->config_type_info;
654655
next_ti = resolved_proc_alias->next_type_info;
655656
env = resolved_proc_alias->env;
657+
alias_name = resolved_proc_alias->name;
656658
}
657659

658660
// The next function of a proc is the entry function when converting a proc to
659661
// IR.
660-
XLS_RETURN_IF_ERROR(
661-
AddToReady(&p->next(),
662-
/*invocation=*/nullptr, p->owner(), next_ti, env, &ready,
663-
ProcId{.proc_instance_stack = {{p, 0}}}, is_top));
664-
XLS_RETURN_IF_ERROR(AddToReady(&p->config(),
665-
/*invocation=*/nullptr, p->owner(), config_ti,
666-
env, &ready,
667-
ProcId{.proc_instance_stack = {{p, 0}}}));
662+
XLS_RETURN_IF_ERROR(AddToReady(
663+
&p->next(),
664+
/*invocation=*/nullptr, p->owner(), next_ti, env, &ready,
665+
ProcId{.proc_instance_stack = {{p, 0}}, .alias_name = alias_name},
666+
is_top));
667+
XLS_RETURN_IF_ERROR(AddToReady(
668+
&p->config(),
669+
/*invocation=*/nullptr, p->owner(), config_ti, env, &ready,
670+
ProcId{.proc_instance_stack = {{p, 0}}, .alias_name = alias_name}));
668671

669672
// Constants and "member" vars are assigned and defined in Procs' "config'"
670673
// functions, so we need to execute those before their "next" functions.

xls/dslx/ir_convert/function_converter.cc

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -828,22 +828,21 @@ absl::Status FunctionConverter::HandleExternNameRef(
828828
imported_info->module->FindMemberWithName(node->identifier());
829829
XLS_RET_CHECK(member.has_value());
830830
return absl::visit(
831-
Visitor{
832-
[&](Function* f) { return DefAlias(f, /*to=*/node); },
833-
[&](ConstantDef* c) -> absl::Status {
834-
XLS_RET_CHECK(node_to_ir_.contains(c->value()))
835-
<< absl::StreamFormat(
836-
"ConstantDef `%s` not found in node_to_ir_ map",
837-
c->ToString());
838-
return DefAlias(c->value(), /*to=*/node);
839-
},
840-
[&](auto) {
841-
return absl::UnimplementedError(absl::StrFormat(
842-
"Unsupported module member type %s for external name "
843-
"reference: `%s` @ %s",
844-
GetModuleMemberTypeName(*member.value()), node->identifier(),
845-
node->span().ToString(file_table())));
846-
}},
831+
Visitor{[&](Function* f) { return DefAlias(f, /*to=*/node); },
832+
[&](ConstantDef* c) -> absl::Status {
833+
XLS_RET_CHECK(node_to_ir_.contains(c->value()))
834+
<< absl::StreamFormat(
835+
"ConstantDef `%s` not found in node_to_ir_ map",
836+
c->ToString());
837+
return DefAlias(c->value(), /*to=*/node);
838+
},
839+
[&](auto) {
840+
return absl::UnimplementedError(absl::StrFormat(
841+
"Unsupported module member type %s for external name "
842+
"reference: `%s` @ %s",
843+
GetModuleMemberTypeName(*member.value()),
844+
node->identifier(), node->span().ToString(file_table())));
845+
}},
847846
*member.value());
848847
}
849848

@@ -3466,11 +3465,23 @@ absl::Status FunctionConverter::HandleProcNextFunction(
34663465
<< initial_element.ToHumanString();
34673466
}
34683467

3469-
XLS_ASSIGN_OR_RETURN(
3470-
std::string mangled_name,
3471-
MangleDslxName(module_->name(), proc_id.ToString(),
3472-
CallingConvention::kProcNext, f->GetFreeParametricKeySet(),
3473-
parametric_env));
3468+
std::string mangled_name;
3469+
if (proc_id.alias_name.has_value() &&
3470+
proc_id.proc_instance_stack.size() == 1) {
3471+
// Don't include the parametrics for the target proc in the mangled name of
3472+
// a proc alias.
3473+
XLS_ASSIGN_OR_RETURN(
3474+
mangled_name, MangleDslxName(module_->name(), proc_id.ToString(),
3475+
CallingConvention::kProcNext,
3476+
absl::btree_set<std::string>{}, nullptr));
3477+
} else {
3478+
XLS_ASSIGN_OR_RETURN(
3479+
mangled_name,
3480+
MangleDslxName(module_->name(), proc_id.ToString(),
3481+
CallingConvention::kProcNext,
3482+
f->GetFreeParametricKeySet(), parametric_env));
3483+
}
3484+
34743485
std::string token_name = "__token";
34753486
std::string state_name = "__state";
34763487

xls/dslx/ir_convert/testdata/ir_converter_test_HandlesBasicProcAlias.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ file_number 0 "test_module.x"
44

55
chan test_module__output_c(bits[32], id=0, kind=streaming, ops=send_only, flow_control=ready_valid, strictness=proven_mutually_exclusive)
66

7-
top proc __test_module__Foo_0_next(__state: bits[32], init={1}) {
7+
top proc __test_module__FooAlias_next(__state: bits[32], init={1}) {
88
__state: bits[32] = state_read(state_element=__state, id=2)
99
literal.6: bits[32] = literal(value=2, id=6)
1010
after_all.4: token = after_all(id=4)

xls/dslx/ir_convert/testdata/ir_converter_test_HandlesParametricProcAlias.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ file_number 0 "test_module.x"
44

55
chan test_module__output_c(bits[16], id=0, kind=streaming, ops=send_only, flow_control=ready_valid, strictness=proven_mutually_exclusive)
66

7-
top proc __test_module__Foo_0__16_next(__state: bits[16], init={1}) {
7+
top proc __test_module__FooAlias_next(__state: bits[16], init={1}) {
88
__state: bits[16] = state_read(state_element=__state, id=2)
99
literal.7: bits[16] = literal(value=2, id=7)
1010
after_all.5: token = after_all(id=5)

xls/dslx/ir_convert/testdata/ir_converter_test_HandlesProcAliasToImportedProc.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ file_number 0 "imported.x"
44

55
chan test_module__output_c(bits[16], id=0, kind=streaming, ops=send_only, flow_control=ready_valid, strictness=proven_mutually_exclusive)
66

7-
top proc __imported__Foo_0__16_next(__state: bits[16], init={1}) {
7+
top proc __imported__FooAlias_next(__state: bits[16], init={1}) {
88
__state: bits[16] = state_read(state_element=__state, id=2)
99
literal.7: bits[16] = literal(value=2, id=7)
1010
after_all.5: token = after_all(id=5)

xls/dslx/type_system/type_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct InvocationCalleeData {
7575

7676
// The information for a `ProcAlias` that is resolved during type inference.
7777
struct ResolvedProcAlias {
78+
std::string name;
7879
Proc* proc;
7980
ParametricEnv env;
8081
TypeInfo* config_type_info;

xls/dslx/type_system_v2/inference_table_converter_impl.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,8 @@ class InferenceTableConverterImpl : public InferenceTableConverter,
23912391

23922392
// For a non-parametric proc, that's it.
23932393
if (alias->parametrics().empty()) {
2394-
return ResolvedProcAlias{.proc = proc,
2394+
return ResolvedProcAlias{.name = alias->identifier(),
2395+
.proc = proc,
23952396
.env = ParametricEnv{},
23962397
.config_type_info = ti,
23972398
.next_type_info = ti};
@@ -2454,7 +2455,8 @@ class InferenceTableConverterImpl : public InferenceTableConverter,
24542455
XLS_RETURN_IF_ERROR(ConvertSubtree(member, std::nullopt, next_context));
24552456
}
24562457

2457-
return ResolvedProcAlias{.proc = proc,
2458+
return ResolvedProcAlias{.name = alias->identifier(),
2459+
.proc = proc,
24582460
.env = env,
24592461
.config_type_info = config_ti,
24602462
.next_type_info = next_ti};

0 commit comments

Comments
 (0)