2018-07-08 20:51:38 -07:00
|
|
|
//===- Builders.cpp - Helpers for constructing MLIR Classes ---------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
#include "mlir/IR/Builders.h"
|
2018-07-10 10:59:53 -07:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
|
|
|
#include "mlir/IR/Attributes.h"
|
2018-08-07 14:24:38 -07:00
|
|
|
#include "mlir/IR/IntegerSet.h"
|
2018-08-27 21:05:16 -07:00
|
|
|
#include "mlir/IR/Location.h"
|
2018-07-08 20:51:38 -07:00
|
|
|
#include "mlir/IR/Module.h"
|
|
|
|
#include "mlir/IR/Types.h"
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
Builder::Builder(Module *module) : context(module->getContext()) {}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
Identifier Builder::getIdentifier(StringRef str) {
|
|
|
|
return Identifier::get(str, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *Builder::createModule() { return new Module(context); }
|
|
|
|
|
2018-08-27 21:05:16 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Locations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
UnknownLoc *Builder::getUnknownLoc() { return UnknownLoc::get(context); }
|
|
|
|
|
|
|
|
UniquedFilename Builder::getUniquedFilename(StringRef filename) {
|
|
|
|
return UniquedFilename::get(filename, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLineColLoc *Builder::getFileLineColLoc(UniquedFilename filename,
|
|
|
|
unsigned line, unsigned column) {
|
|
|
|
return FileLineColLoc::get(filename, line, column, context);
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-08 20:51:38 -07:00
|
|
|
// Types.
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
Eliminate "primitive" types from being a thing, splitting them into FloatType
and OtherType. Other type is now the thing that holds AffineInt, Control,
eventually Resource, Variant, String, etc. FloatType holds the floating point
types, and allows convenient query of isa<FloatType>().
This fixes issues where we allowed control to be the element type of tensor,
memref, vector. At the same time, ban AffineInt from being an element of a
vector/memref/tensor as well since we don't need it.
I updated the spec to match this as well.
PiperOrigin-RevId: 206361942
2018-07-27 13:09:58 -07:00
|
|
|
FloatType *Builder::getBF16Type() { return Type::getBF16(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
Eliminate "primitive" types from being a thing, splitting them into FloatType
and OtherType. Other type is now the thing that holds AffineInt, Control,
eventually Resource, Variant, String, etc. FloatType holds the floating point
types, and allows convenient query of isa<FloatType>().
This fixes issues where we allowed control to be the element type of tensor,
memref, vector. At the same time, ban AffineInt from being an element of a
vector/memref/tensor as well since we don't need it.
I updated the spec to match this as well.
PiperOrigin-RevId: 206361942
2018-07-27 13:09:58 -07:00
|
|
|
FloatType *Builder::getF16Type() { return Type::getF16(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
Eliminate "primitive" types from being a thing, splitting them into FloatType
and OtherType. Other type is now the thing that holds AffineInt, Control,
eventually Resource, Variant, String, etc. FloatType holds the floating point
types, and allows convenient query of isa<FloatType>().
This fixes issues where we allowed control to be the element type of tensor,
memref, vector. At the same time, ban AffineInt from being an element of a
vector/memref/tensor as well since we don't need it.
I updated the spec to match this as well.
PiperOrigin-RevId: 206361942
2018-07-27 13:09:58 -07:00
|
|
|
FloatType *Builder::getF32Type() { return Type::getF32(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
Eliminate "primitive" types from being a thing, splitting them into FloatType
and OtherType. Other type is now the thing that holds AffineInt, Control,
eventually Resource, Variant, String, etc. FloatType holds the floating point
types, and allows convenient query of isa<FloatType>().
This fixes issues where we allowed control to be the element type of tensor,
memref, vector. At the same time, ban AffineInt from being an element of a
vector/memref/tensor as well since we don't need it.
I updated the spec to match this as well.
PiperOrigin-RevId: 206361942
2018-07-27 13:09:58 -07:00
|
|
|
FloatType *Builder::getF64Type() { return Type::getF64(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2018-10-06 17:21:53 -07:00
|
|
|
OtherType *Builder::getIndexType() { return Type::getIndex(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
Eliminate "primitive" types from being a thing, splitting them into FloatType
and OtherType. Other type is now the thing that holds AffineInt, Control,
eventually Resource, Variant, String, etc. FloatType holds the floating point
types, and allows convenient query of isa<FloatType>().
This fixes issues where we allowed control to be the element type of tensor,
memref, vector. At the same time, ban AffineInt from being an element of a
vector/memref/tensor as well since we don't need it.
I updated the spec to match this as well.
PiperOrigin-RevId: 206361942
2018-07-27 13:09:58 -07:00
|
|
|
OtherType *Builder::getTFControlType() { return Type::getTFControl(context); }
|
2018-07-27 11:07:12 -07:00
|
|
|
|
2018-09-19 10:28:46 -07:00
|
|
|
OtherType *Builder::getTFResourceType() { return Type::getTFResource(context); }
|
|
|
|
|
2018-09-19 21:15:43 -07:00
|
|
|
OtherType *Builder::getTFVariantType() { return Type::getTFVariant(context); }
|
|
|
|
|
2018-09-20 11:59:17 -07:00
|
|
|
OtherType *Builder::getTFComplex64Type() {
|
|
|
|
return Type::getTFComplex64(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
OtherType *Builder::getTFComplex128Type() {
|
|
|
|
return Type::getTFComplex128(context);
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:10:45 -07:00
|
|
|
OtherType *Builder::getTFF32REFType() { return Type::getTFF32REF(context); }
|
|
|
|
|
2018-08-01 12:55:27 -07:00
|
|
|
OtherType *Builder::getTFStringType() { return Type::getTFString(context); }
|
|
|
|
|
2018-07-08 20:51:38 -07:00
|
|
|
IntegerType *Builder::getIntegerType(unsigned width) {
|
|
|
|
return Type::getInteger(width, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionType *Builder::getFunctionType(ArrayRef<Type *> inputs,
|
|
|
|
ArrayRef<Type *> results) {
|
|
|
|
return FunctionType::get(inputs, results, context);
|
|
|
|
}
|
|
|
|
|
2018-08-10 11:56:47 -07:00
|
|
|
MemRefType *Builder::getMemRefType(ArrayRef<int> shape, Type *elementType,
|
2018-10-09 16:39:24 -07:00
|
|
|
ArrayRef<AffineMap> affineMapComposition,
|
2018-08-10 11:56:47 -07:00
|
|
|
unsigned memorySpace) {
|
|
|
|
return MemRefType::get(shape, elementType, affineMapComposition, memorySpace);
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:49:39 -07:00
|
|
|
VectorType *Builder::getVectorType(ArrayRef<int> shape, Type *elementType) {
|
2018-07-08 20:51:38 -07:00
|
|
|
return VectorType::get(shape, elementType);
|
|
|
|
}
|
|
|
|
|
|
|
|
RankedTensorType *Builder::getTensorType(ArrayRef<int> shape,
|
|
|
|
Type *elementType) {
|
|
|
|
return RankedTensorType::get(shape, elementType);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnrankedTensorType *Builder::getTensorType(Type *elementType) {
|
|
|
|
return UnrankedTensorType::get(elementType);
|
|
|
|
}
|
2018-07-10 10:59:53 -07:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attributes.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
BoolAttr *Builder::getBoolAttr(bool value) {
|
|
|
|
return BoolAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntegerAttr *Builder::getIntegerAttr(int64_t value) {
|
|
|
|
return IntegerAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
FloatAttr *Builder::getFloatAttr(double value) {
|
|
|
|
return FloatAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringAttr *Builder::getStringAttr(StringRef bytes) {
|
|
|
|
return StringAttr::get(bytes, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) {
|
|
|
|
return ArrayAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMapAttr *Builder::getAffineMapAttr(AffineMap map) {
|
|
|
|
return AffineMapAttr::get(map);
|
2018-07-18 16:29:21 -07:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:54:46 -07:00
|
|
|
TypeAttr *Builder::getTypeAttr(Type *type) {
|
|
|
|
return TypeAttr::get(type, context);
|
|
|
|
}
|
|
|
|
|
2018-08-21 17:55:22 -07:00
|
|
|
FunctionAttr *Builder::getFunctionAttr(const Function *value) {
|
2018-08-19 21:17:22 -07:00
|
|
|
return FunctionAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-08-07 14:24:38 -07:00
|
|
|
// Affine Expressions, Affine Maps, and Integet Sets.
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getAffineMap(unsigned dimCount, unsigned symbolCount,
|
|
|
|
ArrayRef<AffineExpr> results,
|
|
|
|
ArrayRef<AffineExpr> rangeSizes) {
|
2018-10-08 13:47:18 -07:00
|
|
|
return AffineMap::get(dimCount, symbolCount, results, rangeSizes);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-08 13:47:18 -07:00
|
|
|
AffineExpr Builder::getAffineDimExpr(unsigned position) {
|
2018-10-08 10:20:25 -07:00
|
|
|
return mlir::getAffineDimExpr(position, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-08 13:47:18 -07:00
|
|
|
AffineExpr Builder::getAffineSymbolExpr(unsigned position) {
|
2018-10-08 10:20:25 -07:00
|
|
|
return mlir::getAffineSymbolExpr(position, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-08 13:47:18 -07:00
|
|
|
AffineExpr Builder::getAffineConstantExpr(int64_t constant) {
|
2018-10-08 10:20:25 -07:00
|
|
|
return mlir::getAffineConstantExpr(constant, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-08-07 14:24:38 -07:00
|
|
|
IntegerSet *Builder::getIntegerSet(unsigned dimCount, unsigned symbolCount,
|
2018-10-08 13:47:18 -07:00
|
|
|
ArrayRef<AffineExpr> constraints,
|
2018-08-07 14:24:38 -07:00
|
|
|
ArrayRef<bool> isEq) {
|
|
|
|
return IntegerSet::get(dimCount, symbolCount, constraints, isEq, context);
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getConstantAffineMap(int64_t val) {
|
2018-10-03 15:39:12 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0,
|
2018-10-08 13:47:18 -07:00
|
|
|
{getAffineConstantExpr(val)}, {});
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getDimIdentityMap() {
|
2018-10-08 10:20:25 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0,
|
2018-10-08 13:47:18 -07:00
|
|
|
{getAffineDimExpr(0)}, {});
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getMultiDimIdentityMap(unsigned rank) {
|
2018-10-08 13:47:18 -07:00
|
|
|
SmallVector<AffineExpr, 4> dimExprs;
|
2018-10-08 11:10:11 -07:00
|
|
|
dimExprs.reserve(rank);
|
|
|
|
for (unsigned i = 0; i < rank; ++i)
|
|
|
|
dimExprs.push_back(getAffineDimExpr(i));
|
2018-10-08 13:47:18 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs, {});
|
2018-10-08 11:10:11 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getSymbolIdentityMap() {
|
2018-10-08 10:20:25 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1,
|
2018-10-08 13:47:18 -07:00
|
|
|
{getAffineSymbolExpr(0)}, {});
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getSingleDimShiftAffineMap(int64_t shift) {
|
2018-10-03 15:34:57 -07:00
|
|
|
// expr = d0 + shift.
|
2018-10-08 10:20:25 -07:00
|
|
|
auto expr = getAffineDimExpr(0) + shift;
|
2018-10-08 13:47:18 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, {expr}, {});
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) {
|
2018-10-08 13:47:18 -07:00
|
|
|
SmallVector<AffineExpr, 4> shiftedResults;
|
2018-10-09 16:39:24 -07:00
|
|
|
shiftedResults.reserve(map.getNumResults());
|
|
|
|
for (auto resultExpr : map.getResults()) {
|
2018-10-09 10:59:27 -07:00
|
|
|
shiftedResults.push_back(resultExpr + shift);
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
2018-10-09 16:39:24 -07:00
|
|
|
return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults,
|
|
|
|
map.getRangeSizes());
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
|
|
|
|
2018-07-19 09:52:39 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-24 10:15:13 -07:00
|
|
|
// CFG function elements.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-24 21:13:19 -07:00
|
|
|
/// Add new basic block and set the insertion point to the end of it. If an
|
|
|
|
/// 'insertBefore' basic block is passed, the block will be placed before the
|
|
|
|
/// specified block. If not, the block will be appended to the end of the
|
|
|
|
/// current function.
|
|
|
|
BasicBlock *CFGFuncBuilder::createBlock(BasicBlock *insertBefore) {
|
2018-07-24 10:15:13 -07:00
|
|
|
BasicBlock *b = new BasicBlock();
|
2018-08-24 21:13:19 -07:00
|
|
|
|
|
|
|
// If we are supposed to insert before a specific block, do so, otherwise add
|
|
|
|
// the block to the end of the function.
|
|
|
|
if (insertBefore)
|
|
|
|
function->getBlocks().insert(CFGFunction::iterator(insertBefore), b);
|
|
|
|
else
|
|
|
|
function->push_back(b);
|
|
|
|
|
2018-07-24 10:15:13 -07:00
|
|
|
setInsertionPoint(b);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2018-08-07 09:12:35 -07:00
|
|
|
/// Create an operation given the fields represented as an OperationState.
|
|
|
|
OperationInst *CFGFuncBuilder::createOperation(const OperationState &state) {
|
|
|
|
SmallVector<CFGValue *, 8> operands;
|
|
|
|
operands.reserve(state.operands.size());
|
|
|
|
for (auto elt : state.operands)
|
|
|
|
operands.push_back(cast<CFGValue>(elt));
|
|
|
|
|
2018-08-23 14:32:25 -07:00
|
|
|
auto *op = OperationInst::create(state.location, state.name, operands,
|
|
|
|
state.types, state.attributes, context);
|
2018-08-07 09:12:35 -07:00
|
|
|
block->getOperations().insert(insertPoint, op);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2018-07-24 10:15:13 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Statements.
|
2018-07-19 09:52:39 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-07 09:12:35 -07:00
|
|
|
/// Create an operation given the fields represented as an OperationState.
|
|
|
|
OperationStmt *MLFuncBuilder::createOperation(const OperationState &state) {
|
|
|
|
SmallVector<MLValue *, 8> operands;
|
|
|
|
operands.reserve(state.operands.size());
|
|
|
|
for (auto elt : state.operands)
|
|
|
|
operands.push_back(cast<MLValue>(elt));
|
|
|
|
|
2018-08-23 14:32:25 -07:00
|
|
|
auto *op = OperationStmt::create(state.location, state.name, operands,
|
|
|
|
state.types, state.attributes, context);
|
2018-08-07 09:12:35 -07:00
|
|
|
block->getStatements().insert(insertPoint, op);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2018-10-04 17:15:30 -07:00
|
|
|
/// Create an operation given the fields.
|
|
|
|
OperationStmt *MLFuncBuilder::createOperation(Location *location,
|
2018-10-09 22:08:52 -07:00
|
|
|
OperationName name,
|
2018-10-04 17:15:30 -07:00
|
|
|
ArrayRef<MLValue *> operands,
|
|
|
|
ArrayRef<Type *> types,
|
|
|
|
ArrayRef<NamedAttribute> attrs) {
|
|
|
|
auto *op = OperationStmt::create(location, name, operands, types, attrs,
|
|
|
|
getContext());
|
|
|
|
block->getStatements().insert(insertPoint, op);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2018-08-27 21:05:16 -07:00
|
|
|
ForStmt *MLFuncBuilder::createFor(Location *location,
|
2018-08-24 23:38:14 -07:00
|
|
|
ArrayRef<MLValue *> lbOperands,
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap lbMap,
|
2018-08-24 23:38:14 -07:00
|
|
|
ArrayRef<MLValue *> ubOperands,
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap ubMap, int64_t step) {
|
|
|
|
auto *stmt =
|
|
|
|
ForStmt::create(location, lbOperands, lbMap, ubOperands, ubMap, step);
|
2018-08-23 14:32:25 -07:00
|
|
|
block->getStatements().insert(insertPoint, stmt);
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:12:38 -07:00
|
|
|
ForStmt *MLFuncBuilder::createFor(Location *location, int64_t lb, int64_t ub,
|
|
|
|
int64_t step) {
|
2018-10-09 16:39:24 -07:00
|
|
|
auto lbMap = AffineMap::getConstantMap(lb, context);
|
|
|
|
auto ubMap = AffineMap::getConstantMap(ub, context);
|
2018-09-13 08:12:38 -07:00
|
|
|
return createFor(location, {}, lbMap, {}, ubMap, step);
|
|
|
|
}
|
|
|
|
|
2018-08-28 15:26:20 -07:00
|
|
|
IfStmt *MLFuncBuilder::createIf(Location *location,
|
|
|
|
ArrayRef<MLValue *> operands, IntegerSet *set) {
|
|
|
|
auto *stmt = IfStmt::create(location, operands, set);
|
2018-07-31 23:14:16 -07:00
|
|
|
block->getStatements().insert(insertPoint, stmt);
|
2018-07-19 09:52:39 -07:00
|
|
|
return stmt;
|
|
|
|
}
|