Skip to content

Commit 8ee9646

Browse files
authored
[LV] Simplify creation of vp.load/vp.store/vp.reduce intrinsics (#143804)
The use of VectorBuilder here was simply obscuring what was actually going on. For vp.load and vp.store, the resulting code is significantly more idiomatic. For the vp.reduce cases, we remove several layers of indirection, including passing parameters via implicit state on the builder. In both cases, the code is significantly easier to follow.
1 parent d659046 commit 8ee9646

File tree

8 files changed

+31
-551
lines changed

8 files changed

+31
-551
lines changed

llvm/include/llvm/IR/VectorBuilder.h

Lines changed: 0 additions & 120 deletions
This file was deleted.

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/Analysis/IVDescriptors.h"
1717
#include "llvm/Analysis/LoopAccessAnalysis.h"
1818
#include "llvm/Analysis/TargetTransformInfo.h"
19-
#include "llvm/IR/VectorBuilder.h"
2019
#include "llvm/Support/Compiler.h"
2120
#include "llvm/Transforms/Utils/ValueMapper.h"
2221

@@ -423,8 +422,9 @@ LLVM_ABI Value *createSimpleReduction(IRBuilderBase &B, Value *Src,
423422
RecurKind RdxKind);
424423
/// Overloaded function to generate vector-predication intrinsics for
425424
/// reduction.
426-
LLVM_ABI Value *createSimpleReduction(VectorBuilder &VB, Value *Src,
427-
RecurKind RdxKind);
425+
LLVM_ABI Value *createSimpleReduction(IRBuilderBase &B, Value *Src,
426+
RecurKind RdxKind, Value *Mask,
427+
Value *EVL);
428428

429429
/// Create a reduction of the given vector \p Src for a reduction of kind
430430
/// RecurKind::AnyOf. The start value of the reduction is \p InitVal.
@@ -442,8 +442,9 @@ LLVM_ABI Value *createOrderedReduction(IRBuilderBase &B, RecurKind RdxKind,
442442
Value *Src, Value *Start);
443443
/// Overloaded function to generate vector-predication intrinsics for ordered
444444
/// reduction.
445-
LLVM_ABI Value *createOrderedReduction(VectorBuilder &VB, RecurKind RdxKind,
446-
Value *Src, Value *Start);
445+
LLVM_ABI Value *createOrderedReduction(IRBuilderBase &B, RecurKind RdxKind,
446+
Value *Src, Value *Start, Value *Mask,
447+
Value *EVL);
447448

448449
/// Get the intersection (logical and) of all of the potential IR flags
449450
/// of each scalar operation (VL) that will be converted into a vector (I).

llvm/lib/IR/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ add_llvm_component_library(LLVMCore
7474
User.cpp
7575
Value.cpp
7676
ValueSymbolTable.cpp
77-
VectorBuilder.cpp
7877
VectorTypeUtils.cpp
7978
Verifier.cpp
8079
VFABIDemangler.cpp

llvm/lib/IR/VectorBuilder.cpp

Lines changed: 0 additions & 116 deletions
This file was deleted.

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,18 +1319,19 @@ Value *llvm::createSimpleReduction(IRBuilderBase &Builder, Value *Src,
13191319
}
13201320
}
13211321

1322-
Value *llvm::createSimpleReduction(VectorBuilder &VBuilder, Value *Src,
1323-
RecurKind Kind) {
1322+
Value *llvm::createSimpleReduction(IRBuilderBase &Builder, Value *Src,
1323+
RecurKind Kind, Value *Mask, Value *EVL) {
13241324
assert(!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
13251325
!RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind) &&
13261326
"AnyOf or FindLastIV reductions are not supported.");
13271327
Intrinsic::ID Id = getReductionIntrinsicID(Kind);
1328-
auto *SrcTy = cast<VectorType>(Src->getType());
1329-
Type *SrcEltTy = SrcTy->getElementType();
1330-
Value *Iden =
1331-
getRecurrenceIdentity(Kind, SrcEltTy, VBuilder.getFastMathFlags());
1332-
Value *Ops[] = {Iden, Src};
1333-
return VBuilder.createSimpleReduction(Id, SrcTy, Ops);
1328+
auto VPID = VPIntrinsic::getForIntrinsic(Id);
1329+
assert(VPReductionIntrinsic::isVPReduction(VPID) &&
1330+
"No VPIntrinsic for this reduction");
1331+
auto *EltTy = cast<VectorType>(Src->getType())->getElementType();
1332+
Value *Iden = getRecurrenceIdentity(Kind, EltTy, Builder.getFastMathFlags());
1333+
Value *Ops[] = {Iden, Src, Mask, EVL};
1334+
return Builder.CreateIntrinsic(EltTy, VPID, Ops);
13341335
}
13351336

13361337
Value *llvm::createOrderedReduction(IRBuilderBase &B, RecurKind Kind,
@@ -1343,17 +1344,21 @@ Value *llvm::createOrderedReduction(IRBuilderBase &B, RecurKind Kind,
13431344
return B.CreateFAddReduce(Start, Src);
13441345
}
13451346

1346-
Value *llvm::createOrderedReduction(VectorBuilder &VBuilder, RecurKind Kind,
1347-
Value *Src, Value *Start) {
1347+
Value *llvm::createOrderedReduction(IRBuilderBase &Builder, RecurKind Kind,
1348+
Value *Src, Value *Start, Value *Mask,
1349+
Value *EVL) {
13481350
assert((Kind == RecurKind::FAdd || Kind == RecurKind::FMulAdd) &&
13491351
"Unexpected reduction kind");
13501352
assert(Src->getType()->isVectorTy() && "Expected a vector type");
13511353
assert(!Start->getType()->isVectorTy() && "Expected a scalar type");
13521354

13531355
Intrinsic::ID Id = getReductionIntrinsicID(RecurKind::FAdd);
1354-
auto *SrcTy = cast<VectorType>(Src->getType());
1355-
Value *Ops[] = {Start, Src};
1356-
return VBuilder.createSimpleReduction(Id, SrcTy, Ops);
1356+
auto VPID = VPIntrinsic::getForIntrinsic(Id);
1357+
assert(VPReductionIntrinsic::isVPReduction(VPID) &&
1358+
"No VPIntrinsic for this reduction");
1359+
auto *EltTy = cast<VectorType>(Src->getType())->getElementType();
1360+
Value *Ops[] = {Start, Src, Mask, EVL};
1361+
return Builder.CreateIntrinsic(EltTy, VPID, Ops);
13571362
}
13581363

13591364
void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue,

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "llvm/IR/Intrinsics.h"
3131
#include "llvm/IR/Type.h"
3232
#include "llvm/IR/Value.h"
33-
#include "llvm/IR/VectorBuilder.h"
3433
#include "llvm/Support/Casting.h"
3534
#include "llvm/Support/CommandLine.h"
3635
#include "llvm/Support/Debug.h"
@@ -2524,21 +2523,17 @@ void VPReductionEVLRecipe::execute(VPTransformState &State) {
25242523
Value *VecOp = State.get(getVecOp());
25252524
Value *EVL = State.get(getEVL(), VPLane(0));
25262525

2527-
VectorBuilder VBuilder(Builder);
2528-
VBuilder.setEVL(EVL);
25292526
Value *Mask;
2530-
// TODO: move the all-true mask generation into VectorBuilder.
25312527
if (VPValue *CondOp = getCondOp())
25322528
Mask = State.get(CondOp);
25332529
else
25342530
Mask = Builder.CreateVectorSplat(State.VF, Builder.getTrue());
2535-
VBuilder.setMask(Mask);
25362531

25372532
Value *NewRed;
25382533
if (isOrdered()) {
2539-
NewRed = createOrderedReduction(VBuilder, Kind, VecOp, Prev);
2534+
NewRed = createOrderedReduction(Builder, Kind, VecOp, Prev, Mask, EVL);
25402535
} else {
2541-
NewRed = createSimpleReduction(VBuilder, VecOp, Kind);
2536+
NewRed = createSimpleReduction(Builder, VecOp, Kind, Mask, EVL);
25422537
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind))
25432538
NewRed = createMinMaxOp(Builder, Kind, NewRed, Prev);
25442539
else
@@ -3086,10 +3081,8 @@ void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {
30863081
Builder.CreateIntrinsic(DataTy, Intrinsic::vp_gather, {Addr, Mask, EVL},
30873082
nullptr, "wide.masked.gather");
30883083
} else {
3089-
VectorBuilder VBuilder(Builder);
3090-
VBuilder.setEVL(EVL).setMask(Mask);
3091-
NewLI = cast<CallInst>(VBuilder.createVectorInstruction(
3092-
Instruction::Load, DataTy, Addr, "vp.op.load"));
3084+
NewLI = Builder.CreateIntrinsic(DataTy, Intrinsic::vp_load,
3085+
{Addr, Mask, EVL}, nullptr, "vp.op.load");
30933086
}
30943087
NewLI->addParamAttr(
30953088
0, Attribute::getWithAlignment(NewLI->getContext(), Alignment));
@@ -3204,11 +3197,9 @@ void VPWidenStoreEVLRecipe::execute(VPTransformState &State) {
32043197
Intrinsic::vp_scatter,
32053198
{StoredVal, Addr, Mask, EVL});
32063199
} else {
3207-
VectorBuilder VBuilder(Builder);
3208-
VBuilder.setEVL(EVL).setMask(Mask);
3209-
NewSI = cast<CallInst>(VBuilder.createVectorInstruction(
3210-
Instruction::Store, Type::getVoidTy(EVL->getContext()),
3211-
{StoredVal, Addr}));
3200+
NewSI = Builder.CreateIntrinsic(Type::getVoidTy(EVL->getContext()),
3201+
Intrinsic::vp_store,
3202+
{StoredVal, Addr, Mask, EVL});
32123203
}
32133204
NewSI->addParamAttr(
32143205
1, Attribute::getWithAlignment(NewSI->getContext(), Alignment));

0 commit comments

Comments
 (0)