Skip to content

clang-cl.exe wrong defaults for cmpxchg16 #166786

@confined8095

Description

@confined8095

Description

From Clang's user manual:

clang-cl is an alternative command-line interface to Clang, designed for compatibility with the Visual C++ compiler, cl.exe.

Currently clang refuses to emit lock cmpcxhg16 when calling _InterlockedCompareExchange128.
This behavior does not match cl.exe.

Affected versions

At least Clang 19 and 21, I did not try older versions.

Steps to reproduce

cmpxchg16.c:

#include <intrin.h>

#if _MSC_VER < 1930
#error "Old"
#endif

__declspec(dllexport) char test(volatile long long* dst, long long hi, long long lo, long long* result) {
    return _InterlockedCompareExchange128(dst, hi, lo, result);
}

compile with cl.exe works:

"cl.exe" -nologo -Focmpxchg16-msvc.c.obj -c cmpxchg16.c
"link.exe" -nologo -nodefaultlib -noentry -machine:x64 -dll cmpxchg16-msvc.c.obj -out:cmpxchg16-msvc.dll

compile with clang-cl.exe does not work:

"D:\\LLVM\\LLVM-21.1.4\\bin\\clang-cl.exe" -nologo -Focmpxchg16-clang.c.obj -c cmpxchg16.c
"D:\\LLVM\\LLVM-21.1.4\\bin\\lld-link.exe" -nologo -nodefaultlib -noentry -machine:x64 -dll cmpxchg16-clang.c.obj -out:cmpxchg16-clang.dll

instead it outputs:

lld-link: error: undefined symbol: __security_cookie
>>> referenced by cmpxchg16-clang.c.obj:(test)
>>> referenced by cmpxchg16-clang.c.obj:(test)

lld-link: error: undefined symbol: __atomic_compare_exchange_16
>>> referenced by cmpxchg16-clang.c.obj:(test)

lld-link: error: undefined symbol: __security_check_cookie
>>> referenced by cmpxchg16-clang.c.obj:(test)

Workaround

Adding -mcx16 to clang-cl.exe CLI flags. This option is not supported by real cl.exe and integrating it to buildsystem conditionally might not be trivial.

Additional notes

Required instructions for Windows 10 from Microsoft docs:

Support for CMPXCHG16b, LAHF/SAHF, and PrefetchW instructions

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-cl`clang-cl` driver. Don't use for other compiler partsdiverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions