Skip to content

Commit

Permalink
[DSLX:fmt] Re-enable autofmt tests with explicit struct instantiations.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 584710766
  • Loading branch information
cdleary authored and copybara-github committed Nov 22, 2023
1 parent a17ff2d commit e6f243e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 51 deletions.
42 changes: 23 additions & 19 deletions xls/dslx/fmt/ast_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ DocRef Fmt(const NameDefTree& n, const Comments& comments, DocArena& arena);
DocRef FmtExpr(const Expr& n, const Comments& comments, DocArena& arena,
bool suppress_parens);

// A parametric argument, as in parametric instantiation:
//
// f<FOO as u32>()
// ^^^^^^^^^^~~~ parametric argument, expr or types can go here generally
DocRef FmtParametricArg(const ExprOrType& n, const Comments& comments,
DocArena& arena) {
return absl::visit(
Visitor{
[&](const Expr* n) {
DocRef guts = Fmt(*n, comments, arena);
if (dynamic_cast<const NameRef*>(n) != nullptr ||
dynamic_cast<const ColonRef*>(n) != nullptr ||
dynamic_cast<const Number*>(n) != nullptr) {
return guts; // No need for enclosing curlies.
}
return ConcatN(arena, {arena.ocurl(), guts, arena.ccurl()});
},
[&](const TypeAnnotation* n) { return Fmt(*n, comments, arena); },
},
n);
}

static DocRef FmtExprPtr(const Expr* n, const Comments& comments,
DocArena& arena) {
XLS_CHECK(n != nullptr);
Expand Down Expand Up @@ -205,7 +227,7 @@ DocRef Fmt(const TypeRefTypeAnnotation& n, const Comments& comments,
if (!n.parametrics().empty()) {
pieces.push_back(arena.oangle());
pieces.push_back(FmtJoin<ExprOrType>(absl::MakeConstSpan(n.parametrics()),
Joiner::kCommaSpace, FmtExprOrType,
Joiner::kCommaSpace, FmtParametricArg,
comments, arena));
pieces.push_back(arena.cangle());
}
Expand Down Expand Up @@ -797,24 +819,6 @@ DocRef FmtExprOrType(const ExprOrType& n, const Comments& comments,
n);
}

static DocRef FmtParametricArg(const ExprOrType& n, const Comments& comments,
DocArena& arena) {
return absl::visit(
Visitor{
[&](const Expr* n) {
DocRef guts = Fmt(*n, comments, arena);
if (dynamic_cast<const NameRef*>(n) != nullptr ||
dynamic_cast<const ColonRef*>(n) != nullptr ||
dynamic_cast<const Number*>(n) != nullptr) {
return guts; // No need for enclosing curlies.
}
return ConcatN(arena, {arena.ocurl(), guts, arena.ccurl()});
},
[&](const TypeAnnotation* n) { return Fmt(*n, comments, arena); },
},
n);
}

DocRef Fmt(const Invocation& n, const Comments& comments, DocArena& arena) {
DocRef callee_doc = Fmt(*n.callee(), comments, arena);

Expand Down
15 changes: 15 additions & 0 deletions xls/dslx/fmt/ast_fmt_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,21 @@ fn f() -> u8 { p<8>(u8:42) }
}
}

TEST(ModuleFmtTest, SimpleParametricStructInstantiation) {
const std::string_view kProgram =
R"(import mol
struct Point<N: u32> { x: bits[N], y: bits[N] }
fn f() -> Point<mol::MOL> { Point<mol::MOL> { x: u8:42, y: u8:64 } }
)";
std::vector<CommentData> comments;
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> m,
ParseModule(kProgram, "fake.x", "fake", &comments));
std::string got = AutoFmt(*m, Comments::Create(comments));
EXPECT_EQ(got, kProgram);
}

TEST(ModuleFmtTest, TypeRefTypeAnnotationModuleLevel) {
constexpr std::string_view kProgram =
R"(type MyU32 = u32;
Expand Down
4 changes: 0 additions & 4 deletions xls/dslx/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ dslx_lang_test(name = "constexpr_pad_via_slice")
dslx_lang_test(
name = "explicit_parametric",
dslx_entry = "instantiates_indirect_foo",
# TODO(leary): 2023-10-20 Explicit instantiations of structs get deleted.
test_autofmt = False,
)

dslx_lang_test(
Expand All @@ -300,8 +298,6 @@ dslx_lang_test(
name = "explicit_parametric_reduced",
# No meaningful entry point to convert.
convert_to_ir = False,
# TODO(leary): 2023-10-20 Explicit instantiations of structs get deleted.
test_autofmt = False,
)

dslx_lang_test(
Expand Down
25 changes: 10 additions & 15 deletions xls/dslx/tests/explicit_parametric.x
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,28 @@
// limitations under the License.

// Tests for explicit instantiations of parametric structs and functions.
struct Generic<X:u32, Y:u32> {
a: bits[X],
b: bits[Y]
}
struct Generic<X: u32, Y: u32> { a: bits[X], b: bits[Y] }

pub fn foo(a: bits[4]) -> Generic<4, 8> {
Generic<u32:4, u32:8>{ a: a, b: bits[8]:0 }
}
pub fn foo(a: bits[4]) -> Generic<4, 8> { Generic<u32:4, u32:8> { a, b: bits[8]:0 } }

pub fn indirect_foo<X: u32, Y: u32 = {(X * X) as u32}>(a: bits[4]) -> Generic<X, 8> {
Generic<{X as u32}, u32:8>{ a: a as bits[X], b: bits[8]:32 }
Generic<{X as u32}, u32:8> { a: a as bits[X], b: bits[8]:32 }
}

pub fn instantiates_indirect_foo(a: bits[16]) -> Generic<16, 8> {
indirect_foo<u32:16>(a as bits[4])
indirect_foo<u32:16>(a as bits[4])
}

pub fn parameterized_zero<C:u32, D:u32>(x: bits[C]) -> Generic<C, D> {
Generic<C, D>{ a: bits[C]:0, b: bits[D]:1 }
pub fn parameterized_zero<C: u32, D: u32>(x: bits[C]) -> Generic<C, D> {
Generic<C, D> { a: bits[C]:0, b: bits[D]:1 }
}

pub fn two_param_indirect<E:u32, F:u32>(value: bits[E]) -> Generic<E, F> {
parameterized_zero<E, F>(value)
pub fn two_param_indirect<E: u32, F: u32>(value: bits[E]) -> Generic<E, F> {
parameterized_zero<E, F>(value)
}

#[test]
fn generic() {
let actual = two_param_indirect<u32:1, u32:2>(u1:0);
assert_eq(actual, Generic<1, 2>{a: u1:0, b: u2:1});
let actual = two_param_indirect<u32:1, u32:2>(u1:0);
assert_eq(actual, Generic<1, 2> { a: u1:0, b: u2:1 });
}
19 changes: 6 additions & 13 deletions xls/dslx/tests/explicit_parametric_reduced.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

struct Generic<X:u32, Y:u32> {
a: bits[X],
b: bits[Y]
}
struct Generic<X: u32, Y: u32> { a: bits[X], b: bits[Y] }

fn zero<C:u32, D:u32>() -> Generic<C, D> {
Generic<C, D>{ a: bits[C]:0, b: bits[D]:0 }
}
fn zero<C: u32, D: u32>() -> Generic<C, D> { Generic<C, D> { a: bits[C]:0, b: bits[D]:0 } }

fn indirection<E:u32, F:u32>() -> Generic<E, F> {
zero<E, F>()
}
fn indirection<E: u32, F: u32>() -> Generic<E, F> { zero<E, F>() }

#[test]
fn test_generic() {
let got = indirection<u32:1, u32:2>();
let want = Generic<u32:1, u32:2>{ a: u1:0, b: u2:0 };
assert_eq(want, got)
let got = indirection<u32:1, u32:2>();
let want = Generic<u32:1, u32:2> { a: u1:0, b: u2:0 };
assert_eq(want, got)
}

0 comments on commit e6f243e

Please sign in to comment.