[PATCH] D118550: [Support][nfc] Add RAII helper for cleaning up time profiler in thread

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 29 20:51:30 PST 2022


int3 created this revision.
int3 added reviewers: lld-macho, anton-afanasyev, russell.gallop.
Herald added subscribers: ormris, dexonsmith, steven_wu, hiraditya.
Herald added a project: lld-macho.
int3 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118550

Files:
  lld/MachO/Writer.cpp
  llvm/include/llvm/Support/TimeProfiler.h
  llvm/lib/LTO/LTO.cpp


Index: llvm/lib/LTO/LTO.cpp
===================================================================
--- llvm/lib/LTO/LTO.cpp
+++ llvm/lib/LTO/LTO.cpp
@@ -1267,9 +1267,9 @@
                 &ResolvedODR,
             const GVSummaryMapTy &DefinedGlobals,
             MapVector<StringRef, BitcodeModule> &ModuleMap) {
-          if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
-            timeTraceProfilerInitialize(Conf.TimeTraceGranularity,
-                                        "thin backend");
+          TimeTraceProfilerThreadScope profilerScope(
+              LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled,
+              Conf.TimeTraceGranularity, "thin backend");
           Error E = runThinLTOBackendThread(
               AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
               ResolvedODR, DefinedGlobals, ModuleMap);
@@ -1280,8 +1280,6 @@
             else
               Err = std::move(E);
           }
-          if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
-            timeTraceProfilerFinishThread();
         },
         BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
         std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
Index: llvm/include/llvm/Support/TimeProfiler.h
===================================================================
--- llvm/include/llvm/Support/TimeProfiler.h
+++ llvm/include/llvm/Support/TimeProfiler.h
@@ -31,6 +31,32 @@
 /// Finish a time trace profiler running on a worker thread.
 void timeTraceProfilerFinishThread();
 
+/// Initialize and clean up the profiler on a worker thread using RAII.
+struct TimeTraceProfilerThreadScope {
+  TimeTraceProfilerThreadScope() = delete;
+  TimeTraceProfilerThreadScope(const TimeTraceProfilerThreadScope &) = delete;
+  TimeTraceProfilerThreadScope &
+  operator=(const TimeTraceProfilerThreadScope &) = delete;
+  TimeTraceProfilerThreadScope(TimeTraceProfilerThreadScope &&) = delete;
+  TimeTraceProfilerThreadScope &
+  operator=(TimeTraceProfilerThreadScope &&) = delete;
+
+  TimeTraceProfilerThreadScope(bool Enable, unsigned TimeTraceGranularity,
+                               StringRef ProcName)
+      : Enable(Enable) {
+    if (Enable)
+      timeTraceProfilerInitialize(TimeTraceGranularity, ProcName);
+  }
+
+  ~TimeTraceProfilerThreadScope() {
+    if (Enable)
+      timeTraceProfilerFinishThread();
+  }
+
+private:
+  bool Enable;
+};
+
 /// Is the time trace profiler enabled, i.e. initialized?
 inline bool timeTraceProfilerEnabled() {
   return getTimeTraceProfilerInstance() != nullptr;
Index: lld/MachO/Writer.cpp
===================================================================
--- lld/MachO/Writer.cpp
+++ lld/MachO/Writer.cpp
@@ -1130,11 +1130,10 @@
   createLoadCommands<LP>();
   finalizeAddresses();
   threadPool.async([&] {
-    if (LLVM_ENABLE_THREADS && config->timeTraceEnabled)
-      timeTraceProfilerInitialize(config->timeTraceGranularity, "writeMapFile");
+    TimeTraceProfilerThreadScope profilerScope(
+        LLVM_ENABLE_THREADS && config->timeTraceEnabled,
+        config->timeTraceGranularity, "writeMapFile");
     writeMapFile();
-    if (LLVM_ENABLE_THREADS && config->timeTraceEnabled)
-      timeTraceProfilerFinishThread();
   });
   finalizeLinkEditSegment();
   writeOutputFile();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118550.404327.patch
Type: text/x-patch
Size: 3317 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220130/4e8ad7ab/attachment.bin>


More information about the llvm-commits mailing list