Skip to content

Commit

Permalink
Record output type for output ports in package interface
Browse files Browse the repository at this point in the history
The package interface proto would record all OutputPort nodes as having a type empty-tuple since that's the nodes result type. The type of the value passed into the output node is what is actually desired however.

PiperOrigin-RevId: 687411299
  • Loading branch information
allight authored and copybara-github committed Oct 18, 2024
1 parent 7a63153 commit 41fe907
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
13 changes: 9 additions & 4 deletions xls/dev_tools/extract_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "xls/ir/channel.h"
#include "xls/ir/function.h"
#include "xls/ir/node.h"
#include "xls/ir/nodes.h"
#include "xls/ir/package.h"
#include "xls/ir/proc.h"
#include "xls/ir/register.h"
Expand Down Expand Up @@ -56,10 +57,14 @@ PackageInterfaceProto ExtractPackageInterface(Package* package) {
f->set_name(ir->name());
f->set_top(package->GetTop() == ir);
};
auto add_named = [&](PackageInterfaceProto::NamedValue* n, Node* node) {
*n->mutable_type() = node->GetType()->ToProto();
auto add_typed = [&](PackageInterfaceProto::NamedValue* n, Node* node,
Type* ty) {
*n->mutable_type() = ty->ToProto();
n->set_name(node->GetName());
};
auto add_named = [&](PackageInterfaceProto::NamedValue* n, Node* node) {
add_typed(n, node, node->GetType());
};
for (const auto& f : package->functions()) {
auto* func = proto.add_functions();
add_common(func->mutable_base(), f.get());
Expand Down Expand Up @@ -93,8 +98,8 @@ PackageInterfaceProto ExtractPackageInterface(Package* package) {
for (Node* port : b->GetInputPorts()) {
add_named(blk->add_input_ports(), port);
}
for (Node* port : b->GetOutputPorts()) {
add_named(blk->add_output_ports(), port);
for (OutputPort* port : b->GetOutputPorts()) {
add_typed(blk->add_output_ports(), port, port->output_type());
}
}
return proto;
Expand Down
30 changes: 30 additions & 0 deletions xls/dev_tools/extract_interface_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,35 @@ top proc add(__token: token, init={token}) {
)pb"));
}

TEST_F(ExtractInterfaceTest, BasicBlock) {
auto p = CreatePackage();
BlockBuilder bb(TestName(), p.get());
XLS_ASSERT_OK(bb.AddClockPort("clk"));
bb.OutputPort(
"baz", bb.InsertRegister("foo", bb.InputPort("bar", p->GetBitsType(32))));
XLS_ASSERT_OK(bb.Build().status());

EXPECT_THAT(ExtractPackageInterface(p.get()),
ProtoEquivalent(
R"pb(
name: "BasicBlock"
blocks {
base { top: false name: "BasicBlock" }
registers {
name: "foo"
type { type_enum: BITS bit_count: 32 }
}
input_ports {
name: "bar"
type { type_enum: BITS bit_count: 32 }
}
output_ports {
name: "baz"
type { type_enum: BITS bit_count: 32 }
}
}
)pb"));
}

} // namespace
} // namespace xls

0 comments on commit 41fe907

Please sign in to comment.