|
| 1 | +//===- ArithToAPFloat.cpp - Arithmetic to APFloat impl conversion ---------===// |
| 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 | +#include "mlir/Conversion/ArithToAPFloat/ArithToAPFloat.h" |
| 10 | + |
| 11 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 12 | +#include "mlir/Dialect/Arith/Transforms/Passes.h" |
| 13 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 14 | +#include "mlir/Dialect/Func/Utils/Utils.h" |
| 15 | +#include "mlir/IR/Verifier.h" |
| 16 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 17 | + |
| 18 | +namespace mlir { |
| 19 | +#define GEN_PASS_DEF_ARITHTOAPFLOATCONVERSIONPASS |
| 20 | +#include "mlir/Conversion/Passes.h.inc" |
| 21 | +} // namespace mlir |
| 22 | + |
| 23 | +using namespace mlir; |
| 24 | +using namespace mlir::func; |
| 25 | + |
| 26 | +#define APFLOAT_BIN_OPS(X) \ |
| 27 | + X(add) \ |
| 28 | + X(subtract) \ |
| 29 | + X(multiply) \ |
| 30 | + X(divide) \ |
| 31 | + X(remainder) \ |
| 32 | + X(mod) |
| 33 | + |
| 34 | +#define APFLOAT_EXTERN_K(OP) kApFloat_##OP |
| 35 | + |
| 36 | +#define APFLOAT_EXTERN_NAME(OP) \ |
| 37 | + static constexpr llvm::StringRef APFLOAT_EXTERN_K(OP) = "_mlir_" \ |
| 38 | + "apfloat_" #OP; |
| 39 | + |
| 40 | +namespace mlir::func { |
| 41 | +#define LOOKUP_OR_CREATE_APFLOAT_FN_DECL(OP) \ |
| 42 | + FailureOr<FuncOp> lookupOrCreateApFloat##OP##Fn( \ |
| 43 | + OpBuilder &b, Operation *moduleOp, \ |
| 44 | + SymbolTableCollection *symbolTables = nullptr); |
| 45 | + |
| 46 | +APFLOAT_BIN_OPS(LOOKUP_OR_CREATE_APFLOAT_FN_DECL) |
| 47 | + |
| 48 | +#undef LOOKUP_OR_CREATE_APFLOAT_FN_DECL |
| 49 | + |
| 50 | +APFLOAT_BIN_OPS(APFLOAT_EXTERN_NAME) |
| 51 | + |
| 52 | +#define LOOKUP_OR_CREATE_APFLOAT_FN_DEFN(OP) \ |
| 53 | + FailureOr<FuncOp> lookupOrCreateApFloat##OP##Fn( \ |
| 54 | + OpBuilder &b, Operation *moduleOp, \ |
| 55 | + SymbolTableCollection *symbolTables) { \ |
| 56 | + return lookupOrCreateFn(b, moduleOp, APFLOAT_EXTERN_K(OP), \ |
| 57 | + {IntegerType::get(moduleOp->getContext(), 32), \ |
| 58 | + IntegerType::get(moduleOp->getContext(), 64), \ |
| 59 | + IntegerType::get(moduleOp->getContext(), 64)}, \ |
| 60 | + {IntegerType::get(moduleOp->getContext(), 64)}, \ |
| 61 | + /*setPrivate*/ true, symbolTables); \ |
| 62 | + } |
| 63 | + |
| 64 | +APFLOAT_BIN_OPS(LOOKUP_OR_CREATE_APFLOAT_FN_DEFN) |
| 65 | +#undef LOOKUP_OR_CREATE_APFLOAT_FN_DEFN |
| 66 | +} // namespace mlir::func |
| 67 | + |
| 68 | +struct FancyAddFLowering : OpRewritePattern<arith::AddFOp> { |
| 69 | + using OpRewritePattern::OpRewritePattern; |
| 70 | + |
| 71 | + LogicalResult matchAndRewrite(arith::AddFOp op, |
| 72 | + PatternRewriter &rewriter) const override { |
| 73 | + // Get APFloat adder function from runtime library. |
| 74 | + auto parent = op->getParentOfType<ModuleOp>(); |
| 75 | + if (!parent) |
| 76 | + return failure(); |
| 77 | + if (!llvm::isa<Float8E5M2Type, Float8E4M3Type, Float8E4M3FNType, |
| 78 | + Float8E5M2FNUZType, Float8E4M3FNUZType, |
| 79 | + Float8E4M3B11FNUZType, Float8E3M4Type, Float4E2M1FNType, |
| 80 | + Float6E2M3FNType, Float6E3M2FNType, Float8E8M0FNUType>( |
| 81 | + op.getType())) |
| 82 | + return failure(); |
| 83 | + FailureOr<Operation *> adder = lookupOrCreateApFloataddFn(rewriter, parent); |
| 84 | + |
| 85 | + // Cast operands to 64-bit integers. |
| 86 | + Location loc = op.getLoc(); |
| 87 | + auto floatTy = cast<FloatType>(op.getType()); |
| 88 | + auto intWType = rewriter.getIntegerType(floatTy.getWidth()); |
| 89 | + auto int64Type = rewriter.getI64Type(); |
| 90 | + Value lhsBits = arith::ExtUIOp::create( |
| 91 | + rewriter, loc, int64Type, |
| 92 | + arith::BitcastOp::create(rewriter, loc, intWType, op.getLhs())); |
| 93 | + Value rhsBits = arith::ExtUIOp::create( |
| 94 | + rewriter, loc, int64Type, |
| 95 | + arith::BitcastOp::create(rewriter, loc, intWType, op.getRhs())); |
| 96 | + |
| 97 | + // Call software implementation of floating point addition. |
| 98 | + int32_t sem = |
| 99 | + llvm::APFloatBase::SemanticsToEnum(floatTy.getFloatSemantics()); |
| 100 | + Value semValue = arith::ConstantOp::create( |
| 101 | + rewriter, loc, rewriter.getI32Type(), |
| 102 | + rewriter.getIntegerAttr(rewriter.getI32Type(), sem)); |
| 103 | + SmallVector<Value> params = {semValue, lhsBits, rhsBits}; |
| 104 | + auto resultOp = |
| 105 | + func::CallOp::create(rewriter, loc, TypeRange(rewriter.getI64Type()), |
| 106 | + SymbolRefAttr::get(*adder), params); |
| 107 | + |
| 108 | + // Truncate result to the original width. |
| 109 | + Value truncatedBits = arith::TruncIOp::create(rewriter, loc, intWType, |
| 110 | + resultOp->getResult(0)); |
| 111 | + rewriter.replaceAllUsesWith( |
| 112 | + op, arith::BitcastOp::create(rewriter, loc, floatTy, truncatedBits)); |
| 113 | + return success(); |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +void arith::populateArithToAPFloatConversionPatterns( |
| 118 | + RewritePatternSet &patterns) { |
| 119 | + patterns.add<FancyAddFLowering>(patterns.getContext()); |
| 120 | +} |
| 121 | + |
| 122 | +namespace { |
| 123 | +struct ArithToAPFloatConversionPass final |
| 124 | + : impl::ArithToAPFloatConversionPassBase<ArithToAPFloatConversionPass> { |
| 125 | + using impl::ArithToAPFloatConversionPassBase< |
| 126 | + ArithToAPFloatConversionPass>::ArithToAPFloatConversionPassBase; |
| 127 | + |
| 128 | + void runOnOperation() override { |
| 129 | + Operation *op = getOperation(); |
| 130 | + RewritePatternSet patterns(op->getContext()); |
| 131 | + arith::populateArithToAPFloatConversionPatterns(patterns); |
| 132 | + if (failed(applyPatternsGreedily(op, std::move(patterns)))) |
| 133 | + return signalPassFailure(); |
| 134 | + } |
| 135 | +}; |
| 136 | +} // namespace |
0 commit comments