https://github.com/nikic updated 
https://github.com/llvm/llvm-project/pull/229700

>From 83b8b7ed01a8eea678ffad5742f739737d90d88a Mon Sep 17 00:00:00 2001
From: Nikita Popov <[email protected]>
Date: Wed, 7 Oct 2026 10:12:12 +0200
Subject: [PATCH 1/2] [IRBuilder] Make IRBuilder inserter non-virtual

Currently IRBuilderBase stores a reference to a virtual Inserter.
Replace this with a single function_ref callback, which is
populated by a method on `IRBuilder<>`, which always has a known
Inserter.
---
 clang/lib/CodeGen/CGBuilder.h                 |  2 +-
 llvm/include/llvm/IR/IRBuilder.h              | 60 ++++++++++---------
 .../Transforms/InstCombine/InstCombiner.h     |  8 +--
 llvm/lib/IR/IRBuilder.cpp                     |  2 -
 .../InstCombine/InstructionCombining.cpp      |  3 -
 .../Instrumentation/BoundsChecking.cpp        |  2 +-
 llvm/lib/Transforms/Scalar/SROA.cpp           |  2 +-
 polly/include/polly/CodeGen/IRBuilder.h       |  2 +-
 8 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h
index 2dc70a9071e96..9c5e91660ad75 100644
--- a/clang/lib/CodeGen/CGBuilder.h
+++ b/clang/lib/CodeGen/CGBuilder.h
@@ -38,7 +38,7 @@ class CGBuilderInserter final : public 
llvm::IRBuilderDefaultInserter {
 
   /// This forwards to CodeGenFunction::InsertHelper.
   void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
-                    llvm::BasicBlock::iterator InsertPt) const override;
+                    llvm::BasicBlock::iterator InsertPt) const;
 
 private:
   CodeGenFunction *CGF = nullptr;
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index c370433331d5a..35b5f5c4133c3 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -59,12 +59,10 @@ class Use;
 /// IRBuilder and needs to be inserted.
 ///
 /// By default, this inserts the instruction at the insertion point.
-class LLVM_ABI IRBuilderDefaultInserter {
+class IRBuilderDefaultInserter {
 public:
-  virtual ~IRBuilderDefaultInserter();
-
-  virtual void InsertHelper(Instruction *I, const Twine &Name,
-                            BasicBlock::iterator InsertPt) const {
+  void InsertHelper(Instruction *I, const Twine &Name,
+                    BasicBlock::iterator InsertPt) const {
     if (InsertPt.isValid())
       I->insertInto(InsertPt.getNodeParent(), InsertPt);
     I->setName(Name);
@@ -73,17 +71,15 @@ class LLVM_ABI IRBuilderDefaultInserter {
 
 /// Provides an 'InsertHelper' that calls a user-provided callback after
 /// performing the default insertion.
-class LLVM_ABI IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
+class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
   std::function<void(Instruction *)> Callback;
 
 public:
-  ~IRBuilderCallbackInserter() override;
-
   IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
       : Callback(std::move(Callback)) {}
 
   void InsertHelper(Instruction *I, const Twine &Name,
-                    BasicBlock::iterator InsertPt) const override {
+                    BasicBlock::iterator InsertPt) const {
     IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
     Callback(I);
   }
@@ -118,13 +114,16 @@ class IRBuilderBase {
   DebugLoc StoredDL;
 
 protected:
+  using InsertFn = function_ref<void(const IRBuilderBase &, Instruction *,
+                                     const Twine &, BasicBlock::iterator)>;
+
   // TODO: Remove this in favor of InsertPt.getNodeParent(), so they cannot
   // go out of sync.
   BasicBlock *BB;
   BasicBlock::iterator InsertPt;
   LLVMContext &Context;
   const IRBuilderFolder &Folder;
-  const IRBuilderDefaultInserter &Inserter;
+  InsertFn InsertCB;
 
   MDNode *DefaultFPMathTag = nullptr;
   FastMathFlags FMF;
@@ -137,15 +136,15 @@ class IRBuilderBase {
 
 public:
   IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
-                const IRBuilderDefaultInserter &Inserter)
-      : Context(context), Folder(Folder), Inserter(Inserter) {
+                InsertFn InsertCB)
+      : Context(context), Folder(Folder), InsertCB(InsertCB) {
     ClearInsertionPoint();
   }
 
   /// Insert and return the specified instruction.
   template<typename InstTy>
   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
-    Inserter.InsertHelper(I, Name, InsertPt);
+    InsertCB(*this, I, Name, InsertPt);
     SetInstDebugLocation(I);
     return I;
   }
@@ -2910,75 +2909,82 @@ class IRBuilder : public IRBuilderBase {
   FolderTy Folder;
   InserterTy Inserter;
 
+  static void CallInsertHelper(const IRBuilderBase &IRB, Instruction *I,
+                               const Twine &Name,
+                               BasicBlock::iterator InsertPt) {
+    static_cast<const IRBuilder *>(&IRB)->Inserter.InsertHelper(I, Name,
+                                                                InsertPt);
+  }
+
 public:
   // TODO: Deprecate ctors accepting LLVMContext.
   IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter)
-      : IRBuilderBase(C, this->Folder, this->Inserter), Folder(Folder),
+      : IRBuilderBase(C, this->Folder, CallInsertHelper), Folder(Folder),
         Inserter(Inserter) {}
 
   IRBuilder(LLVMContext &C, FolderTy Folder)
-      : IRBuilderBase(C, this->Folder, this->Inserter), Folder(Folder) {}
+      : IRBuilderBase(C, this->Folder, CallInsertHelper), Folder(Folder) {}
 
   explicit IRBuilder(LLVMContext &C)
-      : IRBuilderBase(C, this->Folder, this->Inserter) {}
+      : IRBuilderBase(C, this->Folder, CallInsertHelper) {}
 
   IRBuilder(Module &M, FolderTy Folder, InserterTy Inserter)
-      : IRBuilderBase(M.getContext(), this->Folder, this->Inserter),
+      : IRBuilderBase(M.getContext(), this->Folder, CallInsertHelper),
         Folder(Folder), Inserter(Inserter) {}
 
   IRBuilder(Module &M, FolderTy Folder)
-      : IRBuilderBase(M.getContext(), this->Folder, this->Inserter),
+      : IRBuilderBase(M.getContext(), this->Folder, CallInsertHelper),
         Folder(Folder) {}
 
   explicit IRBuilder(Module &M)
-      : IRBuilderBase(M.getContext(), this->Folder, this->Inserter) {}
+      : IRBuilderBase(M.getContext(), this->Folder, CallInsertHelper) {}
 
   explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder)
-      : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter),
+      : IRBuilderBase(TheBB->getContext(), this->Folder, CallInsertHelper),
         Folder(Folder) {
     SetInsertPoint(TheBB);
   }
 
   explicit IRBuilder(BasicBlock *TheBB)
-      : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter) {
+      : IRBuilderBase(TheBB->getContext(), this->Folder, CallInsertHelper) {
     SetInsertPoint(TheBB);
   }
 
   explicit IRBuilder(Instruction *IP)
-      : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter) {
+      : IRBuilderBase(IP->getContext(), this->Folder, CallInsertHelper) {
     SetInsertPoint(IP);
   }
 
   [[deprecated("Use the overload without BasicBlock argument instead")]]
   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder)
-      : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter),
+      : IRBuilderBase(TheBB->getContext(), this->Folder, CallInsertHelper),
         Folder(Folder) {
     SetInsertPoint(IP);
   }
 
   [[deprecated("Use the overload without BasicBlock argument instead")]]
   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
-      : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter) {
+      : IRBuilderBase(TheBB->getContext(), this->Folder, CallInsertHelper) {
     SetInsertPoint(IP);
   }
 
   IRBuilder(BasicBlock::iterator IP, FolderTy Folder, InserterTy Inserter)
       : IRBuilderBase(IP.getNodeParent()->getContext(), this->Folder,
-                      this->Inserter),
+                      CallInsertHelper),
         Folder(Folder), Inserter(Inserter) {
     SetInsertPoint(IP);
   }
 
   IRBuilder(BasicBlock::iterator IP, FolderTy Folder)
       : IRBuilderBase(IP.getNodeParent()->getContext(), this->Folder,
-                      this->Inserter),
+                      CallInsertHelper),
         Folder(Folder) {
     SetInsertPoint(IP);
   }
 
   explicit IRBuilder(BasicBlock::iterator IP)
       : IRBuilderBase(IP.getNodeParent()->getContext(), this->Folder,
-                      this->Inserter) {
+                      CallInsertHelper) {
     SetInsertPoint(IP);
   }
 
diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h 
b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
index 42924a0648efc..64d1df6d5a7e2 100644
--- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
+++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
@@ -49,16 +49,14 @@ class TargetTransformInfo;
 class LLVM_LIBRARY_VISIBILITY InstCombiner {
   /// IRBuilder inserter that adds new instructions to the worklist and new
   /// assumptions to the AssumptionCache.
-  class LLVM_ABI IRBuilderInstCombineInserter final
-      : public IRBuilderDefaultInserter {
+  class IRBuilderInstCombineInserter final : public IRBuilderDefaultInserter {
     InstCombiner &IC;
 
   public:
-    ~IRBuilderInstCombineInserter() override;
     IRBuilderInstCombineInserter(InstCombiner &IC) : IC(IC) {}
 
-    void InsertHelper(Instruction *I, const Twine &Name,
-                      BasicBlock::iterator InsertPt) const override;
+    LLVM_ABI void InsertHelper(Instruction *I, const Twine &Name,
+                               BasicBlock::iterator InsertPt) const;
   };
 
   /// Only used to call target specific intrinsic combining.
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 07125506e6094..8b27f0dfd69fd 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -1431,8 +1431,6 @@ CallInst *IRBuilderBase::CreateNonnullAssumption(Value 
*PtrValue) {
   return CreateAssumption(OperandBundleDef("nonnull", PtrValue));
 }
 
-IRBuilderDefaultInserter::~IRBuilderDefaultInserter() = default;
-IRBuilderCallbackInserter::~IRBuilderCallbackInserter() = default;
 IRBuilderFolder::~IRBuilderFolder() = default;
 void ConstantFolder::anchor() {}
 void NoFolder::anchor() {}
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp 
b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 265310ea0ff41..8d299bcbc4369 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -133,9 +133,6 @@ STATISTIC(NumReassoc  , "Number of reassociations");
 DEBUG_COUNTER(VisitCounter, "instcombine-visit",
               "Controls which instructions are visited");
 
-InstCombiner::IRBuilderInstCombineInserter::~IRBuilderInstCombineInserter() =
-    default;
-
 void InstCombiner::IRBuilderInstCombineInserter::InsertHelper(
     Instruction *I, const Twine &Name, BasicBlock::iterator InsertPt) const {
   IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
diff --git a/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp 
b/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
index 66e890ee62dbd..4dd385e74014d 100644
--- a/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
+++ b/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
@@ -48,7 +48,7 @@ class NoSanitizeInserter final : public 
IRBuilderDefaultInserter {
   NoSanitizeInserter() = default;
 
   void InsertHelper(Instruction *I, const Twine &Name,
-                    BasicBlock::iterator InsertPt) const override {
+                    BasicBlock::iterator InsertPt) const {
     IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
     if (!NoSanitizeMD)
       NoSanitizeMD = MDNode::get(I->getContext(), {});
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp 
b/llvm/lib/Transforms/Scalar/SROA.cpp
index e4c8c3029fe05..fa3ac618b7108 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -514,7 +514,7 @@ class IRBuilderPrefixedInserter final : public 
IRBuilderDefaultInserter {
   void SetNamePrefix(const Twine &P) { Prefix = P.str(); }
 
   void InsertHelper(Instruction *I, const Twine &Name,
-                    BasicBlock::iterator InsertPt) const override {
+                    BasicBlock::iterator InsertPt) const {
     IRBuilderDefaultInserter::InsertHelper(I, getNameWithPrefix(Name),
                                            InsertPt);
   }
diff --git a/polly/include/polly/CodeGen/IRBuilder.h 
b/polly/include/polly/CodeGen/IRBuilder.h
index aa112ba760b1f..9993ac6bdb48f 100644
--- a/polly/include/polly/CodeGen/IRBuilder.h
+++ b/polly/include/polly/CodeGen/IRBuilder.h
@@ -126,7 +126,7 @@ class IRInserter final : public 
llvm::IRBuilderDefaultInserter {
   IRInserter(ScopAnnotator &A) : Annotator(&A) {}
 
   void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
-                    llvm::BasicBlock::iterator InsertPt) const override {
+                    llvm::BasicBlock::iterator InsertPt) const {
     llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
     if (Annotator)
       Annotator->annotate(I);

>From 500c0e0ae83f411658e6ab6dda6453ddbe5f0d63 Mon Sep 17 00:00:00 2001
From: Nikita Popov <[email protected]>
Date: Wed, 7 Oct 2026 14:31:35 +0200
Subject: [PATCH 2/2] Use fn pointer, make ctor protected, delete copy ctor

---
 llvm/include/llvm/IR/IRBuilder.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 35b5f5c4133c3..a89be217d2d9f 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -114,8 +114,8 @@ class IRBuilderBase {
   DebugLoc StoredDL;
 
 protected:
-  using InsertFn = function_ref<void(const IRBuilderBase &, Instruction *,
-                                     const Twine &, BasicBlock::iterator)>;
+  using InsertFn = void (*)(const IRBuilderBase &, Instruction *, const Twine 
&,
+                            BasicBlock::iterator);
 
   // TODO: Remove this in favor of InsertPt.getNodeParent(), so they cannot
   // go out of sync.
@@ -134,13 +134,17 @@ class IRBuilderBase {
 
   ArrayRef<OperandBundleDef> DefaultOperandBundles;
 
-public:
   IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
                 InsertFn InsertCB)
       : Context(context), Folder(Folder), InsertCB(InsertCB) {
     ClearInsertionPoint();
   }
 
+public:
+  // IRBuilderBase contains references to IRBuilder members and is not safe
+  // to copy.
+  IRBuilderBase(const IRBuilderBase &) = delete;
+
   /// Insert and return the specified instruction.
   template<typename InstTy>
   InstTy *Insert(InstTy *I, const Twine &Name = "") const {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to