llvm / llvm-project

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

[clang][alias fn attr] `error: alias must point to a defined variable or function` false negative when optimizations are enabled #89474

Open nickdesaulniers opened 5 months ago

nickdesaulniers commented 5 months ago

https://godbolt.org/z/TrGeazYc5

extern inline __attribute__((gnu_inline))
void bar (void) {}

void foo [[gnu::alias("_Z3barv")]] (void);
void foo2 (void) __attribute__((alias("_Z3barv")));
void foo3 (void) asm("_Z3barv");
<source>:4:12: error: alias must point to a defined variable or function
    4 | void foo [[gnu::alias("_Z3barv")]] (void);
      |            ^
<source>:4:12: note: the function or variable specified in an alias must refer to its mangled name
<source>:5:33: error: alias must point to a defined variable or function
    5 | void foo2 (void) __attribute__((alias("_Z3barv")));
      |                                 ^
<source>:5:33: note: the function or variable specified in an alias must refer to its mangled name

we only observe these diagnostics at -O0, not -O2.

via #60481

shafik commented 5 months ago

Worth noting this crashes on asserts build: https://godbolt.org/z/basjzojxE

Alias must point to a definition
ptr @_Z3foov
Alias must point to a definition
ptr @_Z4foo2v
fatal error: error in backend: Broken module found, compilation aborted!

Look related maybe duplicate: https://github.com/llvm/llvm-project/issues/69066

shafik commented 5 months ago

It looks like we diagnose this in codegen:

https://github.com/llvm/llvm-project/blob/adc4f6233df734fbe3793118ecc89d3584e0c90f/clang/lib/CodeGen/CodeGenModule.cpp#L591-L594

and:

https://github.com/llvm/llvm-project/blob/adc4f6233df734fbe3793118ecc89d3584e0c90f/clang/lib/CodeGen/CodeGenModule.cpp#L612-L619

I think then it makes more sense now that this is optimization sensitive.

CC @AaronBallman @erichkeane

AaronBallman commented 5 months ago

Both of those checks should always be called: https://github.com/llvm/llvm-project/blob/dc20a0ea1fd04a2ef95eb5c73e9f88357fc5f694/clang/lib/CodeGen/CodeGenModule.cpp#L832 calls checkAliases() which has no optimization-specific logic. So perhaps optimizations are removing some aliases from the list?