MLIR 24.0.0git
OpToFuncCallLowering.h
Go to the documentation of this file.
1//===- OpToFuncCallLowering.h - GPU ops lowering to custom calls *- 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#ifndef MLIR_CONVERSION_GPUCOMMON_OPTOFUNCCALLLOWERING_H_
9#define MLIR_CONVERSION_GPUCOMMON_OPTOFUNCCALLLOWERING_H_
10
19#include "mlir/IR/Builders.h"
20
21namespace mlir {
22
23namespace {
24/// Detection trait tor the `getFastmath` instance method.
25template <typename T>
26using has_get_fastmath_t = decltype(std::declval<T>().getFastmath());
27} // namespace
28
29/// Rewriting that replaces SourceOp with a CallOp to `f32Func` or `f64Func` or
30/// `f32ApproxFunc` or `f16Func` or `i32Type` depending on the element type and
31/// the fastMathFlag of that Op, if present. The function declaration is added
32/// in case it was not added before.
33///
34/// If the input values are of bf16 type (or f16 type if f16Func is empty), the
35/// value is first casted to f32, the function called and then the result casted
36/// back.
37///
38/// Example with NVVM:
39/// %exp_f32 = math.exp %arg_f32 : f32
40///
41/// will be transformed into
42/// llvm.call @__nv_expf(%arg_f32) : (f32) -> f32
43///
44/// If the fastMathFlag attribute of SourceOp is `afn` or `fast`, this Op lowers
45/// to the approximate calculation function.
46///
47/// Also example with NVVM:
48/// %exp_f32 = math.exp %arg_f32 fastmath<afn> : f32
49///
50/// will be transformed into
51/// llvm.call @__nv_fast_expf(%arg_f32) : (f32) -> f32
52///
53/// Final example with NVVM:
54/// %pow_f32 = math.fpowi %arg_f32, %arg_i32
55///
56/// will be transformed into
57/// llvm.call @__nv_powif(%arg_f32, %arg_i32) : (f32, i32) -> f32
58template <typename SourceOp>
60public:
62 const LLVMTypeConverter &lowering, StringRef f32Func, StringRef f64Func,
63 StringRef f32ApproxFunc, StringRef f16Func, StringRef i32Func = "",
64 PatternBenefit benefit = 1,
65 LLVM::cconv::CConv cconv = LLVM::cconv::CConv::C)
66 : ConvertOpToLLVMPattern<SourceOp>(lowering, benefit), f32Func(f32Func),
69
70 LogicalResult
71 matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor,
72 ConversionPatternRewriter &rewriter) const override {
73 using LLVM::LLVMFuncOp;
74
75 static_assert(
76 std::is_base_of<OpTrait::OneResult<SourceOp>, SourceOp>::value,
77 "expected single result op");
78
79 // This pattern only handles scalar ops. Ops with shaped (e.g. vector)
80 // result types, such as `math.isinf` on `vector<Nxf32>`, are expected to be
81 // scalarized first by `ScalarizeVectorOpLowering`, which is co-registered
82 // for these ops; bail out so that pattern can take over.
83 Type opResultType = op->getResultTypes().front();
84 if (!opResultType.isIntOrIndexOrFloat())
85 return rewriter.notifyMatchFailure(op, "expected scalar result type");
86
87 bool isResultBool = opResultType.isInteger(1);
88 if constexpr (!std::is_base_of<OpTrait::SameOperandsAndResultType<SourceOp>,
89 SourceOp>::value) {
90 assert(op->getNumOperands() > 0 &&
91 "expected op to take at least one operand");
92 assert((op->getResultTypes().front() == op->getOperand(0).getType() ||
93 isResultBool) &&
94 "expected op with same operand and result types");
95 }
96
97 if (!op->template getParentOfType<FunctionOpInterface>()) {
98 return rewriter.notifyMatchFailure(
99 op, "expected op to be within a function region");
100 }
101
102 SmallVector<Value, 1> castedOperands;
103 for (Value operand : adaptor.getOperands())
104 castedOperands.push_back(maybeCast(operand, rewriter));
105
106 Type castedOperandType = castedOperands.front().getType();
107
108 // At ABI level, booleans are treated as i32.
109 Type resultType =
110 isResultBool ? rewriter.getIntegerType(32) : castedOperandType;
111 Type funcType = getFunctionType(resultType, castedOperands);
112 StringRef funcName = getFunctionName(castedOperandType, op);
113 if (funcName.empty())
114 return failure();
115
116 LLVMFuncOp funcOp = appendOrGetFuncOp(funcName, funcType, op);
117 auto callOp =
118 LLVM::CallOp::create(rewriter, op->getLoc(), funcOp, castedOperands);
119 callOp.setCConv(cconv);
120
121 if (resultType == adaptor.getOperands().front().getType()) {
122 rewriter.replaceOp(op, {callOp.getResult()});
123 return success();
124 }
125
126 // Boolean result are mapping to i32 at the ABI level with zero values being
127 // interpreted as false and non-zero values being interpreted as true. Since
128 // there is no guarantee of a specific value being used to indicate true,
129 // compare for inequality with zero (rather than truncate or shift).
130 if (isResultBool) {
131 Value zero = LLVM::ConstantOp::create(rewriter, op->getLoc(),
132 rewriter.getIntegerType(32),
133 rewriter.getI32IntegerAttr(0));
134 Value truncated =
135 LLVM::ICmpOp::create(rewriter, op->getLoc(), LLVM::ICmpPredicate::ne,
136 callOp.getResult(), zero);
137 rewriter.replaceOp(op, {truncated});
138 return success();
139 }
140
141 assert(callOp.getResult().getType().isF32() &&
142 "only f32 types are supposed to be truncated back");
143 Value truncated = LLVM::FPTruncOp::create(
144 rewriter, op->getLoc(), adaptor.getOperands().front().getType(),
145 callOp.getResult());
146 rewriter.replaceOp(op, {truncated});
147 return success();
148 }
149
150 Value maybeCast(Value operand, PatternRewriter &rewriter) const {
151 Type type = operand.getType();
152 if (!isa<Float16Type, BFloat16Type>(type))
153 return operand;
154
155 // If there's an f16 function, no need to cast f16 values.
156 if (!f16Func.empty() && isa<Float16Type>(type))
157 return operand;
158
159 return LLVM::FPExtOp::create(rewriter, operand.getLoc(),
160 Float32Type::get(rewriter.getContext()),
161 operand);
162 }
163
164 Type getFunctionType(Type resultType, ValueRange operands) const {
165 SmallVector<Type> operandTypes(operands.getTypes());
166 return LLVM::LLVMFunctionType::get(resultType, operandTypes);
167 }
168
169 LLVM::LLVMFuncOp appendOrGetFuncOp(StringRef funcName, Type funcType,
170 Operation *op) const {
171 using LLVM::LLVMFuncOp;
172
173 auto funcAttr = StringAttr::get(op->getContext(), funcName);
174 auto funcOp =
176 if (funcOp)
177 return funcOp;
178
179 auto parentFunc = op->getParentOfType<FunctionOpInterface>();
180 assert(parentFunc && "expected there to be a parent function");
181 OpBuilder b(parentFunc);
182
183 // Create a valid global location removing any metadata attached to the
184 // location as debug info metadata inside of a function cannot be used
185 // outside of that function.
186 auto globalloc = op->getLoc()->findInstanceOfOrUnknown<FileLineColLoc>();
187 auto newFuncOp = LLVMFuncOp::create(b, globalloc, funcName, funcType);
188 newFuncOp.setCConv(cconv);
189 return newFuncOp;
190 }
191
192 StringRef getFunctionName(Type type, SourceOp op) const {
193 bool useApprox = false;
194 if constexpr (llvm::is_detected<has_get_fastmath_t, SourceOp>::value) {
195 arith::FastMathFlags flag = op.getFastmath();
196 useApprox = ((uint32_t)arith::FastMathFlags::afn & (uint32_t)flag) &&
197 !f32ApproxFunc.empty();
198 }
199
200 if (isa<Float16Type>(type))
201 return f16Func;
202 if (isa<Float32Type>(type)) {
203 if (useApprox)
204 return f32ApproxFunc;
205 return f32Func;
206 }
207 if (isa<Float64Type>(type))
208 return f64Func;
209
210 if (type.isInteger(32))
211 return i32Func;
212 return "";
213 }
214
215 const std::string f32Func;
216 const std::string f64Func;
217 const std::string f32ApproxFunc;
218 const std::string f16Func;
219 const std::string i32Func;
220 const LLVM::cconv::CConv cconv;
221};
222
223} // namespace mlir
224
225#endif // MLIR_CONVERSION_GPUCOMMON_OPTOFUNCCALLLOWERING_H_
return success()
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
MLIRContext * getContext() const
Definition Builders.h:56
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.h:239
An instance of this location represents a tuple of file, line number, and column number.
Definition Location.h:176
Conversion from types to the LLVM IR dialect.
LocationAttr findInstanceOfOrUnknown()
Return an instance of the given location type if one is nested under the current location else return...
Definition Location.h:60
This class helps build Operations.
Definition Builders.h:210
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:255
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:233
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...
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition Types.cpp:122
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:392
type_range getTypes() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
Include the generated interface declarations.
const LLVM::cconv::CConv cconv
OpToFuncCallLowering(const LLVMTypeConverter &lowering, StringRef f32Func, StringRef f64Func, StringRef f32ApproxFunc, StringRef f16Func, StringRef i32Func="", PatternBenefit benefit=1, LLVM::cconv::CConv cconv=LLVM::cconv::CConv::C)
StringRef getFunctionName(Type type, SourceOp op) const
LLVM::LLVMFuncOp appendOrGetFuncOp(StringRef funcName, Type funcType, Operation *op) const
LogicalResult matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor, ConversionPatternRewriter &rewriter) const override
Methods that operate on the SourceOp type.
Type getFunctionType(Type resultType, ValueRange operands) const
Value maybeCast(Value operand, PatternRewriter &rewriter) const