Repository navigation
branch-4.2: [opt](be) Avoid division in checked int128 multiplication #70921 - #70964
Merged
Merged
Conversation
### Background Decimal multiplication checks native integer overflow before enforcing decimal precision and rescaling. The signed int128 check currently uses a compiler-rt-derived implementation with 128-bit division. This implementation avoids a missing `__muloti4` runtime symbol on some ARM toolchains. ### What problem does this PR solve? Issue Number: None Related PR: #51715 (introduced the ARM-compatible software check), #51533 (fixed signed int128 overflow detection) PR Type: Performance Problem Summary: The division-based check adds CPU overhead to each checked Decimal multiplication. Replace it with unsigned builtin multiplication on operand magnitudes and a sign-dependent bound, preserving the ARM compatibility workaround and the complete signed int128 range. Results, decimal types, precision checks and overflow behavior remain unchanged; most of the diff adds tests. ### Details - **Change map:** 46 production lines changed (14 added, 32 removed), 240 unit/regression test lines added, and 51 automatically generated expected-output lines added. - **Why it was slow:** Ordinary nonzero/nonunit products perform a 128-bit division to detect overflow; the baseline code-generation probes call `__udivti3`. - **Optimization:** Compute magnitudes in unsigned arithmetic so even `INT128_MIN` is safe, use unsigned `__builtin_mul_overflow`, then apply the signed bound. Negative products can have magnitude `2^127`, for example `(2^126) * (-2)`. Preserve the zero/unit fast path and wrapped result bits on overflow. - **Results:** Local release-mode primitive microbenchmark on a dual-socket Intel Xeon Platinum 8457C, Clang 20.1.8, `-O3 -DNDEBUG -march=x86-64-v3`. Both implementations use the same input/output arrays in one binary: 65,536 pairs, five warm-up batches, 100 batches per sample, 11 paired samples with alternating order, pinned to one logical CPU. The shared host was busy, so the table reports median thread CPU time rather than query latency. | Synthetic input distribution | Before, ns/product | After, ns/product | | --- | ---: | ---: | | Discount-shaped positive operands | 13.37 | 10.28 | | Charge-shaped positive operands | 12.33 | 8.77 | | Mixed signs | 18.16 | 9.23 | | Full-width overflowing products | 23.18 | 9.53 | | Random zero/unit factors | 6.22 | 6.36 | - **Applicability and cost:** Applies to checked signed int128 multiplication, including Decimal arithmetic. No allocations or type changes are added. The zero/unit workload is about 2.4% slower by these CPU-time medians despite retaining its fast path; the other measured distributions improve. These measurements do not establish an end-to-end TPC-H or SSB speedup. ### Testing - Local ASan/UBSan BE build and FE build completed; the BE executable passed its GLIBC 2.17 symbol gate. - New unit tests compare 85,141 products with an independent 256-bit reference, plus aliased-output cases. Clang ASan/UBSan and GCC UBSan standalone runs pass. A deliberately broken minimum-value bound fails the boundary and power tests. - All six targeted BE tests pass through the standard ASan/UBSan runner: `run-be-ut.sh --run --filter='ArithmeticOverflowTest.*:CheckOverFlowTest.*'`. - Local regression suites `test_decimal128_checked_multiply`, `test_arithmetic_expressions` and `test_decimalv3_overflow` pass against the newly built BE. The new suite covers column/constant forms, nulls, signed and unsigned machine overflow, decimal precision overflow, and scaled wide-intermediate fallback. Its generated 41 result rows were independently checked with Python arbitrary-precision integers and Decimal. - Clang 16/20 x86_64 and ARM64 code-generation probes, plus GCC x86_64, contain no division or `__muloti4` call for the new helper. ARM validation is compile/code-generation only. - Local self-review, clang-format v16, clang-tidy, build hygiene and whitespace checks completed. ### Release note Reduce CPU overhead of checked Decimal multiplication while preserving existing results and overflow behavior. ### Check List (For Author) - Behavior changed: No - Does this need documentation: No - Pre-submission status: Ready ### AI Disclosure - Tool: Codex (GPT-6) - Generated: Code, unit and regression tests, and this description. - Human review: Not yet reviewed by a human; local self-review and verification were performed by Codex.
Contributor
|
run buildall |
2 similar comments
Contributor
|
run buildall |
Contributor
|
run buildall |
yiguolei
approved these changes
Dec 6, 2026
Contributor
Author
|
PR approved by anyone and no changes requested. |
Contributor
Author
|
PR approved by at least one committer and no changes requested. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-picked from #70921