blob: 0db760b17d7c80a5f3a386db89ee7de469a73918 [file] [log] [blame]
Lei Zhang930c74f2020-12-23 19:32:311//===- LinalgToSPIRV.cpp - Linalg to SPIR-V Patterns ----------------------===//
Lei Zhangdf710002020-01-26 16:10:292//
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#include "mlir/Conversion/LinalgToSPIRV/LinalgToSPIRV.h"
10#include "mlir/Dialect/Linalg/IR/LinalgOps.h"
11#include "mlir/Dialect/Linalg/Utils/Utils.h"
Lei Zhang01178652020-12-17 15:55:4512#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
13#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
14#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
Rob Suderman69d757c2020-02-21 19:54:4915#include "mlir/Dialect/StandardOps/IR/Ops.h"
Lei Zhangdf710002020-01-26 16:10:2916#include "mlir/Dialect/Utils/StructuredOpsUtils.h"
17#include "mlir/IR/AffineExpr.h"
Lei Zhang7c3ae482021-01-09 13:04:4918#include "mlir/Transforms/DialectConversion.h"
Lei Zhangdf710002020-01-26 16:10:2919
20using namespace mlir;
21
22//===----------------------------------------------------------------------===//
23// Utilities
24//===----------------------------------------------------------------------===//
25
26/// Returns a `Value` containing the `dim`-th dimension's size of SPIR-V
27/// location invocation ID. This function will create necessary operations with
28/// `builder` at the proper region containing `op`.
29static Value getLocalInvocationDimSize(Operation *op, int dim, Location loc,
30 OpBuilder *builder) {
31 assert(dim >= 0 && dim < 3 && "local invocation only has three dimensions");
32 Value invocation = spirv::getBuiltinVariableValue(
33 op, spirv::BuiltIn::LocalInvocationId, *builder);
34 Type xType = invocation.getType().cast<ShapedType>().getElementType();
35 return builder->create<spirv::CompositeExtractOp>(
36 loc, xType, invocation, builder->getI32ArrayAttr({dim}));
37}
38
Lei Zhangdf710002020-01-26 16:10:2939//===----------------------------------------------------------------------===//
40// Reduction (single workgroup)
41//===----------------------------------------------------------------------===//
42
43namespace {
44
45/// A pattern to convert a linalg.generic op to SPIR-V ops under the condition
46/// that the linalg.generic op is performing reduction with a workload size that
47/// can fit in one workgroup.
Lei Zhang7c3ae482021-01-09 13:04:4948struct SingleWorkgroupReduction final
49 : public OpConversionPattern<linalg::GenericOp> {
50 using OpConversionPattern::OpConversionPattern;
Lei Zhangdf710002020-01-26 16:10:2951
52 /// Matches the given linalg.generic op as performing reduction and returns
53 /// the binary op kind if successful.
54 static Optional<linalg::RegionMatcher::BinaryOpKind>
55 matchAsPerformingReduction(linalg::GenericOp genericOp);
56
River Riddle31454272020-03-18 03:07:5557 LogicalResult
Lei Zhangdf710002020-01-26 16:10:2958 matchAndRewrite(linalg::GenericOp genericOp, ArrayRef<Value> operands,
59 ConversionPatternRewriter &rewriter) const override;
60};
61
62} // namespace
63
64Optional<linalg::RegionMatcher::BinaryOpKind>
65SingleWorkgroupReduction::matchAsPerformingReduction(
66 linalg::GenericOp genericOp) {
67 Operation *op = genericOp.getOperation();
68
69 // Make sure the linalg.generic is working on memrefs.
70 if (!genericOp.hasBufferSemantics())
71 return llvm::None;
72
Kazuaki Ishizakie5a85122020-03-26 18:51:3773 // Make sure this is reduction with one input and one output.
Nicolas Vasilacheed229132020-09-21 19:30:4274 if (genericOp.getNumInputs() != 1 || genericOp.getNumOutputs() != 1)
Lei Zhangdf710002020-01-26 16:10:2975 return llvm::None;
76
77 auto originalInputType = op->getOperand(0).getType().cast<MemRefType>();
78 auto originalOutputType = op->getOperand(1).getType().cast<MemRefType>();
79
80 // Make sure the original input has one dimension.
81 if (!originalInputType.hasStaticShape() || originalInputType.getRank() != 1)
82 return llvm::None;
83 // Make sure the original output has one element.
84 if (!originalOutputType.hasStaticShape() ||
85 originalOutputType.getNumElements() != 1)
86 return llvm::None;
87
88 if (!genericOp.hasSingleReductionLoop())
89 return llvm::None;
90
91 if (genericOp.indexing_maps().getValue().size() != 2)
92 return llvm::None;
93
River Riddle9db53a12020-07-07 08:35:2394 // TODO: create utility functions for these checks in Linalg
Lei Zhangdf710002020-01-26 16:10:2995 // and use them.
96 auto inputMap = genericOp.indexing_maps().getValue()[0].cast<AffineMapAttr>();
97 auto outputMap =
98 genericOp.indexing_maps().getValue()[1].cast<AffineMapAttr>();
99 // The indexing map for the input should be `(i) -> (i)`.
100 if (inputMap.getValue() !=
Jeremy Bruestle9f3ab922020-04-15 18:12:47101 AffineMap::get(1, 0, getAffineDimExpr(0, op->getContext())))
Lei Zhangdf710002020-01-26 16:10:29102 return llvm::None;
103 // The indexing map for the input should be `(i) -> (0)`.
104 if (outputMap.getValue() !=
Jeremy Bruestle9f3ab922020-04-15 18:12:47105 AffineMap::get(1, 0, getAffineConstantExpr(0, op->getContext())))
Lei Zhangdf710002020-01-26 16:10:29106 return llvm::None;
107
108 return linalg::RegionMatcher::matchAsScalarBinaryOp(genericOp);
109}
110
River Riddle31454272020-03-18 03:07:55111LogicalResult SingleWorkgroupReduction::matchAndRewrite(
Lei Zhangdf710002020-01-26 16:10:29112 linalg::GenericOp genericOp, ArrayRef<Value> operands,
113 ConversionPatternRewriter &rewriter) const {
114 Operation *op = genericOp.getOperation();
115 auto originalInputType = op->getOperand(0).getType().cast<MemRefType>();
116 auto originalOutputType = op->getOperand(1).getType().cast<MemRefType>();
117
118 auto binaryOpKind = matchAsPerformingReduction(genericOp);
119 if (!binaryOpKind)
River Riddle31454272020-03-18 03:07:55120 return failure();
Lei Zhangdf710002020-01-26 16:10:29121
122 // Query the shader interface for local workgroup size to make sure the
123 // invocation configuration fits with the input memref's shape.
124 DenseIntElementsAttr localSize = spirv::lookupLocalWorkGroupSize(genericOp);
125 if (!localSize)
River Riddle31454272020-03-18 03:07:55126 return failure();
Lei Zhangdf710002020-01-26 16:10:29127
128 if ((*localSize.begin()).getSExtValue() != originalInputType.getDimSize(0))
River Riddle31454272020-03-18 03:07:55129 return failure();
Lei Zhangdf710002020-01-26 16:10:29130 if (llvm::any_of(llvm::drop_begin(localSize.getIntValues(), 1),
131 [](const APInt &size) { return !size.isOneValue(); }))
River Riddle31454272020-03-18 03:07:55132 return failure();
Lei Zhangdf710002020-01-26 16:10:29133
River Riddle9db53a12020-07-07 08:35:23134 // TODO: Query the target environment to make sure the current
Lei Zhangdf710002020-01-26 16:10:29135 // workload fits in a local workgroup.
136
137 Value convertedInput = operands[0], convertedOutput = operands[1];
138 Location loc = genericOp.getLoc();
139
140 // Get the invocation ID.
141 Value x = getLocalInvocationDimSize(genericOp, /*dim=*/0, loc, &rewriter);
142
River Riddle9db53a12020-07-07 08:35:23143 // TODO: Load to Workgroup storage class first.
Lei Zhangdf710002020-01-26 16:10:29144
Lei Zhang7c3ae482021-01-09 13:04:49145 auto *typeConverter = getTypeConverter<SPIRVTypeConverter>();
146
Lei Zhangdf710002020-01-26 16:10:29147 // Get the input element accessed by this invocation.
148 Value inputElementPtr = spirv::getElementPtr(
Lei Zhang7c3ae482021-01-09 13:04:49149 *typeConverter, originalInputType, convertedInput, {x}, loc, rewriter);
Lei Zhangdf710002020-01-26 16:10:29150 Value inputElement = rewriter.create<spirv::LoadOp>(loc, inputElementPtr);
151
152 // Perform the group reduction operation.
153 Value groupOperation;
154#define CREATE_GROUP_NON_UNIFORM_BIN_OP(opKind, spvOp) \
155 case linalg::RegionMatcher::BinaryOpKind::opKind: { \
156 groupOperation = rewriter.create<spirv::spvOp>( \
157 loc, originalInputType.getElementType(), spirv::Scope::Subgroup, \
158 spirv::GroupOperation::Reduce, inputElement, \
Lei Zhanga9cb5292020-04-13 19:33:35159 /*cluster_size=*/nullptr); \
Lei Zhangdf710002020-01-26 16:10:29160 } break
161 switch (*binaryOpKind) {
162 CREATE_GROUP_NON_UNIFORM_BIN_OP(IAdd, GroupNonUniformIAddOp);
163 }
164#undef CREATE_GROUP_NON_UNIFORM_BIN_OP
165
166 // Get the output element accessed by this reduction.
167 Value zero = spirv::ConstantOp::getZero(
Lei Zhang7c3ae482021-01-09 13:04:49168 typeConverter->getIndexType(rewriter.getContext()), loc, rewriter);
Lei Zhangdf710002020-01-26 16:10:29169 SmallVector<Value, 1> zeroIndices(originalOutputType.getRank(), zero);
170 Value outputElementPtr =
Lei Zhang7c3ae482021-01-09 13:04:49171 spirv::getElementPtr(*typeConverter, originalOutputType, convertedOutput,
Lei Zhangdf710002020-01-26 16:10:29172 zeroIndices, loc, rewriter);
173
174 // Write out the final reduction result. This should be only conducted by one
175 // invocation. We use spv.GroupNonUniformElect to find the invocation with the
176 // lowest ID.
177 //
178 // ```
179 // if (spv.GroupNonUniformElect) { output = ... }
180 // ```
181
182 Value condition = rewriter.create<spirv::GroupNonUniformElectOp>(
183 loc, spirv::Scope::Subgroup);
184
Alex Zinenkobb1d9762020-04-23 14:02:46185 auto createAtomicOp = [&](OpBuilder &builder) {
Lei Zhangdf710002020-01-26 16:10:29186#define CREATE_ATOMIC_BIN_OP(opKind, spvOp) \
187 case linalg::RegionMatcher::BinaryOpKind::opKind: { \
Alex Zinenkobb1d9762020-04-23 14:02:46188 builder.create<spirv::spvOp>(loc, outputElementPtr, spirv::Scope::Device, \
189 spirv::MemorySemantics::AcquireRelease, \
190 groupOperation); \
Lei Zhangdf710002020-01-26 16:10:29191 } break
192 switch (*binaryOpKind) { CREATE_ATOMIC_BIN_OP(IAdd, AtomicIAddOp); }
193#undef CREATE_ATOMIC_BIN_OP
194 };
195
Alex Zinenkobb1d9762020-04-23 14:02:46196 spirv::SelectionOp::createIfThen(loc, condition, createAtomicOp, rewriter);
Lei Zhangdf710002020-01-26 16:10:29197
198 rewriter.eraseOp(genericOp);
River Riddle31454272020-03-18 03:07:55199 return success();
Lei Zhangdf710002020-01-26 16:10:29200}
201
202//===----------------------------------------------------------------------===//
203// Pattern population
204//===----------------------------------------------------------------------===//
205
206void mlir::populateLinalgToSPIRVPatterns(MLIRContext *context,
207 SPIRVTypeConverter &typeConverter,
208 OwningRewritePatternList &patterns) {
Lei Zhang7c3ae482021-01-09 13:04:49209 patterns.insert<SingleWorkgroupReduction>(typeConverter, context);
Lei Zhangdf710002020-01-26 16:10:29210}