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
|
|
|
|
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::getAffineIntType() { return Type::getAffineInt(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-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,
|
|
|
|
ArrayRef<AffineMap *> affineMapComposition,
|
|
|
|
unsigned memorySpace) {
|
|
|
|
return MemRefType::get(shape, elementType, affineMapComposition, memorySpace);
|
|
|
|
}
|
|
|
|
|
2018-07-08 20:51:38 -07:00
|
|
|
VectorType *Builder::getVectorType(ArrayRef<unsigned> shape,
|
|
|
|
Type *elementType) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
Extend loop unrolling to unroll by a given factor; add builder for affine
apply op.
- add builder for AffineApplyOp (first one for an operation that has
non-zero operands)
- add support for loop unrolling by a given factor; uses the affine apply op
builder.
While on this, change 'step' of ForStmt to be 'unsigned' instead of
AffineConstantExpr *. Add setters for ForStmt lb, ub, step.
Sample Input:
// CHECK-LABEL: mlfunc @loop_nest_unroll_cleanup() {
mlfunc @loop_nest_unroll_cleanup() {
for %i = 1 to 100 {
for %j = 0 to 17 {
%x = "addi32"(%j, %j) : (affineint, affineint) -> i32
%y = "addi32"(%x, %x) : (i32, i32) -> i32
}
}
return
}
Output:
$ mlir-opt -loop-unroll -unroll-factor=4 /tmp/single2.mlir
#map0 = (d0) -> (d0 + 1)
#map1 = (d0) -> (d0 + 2)
#map2 = (d0) -> (d0 + 3)
mlfunc @loop_nest_unroll_cleanup() {
for %i0 = 1 to 100 {
for %i1 = 0 to 17 step 4 {
%0 = "addi32"(%i1, %i1) : (affineint, affineint) -> i32
%1 = "addi32"(%0, %0) : (i32, i32) -> i32
%2 = affine_apply #map0(%i1)
%3 = "addi32"(%2, %2) : (affineint, affineint) -> i32
%4 = affine_apply #map1(%i1)
%5 = "addi32"(%4, %4) : (affineint, affineint) -> i32
%6 = affine_apply #map2(%i1)
%7 = "addi32"(%6, %6) : (affineint, affineint) -> i32
}
for %i2 = 16 to 17 {
%8 = "addi32"(%i2, %i2) : (affineint, affineint) -> i32
%9 = "addi32"(%8, %8) : (i32, i32) -> i32
}
}
return
}
PiperOrigin-RevId: 209676220
2018-08-21 16:01:23 -07:00
|
|
|
AffineMapAttr *Builder::getAffineMapAttr(AffineMap *map) {
|
|
|
|
return AffineMapAttr::get(map, context);
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount,
|
2018-07-11 21:31:07 -07:00
|
|
|
ArrayRef<AffineExpr *> results,
|
|
|
|
ArrayRef<AffineExpr *> rangeSizes) {
|
|
|
|
return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AffineDimExpr *Builder::getDimExpr(unsigned position) {
|
|
|
|
return AffineDimExpr::get(position, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) {
|
|
|
|
return AffineSymbolExpr::get(position, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineConstantExpr *Builder::getConstantExpr(int64_t constant) {
|
|
|
|
return AffineConstantExpr::get(constant, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) {
|
2018-07-19 14:08:50 -07:00
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-09-18 10:22:03 -07:00
|
|
|
AffineExpr *Builder::getAddExpr(AffineExpr *lhs, int64_t rhs) {
|
|
|
|
return AffineBinaryOpExpr::getAdd(lhs, rhs, context);
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) {
|
2018-07-19 14:08:50 -07:00
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:21:23 -07:00
|
|
|
// Most multiply expressions are pure affine (rhs is a constant).
|
|
|
|
AffineExpr *Builder::getMulExpr(AffineExpr *lhs, int64_t rhs) {
|
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs,
|
|
|
|
getConstantExpr(rhs), context);
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineExpr *Builder::getSubExpr(AffineExpr *lhs, AffineExpr *rhs) {
|
|
|
|
return getAddExpr(lhs, getMulExpr(rhs, getConstantExpr(-1)));
|
|
|
|
}
|
|
|
|
|
2018-09-18 10:22:03 -07:00
|
|
|
AffineExpr *Builder::getSubExpr(AffineExpr *lhs, int64_t rhs) {
|
|
|
|
return AffineBinaryOpExpr::getAdd(lhs, -rhs, context);
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) {
|
2018-07-19 14:08:50 -07:00
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:21:23 -07:00
|
|
|
// Most modulo expressions are pure affine.
|
|
|
|
AffineExpr *Builder::getModExpr(AffineExpr *lhs, uint64_t rhs) {
|
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs,
|
|
|
|
getConstantExpr(rhs), context);
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
|
2018-07-19 14:08:50 -07:00
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:21:23 -07:00
|
|
|
// Most floordiv expressions are pure affine.
|
|
|
|
AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, uint64_t rhs) {
|
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs,
|
|
|
|
getConstantExpr(rhs), context);
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
|
2018-07-19 14:08:50 -07:00
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
2018-07-19 09:52:39 -07:00
|
|
|
|
2018-09-12 10:21:23 -07:00
|
|
|
// Most ceildiv expressions are pure affine.
|
|
|
|
AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, uint64_t rhs) {
|
|
|
|
return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs,
|
|
|
|
getConstantExpr(rhs), context);
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:12:38 -07:00
|
|
|
/// Creates a sum of products affine expression from constant coefficients.
|
|
|
|
/// If c_0, c_1, ... are the coefficients in the order corresponding to
|
|
|
|
/// dimensions, symbols, and constant term, create the affine expression:
|
|
|
|
/// expr = c_0*d0 + c_1*d1 + ... + c_{ndims-1}*d_{ndims-1} + c_{..}*s0 +
|
|
|
|
/// c_{..}*s1 + ... + const
|
|
|
|
AffineExpr *Builder::getAddMulPureAffineExpr(unsigned numDims,
|
|
|
|
unsigned numSymbols,
|
|
|
|
ArrayRef<int64_t> coeffs) {
|
|
|
|
assert(coeffs.size() == numDims + numSymbols + 1);
|
|
|
|
|
|
|
|
AffineExpr *expr = AffineConstantExpr::get(0, context);
|
|
|
|
// Dimensions.
|
|
|
|
for (unsigned j = 0; j < numDims; j++) {
|
|
|
|
if (coeffs[j] == 0)
|
|
|
|
continue;
|
|
|
|
AffineExpr *id = AffineDimExpr::get(j, context);
|
|
|
|
auto *term = AffineBinaryOpExpr::getMul(id, coeffs[j], context);
|
|
|
|
expr = AffineBinaryOpExpr::getAdd(expr, term, context);
|
|
|
|
}
|
|
|
|
// Symbols.
|
|
|
|
for (unsigned j = numDims; j < numDims + numSymbols; j++) {
|
|
|
|
if (coeffs[j] == 0)
|
|
|
|
continue;
|
|
|
|
AffineExpr *id = AffineSymbolExpr::get(j - numDims, context);
|
|
|
|
auto *term = AffineBinaryOpExpr::getMul(id, coeffs[j], context);
|
|
|
|
expr = AffineBinaryOpExpr::getAdd(expr, term, context);
|
|
|
|
}
|
|
|
|
// Constant term.
|
|
|
|
unsigned constTerm = coeffs[coeffs.size() - 1];
|
|
|
|
if (constTerm != 0)
|
|
|
|
expr = AffineBinaryOpExpr::getAdd(expr, constTerm, context);
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2018-08-07 14:24:38 -07:00
|
|
|
IntegerSet *Builder::getIntegerSet(unsigned dimCount, unsigned symbolCount,
|
|
|
|
ArrayRef<AffineExpr *> constraints,
|
|
|
|
ArrayRef<bool> isEq) {
|
|
|
|
return IntegerSet::get(dimCount, symbolCount, constraints, isEq, context);
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:12:38 -07:00
|
|
|
AffineMap *Builder::getConstantAffineMap(int64_t val) {
|
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0, getConstantExpr(val),
|
|
|
|
{}, context);
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AffineMap *Builder::getDimIdentityMap() {
|
2018-09-13 08:12:38 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, getDimExpr(0), {},
|
|
|
|
context);
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AffineMap *Builder::getSymbolIdentityMap() {
|
2018-09-13 08:12:38 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1, getSymbolExpr(0), {},
|
|
|
|
context);
|
2018-08-24 23:38:14 -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-08-27 21:05:16 -07:00
|
|
|
ForStmt *MLFuncBuilder::createFor(Location *location,
|
2018-08-24 23:38:14 -07:00
|
|
|
ArrayRef<MLValue *> lbOperands,
|
|
|
|
AffineMap *lbMap,
|
|
|
|
ArrayRef<MLValue *> ubOperands,
|
|
|
|
AffineMap *ubMap, int64_t step) {
|
|
|
|
auto *stmt = ForStmt::create(location, lbOperands, lbMap, ubOperands, ubMap,
|
|
|
|
step, context);
|
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) {
|
|
|
|
auto *lbMap = AffineMap::getConstantMap(lb, context);
|
|
|
|
auto *ubMap = AffineMap::getConstantMap(ub, context);
|
|
|
|
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;
|
|
|
|
}
|