Skip to content

branch-4.2: [opt](be) Avoid division in checked int128 multiplication #70921 - #70964

Merged
yiguolei merged 1 commit into
branch-4.2from
auto-pick-70921-branch-4.2
Dec 6, 2026
Merged

yiguolei merged 1 commit into
branch-4.2from
auto-pick-70921-branch-4.2

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Cherry-picked from #70921

### 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.
@github-actions
github-actions Bot requested a review from yiguolei as a code owner November 24, 2026 13:25
@yiguolei

Copy link
Copy Markdown
Contributor

run buildall

2 similar comments
@shuke987

Copy link
Copy Markdown
Contributor

run buildall

@Gabriel39

Copy link
Copy Markdown
Contributor

run buildall

@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

PR approved by anyone and no changes requested.

@github-actions github-actions Bot added the approved Indicates a PR has been approved by one committer. label Dec 6, 2026
@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

PR approved by at least one committer and no changes requested.

@yiguolei
yiguolei merged commit 12a787c into branch-4.2 Dec 6, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants