2021-09-23 00:55:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2021-09-30 00:03:40 +08:00
|
|
|
// Also available under a BSD-style license. See LICENSE.
|
2021-09-23 00:55:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The torch-mlir "reference backend" requires a few passes to glue things
|
|
|
|
// together so that the final IR will work with ExecutionEngine.
|
|
|
|
//
|
|
|
|
// There is no actual "backend".
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PassDetail.h"
|
2021-11-16 07:00:53 +08:00
|
|
|
#include "mlir/Dialect/Arithmetic/Transforms/Passes.h"
|
2021-09-23 00:55:09 +08:00
|
|
|
#include "mlir/Dialect/Math/IR/Math.h"
|
2021-10-26 07:16:01 +08:00
|
|
|
#include "mlir/Dialect/Math/Transforms/Approximation.h"
|
2021-09-23 00:55:09 +08:00
|
|
|
#include "mlir/Dialect/Math/Transforms/Passes.h"
|
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
2021-11-08 23:56:40 +08:00
|
|
|
#include "set"
|
2021-09-23 00:55:09 +08:00
|
|
|
#include "torch-mlir/RefBackend/Passes.h"
|
2021-11-08 23:56:40 +08:00
|
|
|
#include <numeric>
|
2021-09-23 00:55:09 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::RefBackend;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass registration
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
#define GEN_PASS_REGISTRATION
|
|
|
|
#include "torch-mlir/RefBackend/Passes.h.inc"
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
void mlir::torch::RefBackend::registerRefBackendPasses() { ::registerPasses(); }
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MungeCallingConventions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-09-24 03:22:28 +08:00
|
|
|
static bool isArgMemRefTypeValid(Type type) {
|
|
|
|
if (auto memRefType = type.dyn_cast<MemRefType>()) {
|
|
|
|
Type elemTy = memRefType.getElementType();
|
|
|
|
if (elemTy.isa<Float32Type>()) {
|
|
|
|
return true;
|
2021-10-16 06:23:59 +08:00
|
|
|
} else if (elemTy.isa<Float64Type>()) {
|
|
|
|
return true;
|
2021-09-24 03:22:28 +08:00
|
|
|
} else if (auto integerTy = elemTy.dyn_cast<IntegerType>()) {
|
|
|
|
if (integerTy.isSignlessInteger(64))
|
|
|
|
return true;
|
2021-10-16 06:23:40 +08:00
|
|
|
if (integerTy.isSignlessInteger(32))
|
|
|
|
return true;
|
2021-12-08 22:05:02 +08:00
|
|
|
if (integerTy.isSignlessInteger(1))
|
|
|
|
return true;
|
2021-09-24 03:22:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-23 00:55:09 +08:00
|
|
|
static void addEmitCInterfaceAttr(FuncOp func) {
|
|
|
|
func->setAttr("llvm.emit_c_interface", UnitAttr::get(func.getContext()));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type getAbiTypeForMemRef(Type type) {
|
|
|
|
return UnrankedMemRefType::get(type.cast<MemRefType>().getElementType(), 0);
|
|
|
|
}
|
|
|
|
|
2021-11-08 23:56:40 +08:00
|
|
|
// Helper function to get the type string for one return value like i32, f64,
|
|
|
|
// mri32 etc. The strings from multiple return values are concatenated to get
|
|
|
|
// the consumeFuncReturnFunc name.
|
|
|
|
static std::string getTypeToken(Type type) {
|
|
|
|
if (type.isSignlessInteger())
|
|
|
|
return ("i" + Twine(type.getIntOrFloatBitWidth())).str();
|
|
|
|
else if (type.isa<mlir::FloatType>())
|
|
|
|
return ("f" + Twine(type.getIntOrFloatBitWidth())).str();
|
|
|
|
else if (auto memRefType = type.dyn_cast<UnrankedMemRefType>())
|
|
|
|
return "mr" + getTypeToken(memRefType.getElementType());
|
|
|
|
|
|
|
|
llvm_unreachable(
|
|
|
|
"Type token should handle all types: memref, float and int type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Systematically derive the consumeFuncReturnFunc name from return value types.
|
|
|
|
static std::string getConsumeReturnFunctionNameForReturnTypes(TypeRange types) {
|
|
|
|
SmallVector<std::string> tokens = {"refbackend_consume_func_return"};
|
|
|
|
for (auto type : types)
|
|
|
|
tokens.push_back(getTypeToken(type));
|
|
|
|
|
|
|
|
return std::accumulate(tokens.begin(), tokens.end(), std::string(),
|
|
|
|
[](std::string &a, std::string &b) {
|
|
|
|
return a.empty() ? b : (a + "_" + b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the original returnOp with a call to consumeFuncReturnFunc and add
|
|
|
|
// the op to the `toErase` vector.
|
|
|
|
static void replaceReturnWithCall(OpBuilder b, ReturnOp op, StringRef funcName,
|
|
|
|
TypeRange retTypes,
|
|
|
|
SmallVectorImpl<Value> &vals,
|
2021-11-07 03:25:06 +08:00
|
|
|
SmallVectorImpl<Operation *> &toErase) {
|
2021-11-08 23:56:40 +08:00
|
|
|
b.create<mlir::CallOp>(op.getLoc(), funcName, TypeRange({}), vals);
|
2021-11-07 03:25:06 +08:00
|
|
|
b.create<mlir::ReturnOp>(op.getLoc());
|
|
|
|
toErase.push_back(op);
|
|
|
|
}
|
|
|
|
|
2021-10-05 10:06:59 +08:00
|
|
|
static LogicalResult mungeFunction(
|
2021-11-08 23:56:40 +08:00
|
|
|
FuncOp func, std::set<std::string> &supportedConsumeFuncReturnFuncs,
|
|
|
|
std::map<std::string, std::vector<Type>> &invokedConsumeFuncReturnFuncs) {
|
2021-09-23 00:55:09 +08:00
|
|
|
// Add `llvm.emit_c_interface`.
|
|
|
|
// This allows ExecutionEngine to resolve the symbol properly.
|
|
|
|
addEmitCInterfaceAttr(func);
|
|
|
|
|
|
|
|
// Rewrite the function as follows:
|
|
|
|
// - replace all memref arguments with unranked memref
|
|
|
|
// - replace all returns with a call to a function, which is going to be
|
|
|
|
// supplied by the code setting up the ExecutionEngine to process the
|
|
|
|
// result. Additionally, ensure that all results are passed as unranked
|
|
|
|
// memrefs.
|
|
|
|
// - replace the function signature accordingly (unranked inputs, no returns).
|
|
|
|
OpBuilder b(func.getBody());
|
|
|
|
|
|
|
|
SmallVector<Type> newArgTypes;
|
|
|
|
for (auto arg : func.getArguments()) {
|
|
|
|
auto type = arg.getType();
|
2021-09-24 03:22:28 +08:00
|
|
|
if (!isArgMemRefTypeValid(type))
|
2021-10-16 06:23:59 +08:00
|
|
|
return emitError(arg.getLoc(),
|
2021-12-08 22:05:02 +08:00
|
|
|
"argument must be a memref of f32, f64, i32, i64, i1");
|
2021-09-23 00:55:09 +08:00
|
|
|
auto cast = b.create<memref::CastOp>(arg.getLoc(), arg, type);
|
|
|
|
arg.replaceAllUsesExcept(cast, cast);
|
|
|
|
arg.setType(getAbiTypeForMemRef(type));
|
|
|
|
newArgTypes.push_back(arg.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<Operation *> toErase;
|
2021-11-08 23:56:40 +08:00
|
|
|
bool isSupported = true;
|
2021-09-23 00:55:09 +08:00
|
|
|
func.walk([&](ReturnOp op) {
|
2021-11-08 23:56:40 +08:00
|
|
|
auto types = op.getOperandTypes();
|
2021-09-23 00:55:09 +08:00
|
|
|
b.setInsertionPoint(op);
|
2021-11-07 03:25:06 +08:00
|
|
|
// Memref Types.
|
2021-11-08 23:56:40 +08:00
|
|
|
std::vector<Type> retTypes;
|
|
|
|
SmallVector<Value> retVals;
|
|
|
|
for (auto en : llvm::enumerate(types)) {
|
|
|
|
Type retType = en.value();
|
|
|
|
Value retVal = op.getOperand(en.index());
|
|
|
|
if (auto memrefReturnType = retType.dyn_cast<MemRefType>()) {
|
|
|
|
auto elemType = memrefReturnType.getElementType();
|
|
|
|
retType = UnrankedMemRefType::get(elemType, 0);
|
|
|
|
// Cast to unranked memref type before sending it as a function
|
|
|
|
// argument.
|
|
|
|
retVal = b.create<memref::CastOp>(
|
|
|
|
op.getLoc(), retVal, getAbiTypeForMemRef(types[en.index()]));
|
|
|
|
}
|
|
|
|
retTypes.push_back(retType);
|
|
|
|
retVals.push_back(retVal);
|
2021-11-07 03:25:06 +08:00
|
|
|
}
|
2021-11-08 23:56:40 +08:00
|
|
|
|
|
|
|
auto supportedFuncsEnd = supportedConsumeFuncReturnFuncs.end();
|
|
|
|
std::string funcName = getConsumeReturnFunctionNameForReturnTypes(retTypes);
|
|
|
|
if (supportedConsumeFuncReturnFuncs.find(funcName) == supportedFuncsEnd) {
|
2022-01-10 21:24:08 +08:00
|
|
|
op.emitError("must have one return value of memref types or scalar types "
|
|
|
|
"of i32, i64, f32, f64, i1, or two return values of memref "
|
|
|
|
"f32 and i64, or three return values of memref f32");
|
2021-11-08 23:56:40 +08:00
|
|
|
isSupported = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto invokedFuncsEnd = invokedConsumeFuncReturnFuncs.end();
|
|
|
|
if (invokedConsumeFuncReturnFuncs.find(funcName) == invokedFuncsEnd)
|
|
|
|
invokedConsumeFuncReturnFuncs.insert({funcName, retTypes});
|
|
|
|
replaceReturnWithCall(b, op, funcName, retTypes, retVals, toErase);
|
2021-09-23 00:55:09 +08:00
|
|
|
});
|
2021-11-08 23:56:40 +08:00
|
|
|
if (!isSupported)
|
2021-09-23 00:55:09 +08:00
|
|
|
return failure();
|
|
|
|
func.setType(FunctionType::get(func.getContext(), newArgTypes, {}));
|
|
|
|
for (Operation *op : toErase)
|
|
|
|
op->erase();
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2021-11-08 23:56:40 +08:00
|
|
|
static std::set<std::string> getSupportedConsumeFuncReturnFuncs(OpBuilder &b) {
|
|
|
|
std::set<std::string> funcNames;
|
2021-12-08 22:05:02 +08:00
|
|
|
Type mri1 = UnrankedMemRefType::get(b.getI1Type(), 0);
|
2021-11-08 23:56:40 +08:00
|
|
|
Type mri32 = UnrankedMemRefType::get(b.getI32Type(), 0);
|
|
|
|
Type mri64 = UnrankedMemRefType::get(b.getI64Type(), 0);
|
|
|
|
Type mrf32 = UnrankedMemRefType::get(b.getF32Type(), 0);
|
|
|
|
Type mrf64 = UnrankedMemRefType::get(b.getF64Type(), 0);
|
|
|
|
Type i64 = b.getI64Type();
|
|
|
|
Type f32 = b.getF32Type();
|
|
|
|
Type f64 = b.getF64Type();
|
|
|
|
|
|
|
|
SmallVector<TypeRange> supportedReturnTypes = {
|
2022-01-10 21:24:08 +08:00
|
|
|
mri1, mri32, mri64, mrf32, mrf64,
|
|
|
|
i64, f32, f64, {mrf32, mri64}, {mrf32, mrf32, mrf32}};
|
2021-11-08 23:56:40 +08:00
|
|
|
|
|
|
|
llvm::for_each(supportedReturnTypes, [&](TypeRange &types) {
|
|
|
|
funcNames.insert(getConsumeReturnFunctionNameForReturnTypes(types));
|
|
|
|
});
|
|
|
|
return funcNames;
|
|
|
|
}
|
|
|
|
|
2021-09-23 00:55:09 +08:00
|
|
|
namespace {
|
|
|
|
class MungeCallingConventions
|
|
|
|
: public MungeCallingConventionsBase<MungeCallingConventions> {
|
|
|
|
void runOnOperation() override {
|
|
|
|
auto module = getOperation();
|
|
|
|
OpBuilder b(module.getBodyRegion());
|
2021-11-08 23:56:40 +08:00
|
|
|
static std::set<std::string> supported =
|
|
|
|
getSupportedConsumeFuncReturnFuncs(b);
|
|
|
|
std::map<std::string, std::vector<Type>> invokedConsumeFuncReturnFuncs;
|
2021-09-23 00:55:09 +08:00
|
|
|
for (auto func : module.getOps<FuncOp>()) {
|
2021-11-08 23:56:40 +08:00
|
|
|
if (failed(mungeFunction(func, supported, invokedConsumeFuncReturnFuncs)))
|
2021-09-23 00:55:09 +08:00
|
|
|
return signalPassFailure();
|
|
|
|
}
|
2021-11-08 23:56:40 +08:00
|
|
|
|
|
|
|
// Create FuncOp for consumeFuncReturnFuncs that are used.
|
|
|
|
for (auto &p : invokedConsumeFuncReturnFuncs) {
|
|
|
|
auto consumeFuncReturnFunc =
|
|
|
|
b.create<FuncOp>(module.getLoc(), p.first,
|
|
|
|
FunctionType::get(module.getContext(), p.second, {}),
|
|
|
|
b.getStringAttr("private"));
|
|
|
|
addEmitCInterfaceAttr(consumeFuncReturnFunc);
|
|
|
|
}
|
2021-09-23 00:55:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
|
|
|
mlir::torch::RefBackend::createMungeCallingConventionsPass() {
|
|
|
|
return std::make_unique<MungeCallingConventions>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExpandOpsForLLVM
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ExpandOpsForLLVM : public ExpandOpsForLLVMBase<ExpandOpsForLLVM> {
|
|
|
|
void runOnOperation() override {
|
|
|
|
auto func = getOperation();
|
|
|
|
auto *context = &getContext();
|
|
|
|
RewritePatternSet patterns(context);
|
|
|
|
populateExpandTanhPattern(patterns);
|
2021-10-26 07:16:01 +08:00
|
|
|
patterns.add<math::ErfPolynomialApproximation>(patterns.getContext());
|
2021-09-23 00:55:09 +08:00
|
|
|
ConversionTarget target(*context);
|
|
|
|
target.addLegalDialect<StandardOpsDialect>();
|
|
|
|
target.addLegalDialect<math::MathDialect>();
|
2021-10-16 02:34:29 +08:00
|
|
|
target.addLegalDialect<arith::ArithmeticDialect>();
|
2021-09-23 00:55:09 +08:00
|
|
|
target.addIllegalOp<math::TanhOp>();
|
2021-10-26 07:16:01 +08:00
|
|
|
target.addIllegalOp<math::ErfOp>();
|
2021-09-23 00:55:09 +08:00
|
|
|
if (failed(applyPartialConversion(func, target, std::move(patterns)))) {
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<FuncOp>>
|
|
|
|
mlir::torch::RefBackend::createExpandOpsForLLVMPass() {
|
|
|
|
return std::make_unique<ExpandOpsForLLVM>();
|
|
|
|
}
|