Skip to content

Commit 3c448c2

Browse files
committed
[mlir][sparse] Updating checkedMul to use intrinsics.
Depends On D138149 Reviewed By: aartbik Differential Revision: https://reviews.llvm.org/D138154
1 parent 9d86299 commit 3c448c2

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

mlir/include/mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,22 @@ template <typename To, typename From>
131131
return static_cast<To>(x);
132132
}
133133

134-
// TODO: would be better to use various architectures' intrinsics to
135-
// detect the overflow directly, instead of doing the assertion beforehand
136-
// (which requires an expensive division).
137-
//
138134
/// A version of `operator*` on `uint64_t` which guards against overflows
139135
/// (when assertions are enabled).
140136
inline uint64_t checkedMul(uint64_t lhs, uint64_t rhs) {
137+
// If assertions are enabled and we have the intrinsic, then use it to
138+
// avoid the expensive division. If assertions are disabled, then don't
139+
// bother with intrinsics (to avoid any possible slowdown vs `operator*`).
140+
#if !defined(NDEBUG) && __has_builtin(__builtin_mul_overflow)
141+
uint64_t result;
142+
bool overflowed = __builtin_mul_overflow(lhs, rhs, &result);
143+
assert(!overflowed && "Integer overflow");
144+
return result;
145+
#else
141146
assert((lhs == 0 || rhs <= std::numeric_limits<uint64_t>::max() / lhs) &&
142147
"Integer overflow");
143148
return lhs * rhs;
149+
#endif
144150
}
145151

146152
} // namespace detail

0 commit comments

Comments
 (0)