mbutrovich commented on code in PR #12062:
URL: https://github.com/apache/arrow-rs/pull/12062#discussion_r4568401722


##########
arrow-arith/src/numeric.rs:
##########
@@ -1195,28 +1406,61 @@ where
             match op {
                 // Equal scales make both decimal multipliers one.
                 Op::Add | Op::AddWrapping if s1 == s2 => {
-                    try_op!(l, l_s, r, r_s, l.add_checked(r))
+                    match overflowing_op(l, l_s, r, r_s, 
T::Native::add_overflowing) {

Review Comment:
   > Can we keep the checked path for equal-scale `Decimal256` add until this 
case improves on `x86-64-v3`?
   
   Done. `Decimal256` now keeps the checked kernel for `add`, `sub`, and `mul`, 
through an `OverflowingOp::ONE_PASS` const that the `i256` impl sets to false. 
The rerun with independent operands still measured `decimal256_add` at 0.75x 
without nulls and 0.90x with nulls with 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6862160802),
 against 1.33x to 2.40x on Neoverse-V2 and Xeon 8581C. With the checked kernel, 
`decimal256_add` now measures 0.99x to 1.06x in every configuration 
([Neoverse-V2](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863263619),
 [Xeon 
8581C](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863287300), 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863303751)).
   
   I looked at the codegen. With `-C target-cpu=x86-64-v3`, LLVM vectorizes the 
`i256` add loop in `collect_overflowing` at two rows per iteration. The 256-bit 
adds stay scalar (`add` and `adc`). The carries and overflow flags move through 
`vinserti128`, `vpermd`, `vmovmskps`, and `vpblendvb`, and the loop spills and 
reloads about 20 stack slots per iteration. That is about 120 instructions per 
two rows, against about 25 per row in the scalar loop that the `x86-64` build 
runs. Computing the flag with the sign-bit test instead of the comparisons in 
[`i256::overflowing_add`](https://github.com/apache/arrow-rs/blob/9c9486188744e80ec86c3b2bcdd649b89945ffe5/arrow-buffer/src/bigint/mod.rs#L849-L866)
 gives the same loop, so the vectorizer is packing the carry, not the flag. 
Returning at the first overflow kept the loop scalar on `x86-64-v3`, but it 
would have added a second loop mode to the shared helper for one type.



##########
arrow-arith/src/numeric.rs:
##########
@@ -1195,28 +1406,61 @@ where
             match op {
                 // Equal scales make both decimal multipliers one.
                 Op::Add | Op::AddWrapping if s1 == s2 => {
-                    try_op!(l, l_s, r, r_s, l.add_checked(r))
+                    match overflowing_op(l, l_s, r, r_s, 
T::Native::add_overflowing) {

Review Comment:
   > Can we keep the checked path for equal-scale `Decimal256` add until this 
case improves on `x86-64-v3`?
   
   Done. `Decimal256` now keeps the checked kernel for `add`, `sub`, and `mul`, 
through an `OverflowingOp::ONE_PASS` const that the `i256` impl sets to false. 
The rerun with independent operands still measured `decimal256_add` at 0.75x 
without nulls and 0.90x with nulls with 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6862160802),
 against 1.33x to 2.40x on Neoverse-V2 and Xeon 8581C. With the checked kernel, 
`decimal256_add` now measures 0.99x to 1.06x in every configuration 
([Neoverse-V2](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863263619),
 [Xeon 
8581C](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863287300), 
[`x86-64-v3`](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6863303751)).
   
   I looked at the codegen. With `-C target-cpu=x86-64-v3`, LLVM vectorizes the 
`i256` add loop in `collect_overflowing` at two rows per iteration. The 256-bit 
adds stay scalar (`add` and `adc`). The carries and overflow flags move through 
`vinserti128`, `vpermd`, `vmovmskps`, and `vpblendvb`, and the loop spills and 
reloads about 20 stack slots per iteration. That is about 120 instructions per 
two rows, against about 25 per row in the scalar loop that the `x86-64` build 
runs. Computing the flag with the sign-bit test instead of the comparisons in 
[`i256::overflowing_add`](https://github.com/apache/arrow-rs/blob/9c9486188744e80ec86c3b2bcdd649b89945ffe5/arrow-buffer/src/bigint/mod.rs#L849-L866)
 gives the same loop, so the vectorizer is packing the carry, not the flag. 
Returning at the first overflow kept the loop scalar on `x86-64-v3`, but it 
would have added a second loop mode to the shared helper for one type.
   
   > Please rerun against the current base with independently generated 
operands.
   
   `create_primitive_array_range` seeds every call with the [same 
seed](https://github.com/apache/arrow-rs/blob/9c9486188744e80ec86c3b2bcdd649b89945ffe5/arrow/src/util/test_util.rs#L21-L23),
 so the `Decimal128` operands were equal too, not only the cloned ones. The 
benchmark now generates each operand with its own seed. The rerun is linked 
above, and the [aligned 
run](https://github.com/apache/arrow-rs/pull/12062#issuecomment-6862158066) 
measured 0.99x and 1.10x.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to