2019-06-22 11:20:27 +08:00
|
|
|
//===- Module.cpp - MLIR Module Operation ---------------------------------===//
|
|
|
|
//
|
|
|
|
// 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/Module.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/OpImplementation.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Module Operation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ModuleOp::build(Builder *builder, OperationState *result) {
|
2019-08-07 01:33:11 +08:00
|
|
|
ensureTerminator(*result->addRegion(), *builder, result->location);
|
2019-06-22 11:20:27 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 04:21:24 +08:00
|
|
|
/// Construct a module from the given context.
|
2019-07-10 08:57:24 +08:00
|
|
|
ModuleOp ModuleOp::create(Location loc) {
|
|
|
|
OperationState state(loc, "module");
|
|
|
|
Builder builder(loc->getContext());
|
2019-07-04 04:21:24 +08:00
|
|
|
ModuleOp::build(&builder, &state);
|
|
|
|
return llvm::cast<ModuleOp>(Operation::create(state));
|
|
|
|
}
|
|
|
|
|
2019-06-22 11:20:27 +08:00
|
|
|
ParseResult ModuleOp::parse(OpAsmParser *parser, OperationState *result) {
|
|
|
|
// If module attributes are present, parse them.
|
|
|
|
if (succeeded(parser->parseOptionalKeyword("attributes")))
|
|
|
|
if (parser->parseOptionalAttributeDict(result->attributes))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Parse the module body.
|
|
|
|
auto *body = result->addRegion();
|
|
|
|
if (parser->parseRegion(*body, llvm::None, llvm::None))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Ensure that this module has a valid terminator.
|
2019-08-07 01:33:11 +08:00
|
|
|
ensureTerminator(*body, parser->getBuilder(), result->location);
|
2019-06-22 11:20:27 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleOp::print(OpAsmPrinter *p) {
|
|
|
|
*p << "module";
|
|
|
|
|
|
|
|
// Print the module attributes.
|
|
|
|
auto attrs = getAttrs();
|
|
|
|
if (!attrs.empty()) {
|
|
|
|
*p << " attributes";
|
|
|
|
p->printOptionalAttrDict(attrs, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the region.
|
|
|
|
p->printRegion(getOperation()->getRegion(0), /*printEntryBlockArgs=*/false,
|
|
|
|
/*printBlockTerminators=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult ModuleOp::verify() {
|
|
|
|
auto &bodyRegion = getOperation()->getRegion(0);
|
|
|
|
|
|
|
|
// The body must contain a single basic block.
|
|
|
|
if (bodyRegion.empty() || std::next(bodyRegion.begin()) != bodyRegion.end())
|
|
|
|
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");
|
|
|
|
|
|
|
|
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(); }
|