Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help LLVM vectorize comparison kernel ~50-80% faster #2646

Merged
merged 3 commits into from
Sep 4, 2022

Conversation

tustvold
Copy link
Contributor

@tustvold tustvold commented Sep 4, 2022

Which issue does this PR close?

Closes #.

Rationale for this change

I was messing around in godbolt and realised these loops were not getting vectorised correctly

Default flags

eq Float32              time:   [36.531 us 36.547 us 36.566 us]                        
                        change: [-45.190% -45.147% -45.104%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild
  3 (3.00%) high severe
neq Float32             time:   [36.677 us 36.695 us 36.718 us]                         
                        change: [-48.185% -48.131% -48.071%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  2 (2.00%) low mild
  2 (2.00%) high mild
  8 (8.00%) high severe

Target CPU

eq Float32              time:   [19.023 us 19.040 us 19.060 us]                        
                        change: [-71.418% -71.385% -71.347%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 16 outliers among 100 measurements (16.00%)
  9 (9.00%) high mild
  7 (7.00%) high severe

neq Float32             time:   [19.172 us 19.184 us 19.197 us]                         
                        change: [-72.938% -72.919% -72.900%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild
  6 (6.00%) high severe

Once this is in, I will give the same treatment to the scalar kernels.

As an added bonus this likely reduces the amount of generated LLVM IR, and so may help very slightly with compile times.

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Sep 4, 2022
@tustvold tustvold requested a review from viirya September 4, 2022 11:10
@tustvold tustvold force-pushed the vectorized-compare-op branch from 52032d3 to 6e56f3c Compare September 4, 2022 11:12
let buffer = unsafe { MutableBuffer::from_trusted_len_iter_bool(comparison) };
let mut buffer = MutableBuffer::new((left.len() + 7) / 8);

let chunks = left.len() / 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we change the implementation of from_trusted_len_iter_bool instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that LLVM isn't able to elide the conditional due to Iterator::next, and this prevents it from working correctly. I couldn't find a way around this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some comment about why we don't use iter here, to avoid other developers bringing the iter back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new collect_bools method with docs on why it exists

.map(|i| unsafe { op(left.value_unchecked(i), right.value_unchecked(i)) });
// same size as $left.len() and $right.len()
let buffer = unsafe { MutableBuffer::from_trusted_len_iter_bool(comparison) };
let mut buffer = MutableBuffer::new((left.len() + 7) / 8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the ceil function in bit_util.rs, you can use it.

let buffer = unsafe { MutableBuffer::from_trusted_len_iter_bool(comparison) };
let mut buffer = MutableBuffer::new((left.len() + 7) / 8);

let chunks = left.len() / 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some comment about why we don't use iter here, to avoid other developers bringing the iter back.

@tustvold
Copy link
Contributor Author

tustvold commented Sep 4, 2022

Latest numbers

eq Float32              time:   [18.303 us 18.316 us 18.330 us]                        
                        change: [-72.558% -72.530% -72.503%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe

eq scalar Float32       time:   [7.8778 us 7.8809 us 7.8846 us]                               
                        change: [-81.777% -81.768% -81.758%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  3 (3.00%) high mild
  5 (5.00%) high severe

neq Float32             time:   [18.294 us 18.310 us 18.326 us]                         
                        change: [-74.175% -74.153% -74.132%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild

neq scalar Float32      time:   [7.9744 us 7.9777 us 7.9812 us]                                
                        change: [-81.537% -81.526% -81.512%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) high mild
  5 (5.00%) high severe

lt Float32              time:   [18.281 us 18.295 us 18.310 us]                        
                        change: [-62.585% -62.538% -62.494%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 18 outliers among 100 measurements (18.00%)
  8 (8.00%) low severe
  5 (5.00%) high mild
  5 (5.00%) high severe

lt scalar Float32       time:   [7.8722 us 7.8759 us 7.8811 us]                               
                        change: [-83.746% -83.738% -83.731%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) high mild
  5 (5.00%) high severe

lt_eq Float32           time:   [18.299 us 18.309 us 18.320 us]                           
                        change: [-63.258% -63.198% -63.139%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high severe

lt_eq scalar Float32    time:   [7.8834 us 7.8887 us 7.8943 us]                                  
                        change: [-84.493% -84.472% -84.452%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  9 (9.00%) high mild
  1 (1.00%) high severe

gt Float32              time:   [18.277 us 18.284 us 18.293 us]                        
                        change: [-62.697% -62.632% -62.586%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) low mild
  3 (3.00%) high mild
  2 (2.00%) high severe

gt scalar Float32       time:   [7.8602 us 7.8642 us 7.8689 us]                               
                        change: [-82.399% -82.389% -82.380%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  2 (2.00%) high mild
  8 (8.00%) high severe

gt_eq Float32           time:   [18.299 us 18.312 us 18.324 us]                           
                        change: [-62.749% -62.710% -62.671%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe

gt_eq scalar Float32    time:   [7.8686 us 7.8722 us 7.8763 us]                                  
                        change: [-82.149% -82.141% -82.132%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  1 (1.00%) low mild
  3 (3.00%) high mild
  1 (1.00%) high severe

@tustvold tustvold changed the title Help LLVM vectorize comparison kernel ~50-70% faster Help LLVM vectorize comparison kernel ~50-80% faster Sep 4, 2022
.map(|i| unsafe { op(left.value_unchecked(i), right.value_unchecked(i)) });
// same size as $left.len() and $right.len()
let buffer = unsafe { MutableBuffer::from_trusted_len_iter_bool(comparison) };
let buffer = MutableBuffer::collect_bool(left.len(), |i| unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the safety comments?

@Dandandan
Copy link
Contributor

Looks good! Happy about the scalar perf too. One remaining comment on the (un)safety remarks

Copy link
Member

@viirya viirya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Dandandan Dandandan merged commit b46fc92 into apache:master Sep 4, 2022
@Dandandan
Copy link
Contributor

Thanks @tustvold 🚀

@ursabot
Copy link

ursabot commented Sep 4, 2022

Benchmark runs are scheduled for baseline = 6d86472 and contender = b46fc92. b46fc92 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

result.push_unchecked(byte_accum);
}
result
Self::collect_bool(len, |_| iterator.next().unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tustvold one last thought, I wonder if using unwrap_unchecked here would result in the same performance (as it removes the condition).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave this a go and honestly I don't really understand the results. The scalar comparison is on par with this PR, but the non-scalar variants are worse by 50% compared to master... My 2 cents is that not using iterators for performance critical code makes for code that is easier to reason about, both by humans and LLVM, and so even if we can somehow finagle from_trusted_len_iter_bool to perform the same, I'm more comfortable with a simple loop 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants