| Issue |
234874
|
| Summary |
[X86] clang-cl miscompiles testb/cmovnz inline asm at /O1 and /O2 in LLVM 23.1.x (InlinerPass, regression from 22.1.3)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
QianNangong
|
## Summary
clang-cl from **LLVM 23.1.x** miscompiles the `testb`/`cmovnz` inline-asm block below at **/O1 and /O2**, producing corrupted results in the caller. This is a regression: LLVM **22.1.3** (Visual Studio 2026 bundled) and **MSVC v145** generate correct code at all optimization levels tested. The first bad optimization pass (via `-mllvm -opt-bisect-limit`) is:
```
BISECT: running pass (1863) inline on (memlz_decode_8_sse)
```
i.e. **InlinerPass**, when it inlines the asm-containing `cmov` helper into the decode function. With `-opt-bisect-limit=1862` the program is correct; with `=1863` it is wrong.
## Environment
- Windows 11 x64
- clang-cl **LLVM 23.1.4** (external LLVM distribution)
- Reference toolchains: clang-cl LLVM 22.1.3 (VS 2026 bundled), MSVC v145
## Reproducer
Uses the memlz 0.6 compression library (header-only), commit `e76a6a37bcd77bad28e72871e8499e9bd4404ff8` of https://github.com/rrrlasse/memlz . The miscompiled construct is its cmov helper:
```c
inline uint32_t memlz_cmov32(bool cond, uint32_t then_val, uint32_t else_val) {
__asm__(
"testb %1, %1\n\t"
"cmovnzl %2, %0"
: "+r" (else_val)
: "r" (cond), "r" (then_val)
: "cc"
);
return else_val;
}
```
Driver (~35 lines; build with `clang-cl /O2 repro.cpp`):
```cpp
#define MEMLZ_IMPLEMENTATION
#include "memlz.h" // memlz 0.6, commit e76a6a3
int main()
{
std::vector<uint8_t> src(1u << 20);
for (size_t i = 0; i < src.size(); ++i)
src[i] = static_cast<uint8_t>((i * 2654435761u) >> 13);
std::vector<uint8_t> comp((size_t)(memlz_max_compressed_len(src.size()) * 1.5));
const size_t clen = memlz_compress(comp.data(), src.data(), src.size());
std::vector<uint8_t> dec(src.size());
const size_t dlen = memlz_decompress(dec.data(), comp.data());
// Expected: dlen == src.size() and contents identical.
// Actual (LLVM 23.1.4, /O1 and /O2): dlen matches but contents differ.
return (dlen != src.size() || std::memcmp(dec.data(), src.data(), src.size()) != 0) ? 1 : 0;
}
```
## Results matrix (roundtrip of 1 MiB deterministic high-entropy data)
| Toolchain | /Od | /O1 | /O2 | /O3 |
|---|---|---|---|---|
| clang-cl 23.1.4 | OK | **wrong output** | **wrong output** | OK |
| clang-cl 22.1.3 | OK | OK | OK | OK |
| MSVC v145 | OK | — | OK | — |
The failure is deterministic and input-dependent: small or constant inputs pass; high-entropy data fails.
## Additional evidence
- `-mllvm -opt-bisect-limit` bisection: OK through pass 1862, first bad at **pass 1863 = `inline`** applied to `memlz_decode_8_sse` (which contains the asm cmov call).
- `-fno-strict-aliasing` does **not** change the result (not an aliasing issue).
- `#pragma clang optimize off` around the header makes /O2 correct.
- Replacing the asm cmov with the portable branch (`cond ? then_val : else_val`) makes every level correct.
## Notes
A ready-to-run package (repro.cpp + the exact `memlz.h` from the referenced commit + a one-click build matrix script) is available; happy to attach or share it in whatever form is most convenient. Reported downstream at rrrlasse/memlz#7 (closed, retargeted here).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs