MLIR  20.0.0git
Namespaces | Macros | Functions
MathToFuncs.cpp File Reference
#include "mlir/Conversion/MathToFuncs/MathToFuncs.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Utils/IndexingUtils.h"
#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/Dialect/Vector/Utils/VectorUtils.h"
#include "mlir/IR/ImplicitLocOpBuilder.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/Debug.h"
#include "mlir/Conversion/Passes.h.inc"

Go to the source code of this file.

Namespaces

 mlir
 Include the generated interface declarations.
 

Macros

#define GEN_PASS_DEF_CONVERTMATHTOFUNCS
 
#define DEBUG_TYPE   "math-to-funcs"
 
#define DBGS()   (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
 

Functions

static FunctionType getElementalFuncTypeForOp (Operation *op)
 
static func::FuncOp createElementIPowIFunc (ModuleOp *module, Type elementType)
 Create linkonce_odr function to implement the power function with the given elementType type inside module. More...
 
static func::FuncOp createElementFPowIFunc (ModuleOp *module, FunctionType funcType)
 Create linkonce_odr function to implement the power function with the given funcType type inside module. More...
 
static func::FuncOp createCtlzFunc (ModuleOp *module, Type elementType)
 Create function to implement the ctlz function the given elementType type inside module. More...
 

Macro Definition Documentation

◆ DBGS

#define DBGS ( )    (llvm::dbgs() << "[" DEBUG_TYPE "]: ")

Definition at line 36 of file MathToFuncs.cpp.

◆ DEBUG_TYPE

#define DEBUG_TYPE   "math-to-funcs"

Definition at line 35 of file MathToFuncs.cpp.

◆ GEN_PASS_DEF_CONVERTMATHTOFUNCS

#define GEN_PASS_DEF_CONVERTMATHTOFUNCS

Definition at line 29 of file MathToFuncs.cpp.

Function Documentation

◆ createCtlzFunc()

static func::FuncOp createCtlzFunc ( ModuleOp *  module,
Type  elementType 
)
static

Create function to implement the ctlz function the given elementType type inside module.

The elementType must be IntegerType, an the created function has 'IntegerType (*)(IntegerType)' function type.

template <typename T> T __mlir_math_ctlz_*(T x) { bits = sizeof(x) * 8; if (x == 0) return bits;

uint32_t n = 0; for (int i = 1; i < bits; ++i) { if (x < 0) continue; n++; x <<= 1; } return n; }

Converts to (for i32):

func.func private @__mlir_math_ctlz_i32(arg: i32) -> i32 { c_32 = arith.constant 32 : index c_0 = arith.constant 0 : i32 arg_eq_zero = arith.cmpi eq, arg, c_0 : i1 out = scf.if arg_eq_zero { scf.yield c_32 : i32 } else { c_1index = arith.constant 1 : index c_1i32 = arith.constant 1 : i32 n = arith.constant 0 : i32 arg_out, n_out = scf.for i = c_1index to c_32 step c_1index iter_args(arg_iter = arg, n_iter = n) -> (i32, i32) { cond = arith.cmpi slt, arg_iter, c_0 : i32 yield_val = scf.if cond { scf.yield arg_iter, n_iter : i32, i32 } else { arg_next = arith.shli arg_iter, c_1i32 : i32 n_next = arith.addi n_iter, c_1i32 : i32 scf.yield arg_next, n_next : i32, i32 } scf.yield yield_val: i32, i32 } scf.yield n_out : i32 } return out: i32 }

Definition at line 651 of file MathToFuncs.cpp.

References mlir::ImplicitLocOpBuilder::atBlockEnd(), mlir::ImplicitLocOpBuilder::create(), mlir::OpBuilder::create(), mlir::Operation::create(), DBGS, mlir::get(), mlir::Builder::getContext(), mlir::Builder::getIndexType(), mlir::Builder::getIntegerAttr(), mlir::Type::getIntOrFloatBitWidth(), mlir::OpState::getLoc(), mlir::Type::print(), and mlir::OpBuilder::setInsertionPointToStart().

◆ createElementFPowIFunc()

static func::FuncOp createElementFPowIFunc ( ModuleOp *  module,
FunctionType  funcType 
)
static

Create linkonce_odr function to implement the power function with the given funcType type inside module.

The funcType must be 'FloatType (*)(FloatType, IntegerType)' function type.

template <typename T> Tb __mlir_math_fpowi_*(Tb b, Tp p) { if (p == Tp{0}) return Tb{1}; bool isNegativePower{p < Tp{0}} bool isMin{p == std::numeric_limits<Tp>::min()}; if (isMin) { p = std::numeric_limits<Tp>::max(); } else if (isNegativePower) { p = -p; } Tb result = Tb{1}; Tb origBase = Tb{b}; while (true) { if (p & Tp{1}) result *= b; p >>= Tp{1}; if (p == Tp{0}) break; b *= b; } if (isMin) { result *= origBase; } if (isNegativePower) { result = Tb{1} / result; } return result; }

if (isNegativePower) { result = Tb{1} / result; }

Definition at line 412 of file MathToFuncs.cpp.

References mlir::ImplicitLocOpBuilder::atBlockEnd(), mlir::ImplicitLocOpBuilder::create(), mlir::OpBuilder::createBlock(), mlir::Region::end(), mlir::get(), mlir::Block::getArgument(), mlir::Builder::getContext(), mlir::Builder::getFloatAttr(), mlir::Builder::getIntegerAttr(), mlir::ImplicitLocOpBuilder::getLoc(), mlir::Block::getParent(), and mlir::OpBuilder::setInsertionPointToEnd().

◆ createElementIPowIFunc()

static func::FuncOp createElementIPowIFunc ( ModuleOp *  module,
Type  elementType 
)
static

Create linkonce_odr function to implement the power function with the given elementType type inside module.

The elementType must be IntegerType, an the created function has 'IntegerType (*)(IntegerType, IntegerType)' function type.

template <typename T> T __mlir_math_ipowi_*(T b, T p) { if (p == T(0)) return T(1); if (p < T(0)) { if (b == T(0)) return T(1) / T(0); // trigger div-by-zero if (b == T(1)) return T(1); if (b == T(-1)) { if (p & T(1)) return T(-1); return T(1); } return T(0); } T result = T(1); while (true) { if (p & T(1)) result *= b; p >>= T(1); if (p == T(0)) return result; b *= b; } }

Definition at line 185 of file MathToFuncs.cpp.

References mlir::ImplicitLocOpBuilder::atBlockEnd(), mlir::ImplicitLocOpBuilder::create(), mlir::OpBuilder::createBlock(), mlir::Region::end(), mlir::get(), mlir::Builder::getContext(), mlir::Builder::getIntegerAttr(), mlir::Type::getIntOrFloatBitWidth(), mlir::ImplicitLocOpBuilder::getLoc(), mlir::Block::getParent(), and mlir::OpBuilder::setInsertionPointToEnd().

◆ getElementalFuncTypeForOp()

static FunctionType getElementalFuncTypeForOp ( Operation op)
static

Definition at line 142 of file MathToFuncs.cpp.