github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/IR/Module.cpp (about)

     1  //===- Module.cpp - MLIR Module Operation ---------------------------------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  
    18  #include "mlir/IR/Module.h"
    19  #include "mlir/IR/Builders.h"
    20  #include "mlir/IR/OpImplementation.h"
    21  
    22  using namespace mlir;
    23  
    24  //===----------------------------------------------------------------------===//
    25  // Module Operation.
    26  //===----------------------------------------------------------------------===//
    27  
    28  void ModuleOp::build(Builder *builder, OperationState *result) {
    29    ensureTerminator(*result->addRegion(), *builder, result->location);
    30  }
    31  
    32  /// Construct a module from the given context.
    33  ModuleOp ModuleOp::create(Location loc) {
    34    OperationState state(loc, "module");
    35    Builder builder(loc->getContext());
    36    ModuleOp::build(&builder, &state);
    37    return llvm::cast<ModuleOp>(Operation::create(state));
    38  }
    39  
    40  ParseResult ModuleOp::parse(OpAsmParser *parser, OperationState *result) {
    41    // If module attributes are present, parse them.
    42    if (succeeded(parser->parseOptionalKeyword("attributes")))
    43      if (parser->parseOptionalAttributeDict(result->attributes))
    44        return failure();
    45  
    46    // Parse the module body.
    47    auto *body = result->addRegion();
    48    if (parser->parseRegion(*body, llvm::None, llvm::None))
    49      return failure();
    50  
    51    // Ensure that this module has a valid terminator.
    52    ensureTerminator(*body, parser->getBuilder(), result->location);
    53    return success();
    54  }
    55  
    56  void ModuleOp::print(OpAsmPrinter *p) {
    57    *p << "module";
    58  
    59    // Print the module attributes.
    60    auto attrs = getAttrs();
    61    if (!attrs.empty()) {
    62      *p << " attributes";
    63      p->printOptionalAttrDict(attrs, {});
    64    }
    65  
    66    // Print the region.
    67    p->printRegion(getOperation()->getRegion(0), /*printEntryBlockArgs=*/false,
    68                   /*printBlockTerminators=*/false);
    69  }
    70  
    71  LogicalResult ModuleOp::verify() {
    72    auto &bodyRegion = getOperation()->getRegion(0);
    73  
    74    // The body must contain a single basic block.
    75    if (bodyRegion.empty() || std::next(bodyRegion.begin()) != bodyRegion.end())
    76      return emitOpError("expected body region to have a single block");
    77  
    78    // Check that the body has no block arguments.
    79    auto *body = &bodyRegion.front();
    80    if (body->getNumArguments() != 0)
    81      return emitOpError("expected body to have no arguments");
    82  
    83    return success();
    84  }
    85  
    86  /// Return body of this module.
    87  Region &ModuleOp::getBodyRegion() { return getOperation()->getRegion(0); }
    88  Block *ModuleOp::getBody() { return &getBodyRegion().front(); }