[libc] [llvm] [libc] Improve qsort (PR #120450)

Schrodinger ZHU Yifan via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 5 23:23:52 PST 2025


SchrodingerZhu wrote:

@Voultapher In case you are interested:

I did not mention too much details about __builtin_memcpy in the before since both functions work as expected.

Basically, in GNU extension, many libc function has its builtin counterparts. They are used to do libcall optimizations. So semantically, calling __builtin_XXX should be understood as the same as calling the c function ::XXX. However, due to lib all optimizations, all of them are usually transformed into compiler intrinsics. For trivial case such as fixed size copy, they are optimized into direct hardware instructions. For large sizes or the cases where it is hard to decide the size or the most efficient copy algorithm, it falls back to a call to memcpy.

__builtin_inline_memcpy is a clang-specific intrinsic. It supports only static sizes and it does not have the same semantic of ::memcpy. It guarantees that no libcall will be ever emitted. Hence it is a good candidate if one wants to use builtin intrinsics to implement memcpy itself.
The LLVM compiler will decides the most proper hardware instructions to do the copy according to the cost model . E.g. on x86, small sized objects are copied using mov or sse/avx2/avx512. And larger sizes are copied using rep movsb if erms is available or just loops on older machines.

https://github.com/llvm/llvm-project/pull/120450


More information about the llvm-commits mailing list