1//===- EmulateUnsupportedFloats.cpp - Promote small floats --*- 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// This pass promotes small floats (of some unsupported types T) to a supported
9// type U by wrapping all float operations on Ts with expansion to and
10// truncation from U, then operating on U.
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/Arith/Transforms/Passes.h"
14
15#include "mlir/Dialect/Arith/IR/Arith.h"
16#include "mlir/Dialect/Arith/Utils/Utils.h"
17#include "mlir/Dialect/Vector/IR/VectorOps.h"
18#include "mlir/IR/BuiltinTypes.h"
19#include "mlir/IR/Location.h"
20#include "mlir/IR/PatternMatch.h"
21#include "mlir/Transforms/DialectConversion.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/Support/ErrorHandling.h"
24#include <optional>
25
26namespace mlir::arith {
27#define GEN_PASS_DEF_ARITHEMULATEUNSUPPORTEDFLOATS
28#include "mlir/Dialect/Arith/Transforms/Passes.h.inc"
29} // namespace mlir::arith
30
31using namespace mlir;
32
33namespace {
34struct EmulateUnsupportedFloatsPass
35 : arith::impl::ArithEmulateUnsupportedFloatsBase<
36 EmulateUnsupportedFloatsPass> {
37 using arith::impl::ArithEmulateUnsupportedFloatsBase<
38 EmulateUnsupportedFloatsPass>::ArithEmulateUnsupportedFloatsBase;
39
40 void runOnOperation() override;
41};
42
43struct EmulateFloatPattern final : ConversionPattern {
44 EmulateFloatPattern(const TypeConverter &converter, MLIRContext *ctx)
45 : ConversionPattern::ConversionPattern(
46 converter, Pattern::MatchAnyOpTypeTag(), 1, ctx) {}
47
48 LogicalResult
49 matchAndRewrite(Operation *op, ArrayRef<Value> operands,
50 ConversionPatternRewriter &rewriter) const override;
51};
52} // end namespace
53
54LogicalResult EmulateFloatPattern::matchAndRewrite(
55 Operation *op, ArrayRef<Value> operands,
56 ConversionPatternRewriter &rewriter) const {
57 if (getTypeConverter()->isLegal(op))
58 return failure();
59 // The rewrite doesn't handle cloning regions.
60 if (op->getNumRegions() != 0)
61 return failure();
62
63 Location loc = op->getLoc();
64 const TypeConverter *converter = getTypeConverter();
65 SmallVector<Type> resultTypes;
66 if (failed(Result: converter->convertTypes(types: op->getResultTypes(), results&: resultTypes))) {
67 // Note to anyone looking for this error message: this is a "can't happen".
68 // If you're seeing it, there's a bug.
69 return op->emitOpError(message: "type conversion failed in float emulation");
70 }
71 OperationState state(loc, op->getName(), operands, resultTypes,
72 op->getDiscardableAttrDictionary().getValue(),
73 op->getSuccessors());
74 state.propertiesAttr = op->getPropertiesAsAttribute();
75 Operation *expandedOp = rewriter.create(state);
76 SmallVector<Value> newResults(expandedOp->getResults());
77 for (auto [res, oldType, newType] : llvm::zip_equal(
78 t: MutableArrayRef{newResults}, u: op->getResultTypes(), args&: resultTypes)) {
79 if (oldType != newType) {
80 auto truncFOp = arith::TruncFOp::create(builder&: rewriter, location: loc, out: oldType, in: res);
81 truncFOp.setFastmath(arith::FastMathFlags::contract);
82 res = truncFOp.getResult();
83 }
84 }
85 rewriter.replaceOp(op, newValues: newResults);
86 return success();
87}
88
89void mlir::arith::populateEmulateUnsupportedFloatsConversions(
90 TypeConverter &converter, ArrayRef<Type> sourceTypes, Type targetType) {
91 converter.addConversion(callback: [sourceTypes = SmallVector<Type>(sourceTypes),
92 targetType](Type type) -> std::optional<Type> {
93 if (llvm::is_contained(Range: sourceTypes, Element: type))
94 return targetType;
95 if (auto shaped = dyn_cast<ShapedType>(Val&: type))
96 if (llvm::is_contained(Range: sourceTypes, Element: shaped.getElementType()))
97 return shaped.clone(elementType: targetType);
98 // All other types legal
99 return type;
100 });
101 converter.addTargetMaterialization(
102 callback: [](OpBuilder &b, Type target, ValueRange input, Location loc) {
103 auto extFOp = arith::ExtFOp::create(builder&: b, location: loc, out: target, in: input.front(),
104 fastmath: arith::FastMathFlagsAttr{});
105 extFOp.setFastmath(arith::FastMathFlags::contract);
106 return extFOp;
107 });
108}
109
110void mlir::arith::populateEmulateUnsupportedFloatsPatterns(
111 RewritePatternSet &patterns, const TypeConverter &converter) {
112 patterns.add<EmulateFloatPattern>(arg: converter, args: patterns.getContext());
113}
114
115void mlir::arith::populateEmulateUnsupportedFloatsLegality(
116 ConversionTarget &target, const TypeConverter &converter) {
117 // Don't try to legalize functions and other ops that don't need expansion.
118 target.markUnknownOpDynamicallyLegal(fn: [](Operation *op) { return true; });
119 target.addDynamicallyLegalDialect<arith::ArithDialect>(
120 callback: [&](Operation *op) -> std::optional<bool> {
121 return converter.isLegal(op);
122 });
123 // Manually mark arithmetic-performing vector instructions.
124 target.addDynamicallyLegalOp<vector::ContractionOp, vector::ReductionOp,
125 vector::MultiDimReductionOp, vector::FMAOp,
126 vector::OuterProductOp, vector::ScanOp>(
127 callback: [&](Operation *op) { return converter.isLegal(op); });
128 target.addLegalOp<arith::BitcastOp, arith::ExtFOp, arith::TruncFOp,
129 arith::ConstantOp, arith::SelectOp, vector::BroadcastOp>();
130}
131
132void EmulateUnsupportedFloatsPass::runOnOperation() {
133 MLIRContext *ctx = &getContext();
134 Operation *op = getOperation();
135 SmallVector<Type> sourceTypes;
136 Type targetType;
137
138 FloatType parsedTargetType = arith::parseFloatType(ctx, name: targetTypeStr);
139 if (!parsedTargetType) {
140 emitError(loc: UnknownLoc::get(context: ctx), message: "could not map target type '" +
141 targetTypeStr +
142 "' to a known floating-point type");
143 return signalPassFailure();
144 }
145 targetType = parsedTargetType;
146 for (StringRef sourceTypeStr : sourceTypeStrs) {
147 FloatType sourceType = arith::parseFloatType(ctx, name: sourceTypeStr);
148 if (!sourceType) {
149 emitError(loc: UnknownLoc::get(context: ctx), message: "could not map source type '" +
150 sourceTypeStr +
151 "' to a known floating-point type");
152 return signalPassFailure();
153 }
154 sourceTypes.push_back(Elt: sourceType);
155 }
156 if (sourceTypes.empty())
157 (void)emitOptionalWarning(
158 loc: std::nullopt,
159 args: "no source types specified, float emulation will do nothing");
160
161 if (llvm::is_contained(Range&: sourceTypes, Element: targetType)) {
162 emitError(loc: UnknownLoc::get(context: ctx),
163 message: "target type cannot be an unsupported source type");
164 return signalPassFailure();
165 }
166 TypeConverter converter;
167 arith::populateEmulateUnsupportedFloatsConversions(converter, sourceTypes,
168 targetType);
169 RewritePatternSet patterns(ctx);
170 arith::populateEmulateUnsupportedFloatsPatterns(patterns, converter);
171 ConversionTarget target(getContext());
172 arith::populateEmulateUnsupportedFloatsLegality(target, converter);
173
174 if (failed(Result: applyPartialConversion(op, target, patterns: std::move(patterns))))
175 signalPassFailure();
176}
177