From 667d6ed6ff43ca4ed5b733550b5307333af76807 Mon Sep 17 00:00:00 2001 From: Andrew Adams Date: Mon, 16 Oct 2023 10:12:50 -0700 Subject: [PATCH] Check for overflow in Type constructor (#7889) * Check for overflow in Type constructor * Don't try to construct illegal types --- src/Type.h | 4 ++++ test/correctness/vector_reductions.cpp | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Type.h b/src/Type.h index 63e65bd7f771..af5447350810 100644 --- a/src/Type.h +++ b/src/Type.h @@ -304,6 +304,10 @@ struct Type { * lanes: The number of vector elements in the type. */ Type(halide_type_code_t code, int bits, int lanes, const halide_handle_cplusplus_type *handle_type = nullptr) : type(code, (uint8_t)bits, (uint16_t)lanes), handle_type(handle_type) { + user_assert(lanes == type.lanes) + << "Halide only supports vector types with up to 65535 lanes. " << lanes << " lanes requested."; + user_assert(bits == type.bits) + << "Halide only supports types with up to 255 bits. " << bits << " bits requested."; } /** Trivial copy constructor. */ diff --git a/test/correctness/vector_reductions.cpp b/test/correctness/vector_reductions.cpp index b7b4e85354ce..f1c250cfec3d 100644 --- a/test/correctness/vector_reductions.cpp +++ b/test/correctness/vector_reductions.cpp @@ -19,7 +19,11 @@ void add_tasks(const Target &target, std::vector &tasks) { const int src_lanes = dst_lanes * reduce_factor; for (Type src_type : types) { for (int widen_factor : {1, 2, 4}) { - Type dst_type = src_type.with_bits(src_type.bits() * widen_factor); + int dst_bits = src_type.bits() * widen_factor; + if (dst_bits > 64) { + continue; + } + Type dst_type = src_type.with_bits(dst_bits); if (std::find(types.begin(), types.end(), dst_type) == types.end()) { continue; }