From 0b93c092b620f0d70987061ad67f621b9c69925b Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Thu, 3 Oct 2019 10:04:05 -0700 Subject: [PATCH] Make Module::getName return Optional 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 --- mlir/g3doc/LangRef.md | 5 +++-- mlir/include/mlir/IR/Module.h | 6 +++--- mlir/lib/IR/Module.cpp | 26 ++++++++++++-------------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/mlir/g3doc/LangRef.md b/mlir/g3doc/LangRef.md index 39d98de..a0cdaf0 100644 --- a/mlir/g3doc/LangRef.md +++ b/mlir/g3doc/LangRef.md @@ -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 diff --git a/mlir/include/mlir/IR/Module.h b/mlir/include/mlir/IR/Module.h index 40f27d5..e019572 100644 --- a/mlir/include/mlir/IR/Module.h +++ b/mlir/include/mlir/IR/Module.h @@ -47,10 +47,10 @@ public: static StringRef getOperationName() { return "module"; } static void build(Builder *builder, OperationState &result, - StringRef name = {}); + Optional 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 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 getName(); /// Print the this module in the custom top-level form. void print(raw_ostream &os); diff --git a/mlir/lib/IR/Module.cpp b/mlir/lib/IR/Module.cpp index 990d4af..dc76879 100644 --- a/mlir/lib/IR/Module.cpp +++ b/mlir/lib/IR/Module.cpp @@ -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 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 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 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 ModuleOp::getName() { if (auto nameAttr = getAttrOfType(mlir::SymbolTable::getSymbolAttrName())) return nameAttr.getValue(); - return {}; + return llvm::None; } -- 2.7.4