← Back to Pull requests

Fix VPEXPAND byte and word memory load masks #851

Closed
unknown closed this pull request without merging on Aug 29, 2026 04:24 UTC
View on GitHub
unknown unknown commented on Aug 28, 2026 22:02 UTC
Author

Problem

The memory-source handlers for VPEXPANDB and VPEXPANDW build a
dense load mask from the number of enabled destination elements:

Bit64u load_mask = (opmask == BX_CONST64(0xFFFFFFFFFFFFFFFF)) ? opmask : (1 << popcntq(opmask)) - 1;
Bit32u load_mask = (1 << popcntd(opmask)) - 1;

Both shifts begin with the signed 32-bit integer literal 1. For
VPEXPANDB, a population count of 31 shifts into the sign bit, while
counts from 32 through 63 use a shift count outside the type's width.
For VPEXPANDW, count 31 similarly shifts into the sign bit and count
32 uses an out-of-range shift. These operations have undefined behavior
in C++.

In the tested optimized build, VPEXPANDB produced incorrect results
for counts 32 through 63, including an EVEX.256 operation selecting all
32 byte elements. VPEXPANDW produced an incorrect result when all 32
word elements were selected by an EVEX.512 operation.

Only the masked memory-source handlers construct these dense load masks.
The register-source handlers are unaffected.

Fix

Construct the byte load mask with BX_CONST64(1), making shifts through
count 63 well-defined while retaining the existing special case for all
64 byte elements.

For the word load mask, handle all 32 elements with an explicit all-ones
mask and use Bit32u(1) for smaller counts. This also makes the
count-31 boundary unsigned and well-defined.

Architectural reference

The Intel® 64 and IA-32 Architectures Software Developer's Manual,
Volume 2, section “VPEXPANDB/VPEXPANDW—Load Sparse Packed Byte/Word
Integer Values from Dense Memory/Register,” specifies that consecutive
elements from the dense source are copied to destination positions
selected by the writemask.

Consequently, a memory-source operation must load one consecutive source
element for each enabled destination element. The internal dense load
mask must therefore contain exactly popcount(k) low set bits, including
the full 32-byte and 32-word cases.

  • Intel® Software Developer Manuals download page
  • Validation

    A standalone 64-bit CPL3 guest exercised the memory-source forms on the
    Bochs tigerlake CPU model with AVX-512 state enabled (XCR0=0xe7).
    Every test used ordinary mapped memory containing distinctive nonzero
    elements and initialized the destination with nonzero merge sentinels.

    The count-31 cases check the signed-shift boundary. The 64-byte case
    checks the existing full-width special case. The remaining cases cover
    the observed incorrect ranges and both applicable vector lengths at the
    32-byte boundary.

    | Instruction | Vector length | Active elements / mask shape | Before | After |
    |---|---:|---|---:|---:|
    | VPEXPANDB | 512 | 31 low elements | PASS | PASS |
    | VPEXPANDB | 256 | 32 elements (all) | FAIL | PASS |
    | VPEXPANDB | 512 | 32 sparse elements | FAIL | PASS |
    | VPEXPANDB | 512 | 33 sparse elements | FAIL | PASS |
    | VPEXPANDB | 512 | 63 sparse elements | FAIL | PASS |
    | VPEXPANDB | 512 | 64 elements (all) | PASS | PASS |
    | VPEXPANDW | 512 | 31 sparse elements | PASS | PASS |
    | VPEXPANDW | 512 | 32 elements (all) | FAIL | PASS |

    The source file used by the patched test build exactly matches the
    committed source, and the patched tree builds successfully with
    make -j2. git diff --check also passes.

    The validation used mapped memory and confirms the result correction. It
    did not separately test page-fault behavior.

    Diff: 448 lines (+336 / -112) across 5 files Reviewer: stlintel Review duration: 382m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 448 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.365
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #851"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.4% (MEDIUM) · Higher = larger blast radius
    45.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.9% · Safe to merge
    17.9%