Skip to content

Commit

Permalink
Fix cloning a function within a parametric proc so it properly clon…
Browse files Browse the repository at this point in the history
…es the parametric binding.

This was because plain functions keep their parametrics as children, but proc functions don't. As a result, the parametric bindings of a proc were never cloned.

PiperOrigin-RevId: 693924665
  • Loading branch information
dplassgit authored and copybara-github committed Nov 7, 2024
1 parent 062f1ba commit 4bf212f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion xls/dslx/frontend/ast_cloner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ class AstCloner : public AstNodeVisitor {

std::vector<ParametricBinding*> new_parametric_bindings;
new_parametric_bindings.reserve(n->parametric_bindings().size());
for (const auto* pb : n->parametric_bindings()) {
for (const ParametricBinding* pb : n->parametric_bindings()) {
XLS_RETURN_IF_ERROR(ReplaceOrVisit(pb));
new_parametric_bindings.push_back(
down_cast<ParametricBinding*>(old_to_new_.at(pb)));
}
Expand Down
60 changes: 60 additions & 0 deletions xls/dslx/frontend/ast_cloner_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,66 @@ impl MyProc {}
EXPECT_EQ(kExpectedProcDef, clone->ToString());
}

TEST(AstClonerTest, ParametricStructDefAndImpl) {
constexpr std::string_view kProgram = R"(
struct MyStruct<N: u32> {
a: u32[N],
b: s64
}
impl MyStruct {}
)";

constexpr std::string_view kExpectedProcDef = R"(struct MyStruct<N: u32> {
a: u32[N],
b: s64,
})";

FileTable file_table;
XLS_ASSERT_OK_AND_ASSIGN(auto module, ParseModule(kProgram, "fake_path.x",
"the_module", file_table));
StructDef* struct_def = module->GetStructDefs().at(0);
XLS_ASSERT_OK_AND_ASSIGN(AstNode * clone, CloneAst(struct_def));
EXPECT_EQ(kExpectedProcDef, clone->ToString());
}

TEST(AstClonerTest, SimpleParametricProc) {
constexpr std::string_view kProgram = R"(
proc p<N: u32> {
config() { () }
init { () }
next(state: ()) { () }
}
)";
constexpr std::string_view kExpected = R"(fn p.config<N: u32>() -> () {
()
}
fn p.init<N: u32>() -> () {
()
}
fn p.next<N: u32>(state: ()) -> () {
()
}
proc p<N: u32> {
config() {
()
}
init {
()
}
next(state: ()) {
()
}
})";

FileTable file_table;
XLS_ASSERT_OK_AND_ASSIGN(auto module, ParseModule(kProgram, "fake_path.x",
"the_module", file_table));
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> clone,
CloneModule(module.get()));
EXPECT_EQ(kExpected, clone->ToString());
}

TEST(AstClonerTest, StructDefAndImpl) {
constexpr std::string_view kProgram = R"(
struct MyStruct {
Expand Down

0 comments on commit 4bf212f

Please sign in to comment.