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

source code of compiler-rt/test/profile/instrprof-discarded-comdat.cpp