Make Module::getName return Optional<StringRef>
authorAlex Zinenko <zinenko@google.com>
Thu, 3 Oct 2019 17:04:05 +0000 (10:04 -0700)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Thu, 3 Oct 2019 17:04:48 +0000 (10:04 -0700)
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
mlir/include/mlir/IR/Module.h
mlir/lib/IR/Module.cpp

index 39d98de..a0cdaf0 100644 (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
 
index 40f27d5..e019572 100644 (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);
index 990d4af..dc76879 100644 (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;
 }