Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DSLX:cleanup] Remove ConstantArray node from AST, it is misleading / not useful. #1753

Merged
Merged
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
4 changes: 0 additions & 4 deletions xls/dslx/bytecode/bytecode_emitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,6 @@ absl::Status BytecodeEmitter::HandleConstAssert(const ConstAssert* node) {
return absl::OkStatus();
}

absl::Status BytecodeEmitter::HandleConstantArray(const ConstantArray* node) {
return HandleArray(node);
}

absl::Status BytecodeEmitter::HandleConstRef(const ConstRef* node) {
return HandleNameRef(node);
}
Expand Down
1 change: 0 additions & 1 deletion xls/dslx/bytecode/bytecode_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class BytecodeEmitter : public ExprVisitor {
absl::Status HandleColonRef(const ColonRef* node) override;
absl::StatusOr<InterpValue> HandleColonRefInternal(const ColonRef* node);
absl::Status HandleConstAssert(const ConstAssert* node) override;
absl::Status HandleConstantArray(const ConstantArray* node) override;
absl::Status HandleConstRef(const ConstRef* node) override;
absl::Status HandleFor(const For* node) override;
absl::Status HandleFormatMacro(const FormatMacro* node) override;
Expand Down
5 changes: 0 additions & 5 deletions xls/dslx/constexpr_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,6 @@ absl::Status ConstexprEvaluator::HandleColonRef(const ColonRef* expr) {
subject);
}

absl::Status ConstexprEvaluator::HandleConstantArray(
const ConstantArray* expr) {
return HandleArray(expr);
}

absl::Status ConstexprEvaluator::HandleConstAssert(
const ConstAssert* const_assert) {
const FileTable& file_table = *const_assert->owner()->file_table();
Expand Down
1 change: 0 additions & 1 deletion xls/dslx/constexpr_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class ConstexprEvaluator : public xls::dslx::ExprVisitor {
absl::Status HandleChannelDecl(const ChannelDecl* expr) override;
absl::Status HandleColonRef(const ColonRef* expr) override;
absl::Status HandleConstAssert(const ConstAssert* const_assert) override;
absl::Status HandleConstantArray(const ConstantArray* expr) override;
absl::Status HandleConstRef(const ConstRef* expr) override;
absl::Status HandleFor(const For* expr) override;
absl::Status HandleFunctionRef(const FunctionRef* expr) override;
Expand Down
15 changes: 1 addition & 14 deletions xls/dslx/frontend/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -728,19 +728,6 @@ Array::Array(Module* owner, Span span, std::vector<Expr*> members,
members_(std::move(members)),
has_ellipsis_(has_ellipsis) {}

ConstantArray::ConstantArray(Module* owner, Span span,
std::vector<Expr*> members, bool has_ellipsis,
bool in_parens)
: Array(owner, std::move(span), std::move(members), has_ellipsis,
in_parens) {
for (Expr* expr : this->members()) {
CHECK(IsConstant(expr))
<< "non-constant in constant array: " << expr->ToString();
}
}

ConstantArray::~ConstantArray() = default;

// -- class TypeRef

TypeRef::TypeRef(Module* owner, Span span, TypeDefinition type_definition)
Expand Down Expand Up @@ -913,7 +900,7 @@ SelfTypeAnnotation::~SelfTypeAnnotation() = default;
BuiltinNameDef::~BuiltinNameDef() = default;

bool IsConstant(AstNode* node) {
if (IsOneOf<ConstantArray, Number, ConstRef, ColonRef>(node)) {
if (IsOneOf<Number, ConstRef, ColonRef>(node)) {
return true;
}
if (IsOneOf<NameRef>(node)) {
Expand Down
25 changes: 3 additions & 22 deletions xls/dslx/frontend/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
X(Cast) \
X(ChannelDecl) \
X(ColonRef) \
X(ConstantArray) \
X(ConstRef) \
X(For) \
X(FormatMacro) \
Expand Down Expand Up @@ -1116,7 +1115,7 @@ class TypeAlias : public AstNode {
};

// Represents an array expression; e.g. `[a, b, c]`.
class Array : public Expr {
class Array final : public Expr {
public:
Array(Module* owner, Span span, std::vector<Expr*> members, bool has_ellipsis,
bool in_parens = false);
Expand Down Expand Up @@ -1168,22 +1167,6 @@ class Array : public Expr {
bool has_ellipsis_;
};

// A constant array expression is an array expression where all of the
// expressions contained within it are constant.
class ConstantArray : public Array {
public:
// Adds checking for constant-expression-ness of the members beyond
// Array::Array.
ConstantArray(Module* owner, Span span, std::vector<Expr*> members,
bool has_ellipsis, bool in_parens = false);

~ConstantArray() override;

absl::Status Accept(AstNodeVisitor* v) const override {
return v->HandleConstantArray(this);
}
};

// Several different AST nodes define types that can be referred to by a
// TypeRef.
using TypeDefinition =
Expand Down Expand Up @@ -3406,10 +3389,8 @@ class VerbatimNode : public Expr {
std::string text_;
};

// Helper for determining whether an AST node is constant (e.g., can be
// considered a constant value in a ConstantArray). In general a node
// with no children is considered constant, but there are some exceptions
// (e.g., NameRef).
// Helper for determining whether an AST node is constant (e.g., clearly can be
// considered a constant value before type checking).
bool IsConstant(AstNode* n);

} // namespace xls::dslx
Expand Down
17 changes: 0 additions & 17 deletions xls/dslx/frontend/ast_cloner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,6 @@ class AstCloner : public AstNodeVisitor {
return absl::OkStatus();
}

absl::Status HandleConstantArray(const ConstantArray* n) override {
XLS_RETURN_IF_ERROR(VisitChildren(n));

std::vector<Expr*> new_members;
for (const Expr* old_member : n->members()) {
new_members.push_back(down_cast<Expr*>(old_to_new_.at(old_member)));
}
ConstantArray* new_array = module_->Make<ConstantArray>(
n->span(), new_members, n->has_ellipsis(), n->in_parens());
if (n->type_annotation() != nullptr) {
new_array->set_type_annotation(
down_cast<TypeAnnotation*>(old_to_new_.at(n->type_annotation())));
}
old_to_new_[n] = new_array;
return absl::OkStatus();
}

absl::Status HandleConstantDef(const ConstantDef* n) override {
XLS_RETURN_IF_ERROR(VisitChildren(n));
TypeAnnotation* new_type_annotation = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/frontend/ast_cloner_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ fn main() -> foo::BAR {
EXPECT_EQ(kProgram, clone->ToString());
}

TEST(AstClonerTest, ConstantArray) {
TEST(AstClonerTest, IotaArray) {
constexpr std::string_view kProgram = R"(const ARRAY_SIZE = uN[32]:5;
fn main() -> u32[ARRAY_SIZE] {
([u32:0, u32:1, u32:2, u32:3, u32:4])
Expand Down
7 changes: 7 additions & 0 deletions xls/dslx/frontend/ast_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ TEST(AstTest, IsConstantNumber) {
EXPECT_TRUE(IsConstant(number));
}

// Tests the IsConstant predicate on an array with a single `name` reference.
//
// Since the name reference is not constant, the array is not constant.
TEST(AstTest, IsConstantArrayOfNameRefs) {
FileTable file_table;
const Span fake_span;
Expand All @@ -332,6 +335,8 @@ TEST(AstTest, IsConstantArrayOfNameRefs) {
EXPECT_FALSE(IsConstant(array));
}

// Tests the IsConstant predicate on an array with a single `number` literal.
// Since the number literal is constant, the array is constant.
TEST(AstTest, IsConstantArrayOfNumbers) {
FileTable file_table;
const Span fake_span;
Expand All @@ -344,6 +349,8 @@ TEST(AstTest, IsConstantArrayOfNumbers) {
EXPECT_TRUE(IsConstant(array));
}

// Tests the IsConstant predicate on an empty array.
// Since there are no members, the array is constant.
TEST(AstTest, IsConstantEmptyArray) {
FileTable file_table;
const Span fake_span;
Expand Down
11 changes: 7 additions & 4 deletions xls/dslx/frontend/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1217,10 +1217,6 @@ absl::StatusOr<Array*> Parser::ParseArray(Bindings& bindings) {
}

Span span(start_tok.span().start(), cbrack_pos);
if (std::all_of(exprs.begin(), exprs.end(), IsConstant)) {
return module_->Make<ConstantArray>(span, std::move(exprs),
has_trailing_ellipsis);
}
return module_->Make<Array>(span, std::move(exprs), has_trailing_ellipsis);
}

Expand Down Expand Up @@ -1272,11 +1268,18 @@ absl::StatusOr<Expr*> Parser::ParseCast(Bindings& bindings,
return term;
}

// This handles the case where we're trying to make a tuple constant that has
// an explicitly annotated type; e.g.
//
// ```dslx
// const MY_TUPLE = (u32, u64):(u32:32, u64:64);
// ```
if (auto* tuple = dynamic_cast<XlsTuple*>(term);
tuple != nullptr && std::all_of(tuple->members().begin(),
tuple->members().end(), IsConstant)) {
return term;
}

return ParseErrorStatus(
type->span(),
"Old-style cast only permitted for constant arrays/tuples "
Expand Down
10 changes: 5 additions & 5 deletions xls/dslx/frontend/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,7 @@ TEST_F(ParserTest, TupleArrayAndInt) {
auto* tuple = dynamic_cast<XlsTuple*>(e);
EXPECT_EQ(2, tuple->members().size());
auto* array = tuple->members()[0];
EXPECT_NE(dynamic_cast<ConstantArray*>(array), nullptr);
EXPECT_NE(dynamic_cast<Array*>(array), nullptr);
}

TEST_F(ParserTest, Cast) { RoundTripExpr("foo() as u32", {"foo"}); }
Expand Down Expand Up @@ -2424,9 +2424,9 @@ fn f(a: MyStruct) -> u32 {
})");
}

TEST_F(ParserTest, ConstantArray) {
TEST_F(ParserTest, ArrayOfConstantNumberLiterals) {
Expr* e = RoundTripExpr("u32[2]:[0, 1]", {}, false, std::nullopt);
ASSERT_TRUE(dynamic_cast<ConstantArray*>(e) != nullptr);
ASSERT_TRUE(dynamic_cast<Array*>(e) != nullptr);
}

TEST_F(ParserTest, MixedArrayIsNotConstantArray) {
Expand All @@ -2447,9 +2447,9 @@ const b = u32[2]:[a, a];)");
ASSERT_TRUE(dynamic_cast<Array*>(array) != nullptr);
}

TEST_F(ParserTest, EmptyArrayIsConstant) {
TEST_F(ParserTest, EmptyArray) {
Expr* e = RoundTripExpr("u32[2]:[]", {}, false, std::nullopt);
ASSERT_TRUE(dynamic_cast<ConstantArray*>(e) != nullptr);
ASSERT_TRUE(dynamic_cast<Array*>(e) != nullptr);
}

TEST_F(ParserTest, DoubleNegation) { RoundTripExpr("!!x", {"x"}, false); }
Expand Down
4 changes: 0 additions & 4 deletions xls/dslx/ir_convert/extract_conversion_order.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,6 @@ class InvocationVisitor : public ExprVisitor {
return absl::OkStatus();
}

absl::Status HandleConstantArray(const ConstantArray* expr) override {
return HandleArray(expr);
}

absl::Status HandleFor(const For* expr) override {
XLS_RETURN_IF_ERROR(expr->init()->AcceptExpr(this));
XLS_RETURN_IF_ERROR(expr->iterable()->AcceptExpr(this));
Expand Down
9 changes: 0 additions & 9 deletions xls/dslx/ir_convert/function_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ class FunctionConverterVisitor : public AstNodeVisitor {
NO_TRAVERSE_DISPATCH_VISIT(Attr)
NO_TRAVERSE_DISPATCH_VISIT(Array)
NO_TRAVERSE_DISPATCH_VISIT(StatementBlock)
NO_TRAVERSE_DISPATCH_VISIT(ConstantArray)
NO_TRAVERSE_DISPATCH_VISIT(Cast)
NO_TRAVERSE_DISPATCH_VISIT(ColonRef)
NO_TRAVERSE_DISPATCH_VISIT(ConstantDef)
Expand Down Expand Up @@ -3699,14 +3698,6 @@ absl::StatusOr<Bits> FunctionConverter::GetConstBits(
return value.GetBitsWithStatus();
}

absl::Status FunctionConverter::HandleConstantArray(const ConstantArray* node) {
// Note: previously we would force constant evaluation here, but because all
// constexprs should be evaluated during typechecking, we shouldn't need to
// forcibly do constant evaluation at IR conversion time; therefore, we just
// build BValues and let XLS opt constant fold them.
return HandleArray(node);
}

absl::StatusOr<xls::Type*> FunctionConverter::ResolveTypeToIr(
const AstNode* node) {
XLS_ASSIGN_OR_RETURN(std::unique_ptr<Type> type, ResolveType(node));
Expand Down
1 change: 0 additions & 1 deletion xls/dslx/ir_convert/function_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ class FunctionConverter {
absl::Status HandleStatementBlock(const StatementBlock* node);
absl::Status HandleCast(const Cast* node);
absl::Status HandleColonRef(const ColonRef* node);
absl::Status HandleConstantArray(const ConstantArray* node);
absl::Status HandleConstantDef(const ConstantDef* node);
absl::Status HandleFor(const For* node);
absl::Status HandleFormatMacro(const FormatMacro* node);
Expand Down
1 change: 0 additions & 1 deletion xls/dslx/type_system/deduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,6 @@ class DeduceVisitor : public AstNodeVisitor {
DEDUCE_DISPATCH(Attr, DeduceAttr)
DEDUCE_DISPATCH(StatementBlock, DeduceStatementBlock)
DEDUCE_DISPATCH(ChannelDecl, DeduceChannelDecl)
DEDUCE_DISPATCH(ConstantArray, DeduceConstantArray)
DEDUCE_DISPATCH(ColonRef, DeduceColonRef)
DEDUCE_DISPATCH(Index, DeduceIndex)
DEDUCE_DISPATCH(Match, DeduceMatch)
Expand Down
Loading