brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.4 KiB · 4509c01 Raw
95 lines · cpp
1// This is a regression test for ThinLTO indirect-call-promotion when candidate2// 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 be10//   converted to function hash in indexed profiles.11// - Run IRPGO profile use and ThinTLO prelink pipeline and get LLVM bitcodes12//   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 || darwin17 18// The test failed on ppc when building the instrumented binary.19// ld.lld: error: /lib/../lib64/Scrt1.o: ABI version 1 is not supported20// UNSUPPORTED: ppc21 22// This test and IR test llvm/test/Transforms/PGOProfile/thinlto_indirect_call_promotion.ll23// are complementary to each other; a compiler-rt test has better test coverage24// on different platforms, and the IR test is less restrictive in terms of25// running environment and could be executed more widely.26 27// Use lld as linker for more robust test. We need to REQUIRE LLVMgold.so for28// LTO if default linker is GNU ld or gold anyway.29// REQUIRES: lld-available30 31// RUN: rm -rf %t && split-file %s %t && cd %t32 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 main37// RUN: env LLVM_PROFILE_FILE=main.profraw %run ./main38// RUN: llvm-profdata merge main.profraw -o main.profdata39 40// Use profile on lib and get bitcode. Explicitly skip ICP pass to test ICP happens as41// 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.bc43 44// Use profile on main and get bitcode.45// RUN: %clang -fprofile-use=main.profdata -flto=thin -O2 -c main.cpp -o main.bc46 47// Run llvm-lto to get summary file.48// RUN: llvm-lto -thinlto -o summary main.bc lib.bc49 50// Test the imports of functions. Default import thresholds would work but do51// explicit override to be more futureproof. Note all functions have one basic52// block with a function-entry-count of one, so they are actually hot functions53// 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=IMPORTS55 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 168// ICP-REMARK: Promote indirect call to {{.*}}callee1{{.*}} with count 1 out of 169 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.h75void global_func();76 77//--- lib.cpp78#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 internal84// 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.cpp93#include "lib.h"94int main() { global_func(); }95