Skip to content

Commit 43bc584

Browse files
author
Simon Moll
committed
[VP,Integer,#2] ExpandVectorPredication pass
This patch implements expansion of llvm.vp.* intrinsics (https://llvm.org/docs/LangRef.html#vector-predication-intrinsics). VP expansion is required for targets that do not implement VP code generation. Since expansion is controllable with TTI, targets can switch on the VP intrinsics they do support in their backend offering a smooth transition strategy for VP code generation (VE, RISC-V V, ARM SVE, AVX512, ..). Reviewed By: rogfer01 Differential Revision: https://reviews.llvm.org/D78203
1 parent 181c492 commit 43bc584

21 files changed

+823
-1
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

+40
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class TargetLibraryInfo;
6161
class Type;
6262
class User;
6363
class Value;
64+
class VPIntrinsic;
6465
struct KnownBits;
6566
template <typename T> class Optional;
6667

@@ -1379,6 +1380,38 @@ class TargetTransformInfo {
13791380
/// Intrinsics") Use of %evl is discouraged when that is not the case.
13801381
bool hasActiveVectorLength() const;
13811382

1383+
struct VPLegalization {
1384+
enum VPTransform {
1385+
// keep the predicating parameter
1386+
Legal = 0,
1387+
// where legal, discard the predicate parameter
1388+
Discard = 1,
1389+
// transform into something else that is also predicating
1390+
Convert = 2
1391+
};
1392+
1393+
// How to transform the EVL parameter.
1394+
// Legal: keep the EVL parameter as it is.
1395+
// Discard: Ignore the EVL parameter where it is safe to do so.
1396+
// Convert: Fold the EVL into the mask parameter.
1397+
VPTransform EVLParamStrategy;
1398+
1399+
// How to transform the operator.
1400+
// Legal: The target supports this operator.
1401+
// Convert: Convert this to a non-VP operation.
1402+
// The 'Discard' strategy is invalid.
1403+
VPTransform OpStrategy;
1404+
1405+
bool shouldDoNothing() const {
1406+
return (EVLParamStrategy == Legal) && (OpStrategy == Legal);
1407+
}
1408+
VPLegalization(VPTransform EVLParamStrategy, VPTransform OpStrategy)
1409+
: EVLParamStrategy(EVLParamStrategy), OpStrategy(OpStrategy) {}
1410+
};
1411+
1412+
/// \returns How the target needs this vector-predicated operation to be
1413+
/// transformed.
1414+
VPLegalization getVPLegalizationStrategy(const VPIntrinsic &PI) const;
13821415
/// @}
13831416

13841417
/// @}
@@ -1688,6 +1721,8 @@ class TargetTransformInfo::Concept {
16881721
virtual bool supportsScalableVectors() const = 0;
16891722
virtual bool hasActiveVectorLength() const = 0;
16901723
virtual InstructionCost getInstructionLatency(const Instruction *I) = 0;
1724+
virtual VPLegalization
1725+
getVPLegalizationStrategy(const VPIntrinsic &PI) const = 0;
16911726
};
16921727

16931728
template <typename T>
@@ -2259,6 +2294,11 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
22592294
InstructionCost getInstructionLatency(const Instruction *I) override {
22602295
return Impl.getInstructionLatency(I);
22612296
}
2297+
2298+
VPLegalization
2299+
getVPLegalizationStrategy(const VPIntrinsic &PI) const override {
2300+
return Impl.getVPLegalizationStrategy(PI);
2301+
}
22622302
};
22632303

22642304
template <typename T>

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

+7
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,13 @@ class TargetTransformInfoImplBase {
750750

751751
bool hasActiveVectorLength() const { return false; }
752752

753+
TargetTransformInfo::VPLegalization
754+
getVPLegalizationStrategy(const VPIntrinsic &PI) const {
755+
return TargetTransformInfo::VPLegalization(
756+
/* EVLParamStrategy */ TargetTransformInfo::VPLegalization::Discard,
757+
/* OperatorStrategy */ TargetTransformInfo::VPLegalization::Convert);
758+
}
759+
753760
protected:
754761
// Obtain the minimum required size to hold the value (without the sign)
755762
// In case of a vector it returns the min required size for one element.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- ExpandVectorPredication.h - Expand vector predication ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_EXPANDVECTORPREDICATION_H
10+
#define LLVM_CODEGEN_EXPANDVECTORPREDICATION_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
class ExpandVectorPredicationPass
17+
: public PassInfoMixin<ExpandVectorPredicationPass> {
18+
public:
19+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
20+
};
21+
} // end namespace llvm
22+
23+
#endif // LLVM_CODEGEN_EXPANDVECTORPREDICATION_H

llvm/include/llvm/CodeGen/MachinePassRegistry.def

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis, (
103103
#define DUMMY_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)
104104
#endif
105105
DUMMY_FUNCTION_PASS("expandmemcmp", ExpandMemCmpPass, ())
106+
DUMMY_FUNCTION_PASS("expandvp", ExpandVectorPredicationPass, ())
106107
DUMMY_FUNCTION_PASS("gc-lowering", GCLoweringPass, ())
107108
DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
108109
DUMMY_FUNCTION_PASS("sjljehprepare", SjLjEHPreparePass, ())

llvm/include/llvm/CodeGen/Passes.h

+5
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ namespace llvm {
453453
// the corresponding function in a vector library (e.g., SVML, libmvec).
454454
FunctionPass *createReplaceWithVeclibLegacyPass();
455455

456+
/// This pass expands the vector predication intrinsics into unpredicated
457+
/// instructions with selects or just the explicit vector length into the
458+
/// predicate mask.
459+
FunctionPass *createExpandVectorPredicationPass();
460+
456461
// This pass expands memcmp() to load/stores.
457462
FunctionPass *createExpandMemCmpPass();
458463

llvm/include/llvm/IR/IntrinsicInst.h

+2
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,11 @@ class VPIntrinsic : public IntrinsicInst {
400400

401401
/// \return the mask parameter or nullptr.
402402
Value *getMaskParam() const;
403+
void setMaskParam(Value *);
403404

404405
/// \return the vector length parameter or nullptr.
405406
Value *getVectorLengthParam() const;
407+
void setVectorLengthParam(Value *);
406408

407409
/// \return whether the vector length param can be ignored.
408410
bool canIgnoreVectorLengthParam() const;

llvm/include/llvm/InitializePasses.h

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ void initializeEntryExitInstrumenterPass(PassRegistry&);
154154
void initializeExpandMemCmpPassPass(PassRegistry&);
155155
void initializeExpandPostRAPass(PassRegistry&);
156156
void initializeExpandReductionsPass(PassRegistry&);
157+
void initializeExpandVectorPredicationPass(PassRegistry &);
157158
void initializeMakeGuardsExplicitLegacyPassPass(PassRegistry&);
158159
void initializeExternalAAWrapperPassPass(PassRegistry&);
159160
void initializeFEntryInserterPass(PassRegistry&);

llvm/include/llvm/LinkAllPasses.h

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ namespace {
197197
(void) llvm::createMergeFunctionsPass();
198198
(void) llvm::createMergeICmpsLegacyPass();
199199
(void) llvm::createExpandMemCmpPass();
200+
(void) llvm::createExpandVectorPredicationPass();
200201
std::string buf;
201202
llvm::raw_string_ostream os(buf);
202203
(void) llvm::createPrintModulePass(os);

llvm/lib/Analysis/TargetTransformInfo.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,11 @@ bool TargetTransformInfo::preferPredicatedReductionSelect(
10261026
return TTIImpl->preferPredicatedReductionSelect(Opcode, Ty, Flags);
10271027
}
10281028

1029+
TargetTransformInfo::VPLegalization
1030+
TargetTransformInfo::getVPLegalizationStrategy(const VPIntrinsic &VPI) const {
1031+
return TTIImpl->getVPLegalizationStrategy(VPI);
1032+
}
1033+
10291034
bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
10301035
return TTIImpl->shouldExpandReduction(II);
10311036
}

llvm/lib/CodeGen/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_llvm_component_library(LLVMCodeGen
2929
ExpandMemCmp.cpp
3030
ExpandPostRAPseudos.cpp
3131
ExpandReductions.cpp
32+
ExpandVectorPredication.cpp
3233
FaultMaps.cpp
3334
FEntryInserter.cpp
3435
FinalizeISel.cpp

0 commit comments

Comments
 (0)