dxvk: `bit::lzcnt` does not work as intended on MSVC when running on a CPU without BMI1 instruction set
I have a DirectX game I build with MSVC, and I recently added support for Vulkan similar to how Valve did it in Source Engine by directly compiling DXVK sources into the binaries and not linking DirectX SDK libs. The problem is that one of my players reported a crash on startup somewhere in memory allocator, which I quickly traced to bit::lzcnt.
While trying to allocate 4096 bytes of memory, a DxvkPoolAllocator::computeListIndex call is made, which calls into bit::lzcnt and then _lzcnt_u32. On any CPU made in the last ~15 years, this call with 4096 as argument returns 3. For this user, it returns 4294967290 instead. Under MSVC, it simply calls the instruction directly, without checking if it’s supported by the CPU or not. The player’s CPU is i5 750, and it does not support that instruction. Release DLLs from this repository do not crash for them, since they are not built with MSVC and I assume either __builtin_clz is backwards-compatible with the old CPUs, or your compiler falls back to your own implementation of this instruction.
I didn’t look into it much, but I think re-implementing the instruction using _BitScanReverse should fix the issue.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (16 by maintainers)
Thanks for the information. I’ll do some test to check it.
EDIT: I have checked TZCNT, it appears same on 3337U and 11800H since TZCNT is same with BSF for non-zero. So it doesn’t need further fix.