Skip to content

Fix an unfortunate bug with Visual Studio 2015#1862

Merged
Dead2 merged 1 commit intozlib-ng:developfrom
KungFuJesus:fixes/1861
Feb 4, 2025
Merged

Fix an unfortunate bug with Visual Studio 2015#1862
Dead2 merged 1 commit intozlib-ng:developfrom
KungFuJesus:fixes/1861

Conversation

@KungFuJesus
Copy link
Copy Markdown
Collaborator

@KungFuJesus KungFuJesus commented Feb 3, 2025

Evidently this instruction, despite the intrinsic having a register operand,
is a memory-register instruction. There seems to be no alignment requirement
for the source operand. Because of this, compilers when not optimized are doing
the unaligned load and then dumping back to the stack to do the broadcasting load.
In doing this, MSVC seems to be dumping to the stack with an aligned move at an
unaligned address, causing a segfault. GCC does not seem to make this mistake, as
it stashes to an aligned address.

If we're on Visual Studio 2015, let's just do the longer 9 cycle sequence of a 128
bit load followed by a vinserti128. This should fix this (issue #1861).

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced internal memory handling to resolve performance issues on legacy compiler environments, ensuring consistent efficiency and stability across different systems.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 3, 2025

Walkthrough

This pull request updates the chunkmemset_16 function in two architecture-specific files by adding conditional compilation blocks. The changes adjust the method for loading a half-chunk from memory based on detected compiler versions. For older compilers, a different instruction is used to insert the half-chunk into the chunk variable, whereas for newer compilers the existing optimized broadcasting method is maintained. Additionally, a comment is added in one file to explain a workaround for a Visual Studio 2015-related performance issue.

Changes

File(s) Change Summary
arch/x86/chunkset_avx2.c, arch/x86/chunkset_avx512.c Introduced conditional compilation in the chunkmemset_16 function. For older Microsoft Visual C++ versions (<= 1900 in AVX2 and Visual Studio 2015 in AVX512), the code loads a half-chunk from memory and inserts it with an alternative instruction, while for newer compilers the broadcast instruction is used. A comment was added in AVX512 for clarity.

Sequence Diagram(s)

sequenceDiagram
    participant F as chunkmemset_16
    participant C as Compiler Version Checker
    participant D as Data Loading Routine

    F->>C: Check MSVC compiler version
    alt Compiler is old (<= threshold)
        C-->>F: Old compiler detected
        F->>D: Load half-chunk from memory
        D-->>F: Return half-chunk
        F->>D: Insert half-chunk via alternate instruction (_mm256_inserti128_si256)
    else Compiler is new
        C-->>F: New compiler detected
        F->>D: Load and broadcast half-chunk (_mm256_broadcastsi128_si256)
    end
Loading

Possibly related PRs

Suggested labels

Build Env, Architecture

Suggested reviewers

  • Dead2
  • nmoinvaz

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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 fixes/1861 branch 2 times, most recently from ee685a4 to 5100a4b Compare February 3, 2025 02:18
@KungFuJesus KungFuJesus changed the title Skip an intermediate unaligned load in chunkmemset_16 Draft: Skip an intermediate unaligned load in chunkmemset_16 Feb 3, 2025
@KungFuJesus KungFuJesus marked this pull request as draft February 3, 2025 02:41
@KungFuJesus KungFuJesus changed the title Draft: Skip an intermediate unaligned load in chunkmemset_16 Skip an intermediate unaligned load in chunkmemset_16 Feb 3, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.36%. Comparing base (a3c0430) to head (14324cf).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1862   +/-   ##
========================================
  Coverage    80.35%   80.36%           
========================================
  Files          139      139           
  Lines        11176    11176           
  Branches      2867     2867           
========================================
+ Hits          8981     8982    +1     
- Misses        1221     1228    +7     
+ Partials       974      966    -8     

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

Evidently this instruction, despite the intrinsic having a register operand,
is a memory-register instruction. There seems to be no alignment requirement
for the source operand. Because of this, compilers when not optimized are doing
the unaligned load and then dumping back to the stack to do the broadcasting load.
In doing this, MSVC seems to be dumping to the stack with an aligned move at an
unaligned address, causing a segfault.  GCC does not seem to make this mistake, as
it stashes to an aligned address.

If we're on Visual Studio 2015, let's just do the longer 9 cycle sequence of a 128
bit load followed by a vinserti128. This _should_ fix this (issue zlib-ng#1861).
@KungFuJesus KungFuJesus changed the title Skip an intermediate unaligned load in chunkmemset_16 Fix an unfortunate bug with Visual Studio 2015 Feb 3, 2025
GerbilSoft added a commit to GerbilSoft/rom-properties that referenced this pull request Feb 3, 2025
Copy link
Copy Markdown
Member

@nmoinvaz nmoinvaz left a comment

Choose a reason for hiding this comment

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

Lgtm

@KungFuJesus KungFuJesus marked this pull request as ready for review February 3, 2025 04:20
Copy link
Copy Markdown
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

🧹 Nitpick comments (1)
arch/x86/chunkset_avx2.c (1)

35-35: Enhance the comment with more details about the bug.

The comment could be more descriptive about the Visual Studio 2015 bug and its impact. Consider adding details about the segfault that occurs due to unaligned loads and aligned moves.

-    /* See explanation in chunkset_avx512.c */
+    /* Visual Studio 2015 has a compiler bug where unaligned loads are written back
+     * to the stack using an aligned move at an unaligned address, causing segfaults.
+     * See detailed explanation in chunkset_avx512.c */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3c0430 and 14324cf.

📒 Files selected for processing (2)
  • arch/x86/chunkset_avx2.c (1 hunks)
  • arch/x86/chunkset_avx512.c (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (30)
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
  • GitHub Check: Ubuntu GCC ARM HF No NEON ASAN
  • GitHub Check: Ubuntu GCC ARM HF No ACLE ASAN
  • GitHub Check: Ubuntu GCC ARM HF ASAN
🔇 Additional comments (2)
arch/x86/chunkset_avx2.c (1)

36-41: LGTM! The workaround is well-implemented.

The conditional compilation block correctly implements the workaround for Visual Studio 2015's compiler bug by using a longer sequence with a 128-bit load followed by vinserti128. This should prevent the segfault caused by unaligned loads.

arch/x86/chunkset_avx512.c (1)

49-59: LGTM! Well-documented and consistent implementation.

The comment provides excellent context about the Visual Studio 2015 compiler bug, its performance impact, and the rationale for the workaround. The implementation is consistent with the AVX2 version.

Copy link
Copy Markdown
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants