https://github.com/nikic created https://github.com/llvm/llvm-project/pull/229700
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. This is a small compile-time improvement: https://llvm-compile-time-tracker.com/compare.php?from=bd1c9e3efed65cfcc137a183450e56dac3eb9ff4&to=781b5b5e65df02281fceb1d1c3540197159602ea&stat=instructions:u >From 781b5b5e65df02281fceb1d1c3540197159602ea Mon Sep 17 00:00:00 2001 From: Nikita Popov <[email protected]> Date: Wed, 7 Oct 2026 10:12:12 +0200 Subject: [PATCH] [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 | 54 ++++++++++--------- .../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, 37 insertions(+), 38 deletions(-) diff --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h index 18e738f10ba3b7..9a3aebf34919ee 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 bbeefa8ef90c02..f268f7f29442b0 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -58,12 +58,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); @@ -72,17 +70,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); } @@ -117,13 +113,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; @@ -136,15 +135,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; } @@ -2909,63 +2908,70 @@ 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: 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) {} 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 ce9d42c58e3f25..9ff645dfd7dbb0 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 07125506e6094a..8b27f0dfd69fda 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 265310ea0ff41f..8d299bcbc43690 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 3aed97bf8363c1..754010e600235a 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 599f9f42f9ca8f..1022cfec22d364 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 aa112ba760b1f7..9993ac6bdb48fc 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); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
