23#include "llvm/Support/FormatVariadic.h"
29static FailureOr<Value>
30createSubgroupDPPReduction(
PatternRewriter &rewriter, gpu::SubgroupReduceOp op,
31 Value input, gpu::AllReduceOperation mode,
37 constexpr int allRows = 0xf;
38 constexpr int allBanks = 0xf;
39 const bool boundCtrl =
true;
42 dpp = amdgpu::DPPOp::create(
43 rewriter, loc, res.getType(), res, res, amdgpu::DPPPerm::quad_perm,
51 dpp = amdgpu::DPPOp::create(
52 rewriter, loc, res.getType(), res, res, amdgpu::DPPPerm::quad_perm,
60 dpp = amdgpu::DPPOp::create(rewriter, loc, res.getType(), res, res,
61 amdgpu::DPPPerm::row_half_mirror,
70 dpp = amdgpu::DPPOp::create(
71 rewriter, loc, res.getType(), res, res, amdgpu::DPPPerm::row_mirror,
72 rewriter.
getUnitAttr(), allRows, allBanks, boundCtrl);
77 if (!
target.has(llvm::AMDGPU::FEAT_GFX10_INSTS)) {
80 dpp = amdgpu::DPPOp::create(rewriter, loc, res.getType(), res, res,
81 amdgpu::DPPPerm::row_bcast_15,
108 amdgpu::SwizzleBitModeOp::create(rewriter, loc, res, 0,
112 }
else if (!
target.has(llvm::AMDGPU::FEAT_GFX13_INSTS)) {
114 Value uint32Max = arith::ConstantOp::create(
116 dpp = ROCDL::PermlaneX16Op::create(rewriter, loc, res.getType(), res, res,
117 uint32Max, uint32Max,
124 op,
"Subgroup reduce lowering to DPP not currently supported for "
128 Value lane31 = arith::ConstantOp::create(
131 ROCDL::ReadlaneOp::create(rewriter, loc, res.getType(), res, lane31);
135 if (!
target.has(llvm::AMDGPU::FEAT_GFX10_INSTS)) {
137 dpp = amdgpu::DPPOp::create(rewriter, loc, res.getType(), res, res,
138 amdgpu::DPPPerm::row_bcast_31,
144 Value lane63 = arith::ConstantOp::create(
147 ROCDL::ReadlaneOp::create(rewriter, loc, res.getType(), res, lane63);
149 }
else if (!
target.has(llvm::AMDGPU::FEAT_GFX13_INSTS)) {
153 Value lane31 = arith::ConstantOp::create(
155 Value lane63 = arith::ConstantOp::create(
158 ROCDL::ReadlaneOp::create(rewriter, loc, res.getType(), res, lane31);
160 ROCDL::ReadlaneOp::create(rewriter, loc, res.getType(), res, lane63);
165 op,
"Subgroup reduce lowering to DPP not currently supported for "
169 assert(res.getType() == input.
getType());
176struct ScalarSubgroupReduceToDPP final
178 ScalarSubgroupReduceToDPP(MLIRContext *ctx,
unsigned subgroupSize,
180 const ROCDL::TargetInfo &target,
181 PatternBenefit benefit)
182 : OpRewritePattern(ctx, benefit), subgroupSize(subgroupSize),
183 matchClustered(matchClustered), target(target) {}
185 LogicalResult matchAndRewrite(gpu::SubgroupReduceOp op,
186 PatternRewriter &rewriter)
const override {
187 if (op.getClusterSize().has_value() != matchClustered) {
189 op, llvm::formatv(
"op is {0}clustered but pattern is configured to "
190 "only match {1}clustered ops",
191 matchClustered ?
"non-" :
"",
192 matchClustered ?
"" :
"non-"));
200 op,
"Subgroup reductions using DPP are currently only available for "
201 "clusters of contiguous lanes.");
203 Type valueTy = op.getType();
206 op,
"Value type is not a compatible scalar.");
208 FailureOr<Value> dpp = createSubgroupDPPReduction(
209 rewriter, op, op.getValue(), op.getOp(), *ci, target);
218 unsigned subgroupSize = 0;
219 bool matchClustered =
false;
220 ROCDL::TargetInfo target;
227 patterns.
add<ScalarSubgroupReduceToDPP>(patterns.
getContext(), subgroupSize,
235 patterns.
add<ScalarSubgroupReduceToDPP>(patterns.
getContext(), subgroupSize,
IntegerAttr getI32IntegerAttr(int32_t value)
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Describes the AMDGPU target a lowering is producing code for: the triple's subarch (which identifies ...
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
FailureOr< SubgroupReduceClusterInfo > getSubgroupReduceClusterInfo(SubgroupReduceOp op, unsigned subgroupSize)
Returns the cluster layout of op in a subgroup of subgroupSize lanes, which must be a power of two.
vector::CombiningKind convertReductionKind(gpu::AllReduceOperation mode)
Returns the matching vector combining kind.
Value makeArithReduction(OpBuilder &b, Location loc, CombiningKind kind, Value v1, Value acc, arith::FastMathFlagsAttr fastmath=nullptr, Value mask=nullptr)
Returns the result value of reducing two scalar/vector values with the corresponding arith operation.
Include the generated interface declarations.
void populateGpuLowerClusteredSubgroupReduceToDPPPatterns(RewritePatternSet &patterns, unsigned subgroupSize, const ROCDL::TargetInfo &target, PatternBenefit benefit=1)
Disjoint counterpart of populateGpuLowerSubgroupReduceToDPPPatterns that only matches gpu....
void populateGpuLowerSubgroupReduceToDPPPatterns(RewritePatternSet &patterns, unsigned subgroupSize, const ROCDL::TargetInfo &target, PatternBenefit benefit=1)
Collect a set of patterns to lower gpu.subgroup_reduce into amdgpu.dpp ops over scalar types.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Lane layout of the clusters reduced by a gpu.subgroup_reduce op.