[PATCH] Tolerate unmangled names in sample profiles.

Diego Novillo dnovillo at google.com
Fri Mar 14 14:37:16 PDT 2014


Hi chandlerc,

The compiler does not always generate linkage names. If a function
has been inlined and its body elided, its linkage name may not be
generated.

When the binary executes, the profiler will use its unmangled name
when attributing samples. This results in unmangled names in the
input profile.

We are currently failing hard when this happens. However, in this case
all that happens is that we fail to attribute samples to the inlined
function. While this means fewer optimization opportunities, it should
not cause a compilation failure.

This patch trades the current error for a warning.

http://llvm-reviews.chandlerc.com/D3087

Files:
  lib/Transforms/Scalar/SampleProfile.cpp
  test/Transforms/SampleProfile/Inputs/bad_fn_header.prof
  test/Transforms/SampleProfile/Inputs/bad_mangle.prof
  test/Transforms/SampleProfile/syntax.ll

Index: lib/Transforms/Scalar/SampleProfile.cpp
===================================================================
--- lib/Transforms/Scalar/SampleProfile.cpp
+++ lib/Transforms/Scalar/SampleProfile.cpp
@@ -251,12 +251,19 @@
     return Profiles[F.getName()];
   }
 
-  /// \brief Report a parse error message and stop compilation.
+  /// \brief Report a parse error message.
   void reportParseError(int64_t LineNumber, Twine Msg) const {
     DiagnosticInfoSampleProfile Diag(Filename.data(), LineNumber, Msg);
     M.getContext().diagnose(Diag);
   }
 
+  /// \brief Report a parse warning message.
+  void reportParseWarning(int64_t LineNumber, Twine Msg) const {
+    DiagnosticInfoSampleProfile Diag(Filename.data(), LineNumber, Msg,
+                                     DS_Warning);
+    M.getContext().diagnose(Diag);
+  }
+
 protected:
   /// \brief Map every function to its associated profile.
   ///
@@ -463,7 +470,7 @@
   // Read the profile of each function. Since each function may be
   // mentioned more than once, and we are collecting flat profiles,
   // accumulate samples as we parse them.
-  Regex HeadRE("^([^:]+):([0-9]+):([0-9]+)$");
+  Regex HeadRE("^([^0-9].*):([0-9]+):([0-9]+)$");
   Regex LineSample("^([0-9]+)\\.?([0-9]+)?: ([0-9]+)(.*)$");
   while (!LineIt.is_at_eof()) {
     // Read the header of each function. The function header should
@@ -480,6 +487,27 @@
     }
     assert(Matches.size() == 4);
     StringRef FName = Matches[1];
+
+    // Do some light validation on the function name to detect some cases
+    // where we do not have a valid mangled function name. This happens
+    // when functions are inlined and their bodies elided. In this case
+    // the compiler may decide not to emit a linkage name for the function.
+    // The profiler will then use the umangled name of the function.
+    //
+    // FIXME: this does not detect all cases. It only works with unmangled
+    // names that contain "invalid" characters (spaces, ':', '<', etc).
+    if (FName.substr(0, 2) != "_Z") {
+      // Only check names that do not start with _Z. Anything starting
+      // with _Z should be a properly mangled name.
+      Regex InvalidChars("[:,<>-]");
+      if (InvalidChars.match(FName))
+        reportParseWarning(LineIt.line_number(),
+                           "Function '" + FName +
+                               "' is not properly mangled. No "
+                               "samples will be collected "
+                               "for it");
+    }
+
     unsigned NumSamples, NumHeadSamples;
     Matches[2].getAsInteger(10, NumSamples);
     Matches[3].getAsInteger(10, NumHeadSamples);
Index: test/Transforms/SampleProfile/Inputs/bad_fn_header.prof
===================================================================
--- test/Transforms/SampleProfile/Inputs/bad_fn_header.prof
+++ test/Transforms/SampleProfile/Inputs/bad_fn_header.prof
@@ -1,3 +1,3 @@
-empty:100:BAD
+3empty:100:BAD
 0: 0
 1: 100
Index: test/Transforms/SampleProfile/Inputs/bad_mangle.prof
===================================================================
--- /dev/null
+++ test/Transforms/SampleProfile/Inputs/bad_mangle.prof
@@ -0,0 +1,3 @@
+double convert<std::string, float>(float):2909472:181842
+0: 181842
+1: 181842
Index: test/Transforms/SampleProfile/syntax.ll
===================================================================
--- test/Transforms/SampleProfile/syntax.ll
+++ test/Transforms/SampleProfile/syntax.ll
@@ -5,15 +5,17 @@
 ; RUN: not opt < %s -sample-profile -sample-profile-file=%S/Inputs/bad_line_values.prof 2>&1 | FileCheck -check-prefix=BAD-LINE-VALUES %s
 ; RUN: not opt < %s -sample-profile -sample-profile-file=%S/Inputs/bad_discriminator_value.prof 2>&1 | FileCheck -check-prefix=BAD-DISCRIMINATOR-VALUE %s
 ; RUN: not opt < %s -sample-profile -sample-profile-file=%S/Inputs/bad_samples.prof 2>&1 | FileCheck -check-prefix=BAD-SAMPLES %s
+; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/bad_mangle.prof 2>&1 | FileCheck -check-prefix=BAD-MANGLE %s
 
 define void @empty() {
 entry:
   ret void
 }
 ; NO-DEBUG: error: No debug information found in function empty
 ; MISSING-FILE: error: missing.prof: No such file or directory
-; BAD-FN-HEADER: error: {{.*}}bad_fn_header.prof:1: Expected 'mangled_name:NUM:NUM', found empty:100:BAD
+; BAD-FN-HEADER: error: {{.*}}bad_fn_header.prof:1: Expected 'mangled_name:NUM:NUM', found 3empty:100:BAD
 ; BAD-SAMPLE-LINE: error: {{.*}}bad_sample_line.prof:3: Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found 1: BAD
 ; BAD-LINE-VALUES: error: {{.*}}bad_line_values.prof:2: Expected 'mangled_name:NUM:NUM', found -1: 10
 ; BAD-DISCRIMINATOR-VALUE: error: {{.*}}bad_discriminator_value.prof:2: Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found 1.-3: 10
 ; BAD-SAMPLES: error: {{.*}}bad_samples.prof:2: Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found 1.3: -10
+; BAD-MANGLE: warning: {{.*}}bad_mangle.prof:1: Function 'double convert<std::string, float>(float)' is not {{.*}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3087.1.patch
Type: text/x-patch
Size: 5008 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140314/9494798b/attachment.bin>


More information about the llvm-commits mailing list