Parser: treat implicit top-level module as an SSA name scope
authorAlex Zinenko <zinenko@google.com>
Thu, 8 Aug 2019 16:14:24 +0000 (09:14 -0700)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Thu, 8 Aug 2019 16:14:46 +0000 (09:14 -0700)
Now that modules are also operations, nothing prevents one from defining SSA
values in the module.  Doing so in an implicit top-level module, i.e. outside
of a `module` operation, was leading to a crash because the implicit module was
not associated with an SSA name scope.  Create a name scope before parsing the
top-level module to fix this.

PiperOrigin-RevId: 262366891

mlir/lib/Parser/Parser.cpp
mlir/test/IR/module-op.mlir

index 8af9917..e4628cc 100644 (file)
@@ -3985,6 +3985,10 @@ ParseResult ModuleParser::parseTypeAliasDef() {
 /// This is the top-level module parser.
 ParseResult ModuleParser::parseModule(ModuleOp module) {
   OperationParser opParser(getState(), module);
+
+  // Module itself is a name scope.
+  opParser.pushSSANameScope();
+
   while (1) {
     switch (getToken().getKind()) {
     default:
@@ -4016,7 +4020,7 @@ ParseResult ModuleParser::parseModule(ModuleOp module) {
         bodyBlocks.pop_front();
       }
 
-      return success();
+      return opParser.popSSANameScope();
     }
 
     // If we got an error token, then the lexer already emitted an error, just
index 2af237f..a4ca57d 100644 (file)
@@ -33,3 +33,13 @@ module {
 // CHECK-NOT: module {
 module {
 }
+
+// -----
+
+// Check that the implicit top-level module is also a name scope for SSA
+// values.  This should not crash.
+// CHECK: module {
+// CHECK: %{{.*}} = "op"
+// CHECK: }
+%0 = "op"() : () -> i32
+