|
| 1 | +//===- LegalizeToF32.cpp - Legalize functions on small floats ----------===// |
| 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 | +// |
| 9 | +// This file implements legalizing math operations on small floating-point |
| 10 | +// types through arith.extf and arith.truncf. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 15 | +#include "mlir/Dialect/Math/IR/Math.h" |
| 16 | +#include "mlir/Dialect/Math/Transforms/Passes.h" |
| 17 | +#include "mlir/IR/Diagnostics.h" |
| 18 | +#include "mlir/IR/PatternMatch.h" |
| 19 | +#include "mlir/IR/TypeUtilities.h" |
| 20 | +#include "mlir/Transforms/DialectConversion.h" |
| 21 | +#include "llvm/ADT/STLExtras.h" |
| 22 | + |
| 23 | +namespace mlir::math { |
| 24 | +#define GEN_PASS_DEF_MATHLEGALIZETOF32 |
| 25 | +#include "mlir/Dialect/Math/Transforms/Passes.h.inc" |
| 26 | +} // namespace mlir::math |
| 27 | + |
| 28 | +using namespace mlir; |
| 29 | +namespace { |
| 30 | +struct LegalizeToF32RewritePattern final : ConversionPattern { |
| 31 | + LegalizeToF32RewritePattern(TypeConverter &converter, MLIRContext *context) |
| 32 | + : ConversionPattern(converter, MatchAnyOpTypeTag{}, 1, context) {} |
| 33 | + LogicalResult |
| 34 | + matchAndRewrite(Operation *op, ArrayRef<Value> operands, |
| 35 | + ConversionPatternRewriter &rewriter) const override; |
| 36 | +}; |
| 37 | + |
| 38 | +struct LegalizeToF32Pass final |
| 39 | + : mlir::math::impl::MathLegalizeToF32Base<LegalizeToF32Pass> { |
| 40 | + void runOnOperation() override; |
| 41 | +}; |
| 42 | +} // namespace |
| 43 | + |
| 44 | +void mlir::math::populateLegalizeToF32TypeConverter( |
| 45 | + TypeConverter &typeConverter) { |
| 46 | + typeConverter.addConversion( |
| 47 | + [](Type type) -> std::optional<Type> { return type; }); |
| 48 | + typeConverter.addConversion([](FloatType type) -> std::optional<Type> { |
| 49 | + if (type.getWidth() < 32) |
| 50 | + return Float32Type::get(type.getContext()); |
| 51 | + return std::nullopt; |
| 52 | + }); |
| 53 | + typeConverter.addConversion([](ShapedType type) -> std::optional<Type> { |
| 54 | + if (auto elemTy = dyn_cast<FloatType>(type.getElementType())) |
| 55 | + return type.clone(Float32Type::get(type.getContext())); |
| 56 | + return std::nullopt; |
| 57 | + }); |
| 58 | + typeConverter.addTargetMaterialization( |
| 59 | + [](OpBuilder &b, Type target, ValueRange input, Location loc) { |
| 60 | + return b.create<arith::ExtFOp>(loc, target, input); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +void mlir::math::populateLegalizeToF32ConversionTarget( |
| 65 | + ConversionTarget &target, TypeConverter &typeConverter) { |
| 66 | + target.addDynamicallyLegalDialect<MathDialect>( |
| 67 | + [&typeConverter](Operation *op) -> bool { |
| 68 | + return typeConverter.isLegal(op); |
| 69 | + }); |
| 70 | + target.addLegalOp<FmaOp>(); |
| 71 | + target.addLegalOp<arith::ExtFOp, arith::TruncFOp>(); |
| 72 | +} |
| 73 | + |
| 74 | +LogicalResult LegalizeToF32RewritePattern::matchAndRewrite( |
| 75 | + Operation *op, ArrayRef<Value> operands, |
| 76 | + ConversionPatternRewriter &rewriter) const { |
| 77 | + Location loc = op->getLoc(); |
| 78 | + const TypeConverter *converter = getTypeConverter(); |
| 79 | + if (converter->isLegal(op)) |
| 80 | + return rewriter.notifyMatchFailure(loc, "op already legal"); |
| 81 | + OperationState newOp(loc, op->getName()); |
| 82 | + newOp.addOperands(operands); |
| 83 | + |
| 84 | + SmallVector<Type> newResultTypes; |
| 85 | + if (failed(converter->convertTypes(op->getResultTypes(), newResultTypes))) |
| 86 | + return rewriter.notifyMatchFailure(loc, "couldn't convert return types"); |
| 87 | + newOp.addTypes(newResultTypes); |
| 88 | + newOp.addAttributes(op->getAttrs()); |
| 89 | + Operation *legalized = rewriter.create(newOp); |
| 90 | + SmallVector<Value> results = legalized->getResults(); |
| 91 | + for (auto [result, newType, origType] : |
| 92 | + llvm::zip_equal(results, newResultTypes, op->getResultTypes())) { |
| 93 | + if (newType != origType) |
| 94 | + result = rewriter.create<arith::TruncFOp>(loc, origType, result); |
| 95 | + } |
| 96 | + rewriter.replaceOp(op, results); |
| 97 | + return success(); |
| 98 | +} |
| 99 | + |
| 100 | +void mlir::math::populateLegalizeToF32Patterns(RewritePatternSet &patterns, |
| 101 | + TypeConverter &typeConverter) { |
| 102 | + patterns.add<LegalizeToF32RewritePattern>(typeConverter, |
| 103 | + patterns.getContext()); |
| 104 | +} |
| 105 | + |
| 106 | +void LegalizeToF32Pass::runOnOperation() { |
| 107 | + Operation *op = getOperation(); |
| 108 | + MLIRContext &ctx = getContext(); |
| 109 | + |
| 110 | + TypeConverter typeConverter; |
| 111 | + math::populateLegalizeToF32TypeConverter(typeConverter); |
| 112 | + ConversionTarget target(ctx); |
| 113 | + math::populateLegalizeToF32ConversionTarget(target, typeConverter); |
| 114 | + RewritePatternSet patterns(&ctx); |
| 115 | + math::populateLegalizeToF32Patterns(patterns, typeConverter); |
| 116 | + if (failed(applyPartialConversion(op, target, std::move(patterns)))) |
| 117 | + return signalPassFailure(); |
| 118 | +} |
0 commit comments