From 70ca59ac502a998e383b6bc92eb3538895c93f96 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Thu, 8 Aug 2019 09:14:24 -0700 Subject: [PATCH] Parser: treat implicit top-level module as an SSA name scope 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 | 6 +++++- mlir/test/IR/module-op.mlir | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp index 8af9917..e4628cc 100644 --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -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 diff --git a/mlir/test/IR/module-op.mlir b/mlir/test/IR/module-op.mlir index 2af237f..a4ca57d 100644 --- a/mlir/test/IR/module-op.mlir +++ b/mlir/test/IR/module-op.mlir @@ -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 + -- 2.7.4