19#include "llvm/Support/FormatVariadic.h"
25#define GEN_PASS_DEF_CONVERTMATHTOXEVM
26#include "mlir/Conversion/Passes.h.inc"
31#define DEBUG_TYPE "math-to-xevm"
34 auto vecType = dyn_cast<VectorType>(type);
35 return vecType && vecType.getShape().size() == 1 &&
36 vecType.getShape()[0] == 1 && vecType.getElementType().isFloat();
42 if (
auto vecType = dyn_cast<VectorType>(type)) {
43 if (!vecType.getElementType().isFloat())
48 if (
shape.size() != 1)
72 std::string mangledFuncName =
75 auto appendFloatToMangledFunc = [&mangledFuncName](
Type type) {
77 mangledFuncName +=
"f";
78 else if (type.isF16())
79 mangledFuncName +=
"Dh";
80 else if (type.isF64())
81 mangledFuncName +=
"d";
84 for (
auto type : operandTypes) {
85 if (
auto vecType = dyn_cast<VectorType>(type)) {
86 mangledFuncName +=
"Dv" + std::to_string(vecType.getShape()[0]) +
"_";
87 appendFloatToMangledFunc(vecType.getElementType());
89 appendFloatToMangledFunc(type);
92 return mangledFuncName;
97 ConversionPatternRewriter &rewriter)
const override {
101 arith::FastMathFlags fastFlags = op.getFastmath();
102 if (!arith::bitEnumContainsAll(fastFlags, arith::FastMathFlags::afn))
103 return rewriter.notifyMatchFailure(op,
"not a fastmath `afn` operation");
114 for (
Value &operand : operands) {
115 Type opTy = operand.getType();
120 return rewriter.notifyMatchFailure(
121 op, llvm::formatv(
"incompatible operand type: '{0}'", opTy));
122 if (unwrapSizeOneVec) {
124 "expected all operands to be size-1 vectors");
125 opTy = cast<VectorType>(opTy).getElementType();
126 operand = vector::ExtractOp::create(rewriter, loc, operand,
129 operandTypes.push_back(opTy);
132 Type resultType = unwrapSizeOneVec
133 ? cast<VectorType>(op.getType()).getElementType()
136 auto moduleOp = op->template getParentWithTrait<OpTrait::SymbolTable>();
139 operandTypes, resultType);
140 assert(!failed(funcOpRes));
141 LLVM::LLVMFuncOp funcOp = funcOpRes.value();
143 auto callOp = LLVM::CallOp::create(rewriter, loc, funcOp, operands);
148 callOp.setFastmathFlagsAttr(
152 if (unwrapSizeOneVec) {
154 rewriter.replaceOpWithNewOp<vector::BroadcastOp>(op, op.getType(),
157 rewriter.replaceOp(op, callOp);
165template <
typename OpTy>
170 std::string prefix =
"__spirv_ocl_";
171 std::string mangledName =
"_Z" +
172 std::to_string(prefix.size() + opName.size()) +
173 prefix + opName.str();
177 converter, mangledName +
"f", mangledName +
"d",
179 "", benefit, LLVM::cconv::CConv::SPIR_FUNC);
239 patterns.
getContext(),
"__spirv_ocl_native_exp", benefit);
241 patterns.
getContext(),
"__spirv_ocl_native_cos", benefit);
243 patterns.
getContext(),
"__spirv_ocl_native_exp2", benefit);
245 patterns.
getContext(),
"__spirv_ocl_native_log", benefit);
247 patterns.
getContext(),
"__spirv_ocl_native_log2", benefit);
249 patterns.
getContext(),
"__spirv_ocl_native_log10", benefit);
251 patterns.
getContext(),
"__spirv_ocl_native_powr", benefit);
253 patterns.
getContext(),
"__spirv_ocl_native_rsqrt", benefit);
255 patterns.
getContext(),
"__spirv_ocl_native_sin", benefit);
257 patterns.
getContext(),
"__spirv_ocl_native_sqrt", benefit);
259 patterns.
getContext(),
"__spirv_ocl_native_tan", benefit);
262 patterns.
getContext(),
"__spirv_ocl_native_divide", benefit);
266struct ConvertMathToXeVMPass
267 :
public impl::ConvertMathToXeVMBase<ConvertMathToXeVMPass> {
269 void runOnOperation()
override;
273void ConvertMathToXeVMPass::runOnOperation() {
274 Operation *op = getOperation();
277 const auto &dl = getAnalysis<DataLayoutAnalysis>();
280 LowerToLLVMOptions
options(ctx, dl.getAtOrAbove(op));
281 LLVMTypeConverter converter(ctx,
options);
291 .addIllegalOp<LLVM::CosOp, LLVM::ExpOp, LLVM::Exp2Op, LLVM::LogOp,
292 LLVM::Log10Op, LLVM::Log2Op, LLVM::SinOp, LLVM::SqrtOp>();
294 target.addLegalDialect<BuiltinDialect, LLVM::LLVMDialect>();
298 target.addLegalOp<vector::ExtractOp, vector::BroadcastOp>();
300 applyPartialConversion(getOperation(),
target, std::move(patterns))))
static bool isSizeOneVector(Type type)
static bool isSPIRVCompatibleFloatOrVec(Type type)
static void populateOCLExtSetOpPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns, PatternBenefit benefit, StringRef opName)
static llvm::ManagedStatic< PassManagerOptions > options
Conversion from types to the LLVM IR dialect.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
Location getLoc()
The source location the operation was defined or derived from.
This provides public APIs that all operations should have.
MLIRContext * getContext()
Return the context this operation is associated with.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
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.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isFloat() const
Return true if this is an float type (with the specified width).
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
TargetOp::Properties getProperties() const
ArrayRef< NamedAttribute > getDiscardableAttrs() const
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateFn(OpBuilder &b, Operation *moduleOp, StringRef name, ArrayRef< Type > paramTypes={}, Type resultType={}, bool isVarArg=false, bool isReserved=false, SymbolTableCollection *symbolTables=nullptr)
Create a FuncOp with signature resultType(paramTypes) and name name`.
Include the generated interface declarations.
void populateMathToScalarOCLExtSetConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns, PatternBenefit benefit=1)
Populate the given list with patterns that convert from Math to OCL LLVM-SPV builtin calls.
void populateMathToXeVMConversionPatterns(RewritePatternSet &patterns, bool convertArith, PatternBenefit benefit=1)
Populate the given list with patterns that convert from Math to XeVM calls.
Convert math ops marked with fast (afn) to native OpenCL intrinsics.
const StringRef nativeFunc
ConvertNativeFuncPattern(MLIRContext *context, StringRef nativeFunc, PatternBenefit benefit=1)
std::string getMangledNativeFuncName(const ArrayRef< Type > operandTypes) const
LogicalResult matchAndRewrite(Op op, typename Op::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override
Rewriting that replaces SourceOp with a CallOp to f32Func or f64Func or f32ApproxFunc or f16Func or i...
Unrolls SourceOp to array/vector elements.