Skip to content

Commit bcdfc12

Browse files
Merge pull request #1901 from xlsynth:cdleary/2025-01-30-constexpr-match-namerefs
PiperOrigin-RevId: 721824651
2 parents 5c08f4a + 77947e8 commit bcdfc12

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

xls/dslx/bytecode/bytecode_emitter_test.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,7 @@ fn unops() {
508508
// Tests array creation.
509509
TEST(BytecodeEmitterTest, Arrays) {
510510
constexpr std::string_view kProgram = R"(#[test]
511-
fn arrays() -> u32[3] {
512-
let a = u32:32;
511+
fn arrays(a: u32) -> u32[3] {
513512
u32[3]:[u32:0, u32:1, a]
514513
}
515514
)";
@@ -519,8 +518,8 @@ fn arrays() -> u32[3] {
519518
EmitBytecodes(&import_data, kProgram, "arrays"));
520519

521520
const std::vector<Bytecode>& bytecodes = bf->bytecodes();
522-
ASSERT_EQ(bytecodes.size(), 6);
523-
const Bytecode* bc = &bytecodes[5];
521+
ASSERT_EQ(bytecodes.size(), 4);
522+
const Bytecode* bc = &bytecodes[3];
524523
ASSERT_EQ(bc->op(), Bytecode::Op::kCreateArray);
525524
ASSERT_TRUE(bc->has_data());
526525
XLS_ASSERT_OK_AND_ASSIGN(Bytecode::NumElements num_elements,

xls/dslx/frontend/ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ class AnyTypeAnnotation : public TypeAnnotation {
638638
return {};
639639
}
640640

641-
std::string ToString() const { return "Any"; };
641+
std::string ToString() const override { return "Any"; };
642642
};
643643

644644
// Represents an array type annotation; e.g. `u32[5]`.

xls/dslx/type_system/deduce.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,11 @@ absl::StatusOr<std::unique_ptr<Type>> DeduceNameRef(const NameRef* node,
19531953
AstNode* name_def = ToAstNode(node->name_def());
19541954
XLS_RET_CHECK(name_def != nullptr);
19551955

1956+
if (std::optional<InterpValue> const_expr =
1957+
ctx->type_info()->GetConstExprOption(name_def)) {
1958+
ctx->type_info()->NoteConstExpr(node, const_expr.value());
1959+
}
1960+
19561961
std::optional<Type*> item = ctx->type_info()->GetItem(name_def);
19571962
if (item.has_value()) {
19581963
auto type = (*item)->CloneToUnique();

xls/dslx/type_system/typecheck_module_test.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4235,6 +4235,56 @@ fn test_f() {
42354235
XLS_EXPECT_OK(Typecheck(kProgram));
42364236
}
42374237

4238+
TEST(TypecheckTest, MatchPackageLevelConstant) {
4239+
constexpr std::string_view kProgram = R"(
4240+
const FOO = u8:0xff;
4241+
fn f(x: u8) -> u2 {
4242+
match x {
4243+
FOO => u2:0,
4244+
_ => u2:0,
4245+
}
4246+
}
4247+
)";
4248+
XLS_ASSERT_OK_AND_ASSIGN(TypecheckResult result, Typecheck(kProgram));
4249+
const TypeInfo& type_info = *result.tm.type_info;
4250+
// Get the pattern match for the first arm of the match expression inside of
4251+
// function `f`.
4252+
Function* f = result.tm.module->GetFunction("f").value();
4253+
Statement* stmt = f->body()->statements()[0];
4254+
Expr* expr = std::get<Expr*>(stmt->wrapped());
4255+
Match* m = dynamic_cast<Match*>(expr);
4256+
ASSERT_NE(m, nullptr);
4257+
const MatchArm* arm = m->arms()[0];
4258+
const NameDefTree* pattern = arm->patterns()[0];
4259+
4260+
// Check that the pattern is just a leaf NameRef.
4261+
NameRef* name_ref = std::get<NameRef*>(pattern->leaf());
4262+
ASSERT_NE(name_ref, nullptr);
4263+
EXPECT_EQ(name_ref->identifier(), "FOO");
4264+
4265+
std::optional<InterpValue> const_expr =
4266+
type_info.GetConstExprOption(name_ref);
4267+
ASSERT_TRUE(const_expr.has_value());
4268+
EXPECT_EQ(const_expr->ToString(), "u8:255");
4269+
}
4270+
4271+
TEST(TypecheckTest, MatchPackageLevelConstantIntoTypeAlias) {
4272+
constexpr std::string_view kProgram = R"(
4273+
const C = u32:2;
4274+
fn f(x: u32) -> u2 {
4275+
match x {
4276+
C => {
4277+
const D = C;
4278+
type T = uN[D];
4279+
T::MAX
4280+
},
4281+
_ => u2:0,
4282+
}
4283+
}
4284+
)";
4285+
XLS_EXPECT_OK(Typecheck(kProgram));
4286+
}
4287+
42384288
// Table-oriented test that lets us validate that *types on parameters* are
42394289
// compatible with *particular values* that should be type-compatible.
42404290
TEST(PassValueToIdentityFnTest, ParameterVsValue) {

0 commit comments

Comments
 (0)