[llvm] [llvm][Instrumentor] Fix memory leaks (PR #196317)

Kevin Sala Penades via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 06:56:55 PDT 2026


https://github.com/kevinsala updated https://github.com/llvm/llvm-project/pull/196317

>From 65ad7a448e2d203cc7b6d2171aef7b95602f55a2 Mon Sep 17 00:00:00 2001
From: Kevin Sala <salapenades1 at llnl.gov>
Date: Wed, 6 May 2026 20:45:45 -0700
Subject: [PATCH] [llvm][Instrumentor] Fix memory leaks

Should fix memory leaks introduced in #138958
---
 .../llvm/Transforms/IPO/Instrumentor.h        | 23 +++++++++----------
 .../llvm/Transforms/IPO/InstrumentorUtils.h   |  6 +++++
 llvm/lib/Transforms/IPO/Instrumentor.cpp      | 20 +++++++++-------
 3 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/Transforms/IPO/Instrumentor.h b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
index c115bea24bb27..5ddfd3dd4466c 100644
--- a/llvm/include/llvm/Transforms/IPO/Instrumentor.h
+++ b/llvm/include/llvm/Transforms/IPO/Instrumentor.h
@@ -34,6 +34,7 @@
 
 #include <cstdint>
 #include <functional>
+#include <memory>
 #include <string>
 #include <tuple>
 
@@ -273,17 +274,15 @@ struct BaseConfigurationOption {
 
   /// Create a boolean option with \p Name name, \p Description description and
   /// \p DefaultValue as boolean default value.
-  static BaseConfigurationOption *createBoolOption(InstrumentationConfig &IC,
-                                                   StringRef Name,
-                                                   StringRef Description,
-                                                   bool DefaultValue);
+  static std::unique_ptr<BaseConfigurationOption>
+  createBoolOption(InstrumentationConfig &IC, StringRef Name,
+                   StringRef Description, bool DefaultValue);
 
   /// Create a string option with \p Name name, \p Description description and
   /// \p DefaultValue as string default value.
-  static BaseConfigurationOption *createStringOption(InstrumentationConfig &IC,
-                                                     StringRef Name,
-                                                     StringRef Description,
-                                                     StringRef DefaultValue);
+  static std::unique_ptr<BaseConfigurationOption>
+  createStringOption(InstrumentationConfig &IC, StringRef Name,
+                     StringRef Description, StringRef DefaultValue);
 
   /// Helper union that holds any possible option type.
   union ValueTy {
@@ -384,10 +383,10 @@ struct InstrumentationConfig {
   SmallVector<BaseConfigurationOption *> BaseConfigurationOptions;
 
   /// The base configuration options.
-  BaseConfigurationOption *RuntimePrefix;
-  BaseConfigurationOption *TargetRegex;
-  BaseConfigurationOption *HostEnabled;
-  BaseConfigurationOption *GPUEnabled;
+  std::unique_ptr<BaseConfigurationOption> RuntimePrefix;
+  std::unique_ptr<BaseConfigurationOption> TargetRegex;
+  std::unique_ptr<BaseConfigurationOption> HostEnabled;
+  std::unique_ptr<BaseConfigurationOption> GPUEnabled;
 
   /// The map registered instrumentation opportunities. The map is indexed by
   /// the instrumentation location kind and then by the opportunity name. Notice
diff --git a/llvm/include/llvm/Transforms/IPO/InstrumentorUtils.h b/llvm/include/llvm/Transforms/IPO/InstrumentorUtils.h
index 220ec4e61613a..4d7fd55ae825b 100644
--- a/llvm/include/llvm/Transforms/IPO/InstrumentorUtils.h
+++ b/llvm/include/llvm/Transforms/IPO/InstrumentorUtils.h
@@ -48,6 +48,12 @@ struct InstrumentorIRBuilderTy {
         I->replaceAllUsesWith(PoisonValue::get(I->getType()));
       I->eraseFromParent();
     }
+
+    // Delete the alloca lists that may have been allocated.
+    for (auto &KV : AllocaMap) {
+      if (KV.second)
+        delete KV.second;
+    }
   }
 
   /// Get a temporary alloca to communicate (large) values with the runtime.
diff --git a/llvm/lib/Transforms/IPO/Instrumentor.cpp b/llvm/lib/Transforms/IPO/Instrumentor.cpp
index cb10a8986a99f..cacf361aca85e 100644
--- a/llvm/lib/Transforms/IPO/Instrumentor.cpp
+++ b/llvm/lib/Transforms/IPO/Instrumentor.cpp
@@ -287,22 +287,26 @@ PreservedAnalyses InstrumentorPass::run(Module &M, ModuleAnalysisManager &MAM) {
   return PA;
 }
 
-BaseConfigurationOption *
+std::unique_ptr<BaseConfigurationOption>
 BaseConfigurationOption::createBoolOption(InstrumentationConfig &IConf,
                                           StringRef Name, StringRef Description,
                                           bool DefaultValue) {
-  auto *BCO = new BaseConfigurationOption(Name, Description, BOOLEAN);
+  auto BCO =
+      std::make_unique<BaseConfigurationOption>(Name, Description, BOOLEAN);
   BCO->setBool(DefaultValue);
-  IConf.addBaseChoice(BCO);
+  IConf.addBaseChoice(BCO.get());
   return BCO;
 }
 
-BaseConfigurationOption *BaseConfigurationOption::createStringOption(
-    InstrumentationConfig &IConf, StringRef Name, StringRef Description,
-    StringRef DefaultValue) {
-  auto *BCO = new BaseConfigurationOption(Name, Description, STRING);
+std::unique_ptr<BaseConfigurationOption>
+BaseConfigurationOption::createStringOption(InstrumentationConfig &IConf,
+                                            StringRef Name,
+                                            StringRef Description,
+                                            StringRef DefaultValue) {
+  auto BCO =
+      std::make_unique<BaseConfigurationOption>(Name, Description, STRING);
   BCO->setString(DefaultValue);
-  IConf.addBaseChoice(BCO);
+  IConf.addBaseChoice(BCO.get());
   return BCO;
 }
 



More information about the llvm-commits mailing list