247 lines · cpp
1// REQUIRES: lld, lld-available2 3// Building the instrumented binary will fail because lld doesn't support4// big-endian ELF for PPC (aka ABI 1).5// ld.lld: error: /lib/../lib64/Scrt1.o: ABI version 1 is not supported6// UNSUPPORTED: ppc && host-byteorder-big-endian7 8// RUN: rm -rf %t && mkdir %t && split-file %s %t && cd %t9 10// RUN: %clangxx_pgogen -fuse-ld=lld -O2 -fprofile-generate=. -mllvm -enable-vtable-value-profiling lib.cpp main.cpp -o test11// RUN: env LLVM_PROFILE_FILE=test.profraw ./test12 13// Show vtable profiles from raw profile.14// RUN: llvm-profdata show --function=main --ic-targets --show-vtables test.profraw | FileCheck %s --check-prefixes=COMMON,RAW15 16// Generate indexed profile from raw profile and show the data.17// RUN: llvm-profdata merge --keep-vtable-symbols test.profraw -o test.profdata18// RUN: llvm-profdata show --function=main --ic-targets --show-vtables test.profdata | FileCheck %s --check-prefixes=COMMON,INDEXED19 20// Generate text profile from raw and indexed profiles respectively and show the data.21// RUN: llvm-profdata merge --keep-vtable-symbols --text test.profraw -o raw.proftext22// RUN: llvm-profdata show --function=main --ic-targets --show-vtables --text raw.proftext | FileCheck %s --check-prefix=ICTEXT23// RUN: llvm-profdata merge --keep-vtable-symbols --text test.profdata -o indexed.proftext24// RUN: llvm-profdata show --function=main --ic-targets --show-vtables --text indexed.proftext | FileCheck %s --check-prefix=ICTEXT25 26// Generate indexed profile from text profiles and show the data27// RUN: llvm-profdata merge --keep-vtable-symbols --binary raw.proftext -o text.profraw28// RUN: llvm-profdata show --function=main --ic-targets --show-vtables text.profraw | FileCheck %s --check-prefixes=COMMON,INDEXED29// RUN: llvm-profdata merge --keep-vtable-symbols --binary indexed.proftext -o text.profdata30// RUN: llvm-profdata show --function=main --ic-targets --show-vtables text.profdata | FileCheck %s --check-prefixes=COMMON,INDEXED31 32// COMMON: Counters:33// COMMON-NEXT: main:34// COMMON-NEXT: Hash: 0x068617320ec408a035// COMMON-NEXT: Counters: 436// COMMON-NEXT: Indirect Call Site Count: 237// COMMON-NEXT: Number of instrumented vtables: 238// RAW: Indirect Target Results:39// RAW-NEXT: [ 0, _ZN8Derived14funcEii, 50 ] (25.00%)40// RAW-NEXT: [ 0, {{.*}}lib.cpp;_ZN12_GLOBAL__N_18Derived24funcEii, 150 ] (75.00%)41// RAW-NEXT: [ 1, _ZN8Derived1D0Ev, 250 ] (25.00%)42// RAW-NEXT: [ 1, {{.*}}lib.cpp;_ZN12_GLOBAL__N_18Derived2D0Ev, 750 ] (75.00%)43// RAW-NEXT: VTable Results:44// RAW-NEXT: [ 0, _ZTV8Derived1, 50 ] (25.00%)45// RAW-NEXT: [ 0, {{.*}}lib.cpp;_ZTVN12_GLOBAL__N_18Derived2E, 150 ] (75.00%)46// RAW-NEXT: [ 1, _ZTV8Derived1, 250 ] (25.00%)47// RAW-NEXT: [ 1, {{.*}}lib.cpp;_ZTVN12_GLOBAL__N_18Derived2E, 750 ] (75.00%)48// INDEXED: Indirect Target Results:49// INDEXED-NEXT: [ 0, {{.*}}lib.cpp;_ZN12_GLOBAL__N_18Derived24funcEii, 150 ] (75.00%)50// INDEXED-NEXT: [ 0, _ZN8Derived14funcEii, 50 ] (25.00%)51// INDEXED-NEXT: [ 1, {{.*}}lib.cpp;_ZN12_GLOBAL__N_18Derived2D0Ev, 750 ] (75.00%)52// INDEXED-NEXT: [ 1, _ZN8Derived1D0Ev, 250 ] (25.00%)53// INDEXED-NEXT: VTable Results:54// INDEXED-NEXT: [ 0, {{.*}}lib.cpp;_ZTVN12_GLOBAL__N_18Derived2E, 150 ] (75.00%)55// INDEXED-NEXT: [ 0, _ZTV8Derived1, 50 ] (25.00%)56// INDEXED-NEXT: [ 1, {{.*}}lib.cpp;_ZTVN12_GLOBAL__N_18Derived2E, 750 ] (75.00%)57// INDEXED-NEXT: [ 1, _ZTV8Derived1, 250 ] (25.00%)58// COMMON: Instrumentation level: IR entry_first = 059// COMMON-NEXT: Functions shown: 160// COMMON-NEXT: Total functions: 761// COMMON-NEXT: Maximum function count: 100062// COMMON-NEXT: Maximum internal block count: 100063// COMMON-NEXT: Statistics for indirect call sites profile:64// COMMON-NEXT: Total number of sites: 265// COMMON-NEXT: Total number of sites with values: 266// COMMON-NEXT: Total number of profiled values: 467// COMMON-NEXT: Value sites histogram:68// COMMON-NEXT: NumTargets, SiteCount69// COMMON-NEXT: 2, 270// COMMON-NEXT: Statistics for vtable profile:71// COMMON-NEXT: Total number of sites: 272// COMMON-NEXT: Total number of sites with values: 273// COMMON-NEXT: Total number of profiled values: 474// COMMON-NEXT: Value sites histogram:75// COMMON-NEXT: NumTargets, SiteCount76// COMMON-NEXT: 2, 277 78// ICTEXT: :ir79// ICTEXT: main80// ICTEXT: # Func Hash:81// ICTEXT: 47008871487032745682// ICTEXT: # Num Counters:83// ICTEXT: 484// ICTEXT: # Counter Values:85// ICTEXT: 100086// ICTEXT: 100087// ICTEXT: 20088// ICTEXT: 189// ICTEXT: # Num Value Kinds:90// ICTEXT: 291// ICTEXT: # ValueKind = IPVK_IndirectCallTarget:92// ICTEXT: 093// ICTEXT: # NumValueSites:94// ICTEXT: 295// ICTEXT: 296// ICTEXT: {{.*}}lib.cpp;_ZN12_GLOBAL__N_18Derived24funcEii:15097// ICTEXT: _ZN8Derived14funcEii:5098// ICTEXT: 299// ICTEXT: {{.*}}lib.cpp;_ZN12_GLOBAL__N_18Derived2D0Ev:750100// ICTEXT: _ZN8Derived1D0Ev:250101// ICTEXT: # ValueKind = IPVK_VTableTarget:102// ICTEXT: 2103// ICTEXT: # NumValueSites:104// ICTEXT: 2105// ICTEXT: 2106// ICTEXT: {{.*}}lib.cpp;_ZTVN12_GLOBAL__N_18Derived2E:150107// ICTEXT: _ZTV8Derived1:50108// ICTEXT: 2109// ICTEXT: {{.*}}lib.cpp;_ZTVN12_GLOBAL__N_18Derived2E:750110// ICTEXT: _ZTV8Derived1:250111 112// When vtable value profiles exist, pgo-instr-use pass should annotate them113// even if `-enable-vtable-value-profiling` is not explicitly on.114// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 \115// RUN: -mllvm -print-after=pgo-instr-use -mllvm -filter-print-funcs=main \116// RUN: -mllvm -print-module-scope lib.cpp main.cpp 2>&1 | FileCheck %s --check-prefix=ANNOTATE117 118// ANNOTATE-NOT: Inconsistent number of value sites119// ANNOTATE: !{!"VP", i32 2120 121// When vtable value profiles exist, pgo-instr-use pass will not annotate them122// if `-icp-max-num-vtables` is set to zero.123// RUN: %clangxx -m64 -fprofile-use=test.profdata -fuse-ld=lld -O2 \124// RUN: -mllvm -icp-max-num-vtables=0 -mllvm -print-after=pgo-instr-use \125// RUN: -mllvm -filter-print-funcs=main -mllvm -print-module-scope lib.cpp main.cpp 2>&1 | \126// RUN: FileCheck %s --check-prefix=OMIT127 128// OMIT: Inconsistent number of value sites129// OMIT-NOT: !{!"VP", i32 2130 131// Test indirect call promotion transformation using vtable profiles.132// - Build with `-g` to enable debug information.133// - In real world settings, ICP pass is disabled in prelink pipeline. In134// the postlink pipeline, ICP is enabled after whole-program-devirtualization135// pass. Do the same thing in this test.136// - Enable `-fwhole-program-vtables` generate type metadata and intrinsics.137// - Enable `-fno-split-lto-unit` and `-Wl,-lto-whole-program-visibility` to138// preserve type intrinsics for ICP pass.139// RUN: %clangxx -m64 -fprofile-use=test.profdata -Wl,--lto-whole-program-visibility \140// RUN: -mllvm -disable-icp=true -Wl,-mllvm,-disable-icp=false -fuse-ld=lld \141// RUN: -g -flto=thin -fwhole-program-vtables -fno-split-lto-unit -O2 \142// RUN: -mllvm -enable-vtable-value-profiling -Wl,-mllvm,-enable-vtable-value-profiling \143// RUN: -mllvm -enable-vtable-profile-use \144// RUN: -Wl,-mllvm,-enable-vtable-profile-use -Rpass=pgo-icall-prom \145// RUN: -Wl,-mllvm,-print-after=pgo-icall-prom \146// RUN: -Wl,-mllvm,-filter-print-funcs=main lib.cpp main.cpp 2>&1 \147// RUN: | FileCheck %s --check-prefixes=REMARK,IR --implicit-check-not="!VP"148 149// For the indirect call site `ptr->func`150// REMARK: main.cpp:10:19: Promote indirect call to _ZN12_GLOBAL__N_18Derived24funcEii.llvm.{{.*}} with count 150 out of 200, sink 1 instruction(s) and compare 1 vtable(s): {_ZTVN12_GLOBAL__N_18Derived2E.llvm.{{.*}}}151// REMARK: main.cpp:10:19: Promote indirect call to _ZN8Derived14funcEii with count 50 out of 50, sink 1 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}152//153// For the indirect call site `delete ptr`154// REMARK: main.cpp:12:5: Promote indirect call to _ZN12_GLOBAL__N_18Derived2D0Ev.llvm.{{.*}} with count 750 out of 1000, sink 2 instruction(s) and compare 1 vtable(s): {_ZTVN12_GLOBAL__N_18Derived2E.llvm.{{.*}}}155// REMARK: main.cpp:12:5: Promote indirect call to _ZN8Derived1D0Ev with count 250 out of 250, sink 2 instruction(s) and compare 1 vtable(s): {_ZTV8Derived1}156 157// The IR matchers for indirect callsite `ptr->func`.158// IR-LABEL: @main159// IR: [[OBJ:%.*]] = {{.*}}call {{.*}} @_Z10createTypei160// IR: [[VTABLE:%.*]] = load ptr, ptr [[OBJ]]161// IR: [[CMP1:%.*]] = icmp eq ptr [[VTABLE]], getelementptr inbounds (i8, ptr @_ZTVN12_GLOBAL__N_18Derived2E.llvm.{{.*}}, i32 16)162// IR: br i1 [[CMP1]], label %[[BB1:.*]], label %[[BB2:[a-zA-Z0-9_.]+]],163//164// IR: [[BB1]]:165// IR: [[RESBB1:%.*]] = {{.*}}call {{.*}} @_ZN12_GLOBAL__N_18Derived24funcEii.llvm.{{.*}}166// IR: br label %[[MERGE0:[a-zA-Z0-9_.]+]]167//168// IR: [[BB2]]:169// IR: [[CMP2:%.*]] = icmp eq ptr [[VTABLE]], getelementptr inbounds (i8, ptr @_ZTV8Derived1, i32 16)170// IR: br i1 [[CMP2]], label %[[BB3:.*]], label %[[BB4:[a-zA-Z0-9_.]+]],171//172// IR: [[BB3]]:173// IR: [[RESBB3:%.*]] = {{.*}}call {{.*}} @_ZN8Derived14funcEii174// IR: br label %[[MERGE1:[a-zA-Z0-9_.]+]],175//176// IR: [[BB4]]:177// IR: [[FUNCPTR:%.*]] = load ptr, ptr [[VTABLE]]178// IR: [[RESBB4:%.*]] = {{.*}}call {{.*}} [[FUNCPTR]]179// IR: br label %[[MERGE1]]180//181// IR: [[MERGE1]]:182// IR: [[RES1:%.*]] = phi i32 [ [[RESBB4]], %[[BB4]] ], [ [[RESBB3]], %[[BB3]] ]183// IR: br label %[[MERGE0]]184//185// IR: [[MERGE0]]:186// IR: [[RES2:%.*]] = phi i32 [ [[RES1]], %[[MERGE1]] ], [ [[RESBB1]], %[[BB1]] ]187 188//--- lib.h189#include <stdio.h>190#include <stdlib.h>191class Base {192public:193 virtual int func(int a, int b) = 0;194 195 virtual ~Base() {};196};197 198class Derived1 : public Base {199public:200 int func(int a, int b) override;201 202 ~Derived1() {}203};204 205__attribute__((noinline)) Base *createType(int a);206 207//--- lib.cpp208#include "lib.h"209 210namespace {211class Derived2 : public Base {212public:213 int func(int a, int b) override { return a * (a - b); }214 215 ~Derived2() {}216};217} // namespace218 219int Derived1::func(int a, int b) { return a * b; }220 221Base *createType(int a) {222 Base *base = nullptr;223 if (a % 4 == 0)224 base = new Derived1();225 else226 base = new Derived2();227 return base;228}229 230//--- main.cpp231#include "lib.h"232 233int main(int argc, char **argv) {234 int sum = 0;235 for (int i = 0; i < 1000; i++) {236 int a = rand();237 int b = rand();238 Base *ptr = createType(i);239 if (i % 5 == 0)240 sum += ptr->func(b, a);241 242 delete ptr;243 }244 printf("sum is %d\n", sum);245 return 0;246}247