Skip to content

Commit 0c7e895

Browse files
authored
[flang] Move parser invocations into ParserActions (#130309)
FrontendActions.cpp is currently one of the biggest compilation units in all of flang. Measuring its compilation gives the following metrics: User time (seconds): 139.21 System time (seconds): 4.65 Maximum resident set size (kbytes): 5891440 (5.61 GB) This commit separates out explicit invocations of the parser into a separate compilation unit - ParserActions.cpp - through helper functions in order to decrease the maximum compilation time and memory usage of a single unit. After the split, the measurements of FrontendActions.cpp are as follows: User time (seconds): 70.08 System time (seconds): 3.16 Maximum resident set size (kbytes): 3961492 (3.7 GB) While the ones for the newly created ParserActions.cpp as follows: User time (seconds): 104.33 System time (seconds): 3.37 Maximum resident set size (kbytes): 4185600 (3.99 GB) --------- Signed-off-by: Kajetan Puchalski <[email protected]>
1 parent 99fdb5d commit 0c7e895

File tree

10 files changed

+283
-179
lines changed

10 files changed

+283
-179
lines changed

flang/include/flang/Frontend/CompilerInstance.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
#include "flang/Frontend/CompilerInvocation.h"
1717
#include "flang/Frontend/FrontendAction.h"
18+
#include "flang/Frontend/ParserActions.h"
1819
#include "flang/Frontend/PreprocessorOptions.h"
19-
#include "flang/Parser/parsing.h"
20-
#include "flang/Parser/provenance.h"
2120
#include "flang/Semantics/runtime-type-info.h"
2221
#include "flang/Semantics/semantics.h"
2322
#include "flang/Support/StringOstream.h"

flang/include/flang/Frontend/CompilerInvocation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "flang/Frontend/PreprocessorOptions.h"
1919
#include "flang/Frontend/TargetOptions.h"
2020
#include "flang/Lower/LoweringOptions.h"
21-
#include "flang/Parser/parsing.h"
21+
#include "flang/Parser/options.h"
2222
#include "flang/Semantics/semantics.h"
2323
#include "flang/Support/LangOptions.h"
2424
#include "mlir/Support/Timing.h"
@@ -29,7 +29,7 @@
2929

3030
namespace llvm {
3131
class TargetMachine;
32-
}
32+
} // namespace llvm
3333

3434
namespace Fortran::frontend {
3535

flang/include/flang/Frontend/FrontendActions.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
#ifndef FORTRAN_FRONTEND_FRONTENDACTIONS_H
1414
#define FORTRAN_FRONTEND_FRONTENDACTIONS_H
1515

16-
#include "flang/Frontend/CodeGenOptions.h"
1716
#include "flang/Frontend/FrontendAction.h"
18-
#include "flang/Parser/parsing.h"
19-
#include "flang/Semantics/semantics.h"
17+
#include "flang/Frontend/ParserActions.h"
2018

2119
#include "mlir/IR/BuiltinOps.h"
2220
#include "mlir/IR/OwningOpRef.h"
@@ -26,21 +24,6 @@
2624

2725
namespace Fortran::frontend {
2826

29-
// TODO: This is a copy from f18.cpp. It doesn't really belong here and should
30-
// be moved to a more suitable place in future.
31-
struct MeasurementVisitor {
32-
template <typename A>
33-
bool Pre(const A &) {
34-
return true;
35-
}
36-
template <typename A>
37-
void Post(const A &) {
38-
++objects;
39-
bytes += sizeof(A);
40-
}
41-
size_t objects{0}, bytes{0};
42-
};
43-
4427
//===----------------------------------------------------------------------===//
4528
// Custom Consumer Actions
4629
//===----------------------------------------------------------------------===//
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===- ParserActions.h -------------------------------------------*- C++-*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef FORTRAN_PARSER_ACTIONS_H_
14+
#define FORTRAN_PARSER_ACTIONS_H_
15+
16+
#include <string>
17+
18+
namespace llvm {
19+
class raw_string_ostream;
20+
class raw_ostream;
21+
class StringRef;
22+
} // namespace llvm
23+
24+
namespace Fortran::lower {
25+
class LoweringBridge;
26+
} // namespace Fortran::lower
27+
28+
namespace Fortran::parser {
29+
class Parsing;
30+
class AllCookedSources;
31+
} // namespace Fortran::parser
32+
33+
namespace lower::pft {
34+
class Program;
35+
} // namespace lower::pft
36+
37+
//=== Frontend Parser helpers ===
38+
39+
namespace Fortran::frontend {
40+
class CompilerInstance;
41+
42+
parser::AllCookedSources &getAllCooked(CompilerInstance &ci);
43+
44+
void parseAndLowerTree(CompilerInstance &ci, lower::LoweringBridge &lb);
45+
46+
void dumpTree(CompilerInstance &ci);
47+
48+
void dumpProvenance(CompilerInstance &ci);
49+
50+
void dumpPreFIRTree(CompilerInstance &ci);
51+
52+
void formatOrDumpPrescanner(std::string &buf,
53+
llvm::raw_string_ostream &outForPP,
54+
CompilerInstance &ci);
55+
56+
void debugMeasureParseTree(CompilerInstance &ci, llvm::StringRef filename);
57+
58+
void debugUnparseNoSema(CompilerInstance &ci, llvm::raw_ostream &out);
59+
60+
void debugUnparseWithSymbols(CompilerInstance &ci);
61+
62+
void debugUnparseWithModules(CompilerInstance &ci);
63+
64+
void debugDumpParsingLog(CompilerInstance &ci);
65+
} // namespace Fortran::frontend
66+
67+
#endif // FORTRAN_PARSER_ACTIONS_H_

flang/include/flang/Parser/options.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===-- include/flang/Parser/options.h --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FORTRAN_PARSER_OPTIONS_H_
10+
#define FORTRAN_PARSER_OPTIONS_H_
11+
12+
#include "characters.h"
13+
#include "flang/Support/Fortran-features.h"
14+
15+
#include <optional>
16+
#include <string>
17+
#include <vector>
18+
19+
namespace Fortran::parser {
20+
21+
struct Options {
22+
Options() {}
23+
24+
using Predefinition = std::pair<std::string, std::optional<std::string>>;
25+
26+
bool isFixedForm{false};
27+
int fixedFormColumns{72};
28+
common::LanguageFeatureControl features;
29+
std::vector<std::string> searchDirectories;
30+
std::vector<std::string> intrinsicModuleDirectories;
31+
std::vector<Predefinition> predefinitions;
32+
bool instrumentedParse{false};
33+
bool isModuleFile{false};
34+
bool needProvenanceRangeToCharBlockMappings{false};
35+
Fortran::parser::Encoding encoding{Fortran::parser::Encoding::UTF_8};
36+
bool prescanAndReformat{false}; // -E
37+
bool expandIncludeLinesInPreprocessedOutput{true};
38+
bool showColors{false};
39+
};
40+
41+
} // namespace Fortran::parser
42+
43+
#endif // FORTRAN_PARSER_OPTIONS_H_

flang/include/flang/Parser/parsing.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,18 @@
99
#ifndef FORTRAN_PARSER_PARSING_H_
1010
#define FORTRAN_PARSER_PARSING_H_
1111

12-
#include "characters.h"
1312
#include "instrumented-parser.h"
1413
#include "message.h"
14+
#include "options.h"
1515
#include "parse-tree.h"
1616
#include "provenance.h"
1717
#include "flang/Parser/preprocessor.h"
18-
#include "flang/Support/Fortran-features.h"
1918
#include "llvm/Support/raw_ostream.h"
2019
#include <optional>
2120
#include <string>
22-
#include <utility>
23-
#include <vector>
2421

2522
namespace Fortran::parser {
2623

27-
struct Options {
28-
Options() {}
29-
30-
using Predefinition = std::pair<std::string, std::optional<std::string>>;
31-
32-
bool isFixedForm{false};
33-
int fixedFormColumns{72};
34-
common::LanguageFeatureControl features;
35-
std::vector<std::string> searchDirectories;
36-
std::vector<std::string> intrinsicModuleDirectories;
37-
std::vector<Predefinition> predefinitions;
38-
bool instrumentedParse{false};
39-
bool isModuleFile{false};
40-
bool needProvenanceRangeToCharBlockMappings{false};
41-
Fortran::parser::Encoding encoding{Fortran::parser::Encoding::UTF_8};
42-
bool prescanAndReformat{false}; // -E
43-
bool expandIncludeLinesInPreprocessedOutput{true};
44-
bool showColors{false};
45-
};
46-
4724
class Parsing {
4825
public:
4926
explicit Parsing(AllCookedSources &);

flang/lib/Frontend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_flang_library(flangFrontend
55
CompilerInstance.cpp
66
CompilerInvocation.cpp
77
CodeGenOptions.cpp
8+
ParserActions.cpp
89
FrontendAction.cpp
910
FrontendActions.cpp
1011
FrontendOptions.cpp

flang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "flang/Frontend/FrontendActions.h"
1616
#include "flang/Frontend/FrontendOptions.h"
1717
#include "flang/Frontend/FrontendPluginRegistry.h"
18+
#include "flang/Parser/parsing.h"
1819
#include "clang/Basic/DiagnosticFrontend.h"
1920
#include "llvm/Support/Errc.h"
2021
#include "llvm/Support/VirtualFileSystem.h"

0 commit comments

Comments
 (0)