llvm / llvm-project

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

ThinLTO doesn't apply interprocedural FunctionAttrs optimization #32995

Open llvmbot opened 7 years ago

llvmbot commented 7 years ago
Bugzilla Link 33648
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @modiking

Extended Description

LTO will apply the funcitonattrs pass, but ThinLTO can't apply this optimization right now.

$ cat a.cc

include

include

int some_global = 100; const int garr[] = {1,2,3}; int garr2[] = {1,2,3};

attribute((noinline)) int a() { return 10; }

attribute((noinline)) void b(const int **x) { printf("%p", x[0]); }

attribute((noinline)) const int c(const int x) { // alias return &some_global; }

attribute((noinline)) int *d(int **x) { // alias, readonly arg return x[0]; } $ cat b.cc

include

extern int a(); extern void b(const int *x); extern const int c(const int x); extern int d(int **x);

int main(void) { const int arr2[] = {1,2}; int x = 3; int y = 4; int *arr[] = {&x, &y}; printf("%d\n", a()); b((const int **) arr); printf("%p\n", c(&x)); printf("%p\n", d(arr)); }

$ ../build/bin/clang++ a.cc b.cc -O3 --for-linker=-mllvm --for-linker=-stats -flto=thin -fuse-ld=lld -o thin &>&1 | grep funcitonattrs

nothing

$ ../build/bin/clang++ a.cc b.cc -O3 --for-linker=-mllvm --for-linker=-stats -flto -fuse-ld=lld -o lto &>&1 | grep funcitonattrs

llvmbot commented 7 years ago

Patch to add ReadNone, ReadOnly and NoRecurse propagation: https://reviews.llvm.org/D36850

llvmbot commented 7 years ago

Oops, typo. Should be:

$ cat a.cc

include

include

int some_global = 100; const int garr[] = {1,2,3}; int garr2[] = {1,2,3};

attribute((noinline)) int a() { return 10; }

attribute((noinline)) void b(const int **x) { printf("%p", x[0]); }

attribute((noinline)) const int c(const int x) { // alias return &some_global; }

attribute((noinline)) int *d(int **x) { // alias, readonly arg return x[0]; } $ cat b.cc

include

extern int a(); extern void b(const int *x); extern const int c(const int x); extern int d(int **x);

int main(void) { const int arr2[] = {1,2}; int x = 3; int y = 4; int *arr[] = {&x, &y}; printf("%d\n", a()); b((const int **) arr); printf("%p\n", c(&x)); printf("%p\n", d(arr)); }

$ ../build/bin/clang++ a.cc b.cc -O3 --for-linker=-mllvm --for-linker=-stats -flto=thin -fuse-ld=lld -o thin &>&1 | grep functionattrs

nothing

$ ../build/bin/clang++ a.cc b.cc -O3 --for-linker=-mllvm --for-linker=-stats -flto -fuse-ld=lld -o lto &>&1 | grep functionattrs 1 functionattrs - Number of function returns marked noalias 1 functionattrs - Number of functions marked as norecurse 1 functionattrs - Number of functions marked readnone 1 functionattrs - Number of arguments marked readnone 1 functionattrs - Number of arguments marked returned

llvmbot commented 7 years ago

assigned to @modiking