Skip to content

Fix prefetch hints on clang-cl so prefetchL1 emits prefetcht0 - #654

Closed
MercylinPreethi wants to merge 1 commit into
RenderKit:masterfrom
MercylinPreethi:windows-clang-prefetch-fix
Closed

MercylinPreethi wants to merge 1 commit into
RenderKit:masterfrom
MercylinPreethi:windows-clang-prefetch-fix

Conversation

@MercylinPreethi

Copy link
Copy Markdown
Contributor

Summary

Fix prefetch hints on clang-cl so prefetchL1() emits prefetcht0

  • On clang targeting the MSVC ABI (clang-cl, or clang++ with *-windows-msvc), <windows.h> pulls in winnt.h, which redefines _MM_HINT_T0/T1/T2/NTA to the MSVC values 1/2/3/0 after clang's xmmintrin.h set them to 3/2/1/0.
  • Clang's _mm_prefetch reads the hint as a GCC-style locality (hint & 3), so prefetchL1() became prefetcht2 and prefetchL3() became prefetcht0. Every BVH node and leaf prefetch during traversal was affected.
  • This uses __builtin_prefetch with an explicit locality for clang + MSVC ABI on x86 only. MSVC, GCC, Linux, macOS and ARM64 still use the existing _mm_prefetch path. It does not change results.

Files: common/sys/intrinsics.h

Measured

Embree built from upstream 304b5b744, Release, SSE2 to AVX2

Vendor CPU OS Compiler Single thread
AMD Ryzen 5 9600X (6 cores, 12 threads) Windows clang-cl 21.1.0 +8.1% Mrays/s (1.976 -> 2.135)
AMD Ryzen 5 9600X (6 cores, 12 threads) Windows clang-cl 23.1.5 +7.8% Mrays/s (1.993 -> 2.149)

Prefetch instructions in the AVX2 library (clang-cl 23.1.5):

Build prefetcht0 prefetcht1 prefetcht2
Before 0 432 5757
After 5757 432 0

Test plan

  • Disassembly: prefetcht0 / t1 / t2 / nta now match prefetchL1 / L2 / L3 / NTA on clang-cl.
  • Timing: 5 interleaved rounds, best of 7 passes each, on both compilers. Hit count is identical (1,657,236) before and after.
  • Embree test suite, EMBREE_TESTING_INTENSITY=3, clang-cl 21.1.0 on Ryzen 5 9600X: 625/625 passed

When Embree is built with clang for the MSVC ABI (clang-cl, or clang++
targeting *-windows-msvc), <windows.h> brings in winnt.h, which redefines
_MM_HINT_T0/T1/T2/NTA to the MSVC values 1/2/3/0 after clang's xmmintrin.h
has set them to 3/2/1/0. Clang's _mm_prefetch reads the hint as a GCC-style
locality (hint & 3), so prefetchL1 ended up as prefetcht2 and prefetchL3 as
prefetcht0. Every BVH node and leaf prefetch during traversal was affected.

Use __builtin_prefetch with an explicit locality for this compiler/ABI
combination. MSVC, GCC, Linux, macOS and ARM64 builds still take the existing
_mm_prefetch path.

Checked on clang-cl 21.1.0 (Ryzen 5 9600X): the AVX2 traversal code goes from
0 prefetcht0 / 5298 prefetcht2 to 5298 prefetcht0 / 0 prefetcht2.
@MercylinPreethi
MercylinPreethi marked this pull request as draft November 20, 2026 07:30
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.

2 participants