Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xls/dslx/type_system_v2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ cc_library(
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:statusor",
"@abseil-cpp//absl/strings",
],
)

Expand Down
6 changes: 6 additions & 0 deletions xls/dslx/type_system_v2/fast_concretizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/substitute.h"
#include "xls/common/status/ret_check.h"
#include "xls/common/status/status_macros.h"
#include "xls/dslx/frontend/ast.h"
Expand Down Expand Up @@ -53,6 +54,11 @@ class FastConcretizerImpl : public FastConcretizer,
GetBool(signedness_and_bit_count->signedness));
XLS_ASSIGN_OR_RETURN(uint32_t bit_count,
GetU32(signedness_and_bit_count->bit_count));
if (bit_count > kMaxBitCount) {
return absl::InvalidArgumentError(
absl::Substitute("Bit count $0 exceeds maximum limit of $1.",
bit_count, kMaxBitCount));
}
return std::make_unique<BitsType>(is_signed, bit_count);
}

Expand Down
7 changes: 7 additions & 0 deletions xls/dslx/type_system_v2/inference_table_converter_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,13 @@ class InferenceTableConverterImpl : public InferenceTableConverter,
int64_t bit_count,
evaluator_->EvaluateU32OrExpr(parametric_context,
signedness_and_bit_count.bit_count));
if (bit_count > kMaxBitCount) {
return TypeInferenceErrorStatusForAnnotation(
annotation->span(), annotation,
absl::Substitute("Bit count $0 exceeds maximum limit of $1.",
bit_count, kMaxBitCount),
file_table_);
}
if (node) {
// If resulting type is bits-like, we fabricate a simplified type
// annotation for the node.
Expand Down
5 changes: 5 additions & 0 deletions xls/dslx/type_system_v2/type_annotation_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

namespace xls::dslx {

// The maximum bit count for a type (e.g., array of bits). Too many more bits
// and the compiler itself will run out of memory. Besides, designs will likely
// not have a use case for a type with more bits than this.
inline constexpr int64_t kMaxBitCount = 1000000;

struct StructOrProcRef {
const StructDefBase* def;
std::vector<ExprOrType> parametrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1813,5 +1813,14 @@ const X = A..s8:3;
"is greater than end value 3")));
}

TEST(TypecheckV2Test, TooBigConstant) {
EXPECT_THAT(
R"(
const too_big = -u32:19;
const too_bit_array = sN[too_big]:-1;
)",
TypecheckFails(HasSubstr("Bit count 4294967277 exceeds maximum limit of "
"2147483647.")));
}
} // namespace
} // namespace xls::dslx
Loading