gollvm: annotate fcns with attribute for AutoFDO

When AutoFDO is in effect, annotate functions with a special attribute
to tell the LLVM backend to handle symbol names in a Go-compatible way
(in particular, to not chop off everything after the first "." in the
symbol name when doing profile lookups).

This change depends on the upstream LLVM change at

https://reviews.llvm.org/D58832

Change-Id: If5aedd20040b14fb0dafb6657f3f86c5ad2cb918
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/164639
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/bridge/go-llvm.cpp b/bridge/go-llvm.cpp
index 79071cc..53bccbe 100644
--- a/bridge/go-llvm.cpp
+++ b/bridge/go-llvm.cpp
@@ -67,6 +67,7 @@
     , personalityFunction_(nullptr)
     , dummyPersonalityFunction_(nullptr)
     , gcStrategy_("")
+    , autoFDO_(false)
 {
   // If nobody passed in a linemap, create one for internal use (unit testing)
   if (!linemap_) {
@@ -2625,6 +2626,10 @@
     // allow elim frame pointer or not
     fcn->addFnAttr("no-frame-pointer-elim", noFpElim_ ? "true" : "false");
 
+    // set suffix elision policy if autoFDO in effect
+    if (autoFDO_)
+      fcn->addFnAttr("sample-profile-suffix-elision-policy", "selected");
+
     // no-return
     if ((flags & Backend::function_does_not_return) != 0)
       fcn->addFnAttr(llvm::Attribute::NoReturn);
diff --git a/bridge/go-llvm.h b/bridge/go-llvm.h
index 382934c..9a61c92 100644
--- a/bridge/go-llvm.h
+++ b/bridge/go-llvm.h
@@ -408,6 +408,9 @@
   // Dummy personality function
   llvm::Function *dummyPersonalityFunction();
 
+  // Inform bridge that we may be using AutoFDO.
+  void setEnableAutoFDO() { autoFDO_ = true; }
+
  private:
   Bexpression *errorExpression() const { return errorExpression_; }
   Bstatement *errorStatement() const { return errorStatement_; }
@@ -849,6 +852,9 @@
 
   // GC strategy
   std::string gcStrategy_;
+
+  // Prepare for use of AutoFDO.
+  bool autoFDO_;
 };
 
 #endif
diff --git a/driver/CompileGo.cpp b/driver/CompileGo.cpp
index fcad320..12b5333 100644
--- a/driver/CompileGo.cpp
+++ b/driver/CompileGo.cpp
@@ -612,6 +612,8 @@
   bridge_->setNoInline(args_.hasArg(gollvm::options::OPT_fno_inline));
   bridge_->setTargetCpuAttr(targetCpuAttr_);
   bridge_->setTargetFeaturesAttr(targetFeaturesAttr_);
+  if (!sampleProfileFile_.empty())
+    bridge_->setEnableAutoFDO();
 
   // -f[no-]omit-frame-pointer
   bool omitFp =