2020-11-17 16:37:14 +08:00
|
|
|
//===- BuiltinDialect.cpp - MLIR Builtin Dialect --------------------------===//
|
2018-06-22 00:49:33 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-22 00:49:33 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-11-20 02:43:12 +08:00
|
|
|
//
|
|
|
|
// This file contains the Builtin dialect that contains all of the attributes,
|
|
|
|
// operations, and types that are necessary for the validity of the IR.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2020-11-17 16:37:14 +08:00
|
|
|
#include "mlir/IR/BuiltinDialect.h"
|
2019-01-25 04:25:30 +08:00
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
2019-06-04 03:08:22 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
2020-11-20 02:43:12 +08:00
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
2020-12-04 09:22:29 +08:00
|
|
|
#include "mlir/IR/BuiltinTypes.h"
|
2019-11-29 03:50:47 +08:00
|
|
|
#include "mlir/IR/FunctionImplementation.h"
|
2020-11-17 16:37:14 +08:00
|
|
|
#include "mlir/IR/OpImplementation.h"
|
2021-01-21 08:17:26 +08:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2019-01-25 04:25:30 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2019-05-14 00:00:22 +08:00
|
|
|
|
2018-06-22 00:49:33 +08:00
|
|
|
using namespace mlir;
|
2019-01-25 04:25:30 +08:00
|
|
|
|
2018-06-29 08:02:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-11-17 16:37:14 +08:00
|
|
|
// Builtin Dialect
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct BuiltinOpAsmDialectInterface : public OpAsmDialectInterface {
|
|
|
|
using OpAsmDialectInterface::OpAsmDialectInterface;
|
|
|
|
|
|
|
|
LogicalResult getAlias(Attribute attr, raw_ostream &os) const override {
|
|
|
|
if (attr.isa<AffineMapAttr>()) {
|
|
|
|
os << "map";
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
if (attr.isa<IntegerSetAttr>()) {
|
|
|
|
os << "set";
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
if (attr.isa<LocationAttr>()) {
|
|
|
|
os << "loc";
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
|
|
|
void BuiltinDialect::initialize() {
|
|
|
|
addTypes<ComplexType, BFloat16Type, Float16Type, Float32Type, Float64Type,
|
2021-01-15 23:29:37 +08:00
|
|
|
Float80Type, Float128Type, FunctionType, IndexType, IntegerType,
|
|
|
|
MemRefType, UnrankedMemRefType, NoneType, OpaqueType,
|
|
|
|
RankedTensorType, TupleType, UnrankedTensorType, VectorType>();
|
2020-11-17 16:37:14 +08:00
|
|
|
addAttributes<AffineMapAttr, ArrayAttr, DenseIntOrFPElementsAttr,
|
|
|
|
DenseStringElementsAttr, DictionaryAttr, FloatAttr,
|
|
|
|
SymbolRefAttr, IntegerAttr, IntegerSetAttr, OpaqueAttr,
|
|
|
|
OpaqueElementsAttr, SparseElementsAttr, StringAttr, TypeAttr,
|
|
|
|
UnitAttr>();
|
|
|
|
addAttributes<CallSiteLoc, FileLineColLoc, FusedLoc, NameLoc, OpaqueLoc,
|
|
|
|
UnknownLoc>();
|
|
|
|
addOperations<
|
|
|
|
#define GET_OP_LIST
|
|
|
|
#include "mlir/IR/BuiltinOps.cpp.inc"
|
|
|
|
>();
|
|
|
|
addInterfaces<BuiltinOpAsmDialectInterface>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FuncOp
|
2018-06-29 08:02:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-10 07:17:55 +08:00
|
|
|
FuncOp FuncOp::create(Location location, StringRef name, FunctionType type,
|
|
|
|
ArrayRef<NamedAttribute> attrs) {
|
2019-07-04 04:21:24 +08:00
|
|
|
OperationState state(location, "func");
|
2020-04-23 22:02:46 +08:00
|
|
|
OpBuilder builder(location->getContext());
|
|
|
|
FuncOp::build(builder, state, name, type, attrs);
|
2019-12-19 01:28:48 +08:00
|
|
|
return cast<FuncOp>(Operation::create(state));
|
2019-01-03 02:20:00 +08:00
|
|
|
}
|
2019-07-10 07:17:55 +08:00
|
|
|
FuncOp FuncOp::create(Location location, StringRef name, FunctionType type,
|
2019-12-19 01:28:48 +08:00
|
|
|
iterator_range<dialect_attr_iterator> attrs) {
|
2019-07-04 04:21:24 +08:00
|
|
|
SmallVector<NamedAttribute, 8> attrRef(attrs);
|
|
|
|
return create(location, name, type, llvm::makeArrayRef(attrRef));
|
|
|
|
}
|
2019-07-10 07:17:55 +08:00
|
|
|
FuncOp FuncOp::create(Location location, StringRef name, FunctionType type,
|
|
|
|
ArrayRef<NamedAttribute> attrs,
|
2020-12-18 09:10:12 +08:00
|
|
|
ArrayRef<DictionaryAttr> argAttrs) {
|
2019-07-10 07:17:55 +08:00
|
|
|
FuncOp func = create(location, name, type, attrs);
|
2019-07-04 04:21:24 +08:00
|
|
|
func.setAllArgAttrs(argAttrs);
|
|
|
|
return func;
|
2018-08-14 02:30:36 +08:00
|
|
|
}
|
2019-06-04 03:08:22 +08:00
|
|
|
|
2020-11-17 16:37:14 +08:00
|
|
|
void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name,
|
2020-07-08 07:15:44 +08:00
|
|
|
FunctionType type, ArrayRef<NamedAttribute> attrs,
|
2020-12-18 09:10:12 +08:00
|
|
|
ArrayRef<DictionaryAttr> argAttrs) {
|
2020-11-17 16:37:14 +08:00
|
|
|
state.addAttribute(SymbolTable::getSymbolAttrName(),
|
|
|
|
builder.getStringAttr(name));
|
|
|
|
state.addAttribute(getTypeAttrName(), TypeAttr::get(type));
|
|
|
|
state.attributes.append(attrs.begin(), attrs.end());
|
|
|
|
state.addRegion();
|
2019-06-04 03:08:22 +08:00
|
|
|
|
2020-07-08 07:15:44 +08:00
|
|
|
if (argAttrs.empty())
|
|
|
|
return;
|
2019-07-04 04:21:24 +08:00
|
|
|
assert(type.getNumInputs() == argAttrs.size());
|
|
|
|
SmallString<8> argAttrName;
|
|
|
|
for (unsigned i = 0, e = type.getNumInputs(); i != e; ++i)
|
2020-12-18 09:10:12 +08:00
|
|
|
if (DictionaryAttr argDict = argAttrs[i])
|
2020-11-17 16:37:14 +08:00
|
|
|
state.addAttribute(getArgAttrName(i, argAttrName), argDict);
|
2019-07-04 04:21:24 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 16:37:14 +08:00
|
|
|
static ParseResult parseFuncOp(OpAsmParser &parser, OperationState &result) {
|
2019-08-09 00:41:48 +08:00
|
|
|
auto buildFuncType = [](Builder &builder, ArrayRef<Type> argTypes,
|
2019-08-09 03:11:27 +08:00
|
|
|
ArrayRef<Type> results, impl::VariadicFlag,
|
|
|
|
std::string &) {
|
2019-08-09 00:41:48 +08:00
|
|
|
return builder.getFunctionType(argTypes, results);
|
|
|
|
};
|
|
|
|
|
|
|
|
return impl::parseFunctionLikeOp(parser, result, /*allowVariadic=*/false,
|
2020-11-13 02:46:44 +08:00
|
|
|
buildFuncType);
|
2019-06-04 03:08:22 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 16:37:14 +08:00
|
|
|
static void print(FuncOp op, OpAsmPrinter &p) {
|
|
|
|
FunctionType fnType = op.getType();
|
|
|
|
impl::printFunctionLikeOp(p, op, fnType.getInputs(), /*isVariadic=*/false,
|
2020-11-13 02:46:44 +08:00
|
|
|
fnType.getResults());
|
2019-06-04 03:08:22 +08:00
|
|
|
}
|
2019-06-05 02:01:32 +08:00
|
|
|
|
2020-11-17 16:37:14 +08:00
|
|
|
static LogicalResult verify(FuncOp op) {
|
2019-06-06 09:12:16 +08:00
|
|
|
// If this function is external there is nothing to do.
|
2020-11-17 16:37:14 +08:00
|
|
|
if (op.isExternal())
|
2019-06-06 09:12:16 +08:00
|
|
|
return success();
|
|
|
|
|
|
|
|
// Verify that the argument list of the function and the arg list of the entry
|
Introduce LLVMFuncOp
Originally, MLIR only supported functions of the built-in FunctionType. On the
conversion path to LLVM IR, we were creating MLIR functions that contained LLVM
dialect operations and used LLVM IR types for everything expect top-level
functions (e.g., a second-order function would have a FunctionType that consume
or produces a wrapped LLVM function pointer type). With MLIR functions
becoming operations, it is now possible to introduce non-built-in function
operations. This will let us use conversion patterns for function conversion,
simplify the MLIR-to-LLVM translation by removing the knowledge of the MLIR
built-in function types, and provide stronger correctness verifications (e.g.
LLVM functions only accept LLVM types).
Furthermore, we can currently construct a situation where the same function is
used with two different types: () -> () when its specified and called directly,
and !llvm<"void ()"> when it's passed somewhere on called indirectly. Having a
special function-op that is always of !llvm<"void ()"> type makes the function
model and the llvm dialect type system more consistent.
Introduce LLVMFuncOp to represent a function in the LLVM dialect. Unlike
standard FuncOp, this function has an LLVMType wrapping an LLVM IR function
type. Generalize the common behavior of function-defining operations
(functions live in a symbol table of a module, contain a single region, are
iterable as a list of blocks, and support argument attributes).
This only defines the operation. Custom syntax, conversion and translation
rules will be added in follow-ups.
The operation name mentions LLVM explicitly to avoid confusion with standard
FuncOp, especially in multiple files that use both `mlir` and `mlir::LLVM`
namespaces.
PiperOrigin-RevId: 259550940
2019-07-24 00:26:15 +08:00
|
|
|
// block line up. The trait already verified that the number of arguments is
|
|
|
|
// the same between the signature and the block.
|
2020-11-17 16:37:14 +08:00
|
|
|
auto fnInputTypes = op.getType().getInputs();
|
|
|
|
Block &entryBlock = op.front();
|
2019-06-06 09:12:16 +08:00
|
|
|
for (unsigned i = 0, e = entryBlock.getNumArguments(); i != e; ++i)
|
2020-01-12 00:54:04 +08:00
|
|
|
if (fnInputTypes[i] != entryBlock.getArgument(i).getType())
|
2020-11-17 16:37:14 +08:00
|
|
|
return op.emitOpError("type of entry block argument #")
|
2020-01-12 00:54:04 +08:00
|
|
|
<< i << '(' << entryBlock.getArgument(i).getType()
|
2019-06-06 09:12:16 +08:00
|
|
|
<< ") must match the type of the corresponding argument in "
|
|
|
|
<< "function signature(" << fnInputTypes[i] << ')';
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
/// Clone the internal blocks from this function into dest and all attributes
|
|
|
|
/// from this function to dest.
|
|
|
|
void FuncOp::cloneInto(FuncOp dest, BlockAndValueMapping &mapper) {
|
|
|
|
// Add the attributes of this function to dest.
|
|
|
|
llvm::MapVector<Identifier, Attribute> newAttrs;
|
|
|
|
for (auto &attr : dest.getAttrs())
|
|
|
|
newAttrs.insert(attr);
|
|
|
|
for (auto &attr : getAttrs())
|
|
|
|
newAttrs.insert(attr);
|
2020-11-28 20:35:55 +08:00
|
|
|
dest->setAttrs(DictionaryAttr::get(newAttrs.takeVector(), getContext()));
|
2019-07-04 04:21:24 +08:00
|
|
|
|
|
|
|
// Clone the body.
|
|
|
|
getBody().cloneInto(&dest.getBody(), mapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a deep copy of this function and all of its blocks, remapping
|
|
|
|
/// any operands that use values outside of the function using the map that is
|
|
|
|
/// provided (leaving them alone if no entry is present). Replaces references
|
|
|
|
/// to cloned sub-values with the corresponding value that is copied, and adds
|
|
|
|
/// those mappings to the mapper.
|
|
|
|
FuncOp FuncOp::clone(BlockAndValueMapping &mapper) {
|
|
|
|
FunctionType newType = getType();
|
|
|
|
|
|
|
|
// If the function has a body, then the user might be deleting arguments to
|
|
|
|
// the function by specifying them in the mapper. If so, we don't add the
|
|
|
|
// argument to the input type vector.
|
|
|
|
bool isExternalFn = isExternal();
|
|
|
|
if (!isExternalFn) {
|
|
|
|
SmallVector<Type, 4> inputTypes;
|
|
|
|
inputTypes.reserve(newType.getNumInputs());
|
|
|
|
for (unsigned i = 0, e = getNumArguments(); i != e; ++i)
|
|
|
|
if (!mapper.contains(getArgument(i)))
|
|
|
|
inputTypes.push_back(newType.getInput(i));
|
2020-12-18 04:24:45 +08:00
|
|
|
newType = FunctionType::get(getContext(), inputTypes, newType.getResults());
|
2019-07-04 04:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new function.
|
2019-12-19 01:28:48 +08:00
|
|
|
FuncOp newFunc = cast<FuncOp>(getOperation()->cloneWithoutRegions());
|
2019-07-04 04:21:24 +08:00
|
|
|
newFunc.setType(newType);
|
|
|
|
|
|
|
|
/// Set the argument attributes for arguments that aren't being replaced.
|
|
|
|
for (unsigned i = 0, e = getNumArguments(), destI = 0; i != e; ++i)
|
|
|
|
if (isExternalFn || !mapper.contains(getArgument(i)))
|
|
|
|
newFunc.setArgAttrs(destI++, getArgAttrs(i));
|
|
|
|
|
|
|
|
/// Clone the current function into the new one and return it.
|
|
|
|
cloneInto(newFunc, mapper);
|
|
|
|
return newFunc;
|
|
|
|
}
|
|
|
|
FuncOp FuncOp::clone() {
|
|
|
|
BlockAndValueMapping mapper;
|
|
|
|
return clone(mapper);
|
|
|
|
}
|
2020-11-17 16:37:14 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ModuleOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ModuleOp::build(OpBuilder &builder, OperationState &state,
|
|
|
|
Optional<StringRef> name) {
|
|
|
|
ensureTerminator(*state.addRegion(), builder, state.location);
|
|
|
|
if (name) {
|
|
|
|
state.attributes.push_back(builder.getNamedAttr(
|
|
|
|
mlir::SymbolTable::getSymbolAttrName(), builder.getStringAttr(*name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a module from the given context.
|
|
|
|
ModuleOp ModuleOp::create(Location loc, Optional<StringRef> name) {
|
|
|
|
OpBuilder builder(loc->getContext());
|
|
|
|
return builder.create<ModuleOp>(loc, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(ModuleOp op) {
|
|
|
|
// Check that none of the attributes are non-dialect attributes, except for
|
|
|
|
// the symbol related attributes.
|
|
|
|
for (auto attr : op.getAttrs()) {
|
|
|
|
if (!attr.first.strref().contains('.') &&
|
|
|
|
!llvm::is_contained(
|
|
|
|
ArrayRef<StringRef>{mlir::SymbolTable::getSymbolAttrName(),
|
|
|
|
mlir::SymbolTable::getVisibilityAttrName()},
|
|
|
|
attr.first.strref()))
|
2020-12-03 04:42:01 +08:00
|
|
|
return op.emitOpError() << "can only contain attributes with "
|
|
|
|
"dialect-prefixed names, found: '"
|
|
|
|
<< attr.first << "'";
|
2020-11-17 16:37:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2021-01-21 08:17:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UnrealizedConversionCastOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LogicalResult
|
|
|
|
UnrealizedConversionCastOp::fold(ArrayRef<Attribute> attrOperands,
|
|
|
|
SmallVectorImpl<OpFoldResult> &foldResults) {
|
|
|
|
OperandRange operands = inputs();
|
|
|
|
if (operands.empty())
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Check that the input is a cast with results that all feed into this
|
|
|
|
// operation, and operand types that directly match the result types of this
|
|
|
|
// operation.
|
|
|
|
ResultRange results = outputs();
|
|
|
|
Value firstInput = operands.front();
|
|
|
|
auto inputOp = firstInput.getDefiningOp<UnrealizedConversionCastOp>();
|
|
|
|
if (!inputOp || inputOp.getResults() != operands ||
|
|
|
|
inputOp.getOperandTypes() != results.getTypes())
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// If everything matches up, we can fold the passthrough.
|
|
|
|
foldResults.append(inputOp->operand_begin(), inputOp->operand_end());
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnrealizedConversionCastOp::areCastCompatible(TypeRange inputs,
|
|
|
|
TypeRange outputs) {
|
|
|
|
// `UnrealizedConversionCastOp` is agnostic of the input/output types.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-17 16:37:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TableGen'd op method definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define GET_OP_CLASSES
|
|
|
|
#include "mlir/IR/BuiltinOps.cpp.inc"
|