Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NewIR] Add parser to deserialize #55695

Merged
merged 21 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions paddle/ir/core/parser/ir_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,9 @@ Type Type::Parse(std::istream& is, IrContext* ctx) {
return parser.ParseType();
}

std::unique_ptr<Program> Program::Parse(std::istream& is, IrContext* ctx) {
IrParser parser(ctx, is);
return parser.ParseProgram();
}

} // namespace ir
2 changes: 2 additions & 0 deletions paddle/ir/core/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class IR_API Program {

void Print(std::ostream& os) const;

static std::unique_ptr<Program> Parse(std::istream& is, IrContext* ctx);

Block* block() { return module_.block(); }
const Block* block() const { return module_op().block(); }

Expand Down
10 changes: 4 additions & 6 deletions test/cpp/ir/core/ir_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <fstream>
#include <iostream>

#include "gtest/gtest.h"

#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/program_desc.h"
Expand Down Expand Up @@ -109,10 +109,8 @@ bool ParserTest::ConsumeTestTask(TestTask* test_task, ir::IrContext* ctx) {
auto attr = ir::Attribute::Parse(is_par, ctx);
attr.Print(os);
} else if (test_type == ProgramTest) {
parser.reset(new ir::IrParser(ctx, is_par));
printer.reset(new ir::IrPrinter(os));
auto program = parser->ParseProgram();
printer->PrintProgram(program.get());
auto program = ir::Program::Parse(is_par, ctx);
program->Print(os);
} else if (test_type == TypeTest) {
auto type = ir::Type::Parse(is_par, ctx);
type.Print(os);
Expand Down
14 changes: 6 additions & 8 deletions test/cpp/ir/core/program_translator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ TEST(IrParserTest, MainProgram) {

std::stringstream ss;
program->Print(ss);
ir::IrParser *parser = new ir::IrParser(ctx, ss);
std::unique_ptr<ir::Program> parser_program = parser->ParseProgram();
std::unique_ptr<ir::Program> parser_program = ir::Program::Parse(ss, ctx);
std::stringstream ssp;
parser_program->Print(ssp);
delete parser;
program->Print(ssp);
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved

EXPECT_TRUE(ssp.str() == ss.str());
}

Expand All @@ -138,10 +137,9 @@ TEST(IrParserTest, StartupProgram) {

std::stringstream ss;
program->Print(ss);
ir::IrParser *parser = new ir::IrParser(ctx, ss);
std::unique_ptr<ir::Program> parser_program = parser->ParseProgram();
std::unique_ptr<ir::Program> parser_program = ir::Program::Parse(ss, ctx);
std::stringstream ssp;
parser_program->Print(ssp);
delete parser;
program->Print(ssp);
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved

EXPECT_TRUE(ssp.str() == ss.str());
}