From ab4300787f06678ebafe5cc10ab2c65f91d3df10 Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Thu, 25 Jun 2026 16:03:20 +0800 Subject: [PATCH 1/2] Initial upload. --- ruy/kernel_common.h | 11 +++++------ ruy/platform.h | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ruy/kernel_common.h b/ruy/kernel_common.h index 69e819b20a..346e0d3696 100644 --- a/ruy/kernel_common.h +++ b/ruy/kernel_common.h @@ -130,12 +130,11 @@ struct KernelParams8bit { }; template -void MakeKernelParams8bit(const PMat& lhs, - const PMat& rhs, - const MulParams& mul_params, - int start_row, int start_col, int end_row, - int end_col, Mat* dst, - KernelParams8bit* params) { +RUY_NO_SANITIZE_INTEGER_OVERFLOW void MakeKernelParams8bit( + const PMat &lhs, const PMat &rhs, + const MulParams &mul_params, int start_row, + int start_col, int end_row, int end_col, Mat *dst, + KernelParams8bit *params) { using Params = KernelParams8bit; static_assert(sizeof(DstScalar) <= Params::kMaxDstTypeSize, ""); diff --git a/ruy/platform.h b/ruy/platform.h index 9b6741642a..5c5bf75f22 100644 --- a/ruy/platform.h +++ b/ruy/platform.h @@ -159,4 +159,18 @@ limitations under the License. #define RUY_PLATFORM_EMSCRIPTEN 0 #endif +// Disables UBSan's integer-overflow checks for an annotated function. Only use +// this where the overflow merely yields a wrong numeric result and poses no +// safety risk. Expands to nothing on compilers without the attribute. +#if defined(__clang__) && defined(__has_attribute) +#if __has_attribute(no_sanitize) +#define RUY_NO_SANITIZE_INTEGER_OVERFLOW \ + __attribute__((no_sanitize("signed-integer-overflow", \ + "unsigned-integer-overflow"))) +#endif +#endif +#ifndef RUY_NO_SANITIZE_INTEGER_OVERFLOW +#define RUY_NO_SANITIZE_INTEGER_OVERFLOW +#endif + #endif // RUY_RUY_PLATFORM_H_ From 781542ce3bf209d415e19d73a65e89bf073f3617 Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Tue, 21 Jul 2026 15:57:07 +0800 Subject: [PATCH 2/2] Resolve comments. --- ruy/kernel_common.h | 19 +++++++++++++------ ruy/platform.h | 14 -------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/ruy/kernel_common.h b/ruy/kernel_common.h index 346e0d3696..d4cf293fde 100644 --- a/ruy/kernel_common.h +++ b/ruy/kernel_common.h @@ -130,11 +130,12 @@ struct KernelParams8bit { }; template -RUY_NO_SANITIZE_INTEGER_OVERFLOW void MakeKernelParams8bit( - const PMat &lhs, const PMat &rhs, - const MulParams &mul_params, int start_row, - int start_col, int end_row, int end_col, Mat *dst, - KernelParams8bit *params) { +void MakeKernelParams8bit(const PMat& lhs, + const PMat& rhs, + const MulParams& mul_params, + int start_row, int start_col, int end_row, + int end_col, Mat* dst, + KernelParams8bit* params) { using Params = KernelParams8bit; static_assert(sizeof(DstScalar) <= Params::kMaxDstTypeSize, ""); @@ -176,7 +177,13 @@ RUY_NO_SANITIZE_INTEGER_OVERFLOW void MakeKernelParams8bit( params->rhs_zero_point = rhs.zero_point; params->dst_zero_point = dst->zero_point; params->depth = depth; - params->prod_zp_depth = lhs.zero_point * rhs.zero_point * depth; + // prod_zp_depth intentionally relies on two's-complement wraparound to match + // the wrapping arithmetic performed by the asm kernels. Compute it in unsigned + // so the overflow is well-defined. + params->prod_zp_depth = static_cast( + static_cast(lhs.zero_point) * + static_cast(rhs.zero_point) * + static_cast(depth)); params->flags |= RUY_ASM_FLAG_NEEDS_LEFT_SHIFT; if (mul_params.multiplier_fixedpoint_perchannel()) { // Temporary release-assert to debug some crashes in an application. diff --git a/ruy/platform.h b/ruy/platform.h index 5c5bf75f22..9b6741642a 100644 --- a/ruy/platform.h +++ b/ruy/platform.h @@ -159,18 +159,4 @@ limitations under the License. #define RUY_PLATFORM_EMSCRIPTEN 0 #endif -// Disables UBSan's integer-overflow checks for an annotated function. Only use -// this where the overflow merely yields a wrong numeric result and poses no -// safety risk. Expands to nothing on compilers without the attribute. -#if defined(__clang__) && defined(__has_attribute) -#if __has_attribute(no_sanitize) -#define RUY_NO_SANITIZE_INTEGER_OVERFLOW \ - __attribute__((no_sanitize("signed-integer-overflow", \ - "unsigned-integer-overflow"))) -#endif -#endif -#ifndef RUY_NO_SANITIZE_INTEGER_OVERFLOW -#define RUY_NO_SANITIZE_INTEGER_OVERFLOW -#endif - #endif // RUY_RUY_PLATFORM_H_