[llvm] [LTO] Refactor LTO link optimization remarks handling using RAII (NFC) (PR #181269)

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 17 09:59:15 PST 2026


================
@@ -427,6 +427,21 @@ class LTO {
     LTOK_UnifiedThin,
   };
 
+  /// RAII guard for optimization remarks and LTO-link diagnostics.
+  struct DiagnosticSession {
----------------
teresajohnson wrote:

I spent some time looking into the history. I believe we want to continue to call finalizeOptimizationRemarks for now. Not only to keep this refactoring as NFC as possible, but also because it was added to deal with the fact that lld does not call global destructors during its normal shutdown (confirmed this is still the case today, see unsafeLldMain). The patch to add this handling that mentions this is https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20170213/429361.html.

Additionally, I believe it is safer to invoke finalize (via finalizeOptimizationRemarks) when we are done with LTO::run because it finalizes and deregisters the remarks streamer from the LLVMContext at a point  where it is still valid (LTO owns the LLVMContext which points to the remark streamer). In general this seems cleaner, and is consistent with current intent and behavior.

However, I was able to remove the new RAII object and accomplish this a different way. LTO::run already employs a llvm::scope_exit that invokes LTO::cleanup(). This is a virtual function which was currently empty for the base LTO object, but has an implementation for the new derived DTLTO class (which then invokes the base cleanup). I simply added a call to finalizeOptimizationRemarks in LTO::cleanup.

https://github.com/llvm/llvm-project/pull/181269


More information about the llvm-commits mailing list