From f525ac8be200c118a75579cbf5ad74ee3747ce83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= Date: Thu, 8 Jan 2026 11:45:42 +0100 Subject: [PATCH] [NFC][SPIRV] Avoid temporary SmallVector by using make_early_inc_range (#174748) Follow-up of https://github.com/llvm/llvm-project/pull/174734 Merge the 2 loops and avoid the temporary `SmallVector` by using `make_early_inc_range`. --- .../Target/SPIRV/SPIRVPushConstantAccess.cpp | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/llvm/lib/Target/SPIRV/SPIRVPushConstantAccess.cpp b/llvm/lib/Target/SPIRV/SPIRVPushConstantAccess.cpp index 87a66dc4bb10..1ce476cef668 100644 --- a/llvm/lib/Target/SPIRV/SPIRVPushConstantAccess.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVPushConstantAccess.cpp @@ -24,45 +24,42 @@ #include "llvm/IR/IRBuilder.h" #include "llvm/IR/IntrinsicsSPIRV.h" #include "llvm/IR/Module.h" -#include "llvm/IR/ReplaceConstant.h" #define DEBUG_TYPE "spirv-pushconstant-access" using namespace llvm; static bool replacePushConstantAccesses(Module &M, SPIRVGlobalRegistry *GR) { - SmallVector PushConstants; - for (GlobalVariable &GV : M.globals()) { + bool Changed = false; + for (GlobalVariable &GV : make_early_inc_range(M.globals())) { if (GV.getAddressSpace() != storageClassToAddressSpace(SPIRV::StorageClass::PushConstant)) continue; GV.removeDeadConstantUsers(); - PushConstants.push_back(&GV); - } - for (GlobalVariable *GV : PushConstants) { Type *PCType = llvm::TargetExtType::get( - M.getContext(), "spirv.PushConstant", {GV->getValueType()}); - GlobalVariable *NewGV = new GlobalVariable( - M, PCType, GV->isConstant(), GV->getLinkage(), - /* initializer= */ nullptr, GV->getName(), - /* InsertBefore= */ GV, GV->getThreadLocalMode(), GV->getAddressSpace(), - GV->isExternallyInitialized()); + M.getContext(), "spirv.PushConstant", {GV.getValueType()}); + GlobalVariable *NewGV = + new GlobalVariable(M, PCType, GV.isConstant(), GV.getLinkage(), + /* initializer= */ nullptr, GV.getName(), + /* InsertBefore= */ &GV, GV.getThreadLocalMode(), + GV.getAddressSpace(), GV.isExternallyInitialized()); - for (User *U : make_early_inc_range(GV->users())) { + for (User *U : make_early_inc_range(GV.users())) { Instruction *I = cast(U); IRBuilder<> Builder(I); Value *GetPointerCall = Builder.CreateIntrinsic( NewGV->getType(), Intrinsic::spv_pushconstant_getpointer, {NewGV}); - GR->buildAssignPtr(Builder, GV->getValueType(), GetPointerCall); + GR->buildAssignPtr(Builder, GV.getValueType(), GetPointerCall); - I->replaceUsesOfWith(GV, GetPointerCall); + I->replaceUsesOfWith(&GV, GetPointerCall); } - GV->eraseFromParent(); + GV.eraseFromParent(); + Changed = true; } - return !PushConstants.empty(); + return Changed; } PreservedAnalyses SPIRVPushConstantAccess::run(Module &M,