Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

ThinLTO doesn't apply interprocedural FunctionAttrs optimization #32620

Open Quuxplusone opened 7 years ago

Quuxplusone commented 7 years ago
Bugzilla Link PR33648
Status NEW
Importance P enhancement
Reported by Charles Saternos (charles.saternos@gmail.com)
Reported on 2017-06-29 13:09:57 -0700
Last modified on 2021-06-02 18:08:15 -0700
Version trunk
Hardware PC Linux
CC ditaliano@apple.com, llvm-bugs@lists.llvm.org, modimo@fb.com, tejohnson@google.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
LTO will apply the funcitonattrs pass, but ThinLTO can't apply this
optimization right now.

$ cat a.cc
#include <stdio.h>
#include <assert.h>

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 <stdio.h>

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
Quuxplusone commented 7 years ago
Oops, typo. Should be:

$ cat a.cc
#include <stdio.h>
#include <assert.h>

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 <stdio.h>

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
Quuxplusone commented 7 years ago

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