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

Snehasish Kumar via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 01:20:55 PST 2026


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



It looks like `LLVMRemarkFileHandle` (defined in [`llvm/include/llvm/IR/LLVMRemarkStreamer.h`](https://github.com/llvm/llvm-project/blob/eb30b5cf0b38828090f61f00adb4a7144ddc4231/llvm/include/llvm/IR/LLVMRemarkStreamer.h#L91-L135)) is already an RAII guard that finalizes optimization remarks on destruction. Its inner `Finalizer` struct [calls `finalize()` in its destructor](https://github.com/llvm/llvm-project/blob/eb30b5cf0b38828090f61f00adb4a7144ddc4231/llvm/include/llvm/IR/LLVMRemarkStreamer.h#L103), which [deregisters the remark streamer from the `LLVMContext`](https://github.com/llvm/llvm-project/blob/eb30b5cf0b38828090f61f00adb4a7144ddc4231/llvm/lib/IR/LLVMRemarkStreamer.cpp#L110-L117).

Since `LTO` already holds a `LLVMRemarkFileHandle DiagnosticOutputFile` member, its RAII destruction should already handle finalization. The explicit `finalizeOptimizationRemarks(std::move(Parent.DiagnosticOutputFile))` call in `DiagnosticSession::~DiagnosticSession()` appears redundant with the cleanup that `LLVMRemarkFileHandle` already provides.

Could we avoid the new `DiagnosticSession` struct and instead rely on the existing `LLVMRemarkFileHandle` RAII semantics? The setup could be a helper method on `LTO` that calls `setupLLVMOptimizationRemarks()` and `keep()`.

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


More information about the llvm-commits mailing list