Skip to content

Commit faa9c8a

Browse files
paulwalker-armrorth
authored andcommitted
[NFC][LLVM] Refactor IRBuilder::Create{VScale,ElementCount,TypeSize}. (llvm#142803)
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 return a call to the llvm.vscale() intrinsic and nothing more. As well as simplifying the existing call sites I've also migrated 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.
1 parent fcd30a9 commit faa9c8a

File tree

10 files changed

+42
-71
lines changed

10 files changed

+42
-71
lines changed

clang/lib/CodeGen/TargetBuiltins/ARM.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4792,12 +4792,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID,
47924792
case SVE::BI__builtin_sve_svlen_u32:
47934793
case SVE::BI__builtin_sve_svlen_u64: {
47944794
SVETypeFlags TF(Builtin->TypeModifier);
4795-
auto VTy = cast<llvm::VectorType>(getSVEType(TF));
4796-
auto *NumEls =
4797-
llvm::ConstantInt::get(Ty, VTy->getElementCount().getKnownMinValue());
4798-
4799-
Function *F = CGM.getIntrinsic(Intrinsic::vscale, Ty);
4800-
return Builder.CreateMul(NumEls, Builder.CreateCall(F));
4795+
return Builder.CreateElementCount(Ty, getSVEType(TF)->getElementCount());
48014796
}
48024797

48034798
case SVE::BI__builtin_sve_svtbl2_u8:
@@ -4813,8 +4808,7 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID,
48134808
case SVE::BI__builtin_sve_svtbl2_f32:
48144809
case SVE::BI__builtin_sve_svtbl2_f64: {
48154810
SVETypeFlags TF(Builtin->TypeModifier);
4816-
auto VTy = cast<llvm::ScalableVectorType>(getSVEType(TF));
4817-
Function *F = CGM.getIntrinsic(Intrinsic::aarch64_sve_tbl2, VTy);
4811+
Function *F = CGM.getIntrinsic(Intrinsic::aarch64_sve_tbl2, getSVEType(TF));
48184812
return Builder.CreateCall(F, Ops);
48194813
}
48204814

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -945,17 +945,18 @@ class IRBuilderBase {
945945
LLVM_ABI CallInst *CreateGCGetPointerOffset(Value *DerivedPtr,
946946
const Twine &Name = "");
947947

948-
/// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
949-
/// will be the same type as that of \p Scaling.
950-
LLVM_ABI Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
948+
/// Create a call to llvm.vscale.<Ty>().
949+
LLVM_ABI Value *CreateVScale(Type *Ty, const Twine &Name = "") {
950+
return CreateIntrinsic(Intrinsic::vscale, {Ty}, {}, {}, Name);
951+
}
951952

952953
/// Create an expression which evaluates to the number of elements in \p EC
953954
/// at runtime.
954-
LLVM_ABI Value *CreateElementCount(Type *DstType, ElementCount EC);
955+
LLVM_ABI Value *CreateElementCount(Type *Ty, ElementCount EC);
955956

956957
/// Create an expression which evaluates to the number of units in \p Size
957958
/// at runtime. This works for both units of bits and bytes.
958-
LLVM_ABI Value *CreateTypeSize(Type *DstType, TypeSize Size);
959+
LLVM_ABI Value *CreateTypeSize(Type *Ty, TypeSize Size);
959960

960961
/// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
961962
LLVM_ABI Value *CreateStepVector(Type *DstType, const Twine &Name = "");

llvm/lib/CodeGen/ExpandVectorPredication.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ bool CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) {
507507
// TODO add caching
508508
IRBuilder<> Builder(VPI.getParent(), VPI.getIterator());
509509
Value *FactorConst = Builder.getInt32(StaticElemCount.getKnownMinValue());
510-
Value *VScale = Builder.CreateIntrinsic(Intrinsic::vscale, Int32Ty, {},
511-
/*FMFSource=*/nullptr, "vscale");
510+
Value *VScale = Builder.CreateVScale(Int32Ty, "vscale");
512511
MaxEVL = Builder.CreateMul(VScale, FactorConst, "scalable_size",
513512
/*NUW*/ true, /*NSW*/ false);
514513
} else {

llvm/lib/IR/IRBuilder.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,26 @@ IRBuilderBase::createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
120120
return CI;
121121
}
122122

123-
Value *IRBuilderBase::CreateVScale(Constant *Scaling, const Twine &Name) {
124-
assert(isa<ConstantInt>(Scaling) && "Expected constant integer");
125-
if (cast<ConstantInt>(Scaling)->isZero())
126-
return Scaling;
127-
CallInst *CI =
128-
CreateIntrinsic(Intrinsic::vscale, {Scaling->getType()}, {}, {}, Name);
129-
return cast<ConstantInt>(Scaling)->isOne() ? CI : CreateMul(CI, Scaling);
123+
static Value *CreateVScaleMultiple(IRBuilderBase &B, Type *Ty, uint64_t Scale) {
124+
Value *VScale = B.CreateVScale(Ty);
125+
if (Scale == 1)
126+
return VScale;
127+
128+
return B.CreateMul(VScale, ConstantInt::get(Ty, Scale));
130129
}
131130

132-
Value *IRBuilderBase::CreateElementCount(Type *DstType, ElementCount EC) {
133-
Constant *MinEC = ConstantInt::get(DstType, EC.getKnownMinValue());
134-
return EC.isScalable() ? CreateVScale(MinEC) : MinEC;
131+
Value *IRBuilderBase::CreateElementCount(Type *Ty, ElementCount EC) {
132+
if (EC.isFixed() || EC.isZero())
133+
return ConstantInt::get(Ty, EC.getKnownMinValue());
134+
135+
return CreateVScaleMultiple(*this, Ty, EC.getKnownMinValue());
135136
}
136137

137-
Value *IRBuilderBase::CreateTypeSize(Type *DstType, TypeSize Size) {
138-
Constant *MinSize = ConstantInt::get(DstType, Size.getKnownMinValue());
139-
return Size.isScalable() ? CreateVScale(MinSize) : MinSize;
138+
Value *IRBuilderBase::CreateTypeSize(Type *Ty, TypeSize Size) {
139+
if (Size.isFixed() || Size.isZero())
140+
return ConstantInt::get(Ty, Size.getKnownMinValue());
141+
142+
return CreateVScaleMultiple(*this, Ty, Size.getKnownMinValue());
140143
}
141144

142145
Value *IRBuilderBase::CreateStepVector(Type *DstType, const Twine &Name) {

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,10 +2051,10 @@ instCombineSVECntElts(InstCombiner &IC, IntrinsicInst &II, unsigned NumElts) {
20512051
const auto Pattern = cast<ConstantInt>(II.getArgOperand(0))->getZExtValue();
20522052

20532053
if (Pattern == AArch64SVEPredPattern::all) {
2054-
Constant *StepVal = ConstantInt::get(II.getType(), NumElts);
2055-
auto *VScale = IC.Builder.CreateVScale(StepVal);
2056-
VScale->takeName(&II);
2057-
return IC.replaceInstUsesWith(II, VScale);
2054+
Value *Cnt = IC.Builder.CreateElementCount(
2055+
II.getType(), ElementCount::getScalable(NumElts));
2056+
Cnt->takeName(&II);
2057+
return IC.replaceInstUsesWith(II, Cnt);
20582058
}
20592059

20602060
unsigned MinNumElts = getNumElementsFromSVEPredPattern(Pattern);

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -935,12 +935,9 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
935935
Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
936936
Attribute Attr =
937937
Trunc.getFunction()->getFnAttribute(Attribute::VScaleRange);
938-
if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) {
939-
if (Log2_32(*MaxVScale) < DestWidth) {
940-
Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1));
941-
return replaceInstUsesWith(Trunc, VScale);
942-
}
943-
}
938+
if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax())
939+
if (Log2_32(*MaxVScale) < DestWidth)
940+
return replaceInstUsesWith(Trunc, Builder.CreateVScale(DestTy));
944941
}
945942
}
946943

@@ -1314,10 +1311,8 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
13141311
Zext.getFunction()->getFnAttribute(Attribute::VScaleRange);
13151312
if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) {
13161313
unsigned TypeWidth = Src->getType()->getScalarSizeInBits();
1317-
if (Log2_32(*MaxVScale) < TypeWidth) {
1318-
Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1));
1319-
return replaceInstUsesWith(Zext, VScale);
1320-
}
1314+
if (Log2_32(*MaxVScale) < TypeWidth)
1315+
return replaceInstUsesWith(Zext, Builder.CreateVScale(DestTy));
13211316
}
13221317
}
13231318
}
@@ -1604,12 +1599,9 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
16041599
Sext.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
16051600
Attribute Attr =
16061601
Sext.getFunction()->getFnAttribute(Attribute::VScaleRange);
1607-
if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) {
1608-
if (Log2_32(*MaxVScale) < (SrcBitSize - 1)) {
1609-
Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1));
1610-
return replaceInstUsesWith(Sext, VScale);
1611-
}
1612-
}
1602+
if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax())
1603+
if (Log2_32(*MaxVScale) < (SrcBitSize - 1))
1604+
return replaceInstUsesWith(Sext, Builder.CreateVScale(DestTy));
16131605
}
16141606
}
16151607

llvm/lib/Transforms/Utils/LowerVectorIntrinsics.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,19 @@ bool llvm::lowerUnaryVectorIntrinsicAsLoop(Module &M, CallInst *CI) {
2121
BasicBlock *PostLoopBB = nullptr;
2222
Function *ParentFunc = PreLoopBB->getParent();
2323
LLVMContext &Ctx = PreLoopBB->getContext();
24+
Type *Int64Ty = IntegerType::get(Ctx, 64);
2425

2526
PostLoopBB = PreLoopBB->splitBasicBlock(CI);
2627
BasicBlock *LoopBB = BasicBlock::Create(Ctx, "", ParentFunc, PostLoopBB);
2728
PreLoopBB->getTerminator()->setSuccessor(0, LoopBB);
2829

2930
// Loop preheader
3031
IRBuilder<> PreLoopBuilder(PreLoopBB->getTerminator());
31-
Value *LoopEnd = nullptr;
32-
if (auto *ScalableVecTy = dyn_cast<ScalableVectorType>(VecTy)) {
33-
Value *VScale = PreLoopBuilder.CreateVScale(
34-
ConstantInt::get(PreLoopBuilder.getInt64Ty(), 1));
35-
Value *N = ConstantInt::get(PreLoopBuilder.getInt64Ty(),
36-
ScalableVecTy->getMinNumElements());
37-
LoopEnd = PreLoopBuilder.CreateMul(VScale, N);
38-
} else {
39-
FixedVectorType *FixedVecTy = cast<FixedVectorType>(VecTy);
40-
LoopEnd = ConstantInt::get(PreLoopBuilder.getInt64Ty(),
41-
FixedVecTy->getNumElements());
42-
}
32+
Value *LoopEnd =
33+
PreLoopBuilder.CreateElementCount(Int64Ty, VecTy->getElementCount());
4334

4435
// Loop body
4536
IRBuilder<> LoopBuilder(LoopBB);
46-
Type *Int64Ty = LoopBuilder.getInt64Ty();
4737

4838
PHINode *LoopIndex = LoopBuilder.CreatePHI(Int64Ty, 2);
4939
LoopIndex->addIncoming(ConstantInt::get(Int64Ty, 0U), PreLoopBB);

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ Value *SCEVExpander::visitSequentialUMinExpr(const SCEVSequentialUMinExpr *S) {
14401440
}
14411441

14421442
Value *SCEVExpander::visitVScale(const SCEVVScale *S) {
1443-
return Builder.CreateVScale(ConstantInt::get(S->getType(), 1));
1443+
return Builder.CreateVScale(S->getType());
14441444
}
14451445

14461446
Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty,

llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ Value *LoopIdiomVectorize::createMaskedFindMismatch(
437437
Value *InitialPred = Builder.CreateIntrinsic(
438438
Intrinsic::get_active_lane_mask, {PredVTy, I64Type}, {ExtStart, ExtEnd});
439439

440-
Value *VecLen = Builder.CreateIntrinsic(Intrinsic::vscale, {I64Type}, {});
440+
Value *VecLen = Builder.CreateVScale(I64Type);
441441
VecLen =
442442
Builder.CreateMul(VecLen, ConstantInt::get(I64Type, ByteCompareVF), "",
443443
/*HasNUW=*/true, /*HasNSW=*/true);

llvm/unittests/IR/IRBuilderTest.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,6 @@ TEST_F(IRBuilderTest, IntrinsicsWithScalableVectors) {
212212
EXPECT_EQ(FTy->getParamType(i), Args[i]->getType());
213213
}
214214

215-
TEST_F(IRBuilderTest, CreateVScale) {
216-
IRBuilder<> Builder(BB);
217-
218-
Constant *Zero = Builder.getInt32(0);
219-
Value *VScale = Builder.CreateVScale(Zero);
220-
EXPECT_TRUE(isa<ConstantInt>(VScale) && cast<ConstantInt>(VScale)->isZero());
221-
}
222-
223215
TEST_F(IRBuilderTest, CreateStepVector) {
224216
IRBuilder<> Builder(BB);
225217

0 commit comments

Comments
 (0)