Skip to content

Commit

Permalink
Enable references to global constants in parametric defaults.
Browse files Browse the repository at this point in the history
This basically requires more careful control of the order of conversion from InferenceTable to TypeInfo, because ConstexprEvaluator presumes all prerequisites are proactively loaded.

This also changes inference_table_test to be more of a pure unit test for the table functions, because we are now at the point where equivalent end-to-end testing is being done with less hackery in typecheck_module_v2_test. The hackery previously in inference_table_test relied on the table being the source of node order.

PiperOrigin-RevId: 709194186
  • Loading branch information
richmckeever authored and copybara-github committed Dec 24, 2024
1 parent b959a4e commit dbffe11
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 338 deletions.
7 changes: 3 additions & 4 deletions xls/dslx/type_system_v2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ cc_library(
"//xls/dslx/frontend:ast",
"//xls/dslx/frontend:ast_node",
"//xls/dslx/frontend:ast_node_visitor_with_default",
"//xls/dslx/frontend:ast_utils",
"//xls/dslx/frontend:module",
"//xls/dslx/frontend:pos",
"@com_google_absl//absl/algorithm:container",
Expand All @@ -50,6 +49,7 @@ cc_library(
deps = [
":inference_table",
":type_annotation_utils",
"//xls/common:visitor",
"//xls/common/status:status_macros",
"//xls/dslx:constexpr_evaluator",
"//xls/dslx:errors",
Expand All @@ -58,7 +58,7 @@ cc_library(
"//xls/dslx:warning_collector",
"//xls/dslx/frontend:ast",
"//xls/dslx/frontend:ast_cloner",
"//xls/dslx/frontend:ast_utils",
"//xls/dslx/frontend:ast_node_visitor_with_default",
"//xls/dslx/frontend:module",
"//xls/dslx/frontend:pos",
"//xls/dslx/type_system:deduce_utils",
Expand All @@ -74,6 +74,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:variant",
],
)

Expand All @@ -90,15 +91,13 @@ cc_test(
"//xls/common/status:status_macros",
"//xls/dslx:create_import_data",
"//xls/dslx:import_data",
"//xls/dslx:interp_value",
"//xls/dslx:warning_collector",
"//xls/dslx:warning_kind",
"//xls/dslx/frontend:ast",
"//xls/dslx/frontend:module",
"//xls/dslx/frontend:parser",
"//xls/dslx/frontend:pos",
"//xls/dslx/frontend:scanner",
"//xls/dslx/type_system:parametric_env",
"//xls/dslx/type_system:type_info",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:check",
Expand Down
24 changes: 0 additions & 24 deletions xls/dslx/type_system_v2/inference_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "xls/dslx/errors.h"
#include "xls/dslx/frontend/ast.h"
#include "xls/dslx/frontend/ast_node_visitor_with_default.h"
#include "xls/dslx/frontend/ast_utils.h"
#include "xls/dslx/frontend/module.h"
#include "xls/dslx/frontend/pos.h"

Expand Down Expand Up @@ -275,29 +274,6 @@ class InferenceTableImpl : public InferenceTable {
node, [=](NodeData& data) { data.type_variable = variable; });
}

NodesByParametricInvocation GetNodesByParametricInvocation() const override {
NodesByParametricInvocation result;
absl::flat_hash_set<const AstNode*> static_nodes;
for (const auto& [node, node_data] : node_data_) {
static_nodes.insert(node);
}
for (const std::unique_ptr<ParametricInvocation>& invocation :
parametric_invocations_) {
absl::flat_hash_set<const AstNode*> nodes =
FlattenToSet(&invocation->callee());
nodes.erase(&invocation->callee());
static_nodes.erase(&invocation->callee());
for (const AstNode* node : nodes) {
static_nodes.erase(node);
}
result.emplace_back(invocation.get(),
FilterAndConvertNodeSetToOrderedVector(nodes));
}
result.emplace_back(std::nullopt,
FilterAndConvertNodeSetToOrderedVector(static_nodes));
return result;
}

std::optional<const TypeAnnotation*> GetTypeAnnotation(
const AstNode* node) const override {
const auto it = node_data_.find(node);
Expand Down
14 changes: 1 addition & 13 deletions xls/dslx/type_system_v2/inference_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/substitute.h"
Expand Down Expand Up @@ -183,8 +182,7 @@ class InferenceTable {
std::optional<const Function*> caller,
std::optional<const ParametricInvocation*> caller_invocation) = 0;

// Retrieves all the parametric invocations that have been defined for all
// parametric functions.
// Retrieves all the parametric invocations that have been defined.
virtual std::vector<const ParametricInvocation*> GetParametricInvocations()
const = 0;

Expand All @@ -208,16 +206,6 @@ class InferenceTable {
virtual absl::Status SetTypeAnnotation(const AstNode* node,
const TypeAnnotation* type) = 0;

using NodesByParametricInvocation =
std::vector<std::pair<std::optional<const ParametricInvocation*>,
std::vector<const AstNode*>>>;

// Returns a table of parametric invocation to the nodes in the callee
// function, plus an element at the end for all nodes not in a parametric
// context.
virtual NodesByParametricInvocation GetNodesByParametricInvocation()
const = 0;

// Returns the type annotation for `node` in the table, if any.
virtual std::optional<const TypeAnnotation*> GetTypeAnnotation(
const AstNode* node) const = 0;
Expand Down
Loading

0 comments on commit dbffe11

Please sign in to comment.