Make Module::getName return Optional<StringRef>

Module names are optional so it makes more sense to take and return an optional
any time the name is involved. Also update the language reference to reflect
the module names.

PiperOrigin-RevId: 272684698
This commit is contained in:
Alex Zinenko 2019-10-03 10:04:05 -07:00 committed by A. Unique TensorFlower
parent 8633b6bc8e
commit 0b93c092b6
3 changed files with 18 additions and 19 deletions

View File

@ -300,13 +300,14 @@ Example:
### 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

View File

@ -47,10 +47,10 @@ public:
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);
@ -62,7 +62,7 @@ public:
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);

View File

@ -25,16 +25,16 @@ using namespace mlir;
// 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);
@ -65,15 +65,13 @@ ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) {
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()});
}
@ -112,9 +110,9 @@ LogicalResult ModuleOp::verify() {
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;
}