### Module
``` {.ebnf}
-module ::= `module` (`attributes` attribute-dict)? region
+module ::= `module` symbol-ref-id? (`attributes` attribute-dict)? region
```
An MLIR module represents an opaque top-level container operation. It contains a
single region containing a single block that is comprised of any operations.
Operations within this region must not implicitly capture values defined above
-it.
+it. Modules have an optional symbol name that can be used to refer to them in
+operations.
### Functions
static StringRef getOperationName() { return "module"; }
static void build(Builder *builder, OperationState &result,
- StringRef name = {});
+ Optional<StringRef> name = llvm::None);
/// Construct a module from the given location with an optional name.
- static ModuleOp create(Location loc, StringRef name = {});
+ static ModuleOp create(Location loc, Optional<StringRef> name = llvm::None);
/// Operation hooks.
static ParseResult parse(OpAsmParser &parser, OperationState &result);
Block *getBody();
/// Return the name of this module if present.
- StringRef getName();
+ Optional<StringRef> getName();
/// Print the this module in the custom top-level form.
void print(raw_ostream &os);
// Module Operation.
//===----------------------------------------------------------------------===//
-void ModuleOp::build(Builder *builder, OperationState &result, StringRef name) {
+void ModuleOp::build(Builder *builder, OperationState &result,
+ Optional<StringRef> name) {
ensureTerminator(*result.addRegion(), *builder, result.location);
- if (!name.empty())
- result.attributes.push_back(
- builder->getNamedAttr(mlir::SymbolTable::getSymbolAttrName(),
- builder->getSymbolRefAttr(name)));
+ if (name)
+ result.attributes.push_back(builder->getNamedAttr(
+ mlir::SymbolTable::getSymbolAttrName(), builder->getStringAttr(*name)));
}
/// Construct a module from the given context.
-ModuleOp ModuleOp::create(Location loc, StringRef name) {
+ModuleOp ModuleOp::create(Location loc, Optional<StringRef> name) {
OperationState state(loc, "module");
Builder builder(loc->getContext());
ModuleOp::build(&builder, state, name);
void ModuleOp::print(OpAsmPrinter &p) {
p << "module";
- StringRef name = getName();
- if (!name.empty())
- p << " @" << name;
+ Optional<StringRef> name = getName();
+ if (name)
+ p << " @" << *name;
// Print the module attributes.
auto attrs = getAttrs();
- if (!attrs.empty() &&
- !(attrs.size() == 1 && attrs.front().first.strref() ==
- mlir::SymbolTable::getSymbolAttrName())) {
+ if (!attrs.empty() && !(attrs.size() == 1 && name)) {
p << " attributes";
p.printOptionalAttrDict(attrs, {mlir::SymbolTable::getSymbolAttrName()});
}
Region &ModuleOp::getBodyRegion() { return getOperation()->getRegion(0); }
Block *ModuleOp::getBody() { return &getBodyRegion().front(); }
-StringRef ModuleOp::getName() {
+Optional<StringRef> ModuleOp::getName() {
if (auto nameAttr =
getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName()))
return nameAttr.getValue();
- return {};
+ return llvm::None;
}