Skip to content

Commit

Permalink
Check for overflow in Type constructor (halide#7889)
Browse files Browse the repository at this point in the history
* Check for overflow in Type constructor

* Don't try to construct illegal types
  • Loading branch information
abadams committed Oct 16, 2023
1 parent 7e35494 commit 667d6ed
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
6 changes: 5 additions & 1 deletion test/correctness/vector_reductions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ void add_tasks(const Target &target, std::vector<Task> &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;
}
Expand Down

0 comments on commit 667d6ed

Please sign in to comment.