/home/arjun/llvm-project/mlir/lib/IR/Module.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Module.cpp - MLIR Module Operation ---------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "mlir/IR/Module.h" |
10 | | #include "mlir/IR/Builders.h" |
11 | | #include "mlir/IR/OpImplementation.h" |
12 | | |
13 | | using namespace mlir; |
14 | | |
15 | | //===----------------------------------------------------------------------===// |
16 | | // Module Operation. |
17 | | //===----------------------------------------------------------------------===// |
18 | | |
19 | | void ModuleOp::build(OpBuilder &builder, OperationState &result, |
20 | 0 | Optional<StringRef> name) { |
21 | 0 | ensureTerminator(*result.addRegion(), builder, result.location); |
22 | 0 | if (name) |
23 | 0 | result.attributes.push_back(builder.getNamedAttr( |
24 | 0 | mlir::SymbolTable::getSymbolAttrName(), builder.getStringAttr(*name))); |
25 | 0 | } |
26 | | |
27 | | /// Construct a module from the given context. |
28 | 0 | ModuleOp ModuleOp::create(Location loc, Optional<StringRef> name) { |
29 | 0 | OperationState state(loc, "module"); |
30 | 0 | OpBuilder builder(loc->getContext()); |
31 | 0 | ModuleOp::build(builder, state, name); |
32 | 0 | return cast<ModuleOp>(Operation::create(state)); |
33 | 0 | } |
34 | | |
35 | 0 | ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) { |
36 | 0 | // If the name is present, parse it. |
37 | 0 | StringAttr nameAttr; |
38 | 0 | (void)parser.parseOptionalSymbolName( |
39 | 0 | nameAttr, mlir::SymbolTable::getSymbolAttrName(), result.attributes); |
40 | 0 |
|
41 | 0 | // If module attributes are present, parse them. |
42 | 0 | if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) |
43 | 0 | return failure(); |
44 | 0 | |
45 | 0 | // Parse the module body. |
46 | 0 | auto *body = result.addRegion(); |
47 | 0 | if (parser.parseRegion(*body, llvm::None, llvm::None)) |
48 | 0 | return failure(); |
49 | 0 | |
50 | 0 | // Ensure that this module has a valid terminator. |
51 | 0 | ensureTerminator(*body, parser.getBuilder(), result.location); |
52 | 0 | return success(); |
53 | 0 | } |
54 | | |
55 | 0 | void ModuleOp::print(OpAsmPrinter &p) { |
56 | 0 | p << "module"; |
57 | 0 |
|
58 | 0 | if (Optional<StringRef> name = getName()) { |
59 | 0 | p << ' '; |
60 | 0 | p.printSymbolName(*name); |
61 | 0 | } |
62 | 0 |
|
63 | 0 | // Print the module attributes. |
64 | 0 | p.printOptionalAttrDictWithKeyword(getAttrs(), |
65 | 0 | {mlir::SymbolTable::getSymbolAttrName()}); |
66 | 0 |
|
67 | 0 | // Print the region. |
68 | 0 | p.printRegion(getOperation()->getRegion(0), /*printEntryBlockArgs=*/false, |
69 | 0 | /*printBlockTerminators=*/false); |
70 | 0 | } |
71 | | |
72 | 0 | LogicalResult ModuleOp::verify() { |
73 | 0 | auto &bodyRegion = getOperation()->getRegion(0); |
74 | 0 |
|
75 | 0 | // The body must contain a single basic block. |
76 | 0 | if (!llvm::hasSingleElement(bodyRegion)) |
77 | 0 | return emitOpError("expected body region to have a single block"); |
78 | 0 | |
79 | 0 | // Check that the body has no block arguments. |
80 | 0 | auto *body = &bodyRegion.front(); |
81 | 0 | if (body->getNumArguments() != 0) |
82 | 0 | return emitOpError("expected body to have no arguments"); |
83 | 0 | |
84 | 0 | // Check that none of the attributes are non-dialect attributes, except for |
85 | 0 | // the symbol related attributes. |
86 | 0 | for (auto attr : getOperation()->getMutableAttrDict().getAttrs()) { |
87 | 0 | if (!attr.first.strref().contains('.') && |
88 | 0 | !llvm::is_contained( |
89 | 0 | ArrayRef<StringRef>{mlir::SymbolTable::getSymbolAttrName(), |
90 | 0 | mlir::SymbolTable::getVisibilityAttrName()}, |
91 | 0 | attr.first.strref())) |
92 | 0 | return emitOpError( |
93 | 0 | "can only contain dialect-specific attributes, found: '") |
94 | 0 | << attr.first << "'"; |
95 | 0 | } |
96 | 0 |
|
97 | 0 | return success(); |
98 | 0 | } |
99 | | |
100 | | /// Return body of this module. |
101 | 0 | Region &ModuleOp::getBodyRegion() { return getOperation()->getRegion(0); } |
102 | 0 | Block *ModuleOp::getBody() { return &getBodyRegion().front(); } |
103 | | |
104 | 0 | Optional<StringRef> ModuleOp::getName() { |
105 | 0 | if (auto nameAttr = |
106 | 0 | getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName())) |
107 | 0 | return nameAttr.getValue(); |
108 | 0 | return llvm::None; |
109 | 0 | } |