llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.57k stars 11.81k forks source link

Request to hook up `-fno-builtin-aligned_alloc` #45000

Open clin111 opened 4 years ago

clin111 commented 4 years ago
Bugzilla Link 45655
Version trunk
OS All
CC @DougGregor,@efriedma-quic,@zygoloid

Extended Description

For users that have a different aligned_alloc implementation such as jemalloc: http://jemalloc.net/

it might be desirable to turn off TLI recognition of aligned_alloc (commit D76970) without having to turn off all builtins with -fno-builtin or -ffreestanding. -fno-builtin-aligned_alloc doesn't look like it's recognized by the front end (and doesn't result in a nobuiltin attribute on the call).

As an example of a difference, the above jemalloc library is strict POSIX and will set errno if a non-power-of-2 alignment is given. When the code below is compiled with clang -O2 and jemalloc, the printf will print (nil) 0 because jemalloc will produce NULL, but InstCombine assumes the call will succeed and replaces errno with 0. Having a way to turn off recognition just for this function, would be very helpful to fix the inconsistency.

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main() {
  void *p;

  errno = 0;
  p = (int *)aligned_alloc(0x81,1);
  printf("%p %d\n", p, errno);

  return 0;
}
efriedma-quic commented 4 years ago

I think we just need to add aligned_alloc to clang/include/clang/Basic/Builtins.def

bondhugula commented 4 years ago

It's not immediately clear to me what has to be done here. I've never worked on Clang, but this looks pretty straightforward to fix for someone who has. It's also not clear if InstCombine has to be fixed to do the right thing whenever the alignment isn't known to be a power of two. When the alignment isn't a power of two, aligned_alloc returns NULL and errno should be EINVAL.