20#define GEN_PASS_DEF_ARITHTOAPFLOATCONVERSIONPASS
21#include "mlir/Conversion/Passes.h.inc"
28 StringRef name, FunctionType funcT,
bool setPrivate,
31 assert(!symTable->getRegion(0).empty() &&
"expected non-empty region");
32 b.setInsertionPointToStart(&symTable->getRegion(0).front());
33 FuncOp funcOp = FuncOp::create(
b, symTable->getLoc(), name, funcT);
37 SymbolTable &symbolTable = symbolTables->getSymbolTable(symTable);
38 symbolTable.
insert(funcOp, symTable->getRegion(0).front().begin());
53static FailureOr<FuncOp>
56 auto i32Type = IntegerType::get(symTable->getContext(), 32);
57 auto i64Type = IntegerType::get(symTable->getContext(), 64);
59 std::string funcName = (llvm::Twine(
"_mlir_apfloat_") + name).str();
61 FunctionType::get(
b.getContext(), {i32Type, i64Type, i64Type}, {i64Type});
62 FailureOr<FuncOp>
func =
76template <
typename OpTy>
88 FailureOr<FuncOp> fn =
96 auto floatTy = cast<FloatType>(op.getType());
99 Value lhsBits = arith::ExtUIOp::create(
100 rewriter, loc, int64Type,
101 arith::BitcastOp::create(rewriter, loc, intWType, op.getLhs()));
102 Value rhsBits = arith::ExtUIOp::create(
103 rewriter, loc, int64Type,
104 arith::BitcastOp::create(rewriter, loc, intWType, op.getRhs()));
108 llvm::APFloatBase::SemanticsToEnum(floatTy.getFloatSemantics());
109 Value semValue = arith::ConstantOp::create(
115 SymbolRefAttr::get(*fn), params);
118 Value truncatedBits = arith::TruncIOp::create(rewriter, loc, intWType,
119 resultOp->getResult(0));
121 op, arith::BitcastOp::create(rewriter, loc, floatTy, truncatedBits));
130struct ArithToAPFloatConversionPass final
134 void runOnOperation()
override;
137void ArithToAPFloatConversionPass::runOnOperation() {
139 RewritePatternSet
patterns(context);
140 patterns.add<BinaryArithOpToAPFloatConversion<arith::AddFOp>>(context,
"add",
142 patterns.add<BinaryArithOpToAPFloatConversion<arith::SubFOp>>(
143 context,
"subtract", getOperation());
144 patterns.add<BinaryArithOpToAPFloatConversion<arith::MulFOp>>(
145 context,
"multiply", getOperation());
146 patterns.add<BinaryArithOpToAPFloatConversion<arith::DivFOp>>(
147 context,
"divide", getOperation());
148 patterns.add<BinaryArithOpToAPFloatConversion<arith::RemFOp>>(
149 context,
"remainder", getOperation());
151 ScopedDiagnosticHandler scopedHandler(context, [&
result](Diagnostic &
diag) {
152 if (
diag.getSeverity() == DiagnosticSeverity::Error) {
161 return signalPassFailure();
static FailureOr< FuncOp > lookupOrCreateBinaryFn(OpBuilder &b, SymbolOpInterface symTable, StringRef name, SymbolTableCollection *symbolTables=nullptr)
Helper function to look up or create the symbol for a runtime library function for a binary arithmeti...
static FuncOp createFnDecl(OpBuilder &b, SymbolOpInterface symTable, StringRef name, FunctionType funcT, bool setPrivate, SymbolTableCollection *symbolTables=nullptr)
static std::string diag(const llvm::Value &value)
IntegerAttr getIntegerAttr(Type type, int64_t value)
IntegerType getIntegerType(unsigned width)
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.
RAII guard to reset the insertion point of the builder when destroyed.
This class helps build Operations.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
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...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
This class represents a collection of SymbolTables.
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
FailureOr< FuncOp > lookupFnDecl(SymbolOpInterface symTable, StringRef name, FunctionType funcT, SymbolTableCollection *symbolTables=nullptr)
Look up a FuncOp with signature resultTypes(paramTypes) and name / name`.
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
void walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
LogicalResult matchAndRewrite(OpTy op, PatternRewriter &rewriter) const override
BinaryArithOpToAPFloatConversion(MLIRContext *context, const char *APFloatName, SymbolOpInterface symTable, PatternBenefit benefit=1)
SymbolOpInterface symTable
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})