Skip to content

Commit e0073f6

Browse files
cdlearycopybara-github
authored andcommitted
[DSLX:TS][cleanup] Remove unnecessary "order" in parametric instantiation.
The parametric order is now implied by the parametrics as declared, since parametrics are evaluated from left to right. This is part of the general effort to move away from string-oriented parametric environments (as keys) and towards AST-node-oriented parametric environments. (When we have fully migrated to AST-node-oriented parametric environments we should be able to remove ParametricSymbols as ConcreteTypeDims.) Historical note: previously we would attempt to do more of a "constraint satisfaction" arbitrary ordering, but the value-to-confusion ratio of that approach is not good, so we settled on this left-to-right evaluation. PiperOrigin-RevId: 625088126
1 parent 93244c0 commit e0073f6

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

xls/dslx/type_system/parametric_instantiator_internal.cc

+19-25
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ absl::StatusOr<InterpValue> InterpretExpr(DeduceCtx* ctx, Expr* expr,
142142
// asserting that their values are consistent with other bindings (via argument
143143
// types).
144144
absl::Status EagerlyPopulateParametricEnvMap(
145-
absl::Span<const std::string> parametric_order,
145+
absl::Span<const ParametricWithType> typed_parametrics,
146146
const absl::flat_hash_map<std::string, Expr*>& parametric_default_exprs,
147147
absl::flat_hash_map<std::string, InterpValue>& parametric_env_map,
148148
const Span& span, std::string_view kind_name, DeduceCtx* ctx) {
149149
// Attempt to interpret the parametric "default expressions" in order.
150-
for (const auto& name : parametric_order) {
150+
for (const ParametricWithType& typed_parametric : typed_parametrics) {
151+
std::string_view name = typed_parametric.identifier();
152+
151153
Expr* expr = nullptr;
152154
if (auto it = parametric_default_exprs.find(name);
153155
it != parametric_default_exprs.end() && it->second != nullptr) {
@@ -205,7 +207,7 @@ absl::Status EagerlyPopulateParametricEnvMap(
205207
return TypeInferenceErrorStatus(span, nullptr, message);
206208
}
207209
} else {
208-
parametric_env_map.insert({name, result.value()});
210+
parametric_env_map.insert({std::string{name}, result.value()});
209211
}
210212
}
211213
return absl::OkStatus();
@@ -222,6 +224,7 @@ ParametricInstantiator::ParametricInstantiator(
222224
: span_(std::move(span)),
223225
args_(args),
224226
ctx_(ABSL_DIE_IF_NULL(ctx)),
227+
typed_parametrics_(typed_parametrics),
225228
parametric_env_map_(explicit_parametrics),
226229
parametric_bindings_(parametric_bindings) {
227230
// We add derived type information so we can resolve types based on
@@ -233,10 +236,6 @@ ParametricInstantiator::ParametricInstantiator(
233236
// interpret the expression to an InterpValue.
234237
derived_type_info_ = ctx_->AddDerivedTypeInfo();
235238

236-
// Explicit parametric expressions are conceptually evaluated before other
237-
// parametric expressions.
238-
absl::flat_hash_set<std::string> ordered;
239-
240239
// Note: the first N explicit parametrics (given by the map) must be the first
241240
// N parametrics for the thing we're instantiating; i.e. we can only
242241
// explicitly instantiate from the "left hand side" of the parametric
@@ -250,28 +249,21 @@ ParametricInstantiator::ParametricInstantiator(
250249
for (size_t i = 0; i < explicit_parametrics.size(); ++i) {
251250
const std::string& identifier = parametric_bindings[i]->identifier();
252251
CHECK(explicit_parametrics.contains(identifier));
253-
parametric_order_.push_back(identifier);
254-
ordered.insert(identifier);
255252
}
256253

257-
VLOG(5) << "ParametricInstantiator; span: " << span_ << " ordered: ["
258-
<< absl::StrJoin(ordered, ", ") << "]";
254+
VLOG(5) << "ParametricInstantiator; span: " << span_;
259255

260256
for (const ParametricWithType& parametric : typed_parametrics) {
261-
std::string_view identifier = parametric.identifier();
262-
if (!ordered.contains(identifier)) {
263-
parametric_order_.push_back(std::string{identifier});
264-
ordered.insert(std::string{identifier});
257+
if (parametric.expr() != nullptr) {
258+
ctx_->type_info()->SetItem(parametric.expr(), parametric.type());
265259
}
266260

261+
std::string_view identifier = parametric.identifier();
267262
std::unique_ptr<Type> parametric_expr_type =
268263
parametric.type().CloneToUnique();
269-
270-
if (parametric.expr() != nullptr) {
271-
ctx_->type_info()->SetItem(parametric.expr(), *parametric_expr_type);
272-
}
273264
parametric_binding_types_.emplace(identifier,
274265
std::move(parametric_expr_type));
266+
275267
parametric_default_exprs_[identifier] = parametric.expr();
276268
}
277269
}
@@ -345,7 +337,7 @@ absl::StatusOr<TypeAndParametricEnv> FunctionInstantiator::Instantiate() {
345337
}
346338

347339
XLS_RETURN_IF_ERROR(EagerlyPopulateParametricEnvMap(
348-
parametric_order(), parametric_default_exprs(), parametric_env_map(),
340+
typed_parametrics(), parametric_default_exprs(), parametric_env_map(),
349341
span(), GetKindName(), &ctx()));
350342

351343
// Phase 2: resolve and check.
@@ -382,8 +374,9 @@ absl::StatusOr<TypeAndParametricEnv> FunctionInstantiator::Instantiate() {
382374

383375
parametric_env_expr_scope.Finish();
384376

385-
return TypeAndParametricEnv{std::move(resolved),
386-
ParametricEnv(parametric_env_map())};
377+
return TypeAndParametricEnv{
378+
.type = std::move(resolved),
379+
.parametric_env = ParametricEnv(parametric_env_map())};
387380
}
388381

389382
/* static */ absl::StatusOr<std::unique_ptr<StructInstantiator>>
@@ -409,7 +402,7 @@ absl::StatusOr<TypeAndParametricEnv> StructInstantiator::Instantiate() {
409402
}
410403

411404
XLS_RETURN_IF_ERROR(EagerlyPopulateParametricEnvMap(
412-
parametric_order(), parametric_default_exprs(), parametric_env_map(),
405+
typed_parametrics(), parametric_default_exprs(), parametric_env_map(),
413406
span(), GetKindName(), &ctx()));
414407

415408
// Phase 2: resolve and check.
@@ -427,8 +420,9 @@ absl::StatusOr<TypeAndParametricEnv> StructInstantiator::Instantiate() {
427420

428421
XLS_ASSIGN_OR_RETURN(std::unique_ptr<Type> resolved,
429422
Resolve(*struct_type_, parametric_env_map()));
430-
return TypeAndParametricEnv{std::move(resolved),
431-
ParametricEnv(parametric_env_map())};
423+
return TypeAndParametricEnv{
424+
.type = std::move(resolved),
425+
.parametric_env = ParametricEnv(parametric_env_map())};
432426
}
433427

434428
} // namespace internal

xls/dslx/type_system/parametric_instantiator_internal.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,10 @@ class ParametricInstantiator {
103103
return parametric_default_exprs_;
104104
}
105105

106-
absl::Span<const std::string> parametric_order() const {
107-
return parametric_order_;
108-
}
109-
110106
absl::Span<const InstantiateArg> args() const { return args_; }
107+
absl::Span<const ParametricWithType> typed_parametrics() const {
108+
return typed_parametrics_;
109+
}
111110

112111
const Span& span() const { return span_; }
113112
DeduceCtx& ctx() { return *ctx_; }
@@ -130,8 +129,7 @@ class ParametricInstantiator {
130129
// types in this instantiation.
131130
TypeInfo* derived_type_info_ = nullptr;
132131

133-
// Notes the iteration order in the original parametric bindings.
134-
std::vector<std::string> parametric_order_;
132+
absl::Span<const ParametricWithType> typed_parametrics_;
135133

136134
// Note: the expressions may be null (e.g. when the parametric binding has no
137135
// "default" expression and must be provided by the user).

0 commit comments

Comments
 (0)