Skip to content

Commit

Permalink
Revert "Fix ConcretizeStructAnnotation to deal with deps of default p…
Browse files Browse the repository at this point in the history
…arametric exprs."

This reverts commit afc720d.
  • Loading branch information
rw1nkler committed Sep 18, 2024
1 parent a4b474a commit 72e55dd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 87 deletions.
46 changes: 0 additions & 46 deletions xls/dslx/ir_convert/ir_converter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -885,52 +885,6 @@ fn f(outer_thing_1: u32, outer_thing_2: u32) -> u32 {
ExpectIr(converted, TestName());
}

TEST(IrConverterTest, ParametricDefaultInStruct) {
const char* kProgram = R"(
struct Foo <X: u32, Y: u32 = {X + u32:1}> {
a: uN[X],
b: uN[Y],
}
fn make_zero_foo<X: u32>() -> Foo<X> {
zero!<Foo<X>>()
}
fn test() -> Foo<u32:5> {
make_zero_foo<u32:5>()
}
)";

XLS_ASSERT_OK_AND_ASSIGN(
std::string converted,
ConvertModuleForTest(kProgram, ConvertOptions{.emit_positions = false}));
ExpectIr(converted, TestName());
}

TEST(IrConverterTest, ParametricDefaultClog2InStruct) {
const char* kProgram = R"(
import std;
struct Foo <X: u32, Y: u32 = {std::clog2(X)}> {
a: uN[X],
b: uN[Y],
}
fn make_zero_foo<X: u32>() -> Foo<X> {
zero!<Foo<X>>()
}
fn test() -> Foo<u32:5> {
make_zero_foo<u32:5>()
}
)";

XLS_ASSERT_OK_AND_ASSIGN(
std::string converted,
ConvertModuleForTest(kProgram, ConvertOptions{.emit_positions = false}));
ExpectIr(converted, TestName());
}

TEST(IrConverterTest, UnrollForSimple) {
const char* kProgram = R"(
fn test() -> u32 {
Expand Down

This file was deleted.

35 changes: 6 additions & 29 deletions xls/dslx/type_system/deduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,6 @@ absl::StatusOr<InterpValue> EvaluateConstexprValue(DeduceCtx* ctx,
ctx->GetCurrentParametricEnv(), node, type);
}

// Evaluates the given `dim` to the extent possible with the given `env`.
// Examples:
// - If `dim` is already a constant, the result is `dim`.
// - If `dim` is `X + 1`, and `env` says `X` == `5`, then the result is `6`.
// - If `dim` is `X + 1`, and `env` says `X` == `Y` and does not have `Y`,
// then the result is `Y + 1`.
// - If `dim` is `X + 1`, and `env` says `X` == `Y` and `Y` == `5`, then the
// result is `6`.
TypeDim EvaluateTypeDim(TypeDim dim, const ParametricExpression::Env& env) {
absl::flat_hash_set<std::string> prev_free_variables;
while (std::holds_alternative<TypeDim::OwnedParametric>(dim.value())) {
auto& parametric = std::get<TypeDim::OwnedParametric>(dim.value());
absl::flat_hash_set<std::string> next_free_variables =
parametric->GetFreeVariables();
if (prev_free_variables == next_free_variables) {
break;
}
prev_free_variables = std::move(next_free_variables);
dim = TypeDim(parametric->Evaluate(env));
}
return dim;
}

// Attempts to convert an expression from the full DSL AST into the
// ParametricExpression sub-AST (a limited form that we can embed into a
// TypeDim for later instantiation).
Expand Down Expand Up @@ -1843,10 +1820,6 @@ static absl::StatusOr<std::unique_ptr<Type>> ConcretizeStructAnnotation(
struct_def->identifier()),
ctx->file_table());
}
XLS_ASSIGN_OR_RETURN(std::unique_ptr<ParametricExpression> parametric_expr,
ExprToParametric(defined_parametric->expr(), ctx));
parametric_env.emplace(defined_parametric->identifier(),
TypeDim(std::move(parametric_expr)));
}

ParametricExpression::Env env;
Expand All @@ -1862,15 +1835,19 @@ static absl::StatusOr<std::unique_ptr<Type>> ConcretizeStructAnnotation(
XLS_ASSIGN_OR_RETURN(
std::unique_ptr<Type> mapped_type,
base_type.MapSize([&](const TypeDim& dim) -> absl::StatusOr<TypeDim> {
return EvaluateTypeDim(dim, env);
if (std::holds_alternative<TypeDim::OwnedParametric>(dim.value())) {
auto& parametric = std::get<TypeDim::OwnedParametric>(dim.value());
return TypeDim(parametric->Evaluate(env));
}
return dim;
}));

VLOG(5) << "ConcreteStructAnnotation mapped type: "
<< mapped_type->ToString();

// Attach the nominal parametrics to the type, so that we will remember the
// fact that we have instantiated e.g. Foo<M:u32, N:u32> as Foo<5, 6>.
return mapped_type->AddNominalTypeDims(parametric_env);
return mapped_type->AddNominalTypeDims(std::move(parametric_env));
}

absl::StatusOr<std::unique_ptr<Type>> DeduceTypeRefTypeAnnotation(
Expand Down

0 comments on commit 72e55dd

Please sign in to comment.