Skip to content

[RISCV][LoopIdiomVectorize] Support VP intrinsics in LoopIdiomVectorize #94082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion llvm/include/llvm/Transforms/Vectorize/LoopIdiomVectorize.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@
#include "llvm/Transforms/Scalar/LoopPassManager.h"

namespace llvm {
struct LoopIdiomVectorizePass : PassInfoMixin<LoopIdiomVectorizePass> {
enum class LoopIdiomVectorizeStyle { Masked, Predicated };

class LoopIdiomVectorizePass : public PassInfoMixin<LoopIdiomVectorizePass> {
LoopIdiomVectorizeStyle VectorizeStyle = LoopIdiomVectorizeStyle::Masked;

// The VF used in vectorizing the byte compare pattern.
unsigned ByteCompareVF = 16;

public:
LoopIdiomVectorizePass() = default;
explicit LoopIdiomVectorizePass(LoopIdiomVectorizeStyle S)
: VectorizeStyle(S) {}

LoopIdiomVectorizePass(LoopIdiomVectorizeStyle S, unsigned BCVF)
: VectorizeStyle(S), ByteCompareVF(BCVF) {}

PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U);
};
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/InitializePasses.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Vectorize/LoopIdiomVectorize.h"
#include <optional>
using namespace llvm;

Expand Down Expand Up @@ -572,6 +574,13 @@ void RISCVPassConfig::addPostRegAlloc() {
addPass(createRISCVRedundantCopyEliminationPass());
}

void RISCVTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
PB.registerLateLoopOptimizationsEPCallback([=](LoopPassManager &LPM,
OptimizationLevel Level) {
LPM.addPass(LoopIdiomVectorizePass(LoopIdiomVectorizeStyle::Predicated));
});
}

yaml::MachineFunctionInfo *
RISCVTargetMachine::createDefaultFuncInfoYAML() const {
return new yaml::RISCVMachineFunctionInfo();
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/RISCVTargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class RISCVTargetMachine : public LLVMTargetMachine {
PerFunctionMIParsingState &PFS,
SMDiagnostic &Error,
SMRange &SourceRange) const override;
void registerPassBuilderCallbacks(PassBuilder &PB) override;
};
} // namespace llvm

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
bool shouldFoldTerminatingConditionAfterLSR() const {
return true;
}

std::optional<unsigned> getMinPageSize() const { return 4096; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: LoopIdiomVectorize is the only user of this TTI hook.

};

} // end namespace llvm
Expand Down
178 changes: 160 additions & 18 deletions llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,34 @@ static cl::opt<bool> DisableAll("disable-loop-idiom-vectorize-all", cl::Hidden,
cl::init(false),
cl::desc("Disable Loop Idiom Vectorize Pass."));

static cl::opt<LoopIdiomVectorizeStyle>
LITVecStyle("loop-idiom-vectorize-style", cl::Hidden,
cl::desc("The vectorization style for loop idiom transform."),
cl::values(clEnumValN(LoopIdiomVectorizeStyle::Masked, "masked",
"Use masked vector intrinsics"),
clEnumValN(LoopIdiomVectorizeStyle::Predicated,
"predicated", "Use VP intrinsics")),
cl::init(LoopIdiomVectorizeStyle::Masked));

static cl::opt<bool>
DisableByteCmp("disable-loop-idiom-vectorize-bytecmp", cl::Hidden,
cl::init(false),
cl::desc("Proceed with Loop Idiom Vectorize Pass, but do "
"not convert byte-compare loop(s)."));

static cl::opt<unsigned>
ByteCmpVF("loop-idiom-vectorize-bytecmp-vf", cl::Hidden,
cl::desc("The vectorization factor for byte-compare patterns."),
cl::init(16));

static cl::opt<bool>
VerifyLoops("loop-idiom-vectorize-verify", cl::Hidden, cl::init(false),
cl::desc("Verify loops generated Loop Idiom Vectorize Pass."));

namespace {

class LoopIdiomVectorize {
LoopIdiomVectorizeStyle VectorizeStyle;
unsigned ByteCompareVF;
Loop *CurLoop = nullptr;
DominatorTree *DT;
LoopInfo *LI;
Expand All @@ -86,10 +101,11 @@ class LoopIdiomVectorize {
BasicBlock *VectorLoopIncBlock = nullptr;

public:
explicit LoopIdiomVectorize(DominatorTree *DT, LoopInfo *LI,
const TargetTransformInfo *TTI,
const DataLayout *DL)
: DT(DT), LI(LI), TTI(TTI), DL(DL) {}
LoopIdiomVectorize(LoopIdiomVectorizeStyle S, unsigned VF, DominatorTree *DT,
LoopInfo *LI, const TargetTransformInfo *TTI,
const DataLayout *DL)
: VectorizeStyle(S), ByteCompareVF(VF), DT(DT), LI(LI), TTI(TTI), DL(DL) {
}

bool run(Loop *L);

Expand All @@ -111,6 +127,10 @@ class LoopIdiomVectorize {
GetElementPtrInst *GEPA,
GetElementPtrInst *GEPB, Value *ExtStart,
Value *ExtEnd);
Value *createPredicatedFindMismatch(IRBuilder<> &Builder, DomTreeUpdater &DTU,
GetElementPtrInst *GEPA,
GetElementPtrInst *GEPB, Value *ExtStart,
Value *ExtEnd);

void transformByteCompare(GetElementPtrInst *GEPA, GetElementPtrInst *GEPB,
PHINode *IndPhi, Value *MaxLen, Instruction *Index,
Expand All @@ -128,8 +148,16 @@ PreservedAnalyses LoopIdiomVectorizePass::run(Loop &L, LoopAnalysisManager &AM,

const auto *DL = &L.getHeader()->getDataLayout();

LoopIdiomVectorize LIT(&AR.DT, &AR.LI, &AR.TTI, DL);
if (!LIT.run(&L))
LoopIdiomVectorizeStyle VecStyle = VectorizeStyle;
if (LITVecStyle.getNumOccurrences())
VecStyle = LITVecStyle;

unsigned BCVF = ByteCompareVF;
if (ByteCmpVF.getNumOccurrences())
BCVF = ByteCmpVF;

LoopIdiomVectorize LIV(VecStyle, BCVF, &AR.DT, &AR.LI, &AR.TTI, DL);
if (!LIV.run(&L))
return PreservedAnalyses::all();

return PreservedAnalyses::none();
Expand Down Expand Up @@ -354,20 +382,16 @@ Value *LoopIdiomVectorize::createMaskedFindMismatch(
Value *PtrA = GEPA->getPointerOperand();
Value *PtrB = GEPB->getPointerOperand();

// At this point we know two things must be true:
// 1. Start <= End
// 2. ExtMaxLen <= MinPageSize due to the page checks.
// Therefore, we know that we can use a 64-bit induction variable that
// starts from 0 -> ExtMaxLen and it will not overflow.
ScalableVectorType *PredVTy =
ScalableVectorType::get(Builder.getInt1Ty(), 16);
ScalableVectorType::get(Builder.getInt1Ty(), ByteCompareVF);

Value *InitialPred = Builder.CreateIntrinsic(
Intrinsic::get_active_lane_mask, {PredVTy, I64Type}, {ExtStart, ExtEnd});

Value *VecLen = Builder.CreateIntrinsic(Intrinsic::vscale, {I64Type}, {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See IRBuilders, CreateElementCount

Copy link
Collaborator

@topperc topperc Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the patch is an extraction of the existing code into a function. We shouldn't make any changes here in this patch.

VecLen = Builder.CreateMul(VecLen, ConstantInt::get(I64Type, 16), "",
/*HasNUW=*/true, /*HasNSW=*/true);
VecLen =
Builder.CreateMul(VecLen, ConstantInt::get(I64Type, ByteCompareVF), "",
/*HasNUW=*/true, /*HasNSW=*/true);

Value *PFalse = Builder.CreateVectorSplat(PredVTy->getElementCount(),
Builder.getInt1(false));
Expand All @@ -385,7 +409,8 @@ Value *LoopIdiomVectorize::createMaskedFindMismatch(
LoopPred->addIncoming(InitialPred, VectorLoopPreheaderBlock);
PHINode *VectorIndexPhi = Builder.CreatePHI(I64Type, 2, "mismatch_vec_index");
VectorIndexPhi->addIncoming(ExtStart, VectorLoopPreheaderBlock);
Type *VectorLoadType = ScalableVectorType::get(Builder.getInt8Ty(), 16);
Type *VectorLoadType =
ScalableVectorType::get(Builder.getInt8Ty(), ByteCompareVF);
Value *Passthru = ConstantInt::getNullValue(VectorLoadType);

Value *VectorLhsGep =
Expand Down Expand Up @@ -454,6 +479,109 @@ Value *LoopIdiomVectorize::createMaskedFindMismatch(
return Builder.CreateTrunc(VectorLoopRes64, ResType);
}

Value *LoopIdiomVectorize::createPredicatedFindMismatch(
IRBuilder<> &Builder, DomTreeUpdater &DTU, GetElementPtrInst *GEPA,
GetElementPtrInst *GEPB, Value *ExtStart, Value *ExtEnd) {
Type *I64Type = Builder.getInt64Ty();
Type *I32Type = Builder.getInt32Ty();
Type *ResType = I32Type;
Type *LoadType = Builder.getInt8Ty();
Value *PtrA = GEPA->getPointerOperand();
Value *PtrB = GEPB->getPointerOperand();

auto *JumpToVectorLoop = BranchInst::Create(VectorLoopStartBlock);
Builder.Insert(JumpToVectorLoop);

DTU.applyUpdates({{DominatorTree::Insert, VectorLoopPreheaderBlock,
VectorLoopStartBlock}});

// Set up the first Vector loop block by creating the PHIs, doing the vector
// loads and comparing the vectors.
Builder.SetInsertPoint(VectorLoopStartBlock);
auto *VectorIndexPhi = Builder.CreatePHI(I64Type, 2, "mismatch_vector_index");
VectorIndexPhi->addIncoming(ExtStart, VectorLoopPreheaderBlock);

// Calculate AVL by subtracting the vector loop index from the trip count
Value *AVL = Builder.CreateSub(ExtEnd, VectorIndexPhi, "avl", /*HasNUW=*/true,
/*HasNSW=*/true);

auto *VectorLoadType = ScalableVectorType::get(LoadType, ByteCompareVF);
auto *VF = ConstantInt::get(I32Type, ByteCompareVF);

Value *VL = Builder.CreateIntrinsic(Intrinsic::experimental_get_vector_length,
{I64Type}, {AVL, VF, Builder.getTrue()});
Value *GepOffset = VectorIndexPhi;

Value *VectorLhsGep =
Builder.CreateGEP(LoadType, PtrA, GepOffset, "", GEPA->isInBounds());
VectorType *TrueMaskTy =
VectorType::get(Builder.getInt1Ty(), VectorLoadType->getElementCount());
Value *AllTrueMask = Constant::getAllOnesValue(TrueMaskTy);
Value *VectorLhsLoad = Builder.CreateIntrinsic(
Intrinsic::vp_load, {VectorLoadType, VectorLhsGep->getType()},
{VectorLhsGep, AllTrueMask, VL}, nullptr, "lhs.load");

Value *VectorRhsGep =
Builder.CreateGEP(LoadType, PtrB, GepOffset, "", GEPB->isInBounds());
Value *VectorRhsLoad = Builder.CreateIntrinsic(
Intrinsic::vp_load, {VectorLoadType, VectorLhsGep->getType()},
{VectorRhsGep, AllTrueMask, VL}, nullptr, "rhs.load");

StringRef PredicateStr = CmpInst::getPredicateName(CmpInst::ICMP_NE);
auto *PredicateMDS = MDString::get(VectorLhsLoad->getContext(), PredicateStr);
Value *Pred = MetadataAsValue::get(VectorLhsLoad->getContext(), PredicateMDS);
Value *VectorMatchCmp = Builder.CreateIntrinsic(
Intrinsic::vp_icmp, {VectorLhsLoad->getType()},
{VectorLhsLoad, VectorRhsLoad, Pred, AllTrueMask, VL}, nullptr,
"mismatch.cmp");
Value *CTZ = Builder.CreateIntrinsic(
Intrinsic::vp_cttz_elts, {ResType, VectorMatchCmp->getType()},
{VectorMatchCmp, /*ZeroIsPoison=*/Builder.getInt1(false), AllTrueMask,
VL});
Value *MismatchFound = Builder.CreateICmpNE(CTZ, VL);
auto *VectorEarlyExit = BranchInst::Create(VectorLoopMismatchBlock,
VectorLoopIncBlock, MismatchFound);
Builder.Insert(VectorEarlyExit);

DTU.applyUpdates(
{{DominatorTree::Insert, VectorLoopStartBlock, VectorLoopMismatchBlock},
{DominatorTree::Insert, VectorLoopStartBlock, VectorLoopIncBlock}});

// Increment the index counter and calculate the predicate for the next
// iteration of the loop. We branch back to the start of the loop if there
// is at least one active lane.
Builder.SetInsertPoint(VectorLoopIncBlock);
Value *VL64 = Builder.CreateZExt(VL, I64Type);
Value *NewVectorIndexPhi =
Builder.CreateAdd(VectorIndexPhi, VL64, "",
/*HasNUW=*/true, /*HasNSW=*/true);
VectorIndexPhi->addIncoming(NewVectorIndexPhi, VectorLoopIncBlock);
Value *ExitCond = Builder.CreateICmpNE(NewVectorIndexPhi, ExtEnd);
auto *VectorLoopBranchBack =
BranchInst::Create(VectorLoopStartBlock, EndBlock, ExitCond);
Builder.Insert(VectorLoopBranchBack);

DTU.applyUpdates(
{{DominatorTree::Insert, VectorLoopIncBlock, VectorLoopStartBlock},
{DominatorTree::Insert, VectorLoopIncBlock, EndBlock}});

// If we found a mismatch then we need to calculate which lane in the vector
// had a mismatch and add that on to the current loop index.
Builder.SetInsertPoint(VectorLoopMismatchBlock);

// Add LCSSA phis for CTZ and VectorIndexPhi.
auto *CTZLCSSAPhi = Builder.CreatePHI(CTZ->getType(), 1, "ctz");
CTZLCSSAPhi->addIncoming(CTZ, VectorLoopStartBlock);
auto *VectorIndexLCSSAPhi =
Builder.CreatePHI(VectorIndexPhi->getType(), 1, "mismatch_vector_index");
VectorIndexLCSSAPhi->addIncoming(VectorIndexPhi, VectorLoopStartBlock);

Value *CTZI64 = Builder.CreateZExt(CTZLCSSAPhi, I64Type);
Value *VectorLoopRes64 = Builder.CreateAdd(VectorIndexLCSSAPhi, CTZI64, "",
/*HasNUW=*/true, /*HasNSW=*/true);
return Builder.CreateTrunc(VectorLoopRes64, ResType);
}

Value *LoopIdiomVectorize::expandFindMismatch(
IRBuilder<> &Builder, DomTreeUpdater &DTU, GetElementPtrInst *GEPA,
GetElementPtrInst *GEPB, Instruction *Index, Value *Start, Value *MaxLen) {
Expand Down Expand Up @@ -613,8 +741,22 @@ Value *LoopIdiomVectorize::expandFindMismatch(
// processed in each iteration, etc.
Builder.SetInsertPoint(VectorLoopPreheaderBlock);

Value *VectorLoopRes =
createMaskedFindMismatch(Builder, DTU, GEPA, GEPB, ExtStart, ExtEnd);
// At this point we know two things must be true:
// 1. Start <= End
// 2. ExtMaxLen <= MinPageSize due to the page checks.
// Therefore, we know that we can use a 64-bit induction variable that
// starts from 0 -> ExtMaxLen and it will not overflow.
Value *VectorLoopRes = nullptr;
switch (VectorizeStyle) {
case LoopIdiomVectorizeStyle::Masked:
VectorLoopRes =
createMaskedFindMismatch(Builder, DTU, GEPA, GEPB, ExtStart, ExtEnd);
break;
case LoopIdiomVectorizeStyle::Predicated:
VectorLoopRes = createPredicatedFindMismatch(Builder, DTU, GEPA, GEPB,
ExtStart, ExtEnd);
break;
}

Builder.Insert(BranchInst::Create(EndBlock));

Expand Down
Loading
Loading