1// This is a regression test for ThinLTO indirect-call-promotion when candidate
2// callees need to be imported from another IR module. In the C++ test case,
3// `main` calls `global_func` which is defined in another module. `global_func`
4// has two indirect callees, one has external linkage and one has local linkage.
5// All three functions should be imported into the IR module of main.
6
7// What the test does:
8// - Generate raw profiles from executables and convert it to indexed profiles.
9// During the conversion, a profiled callee address in raw profiles will be
10// converted to function hash in indexed profiles.
11// - Run IRPGO profile use and ThinTLO prelink pipeline and get LLVM bitcodes
12// for both cpp files in the C++ test case.
13// - Generate ThinLTO summary file with LLVM bitcodes, and run `function-import` pass.
14// - Run `pgo-icall-prom` pass for the IR module which needs to import callees.
15
16// REQUIRES: windows || linux || darwin
17
18// The test failed on ppc when building the instrumented binary.
19// ld.lld: error: /lib/../lib64/Scrt1.o: ABI version 1 is not supported
20// UNSUPPORTED: ppc
21
22// This test and IR test llvm/test/Transforms/PGOProfile/thinlto_indirect_call_promotion.ll
23// are complementary to each other; a compiler-rt test has better test coverage
24// on different platforms, and the IR test is less restrictive in terms of
25// running environment and could be executed more widely.
26
27// Use lld as linker for more robust test. We need to REQUIRE LLVMgold.so for
28// LTO if default linker is GNU ld or gold anyway.
29// REQUIRES: lld-available
30
31// RUN: rm -rf %t && split-file %s %t && cd %t
32
33// Do setup work for all below tests.
34// Generate raw profiles from real programs and convert it into indexed profiles.
35// Use clangxx_pgogen for IR level instrumentation for C++.
36// RUN: %clangxx_pgogen -fuse-ld=lld -O2 lib.cpp main.cpp -o main
37// RUN: env LLVM_PROFILE_FILE=main.profraw %run ./main
38// RUN: llvm-profdata merge main.profraw -o main.profdata
39
40// Use profile on lib and get bitcode. Explicitly skip ICP pass to test ICP happens as
41// expected in the IR module that imports functions from lib.
42// RUN: %clang -mllvm -disable-icp -fprofile-use=main.profdata -flto=thin -O2 -c lib.cpp -o lib.bc
43
44// Use profile on main and get bitcode.
45// RUN: %clang -fprofile-use=main.profdata -flto=thin -O2 -c main.cpp -o main.bc
46
47// Run llvm-lto to get summary file.
48// RUN: llvm-lto -thinlto -o summary main.bc lib.bc
49
50// Test the imports of functions. Default import thresholds would work but do
51// explicit override to be more futureproof. Note all functions have one basic
52// block with a function-entry-count of one, so they are actually hot functions
53// per default profile summary hotness cutoff.
54// RUN: opt -passes=function-import -import-instr-limit=100 -import-cold-multiplier=1 -summary-file summary.thinlto.bc main.bc -o main.import.bc -print-imports 2>&1 | FileCheck %s --check-prefix=IMPORTS
55
56// Test that both candidates are ICP'ed and there is no `!VP` in the IR.
57// RUN: opt main.import.bc -icp-lto -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom 2>&1 | FileCheck %s --check-prefixes=ICP-IR,ICP-REMARK --implicit-check-not="!VP"
58
59// IMPORTS-DAG: main.cpp: Import {{.*}}callee1{{.*}}
60// IMPORTS-DAG: main.cpp: Import {{.*}}callee0{{.*}}llvm.[[#]]
61// IMPORTS-DAG: main.cpp: Import {{.*}}global_func{{.*}}
62
63// PGOName-DAG: define {{.*}}callee1{{.*}} !prof ![[#]] {
64// PGOName-DAG: define internal {{.*}}callee0{{.*}} !prof ![[#]] !PGOFuncName ![[#MD:]] {
65// PGOName-DAG: ![[#MD]] = !{!"{{.*}}lib.cpp;{{.*}}callee0{{.*}}"}
66
67// ICP-REMARK: Promote indirect call to {{.*}}callee0{{.*}}llvm.[[#]] with count 1 out of 1
68// ICP-REMARK: Promote indirect call to {{.*}}callee1{{.*}} with count 1 out of 1
69
70// ICP-IR: br i1 %[[#]], label %if.true.direct_targ, label %if.false.orig_indirect, !prof ![[#BRANCH_WEIGHT1:]]
71// ICP-IR: br i1 %[[#]], label %if.true.direct_targ1, label %if.false.orig_indirect2, !prof ![[#BRANCH_WEIGHT1]]
72// ICP-IR: ![[#BRANCH_WEIGHT1]] = !{!"branch_weights", i32 1, i32 0}
73
74//--- lib.h
75void global_func();
76
77//--- lib.cpp
78#include "lib.h"
79static void callee0() {}
80void callee1() {}
81typedef void (*FPT)();
82FPT calleeAddrs[] = {callee0, callee1};
83// `global_func`` might call one of two indirect callees. callee0 has internal
84// linkage and callee1 has external linkage.
85void global_func() {
86 FPT fp = calleeAddrs[0];
87 fp();
88 fp = calleeAddrs[1];
89 fp();
90}
91
92//--- main.cpp
93#include "lib.h"
94int main() { global_func(); }
95

source code of compiler-rt/test/profile/instrprof-thinlto-indirect-call-promotion.cpp