Skip to content

Commit

Permalink
Record the value of impl constants as the colon ref.
Browse files Browse the repository at this point in the history
#1277

PiperOrigin-RevId: 688136490
  • Loading branch information
erinzmoore authored and copybara-github committed Oct 21, 2024
1 parent 9997e09 commit fd70f55
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
8 changes: 2 additions & 6 deletions xls/dslx/bytecode/bytecode_emitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -862,12 +862,8 @@ absl::StatusOr<InterpValue> BytecodeEmitter::HandleColonRefInternal(
return HandleColonRefToValue(module, node);
},
[&](Impl* impl) -> absl::StatusOr<InterpValue> {
std::optional<ConstantDef*> constant_def =
impl->GetConstant(node->attr());
XLS_RET_CHECK(constant_def.has_value());
return type_info_->GetConstExpr(constant_def.value());
},
},
return type_info_->GetConstExpr(node);
}},
resolved_subject);
}

Expand Down
12 changes: 1 addition & 11 deletions xls/dslx/constexpr_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,7 @@ absl::Status ConstexprEvaluator::HandleColonRef(const ColonRef* expr) {
return absl::OkStatus();
},
[&](Impl* impl) -> absl::Status {
std::optional<ConstantDef*> constant_def =
impl->GetConstant(expr->attr());
XLS_RET_CHECK(constant_def.has_value());
XLS_RETURN_IF_ERROR(Evaluate(import_data_, type_info_,
warning_collector_, bindings_,
constant_def.value()->value()));
XLS_RET_CHECK(
type_info_->IsKnownConstExpr(constant_def.value()->value()));
type_info_->NoteConstExpr(
expr, type_info_->GetConstExpr(constant_def.value()->value())
.value());
XLS_RET_CHECK(type_info_->IsKnownConstExpr(expr));
return absl::OkStatus();
},
},
Expand Down
8 changes: 2 additions & 6 deletions xls/dslx/ir_convert/function_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2822,12 +2822,8 @@ absl::Status FunctionConverter::HandleColonRef(const ColonRef* node) {
return absl::OkStatus();
},
[&](Impl* impl) -> absl::Status {
std::optional<ConstantDef*> constant_def =
impl->GetConstant(node->attr());
XLS_RET_CHECK(constant_def.has_value());
XLS_ASSIGN_OR_RETURN(
InterpValue iv,
current_type_info_->GetConstExpr(constant_def.value()));
XLS_ASSIGN_OR_RETURN(InterpValue iv,
current_type_info_->GetConstExpr(node));
XLS_ASSIGN_OR_RETURN(Value value, InterpValueToValue(iv));
DefConst(node, value);
return absl::OkStatus();
Expand Down
30 changes: 17 additions & 13 deletions xls/dslx/type_system/deduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,25 +1026,22 @@ static absl::StatusOr<std::unique_ptr<Type>> DeduceColonRefToArrayType(
}

static absl::StatusOr<std::unique_ptr<Type>> DeduceColonRefToImpl(
StructDef* struct_def, const ColonRef* node, DeduceCtx* ctx) {
Impl* impl, const ColonRef* node, DeduceCtx* ctx) {
VLOG(5) << "DeduceColonRefToImpl: " << node->ToString();
if (!struct_def->impl().has_value()) {
return TypeInferenceErrorStatus(
node->span(), nullptr,
absl::StrFormat("Struct '%s' has no impl defining '%s'",
struct_def->identifier(), node->attr()),
ctx->file_table());
}
Impl* impl = struct_def->impl().value();
std::optional<ConstantDef*> constant = impl->GetConstant(node->attr());
if (!constant.has_value()) {
return TypeInferenceErrorStatus(
node->span(), nullptr,
absl::StrFormat("Name '%s' is not defined by the impl for struct '%s'.",
node->attr(), struct_def->identifier()),
node->attr(), impl->struct_ref()->ToString()),
ctx->file_table());
}
return ctx->Deduce(constant.value());
std::optional<Type*> type = ctx->type_info()->GetItem(constant.value());
XLS_RET_CHECK(type.has_value());
XLS_ASSIGN_OR_RETURN(InterpValue value,
ctx->type_info()->GetConstExpr(constant.value()));
ctx->type_info()->NoteConstExpr(node, value);
return type.value()->CloneToUnique();
}

absl::StatusOr<std::unique_ptr<Type>> DeduceColonRef(const ColonRef* node,
Expand Down Expand Up @@ -1098,8 +1095,15 @@ absl::StatusOr<std::unique_ptr<Type>> DeduceColonRef(const ColonRef* node,
return DeduceColonRefToArrayType(type, node, subject_ctx.get());
},
[&](StructDef* struct_def) -> ReturnT {
return DeduceColonRefToImpl(struct_def, node,
subject_ctx.get());
if (!struct_def->impl().has_value()) {
return TypeInferenceErrorStatus(
node->span(), nullptr,
absl::StrFormat("Struct '%s' has no impl defining '%s'",
struct_def->identifier(), node->attr()),
ctx->file_table());
}
Impl* impl = struct_def->impl().value();
return DeduceColonRefToImpl(impl, node, subject_ctx.get());
},
[&](ColonRef* colon_ref) -> ReturnT {
// Note: this should be unreachable, as it's a colon-reference
Expand Down
2 changes: 2 additions & 0 deletions xls/dslx/type_system/deduce_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ absl::StatusOr<std::unique_ptr<Type>> DeduceConstantArray(

absl::StatusOr<std::unique_ptr<Type>> DeduceNumber(const Number* node,
DeduceCtx* ctx) {
VLOG(5) << "DeduceNumber: " << node->ToString();
auto note_constexpr_value = [&](const Type& type) -> absl::Status {
if (type.HasParametricDims()) {
return absl::OkStatus();
Expand Down Expand Up @@ -610,6 +611,7 @@ static absl::StatusOr<std::unique_ptr<Type>> DeduceConcat(const Binop* node,

absl::StatusOr<std::unique_ptr<Type>> DeduceBinop(const Binop* node,
DeduceCtx* ctx) {
VLOG(5) << "DeduceBinop: " << node->ToString();
if (node->binop_kind() == BinopKind::kConcat) {
return DeduceConcat(node, ctx);
}
Expand Down

0 comments on commit fd70f55

Please sign in to comment.