这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@KungFuJesus
Copy link
Contributor

@KungFuJesus KungFuJesus commented Nov 30, 2024

For reasons that aren't quite so clear, using the masked writes here did not pipeline very well. Either setting up the mask stalled things or masked moves have issues overlapping regular moves. Simply putting the masked moves behind a branch that is rarely taken seemed to do the trick in improving the ILP. While here, put masked loads behind the same branch in case there were ever a hazard for overreading.

Summary by CodeRabbit

  • New Features

    • Introduced several new memory copying functions to enhance performance and safety, including CHUNKCOPY_SAFE and GET_CHUNK_MAG.
    • Expanded the functionality of existing memory operations, improving handling of edge cases and overlaps.
  • Bug Fixes

    • Improved logic in CHUNKCOPY and HALFCHUNKCOPY for correctly managing data copying when lengths are not multiples of chunk sizes.
    • Enhanced memory handling functions with additional safety checks and optimizations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 30, 2024

Walkthrough

The pull request introduces enhancements to memory copying functions in two files: chunkset_avx512.c and chunkset_tpl.h. In chunkset_avx512.c, improvements are made to the CHUNKCOPY and HALFCHUNKCOPY functions for better handling of edge cases when copying data. The chunkset_tpl.h file sees the addition of several new functions and optimizations, including CHUNKCOPY, CHUNKUNROLL, GET_CHUNK_MAG, CHUNKMEMSET, and CHUNKCOPY_SAFE, which refine memory operations and ensure safe copying practices.

Changes

File Change Summary
arch/x86/chunkset_avx512.c Enhanced CHUNKCOPY and HALFCHUNKCOPY functions to improve handling of data remainders and ensure correct memory operations with masked load/store.
chunkset_tpl.h Added and refined multiple memory copying functions: CHUNKCOPY, CHUNKUNROLL, GET_CHUNK_MAG, CHUNKMEMSET, and CHUNKCOPY_SAFE, optimizing memory handling and safety.

Possibly related PRs

  • Compute the "safe" distance properly #1801: The changes in this PR involve the CHUNKMEMSET_SAFE function, which is directly related to the memory operations discussed in the main PR, particularly in the context of safe memory copying and handling edge cases.

Suggested labels

optimization

Suggested reviewers

  • Dead2

Warning

Rate limit exceeded

@KungFuJesus has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 20 minutes and 30 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between f0aed1e and 0c9c7be.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@KungFuJesus KungFuJesus force-pushed the improve_avx512_pipelining branch from 273c1a6 to f0aed1e Compare November 30, 2024 14:28
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
arch/x86/chunkset_avx512.c (1)

163-172: Consider applying the same branching strategy as CHUNKCOPY

While the current implementation is correct, it could potentially benefit from the same optimization strategy used in CHUNKCOPY by:

  1. Moving masked operations behind a branch for lengths < 16 bytes
  2. Using regular loads/stores for full 16-byte chunks

This would align with the PR's goal of improving instruction-level parallelism by reducing the frequency of masked operations in the common case.

Example approach:

 static inline uint8_t* HALFCHUNKCOPY(uint8_t *out, uint8_t const *from, unsigned len) {
     Assert(len > 0, "chunkcopy should never have a length 0");
     halfchunk_t chunk;
 
     uint32_t rem = len % sizeof(halfchunk_t);
-    if (rem == 0) {
-        rem = sizeof(halfchunk_t);
+    
+    if (len < sizeof(halfchunk_t)) {
+        halfmask_t rem_mask = gen_half_mask(rem);
+        chunk = _mm_maskz_loadu_epi8(rem_mask, from);
+        _mm_mask_storeu_epi8(out, rem_mask, chunk);
+        return out + rem;
     }
 
-    halfmask_t rem_mask = gen_half_mask(rem);
-    chunk = _mm_maskz_loadu_epi8(rem_mask, from);
-    _mm_mask_storeu_epi8(out, rem_mask, chunk);
+    loadhalfchunk(from, &chunk);
+    rem = (rem == 0) ? sizeof(halfchunk_t) : rem;
+    storehalfchunk(out, &chunk);
 
     return out + rem;
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5456966 and f0aed1e.

📒 Files selected for processing (2)
  • arch/x86/chunkset_avx512.c (2 hunks)
  • chunkset_tpl.h (0 hunks)
💤 Files with no reviewable changes (1)
  • chunkset_tpl.h
🔇 Additional comments (1)
arch/x86/chunkset_avx512.c (1)

74-85: Excellent optimization for improving instruction-level parallelism!

The changes improve pipelining by:

  1. Moving masked operations behind a rarely taken branch (len < 32 bytes)
  2. Properly handling the remainder calculation with rem = (rem == 0) ? sizeof(chunk_t) : rem
  3. Using masked loads to prevent potential overreads

This aligns well with the PR objectives and should reduce pipeline stalls in the common case where len >= 32 bytes.

For reasons that aren't quite so clear, using the masked writes here
did not pipeline very well. Either setting up the mask stalled things
or masked moves have issues overlapping regular moves. Simply putting
the masked moves behind a branch that is rarely taken seemed to do the
trick in improving the ILP. While here, put masked loads behind the same
branch in case there were ever a hazard for overreading.
@KungFuJesus KungFuJesus force-pushed the improve_avx512_pipelining branch from f0aed1e to 0c9c7be Compare November 30, 2024 14:36
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
arch/x86/chunkset_avx512.c (1)

155-157: Consider adding documentation about alignment requirements

It would be helpful to add a comment explaining that this function operates on 16-byte chunks (half of AVX512's 32-byte registers) and any alignment requirements or assumptions.

 static inline uint8_t* HALFCHUNKCOPY(uint8_t *out, uint8_t const *from, unsigned len) {
     Assert(len > 0, "chunkcopy should never have a length 0");
     halfchunk_t chunk;
+    /* Handle copying using 16-byte (128-bit) chunks, which are half the size of
+     * full AVX512 32-byte registers. This is useful for smaller copies or when
+     * dealing with data that isn't 32-byte aligned. */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f0aed1e and 0c9c7be.

📒 Files selected for processing (2)
  • arch/x86/chunkset_avx512.c (2 hunks)
  • chunkset_tpl.h (0 hunks)
💤 Files with no reviewable changes (1)
  • chunkset_tpl.h
🔇 Additional comments (2)
arch/x86/chunkset_avx512.c (2)

70-81: Excellent optimization for AVX512 pipelining!

The changes improve instruction-level parallelism by:

  1. Moving masked operations behind a rarely taken branch (len < 32 bytes)
  2. Using _mm256_maskz_loadu_epi8 for safe boundary handling
  3. Properly handling remainder with rem = (rem == 0) ? sizeof(chunk_t) : rem

This implementation aligns perfectly with the PR's objective of improving pipelining for AVX512 chunking.


155-164: Clean implementation of half-chunk copying!

The changes follow the same optimization pattern as CHUNKCOPY, using masked operations efficiently for remainder handling. The simplified logic improves code maintainability while maintaining performance.

@codecov
Copy link

codecov bot commented Nov 30, 2024

Codecov Report

Attention: Patch coverage is 0% with 15 lines in your changes missing coverage. Please review.

Project coverage is 32.20%. Comparing base (0ed5ac8) to head (0c9c7be).
Report is 9 commits behind head on develop.

Files with missing lines Patch % Lines
arch/x86/chunkset_avx512.c 0.00% 15 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1821      +/-   ##
===========================================
+ Coverage    32.18%   32.20%   +0.02%     
===========================================
  Files           67       67              
  Lines         5752     5745       -7     
  Branches      1237     1239       +2     
===========================================
- Hits          1851     1850       -1     
- Misses        3644     3645       +1     
+ Partials       257      250       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@KungFuJesus
Copy link
Contributor Author

Before

OS: Linux 6.11.6-gentoo-x86_64 #3 SMP PREEMPT_DYNAMIC Mon Nov  4 10:41:27 EST 2024 x86_64
CPU: Intel(R) Core(TM) i9-10940X CPU @ 3.30GHz
Tool: /home/adam/scratch/zlib-ng/build_clean/minigzip Size: 154,848 B
Levels: 0-9       
Runs: 10         Trim worst: 2         

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 0    100.008%      0.017/0.019/0.022/0.002        0.015/0.018/0.021/0.002      211,973,953
 1     44.409%      0.848/0.858/0.863/0.006        0.332/0.336/0.342/0.004       94,127,497
 2     35.519%      1.427/1.441/1.447/0.008        0.324/0.329/0.337/0.004       75,286,322
 3     33.844%      1.774/1.786/1.792/0.006        0.304/0.312/0.318/0.005       71,735,218
 4     33.146%      2.089/2.099/2.116/0.008        0.295/0.309/0.319/0.008       70,255,223
 5     32.642%      2.407/2.411/2.417/0.004        0.293/0.302/0.307/0.004       69,187,419
 6     32.483%      2.918/2.927/2.935/0.005        0.294/0.302/0.309/0.006       68,850,776
 7     32.255%      4.094/4.115/4.131/0.013        0.297/0.304/0.310/0.005       68,366,759
 8     32.167%      6.582/6.598/6.627/0.018        0.291/0.305/0.310/0.006       68,180,762
 9     31.887%      7.177/7.192/7.218/0.014        0.278/0.287/0.297/0.008       67,586,442

 avg1  40.836%                        2.945                          0.280
 avg2  45.373%                        3.272                          0.312
 tot                                235.577                         22.438      865,550,371

After

OS: Linux 6.11.6-gentoo-x86_64 #3 SMP PREEMPT_DYNAMIC Mon Nov  4 10:41:27 EST 2024 x86_64
CPU: Intel(R) Core(TM) i9-10940X CPU @ 3.30GHz
Tool: /home/adam/scratch/zlib-ng/build-1821/minigzip Size: 154,848 B
Levels: 0-9       
Runs: 10         Trim worst: 2         

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 0    100.008%      0.013/0.017/0.020/0.002        0.018/0.021/0.023/0.002      211,973,953
 1     44.409%      0.849/0.856/0.865/0.005        0.315/0.327/0.332/0.006       94,127,497
 2     35.519%      1.429/1.441/1.449/0.006        0.310/0.320/0.326/0.006       75,286,322
 3     33.844%      1.771/1.786/1.792/0.007        0.298/0.306/0.312/0.004       71,735,218
 4     33.146%      2.090/2.097/2.104/0.004        0.287/0.298/0.306/0.006       70,255,223
 5     32.642%      2.398/2.413/2.420/0.007        0.289/0.298/0.306/0.005       69,187,419
 6     32.483%      2.912/2.924/2.931/0.007        0.291/0.298/0.308/0.005       68,850,776
 7     32.255%      4.103/4.112/4.120/0.005        0.293/0.299/0.305/0.004       68,366,759
 8     32.167%      6.537/6.575/6.592/0.018        0.292/0.299/0.306/0.005       68,180,762
 9     31.887%      7.172/7.178/7.183/0.004        0.279/0.284/0.291/0.005       67,586,442

 avg1  40.836%                        2.940                          0.275
 avg2  45.373%                        3.266                          0.306
 tot                                235.181                         22.003      865,550,371

@KungFuJesus
Copy link
Contributor Author

And for added measure, here's with #1816, with the AVX512 inflate_fast family of functions commented out from the function table:

OS: Linux 6.11.6-gentoo-x86_64 #3 SMP PREEMPT_DYNAMIC Mon Nov  4 10:41:27 EST 2024 x86_64
CPU: Intel(R) Core(TM) i9-10940X CPU @ 3.30GHz
Tool: /home/adam/scratch/zlib-ng/build-1821/minigzip Size: 150,488 B
Levels: 0-9       
Runs: 10         Trim worst: 2         

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 0    100.008%      0.013/0.016/0.020/0.003        0.012/0.017/0.021/0.003      211,973,953
 1     44.409%      0.852/0.857/0.863/0.005        0.317/0.327/0.337/0.007       94,127,497
 2     35.519%      1.431/1.437/1.446/0.006        0.316/0.324/0.328/0.004       75,286,322
 3     33.844%      1.780/1.789/1.793/0.004        0.299/0.306/0.311/0.004       71,735,218
 4     33.146%      2.096/2.103/2.108/0.004        0.295/0.299/0.305/0.003       70,255,223
 5     32.642%      2.408/2.415/2.421/0.005        0.288/0.293/0.300/0.004       69,187,419
 6     32.483%      2.921/2.927/2.933/0.004        0.290/0.294/0.298/0.003       68,850,776
 7     32.255%      4.105/4.110/4.118/0.005        0.283/0.298/0.304/0.007       68,366,759
 8     32.167%      6.536/6.576/6.597/0.023        0.289/0.296/0.304/0.006       68,180,762
 9     31.887%      7.174/7.184/7.193/0.007        0.280/0.286/0.291/0.003       67,586,442

 avg1  40.836%                        2.941                          0.274
 avg2  45.373%                        3.268                          0.304
 tot                                235.318                         21.918      865,550,371

@nmoinvaz nmoinvaz added optimization Architecture Architecture specific labels Dec 3, 2024
Copy link
Member

@Dead2 Dead2 left a comment

Choose a reason for hiding this comment

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

LGTM

@Dead2 Dead2 merged commit 43d74a2 into zlib-ng:develop Dec 10, 2024
143 of 150 checks passed
@Dead2 Dead2 mentioned this pull request Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Architecture Architecture specific optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants