1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
//===-- MathToXeVM.cpp - conversion from Math to XeVM ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Conversion/MathToXeVM/MathToXeVM.h"
#include "mlir/Conversion/ArithCommon/AttrToLLVMConverter.h"
#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/Pass/Pass.h"
#include "llvm/Support/FormatVariadic.h"
namespace mlir {
#define GEN_PASS_DEF_CONVERTMATHTOXEVM
#include "mlir/Conversion/Passes.h.inc"
} // namespace mlir
using namespace mlir;
#define DEBUG_TYPE "math-to-xevm"
/// Convert math ops marked with `fast` (`afn`) to native OpenCL intrinsics.
template <typename Op>
struct ConvertNativeFuncPattern final : public OpConversionPattern<Op> {
ConvertNativeFuncPattern(MLIRContext *context, StringRef nativeFunc,
PatternBenefit benefit = 1)
: OpConversionPattern<Op>(context, benefit), nativeFunc(nativeFunc) {}
LogicalResult
matchAndRewrite(Op op, typename Op::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (!isSPIRVCompatibleFloatOrVec(op.getType()))
return failure();
arith::FastMathFlags fastFlags = op.getFastmath();
if (!arith::bitEnumContainsAll(fastFlags, arith::FastMathFlags::afn))
return rewriter.notifyMatchFailure(op, "not a fastmath `afn` operation");
SmallVector<Type, 1> operandTypes;
for (auto operand : adaptor.getOperands()) {
Type opTy = operand.getType();
// This pass only supports operations on vectors that are already in SPIRV
// supported vector sizes: Distributing unsupported vector sizes to SPIRV
// supported vector sizes are done in other blocking optimization passes.
if (!isSPIRVCompatibleFloatOrVec(opTy))
return rewriter.notifyMatchFailure(
op, llvm::formatv("incompatible operand type: '{0}'", opTy));
operandTypes.push_back(opTy);
}
auto moduleOp = op->template getParentWithTrait<OpTrait::SymbolTable>();
auto funcOpRes = LLVM::lookupOrCreateFn(
rewriter, moduleOp, getMangledNativeFuncName(operandTypes),
operandTypes, op.getType());
assert(!failed(funcOpRes));
LLVM::LLVMFuncOp funcOp = funcOpRes.value();
auto callOp = rewriter.replaceOpWithNewOp<LLVM::CallOp>(
op, funcOp, adaptor.getOperands());
// Preserve fastmath flags in our MLIR op when converting to llvm function
// calls, in order to allow further fastmath optimizations: We thus need to
// convert arith fastmath attrs into attrs recognized by llvm.
arith::AttrConvertFastMathToLLVM<Op, LLVM::CallOp> fastAttrConverter(op);
mlir::NamedAttribute fastAttr = fastAttrConverter.getAttrs()[0];
callOp->setAttr(fastAttr.getName(), fastAttr.getValue());
return success();
}
inline bool isSPIRVCompatibleFloatOrVec(Type type) const {
if (type.isFloat())
return true;
if (auto vecType = dyn_cast<VectorType>(type)) {
if (!vecType.getElementType().isFloat())
return false;
// SPIRV distinguishes between vectors and matrices: OpenCL native math
// intrsinics are not compatible with matrices.
ArrayRef<int64_t> shape = vecType.getShape();
if (shape.size() != 1)
return false;
// SPIRV only allows vectors of size 2, 3, 4, 8, 16.
if (shape[0] == 2 || shape[0] == 3 || shape[0] == 4 || shape[0] == 8 ||
shape[0] == 16)
return true;
}
return false;
}
inline std::string
getMangledNativeFuncName(const ArrayRef<Type> operandTypes) const {
std::string mangledFuncName =
"_Z" + std::to_string(nativeFunc.size()) + nativeFunc.str();
auto appendFloatToMangledFunc = [&mangledFuncName](Type type) {
if (type.isF32())
mangledFuncName += "f";
else if (type.isF16())
mangledFuncName += "Dh";
else if (type.isF64())
mangledFuncName += "d";
};
for (auto type : operandTypes) {
if (auto vecType = dyn_cast<VectorType>(type)) {
mangledFuncName += "Dv" + std::to_string(vecType.getShape()[0]) + "_";
appendFloatToMangledFunc(vecType.getElementType());
} else
appendFloatToMangledFunc(type);
}
return mangledFuncName;
}
const StringRef nativeFunc;
};
void mlir::populateMathToXeVMConversionPatterns(RewritePatternSet &patterns,
bool convertArith) {
patterns.add<ConvertNativeFuncPattern<math::ExpOp>>(patterns.getContext(),
"__spirv_ocl_native_exp");
patterns.add<ConvertNativeFuncPattern<math::CosOp>>(patterns.getContext(),
"__spirv_ocl_native_cos");
patterns.add<ConvertNativeFuncPattern<math::Exp2Op>>(
patterns.getContext(), "__spirv_ocl_native_exp2");
patterns.add<ConvertNativeFuncPattern<math::LogOp>>(patterns.getContext(),
"__spirv_ocl_native_log");
patterns.add<ConvertNativeFuncPattern<math::Log2Op>>(
patterns.getContext(), "__spirv_ocl_native_log2");
patterns.add<ConvertNativeFuncPattern<math::Log10Op>>(
patterns.getContext(), "__spirv_ocl_native_log10");
patterns.add<ConvertNativeFuncPattern<math::PowFOp>>(
patterns.getContext(), "__spirv_ocl_native_powr");
patterns.add<ConvertNativeFuncPattern<math::RsqrtOp>>(
patterns.getContext(), "__spirv_ocl_native_rsqrt");
patterns.add<ConvertNativeFuncPattern<math::SinOp>>(patterns.getContext(),
"__spirv_ocl_native_sin");
patterns.add<ConvertNativeFuncPattern<math::SqrtOp>>(
patterns.getContext(), "__spirv_ocl_native_sqrt");
patterns.add<ConvertNativeFuncPattern<math::TanOp>>(patterns.getContext(),
"__spirv_ocl_native_tan");
if (convertArith)
patterns.add<ConvertNativeFuncPattern<arith::DivFOp>>(
patterns.getContext(), "__spirv_ocl_native_divide");
}
namespace {
struct ConvertMathToXeVMPass
: public impl::ConvertMathToXeVMBase<ConvertMathToXeVMPass> {
using Base::Base;
void runOnOperation() override;
};
} // namespace
void ConvertMathToXeVMPass::runOnOperation() {
RewritePatternSet patterns(&getContext());
populateMathToXeVMConversionPatterns(patterns, convertArith);
ConversionTarget target(getContext());
target.addLegalDialect<BuiltinDialect, LLVM::LLVMDialect>();
if (failed(
applyPartialConversion(getOperation(), target, std::move(patterns))))
signalPassFailure();
}
|