Add flag to llvm-profdata to allow symbols in profile data to be remapped, and
add a tool to generate symbol remapping files.

Summary:
The new tool llvm-cxxmap builds a symbol mapping table from a file containing
a description of partial equivalences to apply to mangled names and files
containing old and new symbol tables.

Reviewers: davidxl

Subscribers: mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D51470

llvm-svn: 342168
diff --git a/llvm/test/tools/llvm-cxxmap/Inputs/after.sym b/llvm/test/tools/llvm-cxxmap/Inputs/after.sym
new file mode 100644
index 0000000..3dc6941
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/Inputs/after.sym
@@ -0,0 +1,2 @@
+_ZN1N1fEN1M1X1YE
+_ZN3foo6detail3qux1fEv
diff --git a/llvm/test/tools/llvm-cxxmap/Inputs/ambiguous.sym b/llvm/test/tools/llvm-cxxmap/Inputs/ambiguous.sym
new file mode 100644
index 0000000..e06fd6a
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/Inputs/ambiguous.sym
@@ -0,0 +1,2 @@
+_ZN1N1fENS_1X1YE
+_ZN1N1fEN1M1X1YE
diff --git a/llvm/test/tools/llvm-cxxmap/Inputs/before.sym b/llvm/test/tools/llvm-cxxmap/Inputs/before.sym
new file mode 100644
index 0000000..c6f563c
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/Inputs/before.sym
@@ -0,0 +1,2 @@
+_ZN3foo4quux1fEv
+_ZN1N1fENS_1X1YE
diff --git a/llvm/test/tools/llvm-cxxmap/Inputs/expected b/llvm/test/tools/llvm-cxxmap/Inputs/expected
new file mode 100644
index 0000000..eb2cdcf
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/Inputs/expected
@@ -0,0 +1,2 @@
+_ZN3foo4quux1fEv _ZN3foo6detail3qux1fEv
+_ZN1N1fENS_1X1YE _ZN1N1fEN1M1X1YE
diff --git a/llvm/test/tools/llvm-cxxmap/Inputs/incomplete.sym b/llvm/test/tools/llvm-cxxmap/Inputs/incomplete.sym
new file mode 100644
index 0000000..a452250
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/Inputs/incomplete.sym
@@ -0,0 +1 @@
+_ZN3foo6detail3qux1fEv
diff --git a/llvm/test/tools/llvm-cxxmap/Inputs/remap.map b/llvm/test/tools/llvm-cxxmap/Inputs/remap.map
new file mode 100644
index 0000000..df3d82d
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/Inputs/remap.map
@@ -0,0 +1,8 @@
+# foo:: and foo::detail:: are equivalent
+name 3foo N3foo6detailE
+
+# foo::qux and foo::quux are equivalent
+type N3foo3quxE N3foo4quuxE
+
+# N::X and M::X are equivalent
+name N1N1XE N1M1XE
diff --git a/llvm/test/tools/llvm-cxxmap/ambiguous.test b/llvm/test/tools/llvm-cxxmap/ambiguous.test
new file mode 100644
index 0000000..5162f7be
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/ambiguous.test
@@ -0,0 +1,2 @@
+RUN: llvm-cxxmap %S/Inputs/before.sym %S/Inputs/ambiguous.sym -r %S/Inputs/remap.map -o /dev/null -Wambiguous 2>&1 | FileCheck %s
+CHECK: warning: {{.*}}:2: symbol _ZN1N1fEN1M1X1YE is equivalent to earlier symbol _ZN1N1fENS_1X1YE
diff --git a/llvm/test/tools/llvm-cxxmap/incomplete.test b/llvm/test/tools/llvm-cxxmap/incomplete.test
new file mode 100644
index 0000000..c1da958
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/incomplete.test
@@ -0,0 +1,2 @@
+RUN: llvm-cxxmap %S/Inputs/before.sym %S/Inputs/incomplete.sym -r %S/Inputs/remap.map -o /dev/null -Wincomplete 2>&1 | FileCheck %s
+CHECK: warning: {{.*}}:2: no new symbol matches old symbol _ZN1N1fENS_1X1YE
diff --git a/llvm/test/tools/llvm-cxxmap/remap.test b/llvm/test/tools/llvm-cxxmap/remap.test
new file mode 100644
index 0000000..e22c36c
--- /dev/null
+++ b/llvm/test/tools/llvm-cxxmap/remap.test
@@ -0,0 +1,5 @@
+RUN: llvm-cxxmap %S/Inputs/before.sym %S/Inputs/after.sym -r %S/Inputs/remap.map -o %t.output -Wambiguous -Wincomplete 2>&1 | FileCheck %s --allow-empty
+RUN: diff %S/Inputs/expected %t.output
+
+CHECK-NOT: warning
+CHECK-NOT: error
diff --git a/llvm/test/tools/llvm-profdata/Inputs/instr-remap.expected b/llvm/test/tools/llvm-profdata/Inputs/instr-remap.expected
new file mode 100644
index 0000000..f5ff527
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/instr-remap.expected
@@ -0,0 +1,29 @@
+# IR level Instrumentation Flag
+:ir
+bar
+# Func Hash:
+1234
+# Num Counters:
+2
+# Counter Values:
+31
+42
+
+bar
+# Func Hash:
+5678
+# Num Counters:
+2
+# Counter Values:
+500
+600
+
+baz
+# Func Hash:
+5678
+# Num Counters:
+2
+# Counter Values:
+7
+8
+
diff --git a/llvm/test/tools/llvm-profdata/Inputs/instr-remap.proftext b/llvm/test/tools/llvm-profdata/Inputs/instr-remap.proftext
new file mode 100644
index 0000000..ddd6671
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/instr-remap.proftext
@@ -0,0 +1,25 @@
+# :ir is the flag to indicate this is IR level profile.
+:ir
+foo
+1234
+2
+1
+2
+
+bar
+1234
+2
+30
+40
+
+foo
+5678
+2
+500
+600
+
+baz
+5678
+2
+7
+8
diff --git a/llvm/test/tools/llvm-profdata/Inputs/instr-remap.remap b/llvm/test/tools/llvm-profdata/Inputs/instr-remap.remap
new file mode 100644
index 0000000..d675fa4
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/instr-remap.remap
@@ -0,0 +1 @@
+foo bar
diff --git a/llvm/test/tools/llvm-profdata/Inputs/sample-remap.expected b/llvm/test/tools/llvm-profdata/Inputs/sample-remap.expected
new file mode 100644
index 0000000..863ea1e
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/sample-remap.expected
@@ -0,0 +1,16 @@
+main:184019:0
+ 4: 534
+ 4.2: 534
+ 5: 1075
+ 5.1: 1075
+ 6: 2080
+ 7: 534
+ 9: 2064 _Z3bazi:1471 _Z3fooi:631
+ 10: inline2:2000
+  1: 2000
+ 10: inline42:1000
+  1: 1000
+_Z3bazi:40602:2437
+ 1: 2437
+_Z3fooi:7711:610
+ 1: 610
diff --git a/llvm/test/tools/llvm-profdata/Inputs/sample-remap.proftext b/llvm/test/tools/llvm-profdata/Inputs/sample-remap.proftext
new file mode 100644
index 0000000..33fce64
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/sample-remap.proftext
@@ -0,0 +1,18 @@
+_Z3bari:20301:1437
+ 1: 1437
+_Z3fooi:7711:610
+ 1: 610
+main:184019:0
+ 4: 534
+ 4.2: 534
+ 5: 1075
+ 5.1: 1075
+ 6: 2080
+ 7: 534
+ 9: 2064 _Z3bari:1471 _Z3fooi:631
+ 10: inline1:1000
+  1: 1000
+ 10: inline2:2000
+  1: 2000
+_Z3bazi:20301:1000
+ 1: 1000
diff --git a/llvm/test/tools/llvm-profdata/Inputs/sample-remap.remap b/llvm/test/tools/llvm-profdata/Inputs/sample-remap.remap
new file mode 100644
index 0000000..465c45c
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/sample-remap.remap
@@ -0,0 +1,2 @@
+_Z3bari _Z3bazi
+inline1 inline42
diff --git a/llvm/test/tools/llvm-profdata/instr-remap.test b/llvm/test/tools/llvm-profdata/instr-remap.test
new file mode 100644
index 0000000..159b737
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/instr-remap.test
@@ -0,0 +1,2 @@
+; RUN: llvm-profdata merge -text %S/Inputs/instr-remap.proftext -r %S/Inputs/instr-remap.remap -o %t.output
+; RUN: diff %S/Inputs/instr-remap.expected %t.output
diff --git a/llvm/test/tools/llvm-profdata/sample-remap.test b/llvm/test/tools/llvm-profdata/sample-remap.test
new file mode 100644
index 0000000..a6afd92
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/sample-remap.test
@@ -0,0 +1,2 @@
+; RUN: llvm-profdata merge -sample -text %S/Inputs/sample-remap.proftext -r %S/Inputs/sample-remap.remap -o %t.output
+; RUN: diff %S/Inputs/sample-remap.expected %t.output