Skip to content

Commit

Permalink
Fix underflow in highestPowOf2Divisor() (#4562)
Browse files Browse the repository at this point in the history
Prevent highestPowOf2Divisor() from producing an underflow when the
argument is INT_MIN.

Co-authored-by: moerafaat <moerafaat@aucegypt.edu>
  • Loading branch information
chsigg and Moerafaat authored Aug 28, 2024
1 parent 11ce839 commit a029585
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion include/triton/Dialect/Triton/IR/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ template <typename Int> Int ceil(Int m, Int n) { return (m + n - 1) / n; }

/// Get the highest power of 2 divisor of an integer.
template <typename T> T highestPowOf2Divisor(T n) {
if (n == 0) {
// When n is 0 or min, return the highest power of 2. The min case is handled
// separately to avoid underflow when T is a signed integer. Technically
// in that case the correct divisor is -n, but this value is outside the
// range of possible values, so we take the next best alternative.
if (n == 0 || n == std::numeric_limits<T>::min()) {
return (static_cast<T>(1) << (sizeof(T) * 8 - 2));
}
return (n & (~(n - 1)));
Expand Down
11 changes: 11 additions & 0 deletions test/Analysis/test-alignment.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,14 @@ tt.func public @chained_for(%8: tensor<128x64x!tt.ptr<bf16>> {tt.divisibility =
}
tt.return
}

// -----

// CHECK-LABEL: @int_min_does_not_underflow_in_analysis
module {
tt.func @int_min_does_not_underflow_in_analysis() -> i64 {
// CHECK: divisibility = [4611686018427387904]
%int_min = arith.constant -9223372036854775808 : i64
tt.return %int_min : i64
}
}

0 comments on commit a029585

Please sign in to comment.