← Back to Pull requests

fix(knn): replace _bit_scan_forward with __builtin_ctz in the kd-tree median search #4009

Closed
unknown closed this pull request without merging on Nov 10, 2026 06:59 UTC
View on GitHub
unknown unknown commented on Nov 09, 2026 15:52 UTC
Author

Description

Newer icx releases fail to compile the kd-tree kNN training kernel on Windows:

kdtree_knn_classification_train_dense_default_impl.i:625:21: error: use of undeclared identifier '_bit_scan_forward'

_bit_scan_forward is not a standard intrinsic. In clang and icx it is a macro defined only in ia32intrin.h, which is included through <x86intrin.h>; upstream LLVM's and intel/llvm's <immintrin.h> do not include it. The file includes only <immintrin.h> and compiled so far because earlier icx headers pulled ia32intrin.h in from <immintrin.h>.

Both uses, in the AVX2 float path of computeBucketID (the lower-bound search behind the approximate median of a kd-tree split), now call __builtin_ctz(maskInt). It is a compiler builtin, so it needs no header, and icx accepts it on Linux and Windows. maskInt is already computed and checked non-zero on both lines, so the result is the same index _bit_scan_forward returned, and the generated code is the same tzcnt. These were the only two uses in the repository.

Testing

The failure cannot be reproduced with the icx on hand, so there is no regression test; the compiler check is the test. Evidence:

  • With the clang 19 headers (which do not include ia32intrin.h from <immintrin.h>), -march=haswell -fsyntax-only reproduces the error on both x86_64-unknown-linux-gnu and x86_64-pc-windows-msvc for the old code and compiles the new code.

  • make -f makefile daal PLAT=lnx32e COMPILER=icx REQCPU=avx2 builds; the rebuilt ..._fpt_flt_cpu_hsw.o has vmovmskps + tzcnt in computeBucketID.

  • bazel test --config=host //cpp/oneapi/dal/algo/knn:tests: 5 of 5 pass.
  • ---

    <details>
    <summary>Checklist:</summary>

    Completeness and readability

  • I have commented my code, particularly in hard-to-understand areas: no comment needed, the change is a one-to-one replacement.

  • I have updated the documentation to reflect the changes or created a separate PR with updates and provided its number in the description, if necessary: not needed.

  • Git commit message contains an appropriate signed-off-by string _(see CONTRIBUTING.md for details)_.

  • I have resolved any merge conflicts that might occur with the base branch.
  • Testing

  • I have run it locally and tested the changes extensively: see Testing above.

  • All CI jobs are green or I have provided justification why they aren't: CI runs on this pull request.

  • I have extended testing suite if new functionality was introduced in this PR: no new functionality.
  • Performance

  • I have provided justification why performance and/or quality metrics have changed or why changes are not expected: same instruction is generated.
  • </details>

    🤖 Generated with Claude Code

    Diff: 363 lines (+272 / -91) across 4 files Reviewer: david-cortes-intel Review duration: 906m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 363 lines diff across 4 files
    0.43
    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.353
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4009"
    general weight: 0.35
    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: 42.7% (MEDIUM) · Higher = larger blast radius
    42.7%
    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: 16.9% · Safe to merge
    16.9%