[pkg-d-commits] [ldc] 155/211: Add PGO data to the ThinLTO module summary (LLVM >= 4.0). (#1892)

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:36:19 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.1.0
in repository ldc.

commit c1c1fcb0272206469b0829e47eae5919a94ebc28
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Wed Nov 23 12:53:55 2016 +0100

    Add PGO data to the ThinLTO module summary (LLVM >= 4.0). (#1892)
---
 driver/toobj.cpp            | 20 +++++++++++++++-----
 tests/linking/thinlto_pgo.d | 26 ++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/driver/toobj.cpp b/driver/toobj.cpp
index b462b34..9a4effc 100644
--- a/driver/toobj.cpp
+++ b/driver/toobj.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
 #endif
 #if LDC_LLVM_VER >= 400
+#include "llvm/Analysis/ProfileSummaryInfo.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #else
 #include "llvm/Bitcode/ReaderWriter.h"
@@ -471,13 +472,22 @@ void writeModule(llvm::Module *m, const char *filename) {
 #if LDC_LLVM_VER >= 309
       Logger::println("Creating module summary for ThinLTO");
 #if LDC_LLVM_VER == 309
-      // TODO: add PGO data in here when available (function freq info).
-      llvm::ModuleSummaryIndexBuilder indexBuilder(m, nullptr);
+      // When the function freq info callback is set to nullptr, LLVM will
+      // calculate it automatically for us.
+      llvm::ModuleSummaryIndexBuilder indexBuilder(
+          m, /* function freq callback */ nullptr);
       auto &moduleSummaryIndex = indexBuilder.getIndex();
 #else
-      // TODO: add PGO data in here when available (function freq info and
-      // profile summary info).
-      auto moduleSummaryIndex = buildModuleSummaryIndex(*m, nullptr, nullptr);
+      llvm::ProfileSummaryInfo PSI(*m);
+      // Set PSIptr to nullptr when there is no PGO information available, such
+      // that LLVM will not try to find PGO information for each function inside
+      // `buildModuleSummaryIndex`. (micro-optimization)
+      auto PSIptr = m->getProfileSummary() ? &PSI : nullptr;
+
+      // When the function freq info callback is set to nullptr, LLVM will
+      // calculate it automatically for us.
+      auto moduleSummaryIndex = buildModuleSummaryIndex(
+          *m, /* function freq callback */ nullptr, PSIptr);
 #endif
 
       llvm::WriteBitcodeToFile(m, bos, true, &moduleSummaryIndex,
diff --git a/tests/linking/thinlto_pgo.d b/tests/linking/thinlto_pgo.d
new file mode 100644
index 0000000..679a687
--- /dev/null
+++ b/tests/linking/thinlto_pgo.d
@@ -0,0 +1,26 @@
+// Test execution path for ThinLTO when PGO data is available.
+// I manually verified that PGO data is added to the ThinLTO module summary, but do not know how to automatically test this reliably.
+
+// REQUIRES: atleast_llvm400
+// REQUIRES: LTO
+
+// RUN: %ldc -fprofile-instr-generate=%t.profraw -run %s  \
+// RUN:   &&  %profdata merge %t.profraw -o %t.profdata \
+// RUN:   &&  %ldc -c -flto=thin -of=%t2%obj -fprofile-instr-use=%t.profdata %s
+
+void coldfunction()
+{
+}
+
+void hotfunction()
+{
+}
+
+void main()
+{
+    coldfunction();
+    foreach (i; 0 .. 1000)
+    {
+        hotfunction();
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list