2019-06-22 11:20:27 +08:00
|
|
|
//===- Module.cpp - MLIR Module Operation ---------------------------------===//
|
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-06-22 11:20:27 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-22 11:20:27 +08:00
|
|
|
|
|
|
|
#include "mlir/IR/Module.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/OpImplementation.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Module Operation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-04 01:04:05 +08:00
|
|
|
void ModuleOp::build(Builder *builder, OperationState &result,
|
|
|
|
Optional<StringRef> name) {
|
2019-09-21 10:47:05 +08:00
|
|
|
ensureTerminator(*result.addRegion(), *builder, result.location);
|
2019-10-04 01:04:05 +08:00
|
|
|
if (name)
|
|
|
|
result.attributes.push_back(builder->getNamedAttr(
|
|
|
|
mlir::SymbolTable::getSymbolAttrName(), builder->getStringAttr(*name)));
|
2019-06-22 11:20:27 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
/// Construct a module from the given context.
|
2019-10-04 01:04:05 +08:00
|
|
|
ModuleOp ModuleOp::create(Location loc, Optional<StringRef> name) {
|
2019-07-10 08:57:24 +08:00
|
|
|
OperationState state(loc, "module");
|
|
|
|
Builder builder(loc->getContext());
|
2019-10-03 23:56:12 +08:00
|
|
|
ModuleOp::build(&builder, state, name);
|
2019-12-19 01:28:48 +08:00
|
|
|
return cast<ModuleOp>(Operation::create(state));
|
2019-07-04 04:21:24 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 10:47:05 +08:00
|
|
|
ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) {
|
2019-10-03 23:56:12 +08:00
|
|
|
// If the name is present, parse it.
|
|
|
|
StringAttr nameAttr;
|
2019-11-14 01:31:45 +08:00
|
|
|
(void)parser.parseOptionalSymbolName(
|
|
|
|
nameAttr, mlir::SymbolTable::getSymbolAttrName(), result.attributes);
|
2019-10-03 23:56:12 +08:00
|
|
|
|
2019-06-22 11:20:27 +08:00
|
|
|
// If module attributes are present, parse them.
|
2019-11-06 09:58:16 +08:00
|
|
|
if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
|
|
|
|
return failure();
|
2019-06-22 11:20:27 +08:00
|
|
|
|
|
|
|
// Parse the module body.
|
2019-09-21 10:47:05 +08:00
|
|
|
auto *body = result.addRegion();
|
2019-09-21 02:36:49 +08:00
|
|
|
if (parser.parseRegion(*body, llvm::None, llvm::None))
|
2019-06-22 11:20:27 +08:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Ensure that this module has a valid terminator.
|
2019-09-21 10:47:05 +08:00
|
|
|
ensureTerminator(*body, parser.getBuilder(), result.location);
|
2019-06-22 11:20:27 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-09-21 11:43:02 +08:00
|
|
|
void ModuleOp::print(OpAsmPrinter &p) {
|
|
|
|
p << "module";
|
2019-06-22 11:20:27 +08:00
|
|
|
|
2019-11-06 09:58:16 +08:00
|
|
|
if (Optional<StringRef> name = getName()) {
|
2019-10-09 08:44:39 +08:00
|
|
|
p << ' ';
|
|
|
|
p.printSymbolName(*name);
|
|
|
|
}
|
2019-10-03 23:56:12 +08:00
|
|
|
|
2019-06-22 11:20:27 +08:00
|
|
|
// Print the module attributes.
|
2019-11-06 09:58:16 +08:00
|
|
|
p.printOptionalAttrDictWithKeyword(getAttrs(),
|
|
|
|
{mlir::SymbolTable::getSymbolAttrName()});
|
2019-06-22 11:20:27 +08:00
|
|
|
|
|
|
|
// Print the region.
|
2019-09-21 11:43:02 +08:00
|
|
|
p.printRegion(getOperation()->getRegion(0), /*printEntryBlockArgs=*/false,
|
|
|
|
/*printBlockTerminators=*/false);
|
2019-06-22 11:20:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult ModuleOp::verify() {
|
|
|
|
auto &bodyRegion = getOperation()->getRegion(0);
|
|
|
|
|
|
|
|
// The body must contain a single basic block.
|
2019-10-31 02:13:52 +08:00
|
|
|
if (!has_single_element(bodyRegion))
|
2019-06-22 11:20:27 +08:00
|
|
|
return emitOpError("expected body region to have a single block");
|
|
|
|
|
|
|
|
// Check that the body has no block arguments.
|
|
|
|
auto *body = &bodyRegion.front();
|
|
|
|
if (body->getNumArguments() != 0)
|
|
|
|
return emitOpError("expected body to have no arguments");
|
|
|
|
|
2019-10-03 23:56:12 +08:00
|
|
|
// Check that none of the attributes are non-dialect attributes, except for
|
|
|
|
// the symbol name attribute.
|
2019-09-14 09:18:22 +08:00
|
|
|
for (auto attr : getOperation()->getAttrList().getAttrs()) {
|
2019-10-03 23:56:12 +08:00
|
|
|
if (!attr.first.strref().contains('.') &&
|
|
|
|
attr.first.strref() != mlir::SymbolTable::getSymbolAttrName())
|
2019-09-14 09:18:22 +08:00
|
|
|
return emitOpError(
|
2019-09-15 00:43:55 +08:00
|
|
|
"can only contain dialect-specific attributes, found: '")
|
2019-09-14 09:18:22 +08:00
|
|
|
<< attr.first << "'";
|
|
|
|
}
|
|
|
|
|
2019-06-22 11:20:27 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
/// Return body of this module.
|
|
|
|
Region &ModuleOp::getBodyRegion() { return getOperation()->getRegion(0); }
|
|
|
|
Block *ModuleOp::getBody() { return &getBodyRegion().front(); }
|
2019-10-03 23:56:12 +08:00
|
|
|
|
2019-10-04 01:04:05 +08:00
|
|
|
Optional<StringRef> ModuleOp::getName() {
|
2019-10-03 23:56:12 +08:00
|
|
|
if (auto nameAttr =
|
|
|
|
getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName()))
|
|
|
|
return nameAttr.getValue();
|
2019-10-04 01:04:05 +08:00
|
|
|
return llvm::None;
|
2019-10-03 23:56:12 +08:00
|
|
|
}
|