https://github.com/paulwalker-arm updated https://github.com/llvm/llvm-project/pull/142803
>From 0819cda292ccc0fa7376bb4c78d4884cf5845410 Mon Sep 17 00:00:00 2001 From: Paul Walker <paul.wal...@arm.com> Date: Wed, 4 Jun 2025 13:06:31 +0100 Subject: [PATCH 1/2] [NFC][LLVM] Refactor IRBuilder::Create{VScale,ElementCount,TypeSize}. CreateVScale took a scaling parameter that had a single use outside of IRBuilder with all other callers having to create a redundant ConstantInt. To work round this some code perferred to use CreateIntrinsic directly. This patch simplifies CreateVScale to only return a call to the llvm.vscale() intrinsic and nothing more. As well as simplifying the existing call sites I've also ported the uses of CreateIntrinsic. Whilst IRBuilder used CreateVScale's scaling parameter as part of the implementations of CreateElementCount and CreateTypeSize, I have follow-on work to switch them to the NUW varaiety and thus they would stop using CreateVScale's scaling as well. To prepare for this I have moved the multiplication and constant folding into the implementations of CreateElementCount and CreateTypeSize. As a final step I have replaced some callers of CreateVScale with CreateElementCount where it's clear from the code they wanted the latter. --- clang/lib/CodeGen/TargetBuiltins/ARM.cpp | 6 +--- llvm/include/llvm/IR/IRBuilder.h | 9 +++-- llvm/lib/CodeGen/ExpandVectorPredication.cpp | 3 +- llvm/lib/IR/IRBuilder.cpp | 33 +++++++++++-------- .../AArch64/AArch64TargetTransformInfo.cpp | 8 ++--- .../InstCombine/InstCombineCasts.cpp | 24 +++++--------- .../Utils/LowerVectorIntrinsics.cpp | 16 ++------- .../Utils/ScalarEvolutionExpander.cpp | 2 +- .../Vectorize/LoopIdiomVectorize.cpp | 2 +- llvm/unittests/IR/IRBuilderTest.cpp | 8 ----- 10 files changed, 43 insertions(+), 68 deletions(-) diff --git a/clang/lib/CodeGen/TargetBuiltins/ARM.cpp b/clang/lib/CodeGen/TargetBuiltins/ARM.cpp index 1cf8f6819b75a..9c77346389e04 100644 --- a/clang/lib/CodeGen/TargetBuiltins/ARM.cpp +++ b/clang/lib/CodeGen/TargetBuiltins/ARM.cpp @@ -4793,11 +4793,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, case SVE::BI__builtin_sve_svlen_u64: { SVETypeFlags TF(Builtin->TypeModifier); auto VTy = cast<llvm::VectorType>(getSVEType(TF)); - auto *NumEls = - llvm::ConstantInt::get(Ty, VTy->getElementCount().getKnownMinValue()); - - Function *F = CGM.getIntrinsic(Intrinsic::vscale, Ty); - return Builder.CreateMul(NumEls, Builder.CreateCall(F)); + return Builder.CreateElementCount(Ty, VTy->getElementCount()); } case SVE::BI__builtin_sve_svtbl2_u8: diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index 0db5179c7a3e4..8ed10cb803a9c 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -945,17 +945,16 @@ class IRBuilderBase { LLVM_ABI CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = ""); - /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale - /// will be the same type as that of \p Scaling. - LLVM_ABI Value *CreateVScale(Constant *Scaling, const Twine &Name = ""); + /// Create a call to llvm.vscale.<Ty>(). + LLVM_ABI Value *CreateVScale(Type *Ty, const Twine &Name = ""); /// Create an expression which evaluates to the number of elements in \p EC /// at runtime. - LLVM_ABI Value *CreateElementCount(Type *DstType, ElementCount EC); + LLVM_ABI Value *CreateElementCount(Type *Ty, ElementCount EC); /// Create an expression which evaluates to the number of units in \p Size /// at runtime. This works for both units of bits and bytes. - LLVM_ABI Value *CreateTypeSize(Type *DstType, TypeSize Size); + LLVM_ABI Value *CreateTypeSize(Type *Ty, TypeSize Size); /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...> LLVM_ABI Value *CreateStepVector(Type *DstType, const Twine &Name = ""); diff --git a/llvm/lib/CodeGen/ExpandVectorPredication.cpp b/llvm/lib/CodeGen/ExpandVectorPredication.cpp index 1bb0763fcf57b..d8e3f5fbb31de 100644 --- a/llvm/lib/CodeGen/ExpandVectorPredication.cpp +++ b/llvm/lib/CodeGen/ExpandVectorPredication.cpp @@ -507,8 +507,7 @@ bool CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) { // TODO add caching IRBuilder<> Builder(VPI.getParent(), VPI.getIterator()); Value *FactorConst = Builder.getInt32(StaticElemCount.getKnownMinValue()); - Value *VScale = Builder.CreateIntrinsic(Intrinsic::vscale, Int32Ty, {}, - /*FMFSource=*/nullptr, "vscale"); + Value *VScale = Builder.CreateVScale(Int32Ty, "vscale"); MaxEVL = Builder.CreateMul(VScale, FactorConst, "scalable_size", /*NUW*/ true, /*NSW*/ false); } else { diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp index 580b0af709337..868aa7a2cb799 100644 --- a/llvm/lib/IR/IRBuilder.cpp +++ b/llvm/lib/IR/IRBuilder.cpp @@ -120,23 +120,30 @@ IRBuilderBase::createCallHelper(Function *Callee, ArrayRef<Value *> Ops, return CI; } -Value *IRBuilderBase::CreateVScale(Constant *Scaling, const Twine &Name) { - assert(isa<ConstantInt>(Scaling) && "Expected constant integer"); - if (cast<ConstantInt>(Scaling)->isZero()) - return Scaling; - CallInst *CI = - CreateIntrinsic(Intrinsic::vscale, {Scaling->getType()}, {}, {}, Name); - return cast<ConstantInt>(Scaling)->isOne() ? CI : CreateMul(CI, Scaling); +Value *IRBuilderBase::CreateVScale(Type *Ty, const Twine &Name) { + return CreateIntrinsic(Intrinsic::vscale, {Ty}, {}, {}, Name); } -Value *IRBuilderBase::CreateElementCount(Type *DstType, ElementCount EC) { - Constant *MinEC = ConstantInt::get(DstType, EC.getKnownMinValue()); - return EC.isScalable() ? CreateVScale(MinEC) : MinEC; +Value *IRBuilderBase::CreateElementCount(Type *Ty, ElementCount EC) { + if (EC.isFixed() || EC.isZero()) + return ConstantInt::get(Ty, EC.getKnownMinValue()); + + Value *VScale = CreateVScale(Ty); + if (EC.getKnownMinValue() == 1) + return VScale; + + return CreateMul(VScale, ConstantInt::get(Ty, EC.getKnownMinValue())); } -Value *IRBuilderBase::CreateTypeSize(Type *DstType, TypeSize Size) { - Constant *MinSize = ConstantInt::get(DstType, Size.getKnownMinValue()); - return Size.isScalable() ? CreateVScale(MinSize) : MinSize; +Value *IRBuilderBase::CreateTypeSize(Type *Ty, TypeSize Size) { + if (Size.isFixed() || Size.isZero()) + return ConstantInt::get(Ty, Size.getKnownMinValue()); + + Value *VScale = CreateVScale(Ty); + if (Size.getKnownMinValue() == 1) + return VScale; + + return CreateMul(VScale, ConstantInt::get(Ty, Size.getKnownMinValue())); } Value *IRBuilderBase::CreateStepVector(Type *DstType, const Twine &Name) { diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp index 68aec80f07e1d..f6167e4333327 100644 --- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp @@ -2051,10 +2051,10 @@ instCombineSVECntElts(InstCombiner &IC, IntrinsicInst &II, unsigned NumElts) { const auto Pattern = cast<ConstantInt>(II.getArgOperand(0))->getZExtValue(); if (Pattern == AArch64SVEPredPattern::all) { - Constant *StepVal = ConstantInt::get(II.getType(), NumElts); - auto *VScale = IC.Builder.CreateVScale(StepVal); - VScale->takeName(&II); - return IC.replaceInstUsesWith(II, VScale); + Value *Cnt = IC.Builder.CreateElementCount( + II.getType(), ElementCount::getScalable(NumElts)); + Cnt->takeName(&II); + return IC.replaceInstUsesWith(II, Cnt); } unsigned MinNumElts = getNumElementsFromSVEPredPattern(Pattern); diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index d234a0566e191..2db79228bf0e6 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -935,12 +935,9 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) { Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { Attribute Attr = Trunc.getFunction()->getFnAttribute(Attribute::VScaleRange); - if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) { - if (Log2_32(*MaxVScale) < DestWidth) { - Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1)); - return replaceInstUsesWith(Trunc, VScale); - } - } + if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) + if (Log2_32(*MaxVScale) < DestWidth) + return replaceInstUsesWith(Trunc, Builder.CreateVScale(DestTy)); } } @@ -1314,10 +1311,8 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) { Zext.getFunction()->getFnAttribute(Attribute::VScaleRange); if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) { unsigned TypeWidth = Src->getType()->getScalarSizeInBits(); - if (Log2_32(*MaxVScale) < TypeWidth) { - Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1)); - return replaceInstUsesWith(Zext, VScale); - } + if (Log2_32(*MaxVScale) < TypeWidth) + return replaceInstUsesWith(Zext, Builder.CreateVScale(DestTy)); } } } @@ -1604,12 +1599,9 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) { Sext.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { Attribute Attr = Sext.getFunction()->getFnAttribute(Attribute::VScaleRange); - if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) { - if (Log2_32(*MaxVScale) < (SrcBitSize - 1)) { - Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1)); - return replaceInstUsesWith(Sext, VScale); - } - } + if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) + if (Log2_32(*MaxVScale) < (SrcBitSize - 1)) + return replaceInstUsesWith(Sext, Builder.CreateVScale(DestTy)); } } diff --git a/llvm/lib/Transforms/Utils/LowerVectorIntrinsics.cpp b/llvm/lib/Transforms/Utils/LowerVectorIntrinsics.cpp index a34ef260dc244..71c10f5b157c7 100644 --- a/llvm/lib/Transforms/Utils/LowerVectorIntrinsics.cpp +++ b/llvm/lib/Transforms/Utils/LowerVectorIntrinsics.cpp @@ -21,6 +21,7 @@ bool llvm::lowerUnaryVectorIntrinsicAsLoop(Module &M, CallInst *CI) { BasicBlock *PostLoopBB = nullptr; Function *ParentFunc = PreLoopBB->getParent(); LLVMContext &Ctx = PreLoopBB->getContext(); + Type *Int64Ty = IntegerType::get(Ctx, 64); PostLoopBB = PreLoopBB->splitBasicBlock(CI); BasicBlock *LoopBB = BasicBlock::Create(Ctx, "", ParentFunc, PostLoopBB); @@ -28,22 +29,11 @@ bool llvm::lowerUnaryVectorIntrinsicAsLoop(Module &M, CallInst *CI) { // Loop preheader IRBuilder<> PreLoopBuilder(PreLoopBB->getTerminator()); - Value *LoopEnd = nullptr; - if (auto *ScalableVecTy = dyn_cast<ScalableVectorType>(VecTy)) { - Value *VScale = PreLoopBuilder.CreateVScale( - ConstantInt::get(PreLoopBuilder.getInt64Ty(), 1)); - Value *N = ConstantInt::get(PreLoopBuilder.getInt64Ty(), - ScalableVecTy->getMinNumElements()); - LoopEnd = PreLoopBuilder.CreateMul(VScale, N); - } else { - FixedVectorType *FixedVecTy = cast<FixedVectorType>(VecTy); - LoopEnd = ConstantInt::get(PreLoopBuilder.getInt64Ty(), - FixedVecTy->getNumElements()); - } + Value *LoopEnd = + PreLoopBuilder.CreateElementCount(Int64Ty, VecTy->getElementCount()); // Loop body IRBuilder<> LoopBuilder(LoopBB); - Type *Int64Ty = LoopBuilder.getInt64Ty(); PHINode *LoopIndex = LoopBuilder.CreatePHI(Int64Ty, 2); LoopIndex->addIncoming(ConstantInt::get(Int64Ty, 0U), PreLoopBB); diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp index 88d2eca36ca51..70afd4133df7c 100644 --- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp @@ -1440,7 +1440,7 @@ Value *SCEVExpander::visitSequentialUMinExpr(const SCEVSequentialUMinExpr *S) { } Value *SCEVExpander::visitVScale(const SCEVVScale *S) { - return Builder.CreateVScale(ConstantInt::get(S->getType(), 1)); + return Builder.CreateVScale(S->getType()); } Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty, diff --git a/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp index 02ffc67c774dd..491f0b76f4ae0 100644 --- a/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp @@ -437,7 +437,7 @@ Value *LoopIdiomVectorize::createMaskedFindMismatch( Value *InitialPred = Builder.CreateIntrinsic( Intrinsic::get_active_lane_mask, {PredVTy, I64Type}, {ExtStart, ExtEnd}); - Value *VecLen = Builder.CreateIntrinsic(Intrinsic::vscale, {I64Type}, {}); + Value *VecLen = Builder.CreateVScale(I64Type); VecLen = Builder.CreateMul(VecLen, ConstantInt::get(I64Type, ByteCompareVF), "", /*HasNUW=*/true, /*HasNSW=*/true); diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp index b7eb0af728331..3a7ba924792ef 100644 --- a/llvm/unittests/IR/IRBuilderTest.cpp +++ b/llvm/unittests/IR/IRBuilderTest.cpp @@ -212,14 +212,6 @@ TEST_F(IRBuilderTest, IntrinsicsWithScalableVectors) { EXPECT_EQ(FTy->getParamType(i), Args[i]->getType()); } -TEST_F(IRBuilderTest, CreateVScale) { - IRBuilder<> Builder(BB); - - Constant *Zero = Builder.getInt32(0); - Value *VScale = Builder.CreateVScale(Zero); - EXPECT_TRUE(isa<ConstantInt>(VScale) && cast<ConstantInt>(VScale)->isZero()); -} - TEST_F(IRBuilderTest, CreateStepVector) { IRBuilder<> Builder(BB); >From a3188cf1dcc88983f30c4488d40de8a246b7e681 Mon Sep 17 00:00:00 2001 From: Paul Walker <paul.wal...@arm.com> Date: Thu, 5 Jun 2025 10:44:45 +0000 Subject: [PATCH 2/2] Inline CreateVScale, add utility function, remove unnecessary casts. --- clang/lib/CodeGen/TargetBuiltins/ARM.cpp | 6 ++---- llvm/include/llvm/IR/IRBuilder.h | 4 +++- llvm/lib/IR/IRBuilder.cpp | 20 ++++++++------------ 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/clang/lib/CodeGen/TargetBuiltins/ARM.cpp b/clang/lib/CodeGen/TargetBuiltins/ARM.cpp index 9c77346389e04..dab311903f6dd 100644 --- a/clang/lib/CodeGen/TargetBuiltins/ARM.cpp +++ b/clang/lib/CodeGen/TargetBuiltins/ARM.cpp @@ -4792,8 +4792,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, case SVE::BI__builtin_sve_svlen_u32: case SVE::BI__builtin_sve_svlen_u64: { SVETypeFlags TF(Builtin->TypeModifier); - auto VTy = cast<llvm::VectorType>(getSVEType(TF)); - return Builder.CreateElementCount(Ty, VTy->getElementCount()); + return Builder.CreateElementCount(Ty, getSVEType(TF)->getElementCount()); } case SVE::BI__builtin_sve_svtbl2_u8: @@ -4809,8 +4808,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, case SVE::BI__builtin_sve_svtbl2_f32: case SVE::BI__builtin_sve_svtbl2_f64: { SVETypeFlags TF(Builtin->TypeModifier); - auto VTy = cast<llvm::ScalableVectorType>(getSVEType(TF)); - Function *F = CGM.getIntrinsic(Intrinsic::aarch64_sve_tbl2, VTy); + Function *F = CGM.getIntrinsic(Intrinsic::aarch64_sve_tbl2, getSVEType(TF)); return Builder.CreateCall(F, Ops); } diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index 8ed10cb803a9c..a0cc20d34303a 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -946,7 +946,9 @@ class IRBuilderBase { const Twine &Name = ""); /// Create a call to llvm.vscale.<Ty>(). - LLVM_ABI Value *CreateVScale(Type *Ty, const Twine &Name = ""); + LLVM_ABI Value *CreateVScale(Type *Ty, const Twine &Name = "") { + return CreateIntrinsic(Intrinsic::vscale, {Ty}, {}, {}, Name); + } /// Create an expression which evaluates to the number of elements in \p EC /// at runtime. diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp index 868aa7a2cb799..201134585f56e 100644 --- a/llvm/lib/IR/IRBuilder.cpp +++ b/llvm/lib/IR/IRBuilder.cpp @@ -120,30 +120,26 @@ IRBuilderBase::createCallHelper(Function *Callee, ArrayRef<Value *> Ops, return CI; } -Value *IRBuilderBase::CreateVScale(Type *Ty, const Twine &Name) { - return CreateIntrinsic(Intrinsic::vscale, {Ty}, {}, {}, Name); +static Value *CreateVScaleMultiple(IRBuilderBase &B, Type *Ty, uint64_t Scale) { + Value *VScale = B.CreateVScale(Ty); + if (Scale == 1) + return VScale; + + return B.CreateMul(VScale, ConstantInt::get(Ty, Scale)); } Value *IRBuilderBase::CreateElementCount(Type *Ty, ElementCount EC) { if (EC.isFixed() || EC.isZero()) return ConstantInt::get(Ty, EC.getKnownMinValue()); - Value *VScale = CreateVScale(Ty); - if (EC.getKnownMinValue() == 1) - return VScale; - - return CreateMul(VScale, ConstantInt::get(Ty, EC.getKnownMinValue())); + return CreateVScaleMultiple(*this, Ty, EC.getKnownMinValue()); } Value *IRBuilderBase::CreateTypeSize(Type *Ty, TypeSize Size) { if (Size.isFixed() || Size.isZero()) return ConstantInt::get(Ty, Size.getKnownMinValue()); - Value *VScale = CreateVScale(Ty); - if (Size.getKnownMinValue() == 1) - return VScale; - - return CreateMul(VScale, ConstantInt::get(Ty, Size.getKnownMinValue())); + return CreateVScaleMultiple(*this, Ty, Size.getKnownMinValue()); } Value *IRBuilderBase::CreateStepVector(Type *DstType, const Twine &Name) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits