Format Key Files using LLVM Style (#403)

* format using llvm style

* merge and format
This commit is contained in:
Tian Jin 2019-12-19 13:27:15 -05:00 committed by Tian Jin
parent 06a968d4a1
commit a6a40cf989
6 changed files with 277 additions and 251 deletions

View File

@ -540,7 +540,8 @@ private:
attributes.push_back(attr); attributes.push_back(attr);
} else { } else {
// TODO: the attributes need special handling // TODO: the attributes need special handling
//std::cout << "missing " << node.op_type() << " " << attr_name << std::endl; // std::cout << "missing " << node.op_type() << " " << attr_name <<
// std::endl;
} }
} }
@ -585,7 +586,8 @@ private:
attributes.push_back(attr); attributes.push_back(attr);
} else { } else {
// TODO: the attributes need special handling // TODO: the attributes need special handling
//std::cout << "missing " << node.op_type() << " " << attr_name << std::endl; // std::cout << "missing " << node.op_type() << " " << attr_name <<
// std::endl;
} }
} }

View File

@ -9,8 +9,6 @@
#include <iostream> #include <iostream>
#include <queue> #include <queue>
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallBitVector.h"
#include "mlir/Dialect/AffineOps/AffineOps.h" #include "mlir/Dialect/AffineOps/AffineOps.h"
#include "mlir/Dialect/StandardOps/Ops.h" #include "mlir/Dialect/StandardOps/Ops.h"
#include "mlir/IR/Block.h" #include "mlir/IR/Block.h"
@ -23,6 +21,8 @@
#include "mlir/IR/Operation.h" #include "mlir/IR/Operation.h"
#include "mlir/IR/PatternMatch.h" #include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/DialectConversion.h" #include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallBitVector.h"
#include "src/compiler/dialect/krnl/krnl_helper.hpp" #include "src/compiler/dialect/krnl/krnl_helper.hpp"
@ -44,13 +44,13 @@ KrnlOpsDialect::KrnlOpsDialect(MLIRContext* context)
// KrnlDefineLoopsOp // KrnlDefineLoopsOp
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void KrnlDefineLoopsOp::build( void KrnlDefineLoopsOp::build(Builder *builder, OperationState &result,
Builder* builder, OperationState& result, int64_t num_loops) { int64_t num_loops) {
// Create the same number of dimension handlers as the number of // Create the same number of dimension handlers as the number of
// dimensions in the associated integer set. // dimensions in the associated integer set.
result.types.append(num_loops, LoopType::get(builder->getContext())); result.types.append(num_loops, LoopType::get(builder->getContext()));
result.addAttribute( result.addAttribute(getNumLoopsAttrName(),
getNumLoopsAttrName(), builder->getI32IntegerAttr(num_loops)); builder->getI32IntegerAttr(num_loops));
} }
void print(OpAsmPrinter &p, KrnlDefineLoopsOp &op) { void print(OpAsmPrinter &p, KrnlDefineLoopsOp &op) {
@ -59,14 +59,15 @@ void print(OpAsmPrinter& p, KrnlDefineLoopsOp& op) {
p << "krnl.define_loops " << numLoopAttr.getValue().getSExtValue(); p << "krnl.define_loops " << numLoopAttr.getValue().getSExtValue();
} }
ParseResult parseKrnlDefineLoopsOp( ParseResult parseKrnlDefineLoopsOp(OpAsmParser &parser,
OpAsmParser& parser, OperationState& result) { OperationState &result) {
// Parse the attribute indicating number of loops defined. // Parse the attribute indicating number of loops defined.
IntegerAttr numLoops; IntegerAttr numLoops;
auto &builder = parser.getBuilder(); auto &builder = parser.getBuilder();
auto intType = builder.getIntegerType(64); auto intType = builder.getIntegerType(64);
if (parser.parseAttribute(numLoops, intType, if (parser.parseAttribute(numLoops, intType,
KrnlDefineLoopsOp::getNumLoopsAttrName(), result.attributes)) KrnlDefineLoopsOp::getNumLoopsAttrName(),
result.attributes))
return failure(); return failure();
auto loopTypes = llvm::SmallVector<Type, 4>( auto loopTypes = llvm::SmallVector<Type, 4>(
@ -79,10 +80,10 @@ ParseResult parseKrnlDefineLoopsOp(
// KrnlOptimizeLoopsOp // KrnlOptimizeLoopsOp
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void KrnlOptimizeLoopsOp::build( void KrnlOptimizeLoopsOp::build(Builder *builder, OperationState &result,
Builder* builder, OperationState& result, int num_optimized_loops) { int num_optimized_loops) {
result.types.append( result.types.append(num_optimized_loops,
num_optimized_loops, LoopType::get(builder->getContext())); LoopType::get(builder->getContext()));
// Create a region and a block for the body. // Create a region and a block for the body.
// Schedule intrinsics will be placed into this region. // Schedule intrinsics will be placed into this region.
Region *region = result.addRegion(); Region *region = result.addRegion();
@ -98,8 +99,8 @@ void print(OpAsmPrinter& p, KrnlOptimizeLoopsOp& op) {
p.printFunctionalType(op); p.printFunctionalType(op);
} }
ParseResult parseKrnlOptimizeLoopsOp( ParseResult parseKrnlOptimizeLoopsOp(OpAsmParser &parser,
OpAsmParser& parser, OperationState& result) { OperationState &result) {
// Parse the schedule body region. // Parse the schedule body region.
Region *region = result.addRegion(); Region *region = result.addRegion();
if (parser.parseRegion(*region, llvm::None, llvm::None)) if (parser.parseRegion(*region, llvm::None, llvm::None))
@ -146,10 +147,11 @@ void KrnlIterateOp::build(Builder* builder, OperationState& result,
KrnlIterateOperandPack operandPack) { KrnlIterateOperandPack operandPack) {
// Record optimized loops and the number of such loops. // Record optimized loops and the number of such loops.
result.addOperands(operandPack.getOperands()); result.addOperands(operandPack.getOperands());
result.addAttribute( result.addAttribute(KrnlIterateOp::getBoundsAttrName(),
KrnlIterateOp::getBoundsAttrName(), operandPack.getAttributes()); operandPack.getAttributes());
result.addAttribute(getNumOptimizedLoopsAttrName(), result.addAttribute(
getNumOptimizedLoopsAttrName(),
builder->getI64IntegerAttr(operandPack.getNumOptimizedLoops())); builder->getI64IntegerAttr(operandPack.getNumOptimizedLoops()));
// Create a region and a block for the body. The arguments of the region are // Create a region and a block for the body. The arguments of the region are
@ -204,10 +206,11 @@ ParseResult parseKrnlIterateOp(OpAsmParser& parser, OperationState& result) {
// Parse optimized loops: // Parse optimized loops:
SmallVector<OpAsmParser::OperandType, 4> optimizedLoopRefs; SmallVector<OpAsmParser::OperandType, 4> optimizedLoopRefs;
if (parser.parseOperandList( if (parser.parseOperandList(optimizedLoopRefs,
optimizedLoopRefs, OpAsmParser::Delimiter::Paren) || OpAsmParser::Delimiter::Paren) ||
parser.resolveOperands(optimizedLoopRefs, parser.resolveOperands(optimizedLoopRefs,
LoopType::get(result.getContext()), result.operands)) LoopType::get(result.getContext()),
result.operands))
return failure(); return failure();
// Record how many optimized loops did we parse. // Record how many optimized loops did we parse.
@ -222,16 +225,16 @@ ParseResult parseKrnlIterateOp(OpAsmParser& parser, OperationState& result) {
return failure(); return failure();
// A function to parse a lower or upper bound. // A function to parse a lower or upper bound.
auto parseBound = [&result, &builder, &parser, &operandParser, &boundMaps]( auto parseBound = [&result, &builder, &parser, &operandParser,
bool isUpper) -> ParseResult { &boundMaps](bool isUpper) -> ParseResult {
// 'min' / 'max' prefixes are generally syntactic sugar, but are required if // 'min' / 'max' prefixes are generally syntactic sugar, but are required if
// the map has multiple results. // the map has multiple results.
bool failedToParsedMinMax = bool failedToParsedMinMax =
failed(parser.parseOptionalKeyword(isUpper ? "min" : "max")); failed(parser.parseOptionalKeyword(isUpper ? "min" : "max"));
// Try parse an SSA operand. // Try parse an SSA operand.
if (succeeded(operandParser.ParseOptionalOperand( if (succeeded(operandParser.ParseOptionalOperand(builder.getIndexType(),
builder.getIndexType(), result.operands))) { result.operands))) {
AffineMap map = builder.getSymbolIdentityMap(); AffineMap map = builder.getSymbolIdentityMap();
boundMaps.emplace_back(AffineMapAttr::get(map)); boundMaps.emplace_back(AffineMapAttr::get(map));
return success(); return success();
@ -243,8 +246,8 @@ ParseResult parseKrnlIterateOp(OpAsmParser& parser, OperationState& result) {
llvm::SMLoc attrLoc = parser.getCurrentLocation(); llvm::SMLoc attrLoc = parser.getCurrentLocation();
Attribute boundAttr; Attribute boundAttr;
llvm::SmallVector<NamedAttribute, 1> tempBoundAttrContainer; llvm::SmallVector<NamedAttribute, 1> tempBoundAttrContainer;
if (parser.parseAttribute( if (parser.parseAttribute(boundAttr, builder.getIndexType(), "temp",
boundAttr, builder.getIndexType(), "temp", tempBoundAttrContainer)) tempBoundAttrContainer))
return failure(); return failure();
if (auto affineMapAttr = boundAttr.dyn_cast<AffineMapAttr>()) { if (auto affineMapAttr = boundAttr.dyn_cast<AffineMapAttr>()) {
@ -255,13 +258,15 @@ ParseResult parseKrnlIterateOp(OpAsmParser& parser, OperationState& result) {
auto map = affineMapAttr.getValue(); auto map = affineMapAttr.getValue();
if (map.getNumDims() != numDims) if (map.getNumDims() != numDims)
return parser.emitError(parser.getNameLoc(), return parser.emitError(
parser.getNameLoc(),
"dim operand count and integer set dim count must match"); "dim operand count and integer set dim count must match");
unsigned numDimAndSymbolOperands = unsigned numDimAndSymbolOperands =
result.operands.size() - currentNumOperands; result.operands.size() - currentNumOperands;
if (numDims + map.getNumSymbols() != numDimAndSymbolOperands) if (numDims + map.getNumSymbols() != numDimAndSymbolOperands)
return parser.emitError(parser.getNameLoc(), return parser.emitError(
parser.getNameLoc(),
"symbol operand count and integer set symbol count must match"); "symbol operand count and integer set symbol count must match");
// If the map has multiple results, make sure that we parsed the min/max // If the map has multiple results, make sure that we parsed the min/max
@ -316,18 +321,18 @@ ParseResult parseKrnlIterateOp(OpAsmParser& parser, OperationState& result) {
// At this point, there shouldn't be any operands left to parse. // At this point, there shouldn't be any operands left to parse.
if (operandParser.hasOperandLeft()) if (operandParser.hasOperandLeft())
return parser.emitError(parser.getCurrentLocation()); return parser.emitError(parser.getCurrentLocation());
result.addAttribute( result.addAttribute(KrnlIterateOp::getBoundsAttrName(),
KrnlIterateOp::getBoundsAttrName(), builder.getArrayAttr(boundMaps)); builder.getArrayAttr(boundMaps));
Region *region = result.addRegion(); Region *region = result.addRegion();
SmallVector<Type, 4> inductionVarTypes( SmallVector<Type, 4> inductionVarTypes(inductionVarRefs.size(),
inductionVarRefs.size(), builder.getIndexType()); builder.getIndexType());
if (parser.parseRegion(*region, inductionVarRefs, inductionVarTypes)) if (parser.parseRegion(*region, inductionVarRefs, inductionVarTypes))
return failure(); return failure();
// Ensure iterate region is closed off with krnl.terminate. // Ensure iterate region is closed off with krnl.terminate.
KrnlIterateOp::ensureTerminator( KrnlIterateOp::ensureTerminator(*region, parser.getBuilder(),
*region, parser.getBuilder(), result.location); result.location);
return success(); return success();
} }
@ -346,13 +351,14 @@ void print(OpAsmPrinter& p, KrnlReturnLoopsOp& op) {
p.printOperands(op.operand_begin(), op.operand_end()); p.printOperands(op.operand_begin(), op.operand_end());
} }
ParseResult parseKrnlReturnLoopsOp( ParseResult parseKrnlReturnLoopsOp(OpAsmParser &parser,
OpAsmParser& parser, OperationState& result) { OperationState &result) {
// Parse the loops to return. // Parse the loops to return.
SmallVector<OpAsmParser::OperandType, 4> timestamp_dim_handlers; SmallVector<OpAsmParser::OperandType, 4> timestamp_dim_handlers;
if (parser.parseOperandList(timestamp_dim_handlers) || if (parser.parseOperandList(timestamp_dim_handlers) ||
parser.resolveOperands(timestamp_dim_handlers, parser.resolveOperands(timestamp_dim_handlers,
LoopType::get(result.getContext()), result.operands)) LoopType::get(result.getContext()),
result.operands))
return failure(); return failure();
return success(); return success();

View File

@ -11,17 +11,16 @@
#include <map> #include <map>
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Sequence.h"
#include "mlir/Dialect/AffineOps/AffineOps.h" #include "mlir/Dialect/AffineOps/AffineOps.h"
#include "mlir/Dialect/StandardOps/Ops.h" #include "mlir/Dialect/StandardOps/Ops.h"
#include "mlir/Pass/Pass.h" #include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h" #include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Sequence.h"
#include "src/compiler/dialect/krnl/krnl_helper.hpp" #include "src/compiler/dialect/krnl/krnl_helper.hpp"
#include "src/compiler/dialect/krnl/krnl_ops.hpp" #include "src/compiler/dialect/krnl/krnl_ops.hpp"
#include "src/compiler/dialect/onnx/onnx_ops.hpp" #include "src/compiler/dialect/onnx/onnx_ops.hpp"
#include "src/compiler/pass/passes.hpp" #include "src/compiler/pass/passes.hpp"
using namespace mlir; using namespace mlir;
@ -218,8 +217,8 @@ getLoopIVsForBroadcasting(Location loc, ConversionPatternRewriter &rewriter,
// If its value is 1, it is broadcasted dimension. // If its value is 1, it is broadcasted dimension.
// Otherwise, non-broadcasted dimension. // Otherwise, non-broadcasted dimension.
auto zero = rewriter.create<ConstantIndexOp>(loc, 0); auto zero = rewriter.create<ConstantIndexOp>(loc, 0);
auto idx = rewriter.create<SelectOp>(loc, broadcastedDims[dimIdx], auto idx = rewriter.create<SelectOp>(loc, broadcastedDims[dimIdx], zero,
zero, loopIVs[loopIdx]); loopIVs[loopIdx]);
newLoopIVs.insert(newLoopIVs.begin(), idx); newLoopIVs.insert(newLoopIVs.begin(), idx);
} else { } else {
// Non-broadcasted dimension // Non-broadcasted dimension
@ -298,17 +297,18 @@ using ScalarIOp = typename ScalarOp<ElementwiseNaryOp>::IOp;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <typename UnaryOp> template <typename UnaryOp>
Value *mapToLowerScalarOp(Operation *op, ArrayRef<Type> result_types, Value *mapToLowerScalarOp(Operation *op, ArrayRef<Type> result_types,
ArrayRef<Value*> operands, ConversionPatternRewriter& rewriter) { ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) {
/* Lower UnaryOp to Ops in the Standard dialect. /* Lower UnaryOp to Ops in the Standard dialect.
*/ */
auto loc = op->getLoc(); auto loc = op->getLoc();
Type element_type = operands.front()->getType(); Type element_type = operands.front()->getType();
if (element_type.isa<IntegerType>()) { if (element_type.isa<IntegerType>()) {
return rewriter.create<ScalarIOp<UnaryOp>>( return rewriter.create<ScalarIOp<UnaryOp>>(loc, result_types, operands,
loc, result_types, operands, mlir::None); mlir::None);
} else if (element_type.isa<FloatType>()) { } else if (element_type.isa<FloatType>()) {
return rewriter.create<ScalarFOp<UnaryOp>>( return rewriter.create<ScalarFOp<UnaryOp>>(loc, result_types, operands,
loc, result_types, operands, mlir::None); mlir::None);
} else { } else {
emitError(loc, "unsupported element type"); emitError(loc, "unsupported element type");
return nullptr; return nullptr;
@ -320,7 +320,8 @@ Value* mapToLowerScalarOp(Operation* op, ArrayRef<Type> result_types,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXTanhOp>(Operation *op, Value *mapToLowerScalarOp<ONNXTanhOp>(Operation *op,
ArrayRef<Type> result_types, ArrayRef<Value*> operands, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// ONNXTanhOp(%X) = DivFOp(SubFOp(ExpOp(%X), ExpOp(NegFOp(%X))), // ONNXTanhOp(%X) = DivFOp(SubFOp(ExpOp(%X), ExpOp(NegFOp(%X))),
// AddFOp(ExpOp(%X), ExpOp(NegFOp(%X)))) // AddFOp(ExpOp(%X), ExpOp(NegFOp(%X))))
@ -343,7 +344,8 @@ Value* mapToLowerScalarOp<ONNXTanhOp>(Operation* op,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXSinhOp>(Operation *op, Value *mapToLowerScalarOp<ONNXSinhOp>(Operation *op,
ArrayRef<Type> result_types, ArrayRef<Value*> operands, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// ONNXSinhOp(%X) = DivFOp(SubFOp(ExpOp(%X), ExpOp(NegFOp(%X))), // ONNXSinhOp(%X) = DivFOp(SubFOp(ExpOp(%X), ExpOp(NegFOp(%X))),
// ConstantOp 2) // ConstantOp 2)
@ -366,7 +368,8 @@ Value* mapToLowerScalarOp<ONNXSinhOp>(Operation* op,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXCoshOp>(Operation *op, Value *mapToLowerScalarOp<ONNXCoshOp>(Operation *op,
ArrayRef<Type> result_types, ArrayRef<Value*> operands, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// ONNXCoshOp(%X) = DivFOp(AddFOp(ExpOp(%X), ExpOp(NegFOp(%X))), // ONNXCoshOp(%X) = DivFOp(AddFOp(ExpOp(%X), ExpOp(NegFOp(%X))),
// ConstantOp 2) // ConstantOp 2)
@ -389,7 +392,8 @@ Value* mapToLowerScalarOp<ONNXCoshOp>(Operation* op,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXSigmoidOp>(Operation *op, Value *mapToLowerScalarOp<ONNXSigmoidOp>(Operation *op,
ArrayRef<Type> result_types, ArrayRef<Value*> operands, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// ONNXSigmoidOp(%X) = DivFOp(ConstantOp 1, // ONNXSigmoidOp(%X) = DivFOp(ConstantOp 1,
// AddFOp(ConstantOp 1, ExpOp(NegFOp(%X)))) // AddFOp(ConstantOp 1, ExpOp(NegFOp(%X))))
@ -410,8 +414,8 @@ Value* mapToLowerScalarOp<ONNXSigmoidOp>(Operation* op,
// Scalar unary ops for lowering ONNXHardSigmoidOp // Scalar unary ops for lowering ONNXHardSigmoidOp
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value* mapToLowerScalarOp<ONNXHardSigmoidOp>(Operation* op, Value *mapToLowerScalarOp<ONNXHardSigmoidOp>(
ArrayRef<Type> result_types, ArrayRef<Value*> operands, Operation *op, ArrayRef<Type> result_types, ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// %Y = AddFOp(MulFOp(alpha, %X), beta) // %Y = AddFOp(MulFOp(alpha, %X), beta)
// %Z = SelectOp(CmpFOp(OGT, %Y, Constant 0), // %Z = SelectOp(CmpFOp(OGT, %Y, Constant 0),
@ -447,7 +451,8 @@ Value* mapToLowerScalarOp<ONNXHardSigmoidOp>(Operation* op,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXEluOp>(Operation *op, ArrayRef<Type> result_types, Value *mapToLowerScalarOp<ONNXEluOp>(Operation *op, ArrayRef<Type> result_types,
ArrayRef<Value*> operands, ConversionPatternRewriter& rewriter) { ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) {
// ONNXEluOp(%X) = SelectOp(CmpFOp(OLT, %X, ConstantOp 0), // ONNXEluOp(%X) = SelectOp(CmpFOp(OLT, %X, ConstantOp 0),
// MulFOp(alpha, SubFOp(ExpOp(%X), 1)), // MulFOp(alpha, SubFOp(ExpOp(%X), 1)),
// %X) // %X)
@ -461,9 +466,10 @@ Value* mapToLowerScalarOp<ONNXEluOp>(Operation* op, ArrayRef<Type> result_types,
auto exp = rewriter.create<ExpOp>(loc, operand); auto exp = rewriter.create<ExpOp>(loc, operand);
auto lessThanZero = auto lessThanZero =
rewriter.create<CmpFOp>(loc, CmpFPredicate::OLT, operand, zero); rewriter.create<CmpFOp>(loc, CmpFPredicate::OLT, operand, zero);
auto result = rewriter.create<SelectOp>(loc, lessThanZero, auto result = rewriter.create<SelectOp>(
rewriter.create<MulFOp>( loc, lessThanZero,
loc, alpha, rewriter.create<SubFOp>(loc, exp, one)), rewriter.create<MulFOp>(loc, alpha,
rewriter.create<SubFOp>(loc, exp, one)),
operand); operand);
return result; return result;
@ -474,7 +480,8 @@ Value* mapToLowerScalarOp<ONNXEluOp>(Operation* op, ArrayRef<Type> result_types,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXReluOp>(Operation *op, Value *mapToLowerScalarOp<ONNXReluOp>(Operation *op,
ArrayRef<Type> result_types, ArrayRef<Value*> operands, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// ONNXReluOp(%X) = SelectOp(CmpFOp(OLT, %X, ConstantOp 0), // ONNXReluOp(%X) = SelectOp(CmpFOp(OLT, %X, ConstantOp 0),
// ConstantOp 0, // ConstantOp 0,
@ -494,8 +501,9 @@ Value* mapToLowerScalarOp<ONNXReluOp>(Operation* op,
// Scalar unary ops for lowering ONNXLeakyReluOp // Scalar unary ops for lowering ONNXLeakyReluOp
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value* mapToLowerScalarOp<ONNXLeakyReluOp>(Operation* op, Value *
ArrayRef<Type> result_types, ArrayRef<Value*> operands, mapToLowerScalarOp<ONNXLeakyReluOp>(Operation *op, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// ONNXLeakyReluOp(%X) = SelectOp(CmpFOp(OLT, %X, ConstantOp 0), // ONNXLeakyReluOp(%X) = SelectOp(CmpFOp(OLT, %X, ConstantOp 0),
// MulFOp(alpha, %X), // MulFOp(alpha, %X),
@ -519,7 +527,8 @@ Value* mapToLowerScalarOp<ONNXLeakyReluOp>(Operation* op,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXSeluOp>(Operation *op, Value *mapToLowerScalarOp<ONNXSeluOp>(Operation *op,
ArrayRef<Type> result_types, ArrayRef<Value*> operands, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) { ConversionPatternRewriter &rewriter) {
// ONNXSeluOp(%X) = SelectOp(CmpFOp(OGT, %X, ConstantOp 0), // ONNXSeluOp(%X) = SelectOp(CmpFOp(OGT, %X, ConstantOp 0),
// MulFOp(gamma, %X), // MulFOp(gamma, %X),
@ -537,9 +546,10 @@ Value* mapToLowerScalarOp<ONNXSeluOp>(Operation* op,
auto exp = rewriter.create<ExpOp>(loc, operand); auto exp = rewriter.create<ExpOp>(loc, operand);
auto greaterThanZero = auto greaterThanZero =
rewriter.create<CmpFOp>(loc, CmpFPredicate::OGT, operand, zero); rewriter.create<CmpFOp>(loc, CmpFPredicate::OGT, operand, zero);
auto select = rewriter.create<SelectOp>(loc, greaterThanZero, operand, auto select = rewriter.create<SelectOp>(
rewriter.create<SubFOp>( loc, greaterThanZero, operand,
loc, rewriter.create<MulFOp>(loc, alpha, exp), alpha)); rewriter.create<SubFOp>(loc, rewriter.create<MulFOp>(loc, alpha, exp),
alpha));
auto result = rewriter.create<MulFOp>(loc, gamma, select); auto result = rewriter.create<MulFOp>(loc, gamma, select);
return result; return result;
@ -549,8 +559,10 @@ Value* mapToLowerScalarOp<ONNXSeluOp>(Operation* op,
// Scalar unary ops for lowering ONNXReciprocalOp // Scalar unary ops for lowering ONNXReciprocalOp
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value* mapToLowerScalarOp<ONNXReciprocalOp>(Operation* op, ArrayRef<Type> result_types, Value *
ArrayRef<Value*> operands, ConversionPatternRewriter& rewriter) { mapToLowerScalarOp<ONNXReciprocalOp>(Operation *op, ArrayRef<Type> result_types,
ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) {
// ONNXReciprocalOp(%X) = DivFOp(ConstantOp 1, %X) // ONNXReciprocalOp(%X) = DivFOp(ConstantOp 1, %X)
auto loc = op->getLoc(); auto loc = op->getLoc();
Value *operand = operands[0]; Value *operand = operands[0];
@ -566,7 +578,8 @@ Value* mapToLowerScalarOp<ONNXReciprocalOp>(Operation* op, ArrayRef<Type> result
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXMaxOp>(Operation *op, ArrayRef<Type> result_types, Value *mapToLowerScalarOp<ONNXMaxOp>(Operation *op, ArrayRef<Type> result_types,
ArrayRef<Value*> operands, ConversionPatternRewriter& rewriter) { ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) {
// ONNXMaxOp(%X, %Y) = SelectOp(CmpFOp(OGT, %X, %Y), // ONNXMaxOp(%X, %Y) = SelectOp(CmpFOp(OGT, %X, %Y),
// %X, // %X,
// %Y) // %Y)
@ -583,7 +596,8 @@ Value* mapToLowerScalarOp<ONNXMaxOp>(Operation* op, ArrayRef<Type> result_types,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
template <> template <>
Value *mapToLowerScalarOp<ONNXMinOp>(Operation *op, ArrayRef<Type> result_types, Value *mapToLowerScalarOp<ONNXMinOp>(Operation *op, ArrayRef<Type> result_types,
ArrayRef<Value*> operands, ConversionPatternRewriter& rewriter) { ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) {
// ONNXMinOp(%X, %Y) = SelectOp(CmpFOp(OLT, %X, %Y), // ONNXMinOp(%X, %Y) = SelectOp(CmpFOp(OLT, %X, %Y),
// %X, // %X,
// %Y) // %Y)
@ -601,7 +615,8 @@ template <typename ElementwiseUnaryOp>
struct ONNXElementwiseUnaryOpLowering : public ConversionPattern { struct ONNXElementwiseUnaryOpLowering : public ConversionPattern {
ONNXElementwiseUnaryOpLowering(MLIRContext *ctx) ONNXElementwiseUnaryOpLowering(MLIRContext *ctx)
: ConversionPattern(ElementwiseUnaryOp::getOperationName(), 1, ctx) {} : ConversionPattern(ElementwiseUnaryOp::getOperationName(), 1, ctx) {}
PatternMatchResult matchAndRewrite(Operation* op, ArrayRef<Value*> operands, PatternMatchResult
matchAndRewrite(Operation *op, ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) const final { ConversionPatternRewriter &rewriter) const final {
// TODO: Check that the types are valid. // TODO: Check that the types are valid.
// An element-wise unary operation must have all operands and the result of // An element-wise unary operation must have all operands and the result of
@ -624,8 +639,8 @@ struct ONNXElementwiseUnaryOpLowering : public ConversionPattern {
if (hasAllConstantDimensions(memRefType)) if (hasAllConstantDimensions(memRefType))
alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc); alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc);
else else
alloc = insertAllocAndDealloc( alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc,
memRefType, loc, rewriter, insertDealloc, {operands[0]}); {operands[0]});
// Number of loops // Number of loops
auto memRefShape = memRefType.getShape(); auto memRefShape = memRefType.getShape();
@ -703,7 +718,8 @@ template <typename ElementwiseVariadicOp>
struct ONNXElementwiseVariadicOpLowering : public ConversionPattern { struct ONNXElementwiseVariadicOpLowering : public ConversionPattern {
ONNXElementwiseVariadicOpLowering(MLIRContext *ctx) ONNXElementwiseVariadicOpLowering(MLIRContext *ctx)
: ConversionPattern(ElementwiseVariadicOp::getOperationName(), 1, ctx) {} : ConversionPattern(ElementwiseVariadicOp::getOperationName(), 1, ctx) {}
PatternMatchResult matchAndRewrite(Operation* op, ArrayRef<Value*> operands, PatternMatchResult
matchAndRewrite(Operation *op, ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) const final { ConversionPatternRewriter &rewriter) const final {
// TODO: Check that the types are valid. // TODO: Check that the types are valid.
// An element-wise variadic operation must have all operands and the result // An element-wise variadic operation must have all operands and the result
@ -725,8 +741,8 @@ struct ONNXElementwiseVariadicOpLowering : public ConversionPattern {
if (hasAllConstantDimensions(memRefType)) if (hasAllConstantDimensions(memRefType))
alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc); alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc);
else else
alloc = insertAllocAndDealloc( alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc,
memRefType, loc, rewriter, insertDealloc, operands); operands);
// Number of loops // Number of loops
auto memRefShape = memRefType.getShape(); auto memRefShape = memRefType.getShape();
@ -815,7 +831,8 @@ struct ONNXReshapeOpLowering : public ConversionPattern {
ONNXReshapeOpLowering(MLIRContext *ctx) ONNXReshapeOpLowering(MLIRContext *ctx)
: ConversionPattern(mlir::ONNXReshapeOp::getOperationName(), 1, ctx) {} : ConversionPattern(mlir::ONNXReshapeOp::getOperationName(), 1, ctx) {}
PatternMatchResult matchAndRewrite(Operation* op, ArrayRef<Value*> operands, PatternMatchResult
matchAndRewrite(Operation *op, ArrayRef<Value *> operands,
ConversionPatternRewriter &rewriter) const final { ConversionPatternRewriter &rewriter) const final {
auto tensorType = (*op->result_type_begin()).cast<TensorType>(); auto tensorType = (*op->result_type_begin()).cast<TensorType>();
auto loc = op->getLoc(); auto loc = op->getLoc();
@ -825,9 +842,9 @@ struct ONNXReshapeOpLowering : public ConversionPattern {
Value *alloc; Value *alloc;
// Compute size in bytes. // Compute size in bytes.
Value* tensorSize = rewriter.create<ConstantOp>(loc, Value *tensorSize = rewriter.create<ConstantOp>(
rewriter.getIntegerAttr( loc, rewriter.getIntegerAttr(rewriter.getIntegerType(64),
rewriter.getIntegerType(64), getMemRefEltSizeInBytes(memRefType))); getMemRefEltSizeInBytes(memRefType)));
bool insertDealloc = checkInsertDealloc(op); bool insertDealloc = checkInsertDealloc(op);
if (hasAllConstantDimensions(memRefType)) { if (hasAllConstantDimensions(memRefType)) {
alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc); alloc = insertAllocAndDealloc(memRefType, loc, rewriter, insertDealloc);
@ -889,8 +906,8 @@ struct TensorTypeConverter : public TypeConverter {
/// inputs. Once unranked results can be handled gracefully this /// inputs. Once unranked results can be handled gracefully this
/// override needs to be removed in favour of the original MLIR one.] /// override needs to be removed in favour of the original MLIR one.]
bool isSignatureLegal(FunctionType funcType) { bool isSignatureLegal(FunctionType funcType) {
return llvm::all_of( return llvm::all_of(funcType.getInputs(),
funcType.getInputs(), [this](Type type) { return isLegal(type); }); [this](Type type) { return isLegal(type); });
} }
}; };
@ -943,8 +960,8 @@ void FrontendToKrnlLoweringPass::runOnModule() {
// Type conversion for function signatures. // Type conversion for function signatures.
// Call MLIR FuncOp signature conversion when result type is // Call MLIR FuncOp signature conversion when result type is
// a ranked tensor. // a ranked tensor.
populateFuncOpTypeConversionPattern( populateFuncOpTypeConversionPattern(patterns, &getContext(),
patterns, &getContext(), tensor_to_memref_converter); tensor_to_memref_converter);
// Frontent operation lowering. // Frontent operation lowering.
patterns.insert<ONNXElementwiseUnaryOpLowering<mlir::ONNXExpOp>, patterns.insert<ONNXElementwiseUnaryOpLowering<mlir::ONNXExpOp>,
@ -981,5 +998,5 @@ std::unique_ptr<Pass> mlir::createLowerToKrnlPass() {
return std::make_unique<FrontendToKrnlLoweringPass>(); return std::make_unique<FrontendToKrnlLoweringPass>();
} }
static PassRegistration<FrontendToKrnlLoweringPass> pass( static PassRegistration<FrontendToKrnlLoweringPass>
"lower-frontend", "Lower frontend ops to Krnl dialect."); pass("lower-frontend", "Lower frontend ops to Krnl dialect.");

View File

@ -17,8 +17,8 @@ namespace {
struct KrnlIterateOpLowering : public OpRewritePattern<KrnlIterateOp> { struct KrnlIterateOpLowering : public OpRewritePattern<KrnlIterateOp> {
using OpRewritePattern<KrnlIterateOp>::OpRewritePattern; using OpRewritePattern<KrnlIterateOp>::OpRewritePattern;
PatternMatchResult matchAndRewrite( PatternMatchResult matchAndRewrite(KrnlIterateOp iterateOp,
KrnlIterateOp iterateOp, PatternRewriter& rewriter) const override { PatternRewriter &rewriter) const override {
auto boundMapAttrs = auto boundMapAttrs =
iterateOp.getAttrOfType<ArrayAttr>(KrnlIterateOp::getBoundsAttrName()) iterateOp.getAttrOfType<ArrayAttr>(KrnlIterateOp::getBoundsAttrName())
.getValue(); .getValue();
@ -38,8 +38,8 @@ struct KrnlIterateOpLowering : public OpRewritePattern<KrnlIterateOp> {
map = boundMapAttrs[boundIdx + boundType] map = boundMapAttrs[boundIdx + boundType]
.cast<AffineMapAttr>() .cast<AffineMapAttr>()
.getValue(); .getValue();
operands.insert( operands.insert(operands.end(), operandItr,
operands.end(), operandItr, operandItr + map.getNumInputs()); operandItr + map.getNumInputs());
std::advance(operandItr, map.getNumInputs()); std::advance(operandItr, map.getNumInputs());
} }
@ -83,8 +83,8 @@ class KrnlTerminatorLowering : public OpRewritePattern<KrnlTerminatorOp> {
public: public:
using OpRewritePattern<KrnlTerminatorOp>::OpRewritePattern; using OpRewritePattern<KrnlTerminatorOp>::OpRewritePattern;
PatternMatchResult matchAndRewrite( PatternMatchResult matchAndRewrite(KrnlTerminatorOp op,
KrnlTerminatorOp op, PatternRewriter& rewriter) const override { PatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<AffineTerminatorOp>(op); rewriter.replaceOpWithNewOp<AffineTerminatorOp>(op);
return matchSuccess(); return matchSuccess();
} }
@ -98,8 +98,8 @@ class KrnlDefineLoopsLowering : public OpRewritePattern<KrnlDefineLoopsOp> {
public: public:
using OpRewritePattern<KrnlDefineLoopsOp>::OpRewritePattern; using OpRewritePattern<KrnlDefineLoopsOp>::OpRewritePattern;
PatternMatchResult matchAndRewrite( PatternMatchResult matchAndRewrite(KrnlDefineLoopsOp op,
KrnlDefineLoopsOp op, PatternRewriter& rewriter) const override { PatternRewriter &rewriter) const override {
rewriter.eraseOp(op); rewriter.eraseOp(op);
return matchSuccess(); return matchSuccess();
} }
@ -113,8 +113,8 @@ class KrnlOptimizeLoopsLowering : public OpRewritePattern<KrnlOptimizeLoopsOp> {
public: public:
using OpRewritePattern<KrnlOptimizeLoopsOp>::OpRewritePattern; using OpRewritePattern<KrnlOptimizeLoopsOp>::OpRewritePattern;
PatternMatchResult matchAndRewrite( PatternMatchResult matchAndRewrite(KrnlOptimizeLoopsOp op,
KrnlOptimizeLoopsOp op, PatternRewriter& rewriter) const override { PatternRewriter &rewriter) const override {
rewriter.eraseOp(op); rewriter.eraseOp(op);
return matchSuccess(); return matchSuccess();
} }
@ -146,7 +146,8 @@ void KrnlToAffineLoweringPass::runOnFunction() {
OwningRewritePatternList patterns; OwningRewritePatternList patterns;
patterns.insert<KrnlIterateOpLowering, KrnlTerminatorLowering, patterns.insert<KrnlIterateOpLowering, KrnlTerminatorLowering,
KrnlDefineLoopsLowering, KrnlOptimizeLoopsLowering>(&getContext()); KrnlDefineLoopsLowering, KrnlOptimizeLoopsLowering>(
&getContext());
if (failed(applyPartialConversion(getFunction(), target, patterns))) if (failed(applyPartialConversion(getFunction(), target, patterns)))
signalPassFailure(); signalPassFailure();
@ -158,5 +159,5 @@ std::unique_ptr<Pass> mlir::createLowerKrnlPass() {
return std::make_unique<KrnlToAffineLoweringPass>(); return std::make_unique<KrnlToAffineLoweringPass>();
} }
static PassRegistration<KrnlToAffineLoweringPass> pass( static PassRegistration<KrnlToAffineLoweringPass> pass("lower-krnl",
"lower-krnl", "Lower Krnl dialect."); "Lower Krnl dialect.");

View File

@ -91,8 +91,8 @@ int main(int ac, char* av[]) {
po::positional_options_description p; po::positional_options_description p;
p.add("onnx-model", -1); p.add("onnx-model", -1);
po::variables_map vm; po::variables_map vm;
po::store( po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(),
po::command_line_parser(ac, av).options(desc).positional(p).run(), vm); vm);
// TODO: allow multiple input files // TODO: allow multiple input files
assert(vm.count("onnx-model") < 2 && "At most one input file can be provided!"); assert(vm.count("onnx-model") < 2 && "At most one input file can be provided!");
@ -137,10 +137,10 @@ int main(int ac, char* av[]) {
// Write LLVM bitcode to disk. // Write LLVM bitcode to disk.
std::error_code EC; std::error_code EC;
llvm::raw_fd_ostream moduleBitcodeStream( llvm::raw_fd_ostream moduleBitcodeStream("model.bc", EC,
"model.bc", EC, llvm::sys::fs::F_None); llvm::sys::fs::F_None);
llvm::WriteBitcodeToFile( llvm::WriteBitcodeToFile(*mlir::translateModuleToLLVMIR(*module),
*mlir::translateModuleToLLVMIR(*module), moduleBitcodeStream); moduleBitcodeStream);
moduleBitcodeStream.flush(); moduleBitcodeStream.flush();
return 0; return 0;