From dae682ce928a8ad43b4734c64f66934ca73321b6 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Mon, 13 Mar 2023 11:39:00 -0700 Subject: [PATCH] [IRBuilder] Add utilities for materializing scalable values [nfc] These idioms already appear a number of places in code, and upcoming changes to the various sanitizers continue to need more instances of the same patterns. Differential Revision: https://reviews.llvm.org/D145945 --- llvm/include/llvm/IR/IRBuilder.h | 8 ++++++++ llvm/lib/IR/IRBuilder.cpp | 10 ++++++++++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 4 +--- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp | 10 ++-------- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp | 6 ++---- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index 3c78faa..0546dda 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -893,6 +893,14 @@ public: /// will be the same type as that of \p Scaling. Value *CreateVScale(Constant *Scaling, const Twine &Name = ""); + /// Create an expression which evaluates to the number of elements in \p EC + /// at runtime. + Value *CreateElementCount(Type *DstType, 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. + Value *CreateTypeSize(Type *DstType, TypeSize Size); + /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...> Value *CreateStepVector(Type *DstType, const Twine &Name = ""); diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp index f871205..3ebd40a 100644 --- a/llvm/lib/IR/IRBuilder.cpp +++ b/llvm/lib/IR/IRBuilder.cpp @@ -107,6 +107,16 @@ Value *IRBuilderBase::CreateVScale(Constant *Scaling, const Twine &Name) { : CreateMul(CI, Scaling); } +Value *IRBuilderBase::CreateElementCount(Type *DstType, ElementCount EC) { + Constant *MinEC = ConstantInt::get(DstType, EC.getKnownMinValue()); + return EC.isScalable() ? CreateVScale(MinEC) : MinEC; +} + +Value *IRBuilderBase::CreateTypeSize(Type *DstType, TypeSize Size) { + Constant *MinSize = ConstantInt::get(DstType, Size.getKnownMinValue()); + return Size.isScalable() ? CreateVScale(MinSize) : MinSize; +} + Value *IRBuilderBase::CreateStepVector(Type *DstType, const Twine &Name) { Type *STy = DstType->getScalarType(); if (isa(DstType)) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 916476a..fc07db9 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -439,9 +439,7 @@ Instruction *InstCombinerImpl::simplifyMaskedScatter(IntrinsicInst &II) { Align Alignment = cast(II.getArgOperand(2))->getAlignValue(); VectorType *WideLoadTy = cast(II.getArgOperand(1)->getType()); ElementCount VF = WideLoadTy->getElementCount(); - Constant *EC = - ConstantInt::get(Builder.getInt32Ty(), VF.getKnownMinValue()); - Value *RunTimeVF = VF.isScalable() ? Builder.CreateVScale(EC) : EC; + Value *RunTimeVF = Builder.CreateElementCount(Builder.getInt32Ty(), VF); Value *LastLane = Builder.CreateSub(RunTimeVF, Builder.getInt32(1)); Value *Extract = Builder.CreateExtractElement(II.getArgOperand(0), LastLane); diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp index 0b07c10..d45b668 100644 --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1518,10 +1518,7 @@ static void instrumentMaskedLoadOrStore(AddressSanitizer *Pass, IRBuilder<> IRB(I); - Constant *MinNumElem = - ConstantInt::get(IntptrTy, VTy->getElementCount().getKnownMinValue()); - assert(isa(VTy) && "generalize if reused for fixed length"); - Value *NumElements = IRB.CreateVScale(MinNumElem); + Value *NumElements = IRB.CreateElementCount(IntptrTy, VTy->getElementCount()); Instruction *BodyIP; Value *Index; @@ -1745,10 +1742,7 @@ void AddressSanitizer::instrumentUnusualSizeOrAlignment( Instruction *I, Instruction *InsertBefore, Value *Addr, TypeSize TypeStoreSize, bool IsWrite, Value *SizeArgument, bool UseCalls, uint32_t Exp) { IRBuilder<> IRB(InsertBefore); - Constant *MinBits = - ConstantInt::get(IntptrTy, TypeStoreSize.getKnownMinValue()); - Value *NumBits = - !TypeStoreSize.isScalable() ? MinBits : IRB.CreateVScale(MinBits); + Value *NumBits = IRB.CreateTypeSize(IntptrTy, TypeStoreSize); Value *Size = IRB.CreateLShr(NumBits, ConstantInt::get(IntptrTy, 3)); Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp index 159bbd0..56211b17 100644 --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -975,14 +975,12 @@ namespace llvm { Value *createStepForVF(IRBuilderBase &B, Type *Ty, ElementCount VF, int64_t Step) { assert(Ty->isIntegerTy() && "Expected an integer step"); - Constant *StepVal = ConstantInt::get(Ty, Step * VF.getKnownMinValue()); - return VF.isScalable() ? B.CreateVScale(StepVal) : StepVal; + return B.CreateElementCount(Ty, VF.multiplyCoefficientBy(Step)); } /// Return the runtime value for VF. Value *getRuntimeVF(IRBuilderBase &B, Type *Ty, ElementCount VF) { - Constant *EC = ConstantInt::get(Ty, VF.getKnownMinValue()); - return VF.isScalable() ? B.CreateVScale(EC) : EC; + return B.CreateElementCount(Ty, VF); } const SCEV *createTripCountSCEV(Type *IdxTy, PredicatedScalarEvolution &PSE) { -- 2.7.4