8000 [fuzzer] Generate arrays with an ellipsis by MaxonT · Pull Request #5591 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions xls/fuzzer/ast_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,16 @@ absl::StatusOr<TypedExpr> AstGenerator::GenerateArray(Context* ctx) {
min_stage = std::max(min_stage, t.min_stage);
}

// Sometimes end the array with an ellipsis: the last element is repeated to
// fill the annotated length, which must be longer than the listed members.
bool use_ellipsis = RandomBool(0.25);
int64_t array_size = static_cast<int64_t>(values.size());
if (use_ellipsis) {
array_size += absl::Uniform<int64_t>(absl::IntervalClosed, bit_gen_, 1, 3);
total_width += (array_size - static_cast<int64_t>(values.size())) *
GetTypeBitCount(value.type);
}

XLS_RETURN_IF_ERROR(VerifyAggregateWidth(total_width));

// Create a type alias for the return type because arrays of tuples do not
Expand All @@ -1652,10 +1662,15 @@ absl::StatusOr<TypedExpr> AstGenerator::GenerateArray(Context* ctx) {
// when parsing is fixed.
auto* element_type_alias = MakeTypeRefTypeAnnotation(value.type);
auto* result_type = module_->Make<ArrayTypeAnnotation>(
fake_span_, element_type_alias, MakeNumber(values.size()));
fake_span_, element_type_alias, MakeNumber(array_size));

Array* array = module_->Make<Array>(fake_span_, value_exprs, use_ellipsis);
if (use_ellipsis) {
// An array with an ellipsis must carry its explicit type annotation.
array->set_type_annotation(result_type);
}

return TypedExpr{.expr = module_->Make<Array>(fake_span_, value_exprs,
/*has_ellipsis=*/false),
return TypedExpr{.expr = array,
.type = result_type,
.last_delaying_op = last_delaying_op,
.min_stage = min_stage};
Expand Down
45 changes: 45 additions & 0 deletions xls/fuzzer/ast_generator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,51 @@ TEST(AstGeneratorMultiTest, GeneratesValidFunctions) {
}
}

// Returns true if `root` contains an array literal with an ellipsis whose
// listed members are not all index expressions. The generator's only other
// ellipsis is the array_slice width argument (`[x[0], ...]`), so this is true
// only when GenerateArray produces an ellipsis array.
bool ContainsEllipsisArrayWithNonIndexMember(AstNode* root) {
std::vector<AstNode*> stack = {root};
while (!stack.empty()) {
AstNode* node = stack.back();
stack.pop_back();
if (auto* array = dynamic_cast<Array*>(node);
array != nullptr && array->has_ellipsis()) {
for (Expr* member : array->members()) {
if (dynamic_cast<Index*>(member) == nullptr) {
return true;
}
}
}
for (AstNode* child : node->GetChildren(/*want_types=*/false)) {
stack.push_back(child);
}
}
return false;
}

// Tests that arrays with an ellipsis (`[x, y, ...]`) are generated by
// GenerateArray, and that the generated programs parse and typecheck.
TEST(AstGeneratorMultiTest, GeneratesArraysWithEllipsis) {
FileTable file_table;
std::mt19937_64 rng{0};
AstGeneratorOptions options;
bool found_ellipsis = false;
for (int64_t i = 0; i < 32; ++i) {
AstGenerator g(options, rng, file_table);
std::string module_name = absl::StrFormat("ellipsis_sample_%d", i);
XLS_ASSERT_OK_AND_ASSIGN(AnnotatedModule module,
g.Generate("main", module_name));
found_ellipsis =
found_ellipsis ||
ContainsEllipsisArrayWithNonIndexMember(module.module.get());
XLS_ASSERT_OK(ParseAndTypecheck<Function>(module.module->ToString(),
module_name));
}
EXPECT_TRUE(found_ellipsis);
}

// Simply tests that we generate a bunch of valid procs with an empty state type
// using seed 0 (that parse and typecheck).
TEST(AstGeneratorMultiTest, GeneratesValidProcsWithEmptyState) {
Expand Down
0