xref: /llvm-project/compiler-rt/test/profile/instrprof-discarded-comdat.cpp (revision f2916becc572047915f537c9abc2b04562c4bdb8)
1*f2916becSPaul Kirth // Check that instrprof does not introduce references to discarded sections when
2*f2916becSPaul Kirth // using comdats.
3*f2916becSPaul Kirth //
4*f2916becSPaul Kirth // Occasionally, it is possible that the same function can be compiled in
5*f2916becSPaul Kirth // different TUs with slightly different linkages, e.g., due to different
6*f2916becSPaul Kirth // compiler options. However, if these are comdat functions, a single
7*f2916becSPaul Kirth // implementation will be chosen at link time. we want to ensure that the
8*f2916becSPaul Kirth // profiling data does not contain a reference to the discarded section.
9*f2916becSPaul Kirth 
10*f2916becSPaul Kirth // UNSUPPORTED: target={{.*windows.*}}
11*f2916becSPaul Kirth 
12*f2916becSPaul Kirth // RUN: mkdir -p %t.d
13*f2916becSPaul Kirth // RUN: %clangxx_pgogen -O2 -fPIC -ffunction-sections -fdata-sections -c %s -o %t.d/a1.o -DOBJECT_1 -mllvm -disable-preinline
14*f2916becSPaul Kirth // RUN: %clangxx_pgogen -O2 -fPIC -ffunction-sections -fdata-sections -c %s -o %t.d/a2.o
15*f2916becSPaul Kirth // RUN: %clangxx_pgogen -fPIC -shared -o %t.d/liba.so %t.d/a1.o %t.d/a2.o 2>&1 | FileCheck %s --allow-empty
16*f2916becSPaul Kirth 
17*f2916becSPaul Kirth // Ensure that we don't get an error when linking
18*f2916becSPaul Kirth // CHECK-NOT: relocation refers to a discarded section: .text._ZN1CIiE1fEi
19*f2916becSPaul Kirth 
20*f2916becSPaul Kirth template <typename T> struct C {
21*f2916becSPaul Kirth   void f(T x);
gC22*f2916becSPaul Kirth   int g(T x) {
23*f2916becSPaul Kirth     f(x);
24*f2916becSPaul Kirth     return v;
25*f2916becSPaul Kirth   }
26*f2916becSPaul Kirth   int v;
27*f2916becSPaul Kirth };
28*f2916becSPaul Kirth 
29*f2916becSPaul Kirth template <typename T>
30*f2916becSPaul Kirth #ifdef OBJECT_1
31*f2916becSPaul Kirth __attribute__((weak))
32*f2916becSPaul Kirth #else
33*f2916becSPaul Kirth __attribute__((noinline))
34*f2916becSPaul Kirth #endif
f(T x)35*f2916becSPaul Kirth void C<T>::f(T x) {
36*f2916becSPaul Kirth   v += x;
37*f2916becSPaul Kirth }
38*f2916becSPaul Kirth 
39*f2916becSPaul Kirth #ifdef OBJECT_1
foo()40*f2916becSPaul Kirth int foo() {
41*f2916becSPaul Kirth   C<int> c;
42*f2916becSPaul Kirth   c.f(1);
43*f2916becSPaul Kirth   return c.g(2);
44*f2916becSPaul Kirth }
45*f2916becSPaul Kirth #else
bar()46*f2916becSPaul Kirth int bar() {
47*f2916becSPaul Kirth   C<int> c;
48*f2916becSPaul Kirth   c.f(3);
49*f2916becSPaul Kirth   return c.g(4);
50*f2916becSPaul Kirth }
51*f2916becSPaul Kirth #endif
52