2018-07-09 11:51:38 +08: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-11 01:59:53 +08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
|
|
|
#include "mlir/IR/Attributes.h"
|
2018-08-08 05:24:38 +08:00
|
|
|
#include "mlir/IR/IntegerSet.h"
|
2018-08-28 12:05:16 +08:00
|
|
|
#include "mlir/IR/Location.h"
|
2018-07-09 11:51:38 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
|
|
|
#include "mlir/IR/Types.h"
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
Builder::Builder(Module *module) : context(module->getContext()) {}
|
|
|
|
|
2018-07-11 01:59:53 +08:00
|
|
|
Identifier Builder::getIdentifier(StringRef str) {
|
|
|
|
return Identifier::get(str, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *Builder::createModule() { return new Module(context); }
|
|
|
|
|
2018-08-28 12:05:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Locations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-09 04:28:35 +08:00
|
|
|
UnknownLoc Builder::getUnknownLoc() { return UnknownLoc::get(context); }
|
2018-08-28 12:05:16 +08:00
|
|
|
|
|
|
|
UniquedFilename Builder::getUniquedFilename(StringRef filename) {
|
|
|
|
return UniquedFilename::get(filename, context);
|
|
|
|
}
|
|
|
|
|
2018-11-09 04:28:35 +08:00
|
|
|
FileLineColLoc Builder::getFileLineColLoc(UniquedFilename filename,
|
|
|
|
unsigned line, unsigned column) {
|
2018-08-28 12:05:16 +08:00
|
|
|
return FileLineColLoc::get(filename, line, column, context);
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:27:28 +08:00
|
|
|
Location Builder::getFusedLoc(ArrayRef<Location> locs, Attribute metadata) {
|
|
|
|
return FusedLoc::get(locs, metadata, context);
|
|
|
|
}
|
|
|
|
|
2018-07-11 01:59:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-09 11:51:38 +08:00
|
|
|
// Types.
|
2018-07-11 01:59:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
FloatType Builder::getBF16Type() { return Type::getBF16(context); }
|
2018-07-09 11:51:38 +08:00
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
FloatType Builder::getF16Type() { return Type::getF16(context); }
|
2018-07-09 11:51:38 +08:00
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
FloatType Builder::getF32Type() { return Type::getF32(context); }
|
2018-07-09 11:51:38 +08:00
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
FloatType Builder::getF64Type() { return Type::getF64(context); }
|
2018-07-09 11:51:38 +08:00
|
|
|
|
2018-11-08 20:04:13 +08:00
|
|
|
IndexType Builder::getIndexType() { return Type::getIndex(context); }
|
2018-07-09 11:51:38 +08:00
|
|
|
|
2018-11-29 03:49:26 +08:00
|
|
|
IntegerType Builder::getI1Type() { return Type::getInteger(1, context); }
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
IntegerType Builder::getIntegerType(unsigned width) {
|
2018-07-09 11:51:38 +08:00
|
|
|
return Type::getInteger(width, context);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
FunctionType Builder::getFunctionType(ArrayRef<Type> inputs,
|
|
|
|
ArrayRef<Type> results) {
|
2018-07-09 11:51:38 +08:00
|
|
|
return FunctionType::get(inputs, results, context);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
MemRefType Builder::getMemRefType(ArrayRef<int> shape, Type elementType,
|
|
|
|
ArrayRef<AffineMap> affineMapComposition,
|
|
|
|
unsigned memorySpace) {
|
2018-08-11 02:56:47 +08:00
|
|
|
return MemRefType::get(shape, elementType, affineMapComposition, memorySpace);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
VectorType Builder::getVectorType(ArrayRef<int> shape, Type elementType) {
|
2018-07-09 11:51:38 +08:00
|
|
|
return VectorType::get(shape, elementType);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
RankedTensorType Builder::getTensorType(ArrayRef<int> shape, Type elementType) {
|
2018-07-09 11:51:38 +08:00
|
|
|
return RankedTensorType::get(shape, elementType);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
UnrankedTensorType Builder::getTensorType(Type elementType) {
|
2018-07-09 11:51:38 +08:00
|
|
|
return UnrankedTensorType::get(elementType);
|
|
|
|
}
|
2018-07-11 01:59:53 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attributes.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
BoolAttr Builder::getBoolAttr(bool value) {
|
2018-07-11 01:59:53 +08:00
|
|
|
return BoolAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
2018-12-27 03:48:58 +08:00
|
|
|
IntegerAttr Builder::getI64IntegerAttr(int64_t value) {
|
2018-11-16 09:53:51 +08:00
|
|
|
return IntegerAttr::get(getIntegerType(64), APInt(64, value));
|
|
|
|
}
|
|
|
|
|
2018-12-27 03:48:58 +08:00
|
|
|
IntegerAttr Builder::getI32IntegerAttr(int32_t value) {
|
|
|
|
return IntegerAttr::get(getIntegerType(32), APInt(32, value));
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:53:51 +08:00
|
|
|
IntegerAttr Builder::getIntegerAttr(Type type, int64_t value) {
|
|
|
|
if (type.isIndex())
|
|
|
|
return IntegerAttr::get(type, APInt(64, value));
|
2018-12-18 02:05:56 +08:00
|
|
|
return IntegerAttr::get(type, APInt(type.getIntOrFloatBitWidth(), value));
|
2018-11-16 09:53:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IntegerAttr Builder::getIntegerAttr(Type type, const APInt &value) {
|
|
|
|
return IntegerAttr::get(type, value);
|
2018-07-11 01:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-12-27 03:48:58 +08:00
|
|
|
FloatAttr Builder::getF64FloatAttr(double value) {
|
2018-12-17 23:19:53 +08:00
|
|
|
return FloatAttr::get(getF64Type(), APFloat(value));
|
|
|
|
}
|
|
|
|
|
2018-12-27 03:48:58 +08:00
|
|
|
FloatAttr Builder::getF32FloatAttr(float value) {
|
2018-11-16 09:53:51 +08:00
|
|
|
return FloatAttr::get(getF32Type(), APFloat(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
FloatAttr Builder::getFloatAttr(Type type, double value) {
|
2018-12-17 23:19:53 +08:00
|
|
|
return FloatAttr::get(type, value);
|
2018-10-21 09:31:49 +08:00
|
|
|
}
|
|
|
|
|
2018-11-16 09:53:51 +08:00
|
|
|
FloatAttr Builder::getFloatAttr(Type type, const APFloat &value) {
|
|
|
|
return FloatAttr::get(type, value);
|
2018-07-11 01:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
StringAttr Builder::getStringAttr(StringRef bytes) {
|
2018-07-11 01:59:53 +08:00
|
|
|
return StringAttr::get(bytes, context);
|
|
|
|
}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
ArrayAttr Builder::getArrayAttr(ArrayRef<Attribute> value) {
|
2018-07-11 01:59:53 +08:00
|
|
|
return ArrayAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
AffineMapAttr Builder::getAffineMapAttr(AffineMap map) {
|
2018-10-10 07:39:24 +08:00
|
|
|
return AffineMapAttr::get(map);
|
2018-07-19 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-26 13:13:03 +08:00
|
|
|
IntegerSetAttr Builder::getIntegerSetAttr(IntegerSet set) {
|
|
|
|
return IntegerSetAttr::get(set);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
TypeAttr Builder::getTypeAttr(Type type) {
|
2018-08-03 16:54:46 +08:00
|
|
|
return TypeAttr::get(type, context);
|
|
|
|
}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
FunctionAttr Builder::getFunctionAttr(const Function *value) {
|
2018-08-20 12:17:22 +08:00
|
|
|
return FunctionAttr::get(value, context);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
ElementsAttr Builder::getSplatElementsAttr(VectorOrTensorType type,
|
2018-10-26 06:46:10 +08:00
|
|
|
Attribute elt) {
|
2018-10-10 23:57:51 +08:00
|
|
|
return SplatElementsAttr::get(type, elt);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
ElementsAttr Builder::getDenseElementsAttr(VectorOrTensorType type,
|
2018-10-26 06:46:10 +08:00
|
|
|
ArrayRef<char> data) {
|
2018-10-19 04:54:44 +08:00
|
|
|
return DenseElementsAttr::get(type, data);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
ElementsAttr Builder::getSparseElementsAttr(VectorOrTensorType type,
|
2018-10-26 06:46:10 +08:00
|
|
|
DenseIntElementsAttr indices,
|
|
|
|
DenseElementsAttr values) {
|
2018-10-24 04:44:04 +08:00
|
|
|
return SparseElementsAttr::get(type, indices, values);
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
ElementsAttr Builder::getOpaqueElementsAttr(VectorOrTensorType type,
|
2018-10-26 06:46:10 +08:00
|
|
|
StringRef bytes) {
|
2018-10-24 04:44:04 +08:00
|
|
|
return OpaqueElementsAttr::get(type, bytes);
|
Add support to constant sparse tensor / vector attribute
The SparseElementsAttr uses (COO) Coordinate List encoding to represents a
sparse tensor / vector. Specifically, the coordinates and values are stored as
two dense elements attributes. The first dense elements attribute is a 2-D
attribute with shape [N, ndims], which contains the indices of the elements
with nonzero values in the constant vector/tensor. The second elements
attribute is a 1-D attribute list with shape [N], which supplies the values for
each element in the first elements attribute. ndims is the rank of the
vector/tensor and N is the total nonzero elements.
The syntax is:
`sparse<` (tensor-type | vector-type)`, ` indices-attribute-list, values-attribute-list `>`
Example: a sparse tensor
sparse<vector<3x4xi32>, [[0, 0], [1, 2]], [1, 2]> represents the dense tensor
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]]
PiperOrigin-RevId: 217764319
2018-10-19 05:02:20 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:59:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-08-08 05:24:38 +08:00
|
|
|
// Affine Expressions, Affine Maps, and Integet Sets.
|
2018-07-11 01:59:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
AffineMap Builder::getAffineMap(unsigned dimCount, unsigned symbolCount,
|
|
|
|
ArrayRef<AffineExpr> results,
|
|
|
|
ArrayRef<AffineExpr> rangeSizes) {
|
2018-10-09 04:47:18 +08:00
|
|
|
return AffineMap::get(dimCount, symbolCount, results, rangeSizes);
|
2018-07-11 01:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:47:18 +08:00
|
|
|
AffineExpr Builder::getAffineDimExpr(unsigned position) {
|
2018-10-09 01:20:25 +08:00
|
|
|
return mlir::getAffineDimExpr(position, context);
|
2018-07-11 01:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:47:18 +08:00
|
|
|
AffineExpr Builder::getAffineSymbolExpr(unsigned position) {
|
2018-10-09 01:20:25 +08:00
|
|
|
return mlir::getAffineSymbolExpr(position, context);
|
2018-07-11 01:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:47:18 +08:00
|
|
|
AffineExpr Builder::getAffineConstantExpr(int64_t constant) {
|
2018-10-09 01:20:25 +08:00
|
|
|
return mlir::getAffineConstantExpr(constant, context);
|
2018-07-11 01:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 00:45:59 +08:00
|
|
|
IntegerSet Builder::getIntegerSet(unsigned dimCount, unsigned symbolCount,
|
|
|
|
ArrayRef<AffineExpr> constraints,
|
|
|
|
ArrayRef<bool> isEq) {
|
2018-10-25 23:33:02 +08:00
|
|
|
return IntegerSet::get(dimCount, symbolCount, constraints, isEq);
|
2018-08-08 05:24:38 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
AffineMap Builder::getConstantAffineMap(int64_t val) {
|
2018-10-04 06:39:12 +08:00
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0,
|
2018-10-09 04:47:18 +08:00
|
|
|
{getAffineConstantExpr(val)}, {});
|
2018-08-25 14:38:14 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
AffineMap Builder::getDimIdentityMap() {
|
2018-10-09 01:20:25 +08:00
|
|
|
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0,
|
2018-10-09 04:47:18 +08:00
|
|
|
{getAffineDimExpr(0)}, {});
|
2018-08-25 14:38:14 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
AffineMap Builder::getMultiDimIdentityMap(unsigned rank) {
|
2018-10-09 04:47:18 +08:00
|
|
|
SmallVector<AffineExpr, 4> dimExprs;
|
2018-10-09 02:10:11 +08:00
|
|
|
dimExprs.reserve(rank);
|
|
|
|
for (unsigned i = 0; i < rank; ++i)
|
|
|
|
dimExprs.push_back(getAffineDimExpr(i));
|
2018-10-09 04:47:18 +08:00
|
|
|
return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs, {});
|
2018-10-09 02:10:11 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
AffineMap Builder::getSymbolIdentityMap() {
|
2018-10-09 01:20:25 +08:00
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1,
|
2018-10-09 04:47:18 +08:00
|
|
|
{getAffineSymbolExpr(0)}, {});
|
2018-08-25 14:38:14 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
AffineMap Builder::getSingleDimShiftAffineMap(int64_t shift) {
|
2018-10-04 06:34:57 +08:00
|
|
|
// expr = d0 + shift.
|
2018-10-09 01:20:25 +08:00
|
|
|
auto expr = getAffineDimExpr(0) + shift;
|
2018-10-09 04:47:18 +08:00
|
|
|
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, {expr}, {});
|
2018-09-29 03:17:26 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:39:24 +08:00
|
|
|
AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) {
|
2018-10-09 04:47:18 +08:00
|
|
|
SmallVector<AffineExpr, 4> shiftedResults;
|
2018-10-10 07:39:24 +08:00
|
|
|
shiftedResults.reserve(map.getNumResults());
|
|
|
|
for (auto resultExpr : map.getResults()) {
|
2018-10-10 01:59:27 +08:00
|
|
|
shiftedResults.push_back(resultExpr + shift);
|
2018-09-29 03:17:26 +08:00
|
|
|
}
|
2018-10-10 07:39:24 +08:00
|
|
|
return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults,
|
|
|
|
map.getRangeSizes());
|
2018-09-29 03:17:26 +08:00
|
|
|
}
|
|
|
|
|
2018-07-20 00:52:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-12-29 08:05:35 +08:00
|
|
|
// Instructions.
|
2018-07-25 01:15:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-29 13:24:30 +08:00
|
|
|
/// Add new block and set the insertion point to the end of it. If an
|
|
|
|
/// 'insertBefore' block is passed, the block will be placed before the
|
2018-08-25 12:13:19 +08:00
|
|
|
/// specified block. If not, the block will be appended to the end of the
|
|
|
|
/// current function.
|
2018-12-29 05:07:39 +08:00
|
|
|
Block *FuncBuilder::createBlock(Block *insertBefore) {
|
|
|
|
Block *b = new Block();
|
2018-08-25 12:13:19 +08: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)
|
2018-12-28 06:35:10 +08:00
|
|
|
function->getBlocks().insert(Function::iterator(insertBefore), b);
|
2018-08-25 12:13:19 +08:00
|
|
|
else
|
|
|
|
function->push_back(b);
|
|
|
|
|
2018-12-28 07:06:22 +08:00
|
|
|
setInsertionPointToEnd(b);
|
2018-07-25 01:15:13 +08:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:12:35 +08:00
|
|
|
/// Create an operation given the fields represented as an OperationState.
|
2018-12-28 13:21:41 +08:00
|
|
|
OperationInst *FuncBuilder::createOperation(const OperationState &state) {
|
2018-12-28 06:35:10 +08:00
|
|
|
auto *op = OperationInst::create(state.location, state.name, state.operands,
|
|
|
|
state.types, state.attributes,
|
|
|
|
state.successors, context);
|
2018-12-29 05:07:39 +08:00
|
|
|
block->getInstructions().insert(insertPoint, op);
|
2018-08-08 00:12:35 +08:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2018-12-29 08:05:35 +08:00
|
|
|
ForInst *FuncBuilder::createFor(Location location, ArrayRef<Value *> lbOperands,
|
2018-12-28 07:06:22 +08:00
|
|
|
AffineMap lbMap, ArrayRef<Value *> ubOperands,
|
|
|
|
AffineMap ubMap, int64_t step) {
|
2018-12-29 08:05:35 +08:00
|
|
|
auto *inst =
|
|
|
|
ForInst::create(location, lbOperands, lbMap, ubOperands, ubMap, step);
|
|
|
|
block->getInstructions().insert(insertPoint, inst);
|
|
|
|
return inst;
|
2018-08-24 05:32:25 +08:00
|
|
|
}
|
|
|
|
|
2018-12-29 08:05:35 +08:00
|
|
|
ForInst *FuncBuilder::createFor(Location location, int64_t lb, int64_t ub,
|
2018-12-28 07:06:22 +08:00
|
|
|
int64_t step) {
|
2018-10-10 07:39:24 +08:00
|
|
|
auto lbMap = AffineMap::getConstantMap(lb, context);
|
|
|
|
auto ubMap = AffineMap::getConstantMap(ub, context);
|
2018-09-13 23:12:38 +08:00
|
|
|
return createFor(location, {}, lbMap, {}, ubMap, step);
|
|
|
|
}
|
|
|
|
|
2018-12-29 08:05:35 +08:00
|
|
|
IfInst *FuncBuilder::createIf(Location location, ArrayRef<Value *> operands,
|
2018-12-28 07:06:22 +08:00
|
|
|
IntegerSet set) {
|
2018-12-29 08:05:35 +08:00
|
|
|
auto *inst = IfInst::create(location, operands, set);
|
|
|
|
block->getInstructions().insert(insertPoint, inst);
|
|
|
|
return inst;
|
2018-07-20 00:52:39 +08:00
|
|
|
}
|