[Mlir-commits] [mlir] 3c448c2 - [mlir][sparse] Updating checkedMul to use intrinsics.
wren romano
llvmlistbot at llvm.org
Wed Nov 16 16:23:05 PST 2022
Author: wren romano
Date: 2022-11-16T16:22:58-08:00
New Revision: 3c448c2cef43c754b48022d99b4801d1de9e7af2
URL: https://github.com/llvm/llvm-project/commit/3c448c2cef43c754b48022d99b4801d1de9e7af2
DIFF: https://github.com/llvm/llvm-project/commit/3c448c2cef43c754b48022d99b4801d1de9e7af2.diff
LOG: [mlir][sparse] Updating checkedMul to use intrinsics.
Depends On D138149
Reviewed By: aartbik
Differential Revision: https://reviews.llvm.org/D138154
Added:
Modified:
mlir/include/mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h
Removed:
################################################################################
diff --git a/mlir/include/mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h b/mlir/include/mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h
index 50aa93e90e04..ab5764c6b1e5 100644
--- a/mlir/include/mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h
+++ b/mlir/include/mlir/ExecutionEngine/SparseTensor/ArithmeticUtils.h
@@ -131,16 +131,22 @@ template <typename To, typename From>
return static_cast<To>(x);
}
-// TODO: would be better to use various architectures' intrinsics to
-// detect the overflow directly, instead of doing the assertion beforehand
-// (which requires an expensive division).
-//
/// A version of `operator*` on `uint64_t` which guards against overflows
/// (when assertions are enabled).
inline uint64_t checkedMul(uint64_t lhs, uint64_t rhs) {
+ // If assertions are enabled and we have the intrinsic, then use it to
+ // avoid the expensive division. If assertions are disabled, then don't
+ // bother with intrinsics (to avoid any possible slowdown vs `operator*`).
+#if !defined(NDEBUG) && __has_builtin(__builtin_mul_overflow)
+ uint64_t result;
+ bool overflowed = __builtin_mul_overflow(lhs, rhs, &result);
+ assert(!overflowed && "Integer overflow");
+ return result;
+#else
assert((lhs == 0 || rhs <= std::numeric_limits<uint64_t>::max() / lhs) &&
"Integer overflow");
return lhs * rhs;
+#endif
}
} // namespace detail
More information about the Mlir-commits
mailing list