Skip to content

Commit 93d3219

Browse files
committed
[flang][driver] Extend the flang bash script to act as a driver
Until now, `f18` would: 1. Use Flang to unparse the input files 2. Call an external Fortran compiler to compile the unparsed source files (generated in step 1) With this patch, `f18` will stop after unparsing the input source files, i.e. step 1 above. The `flang` bash script will take care of step 2, i.e. calling an external Fortran compiler driver to compile them. This way: * the functionality of `f18` is reduced - it will only drive Flang (as opposed to delegating code-generation to an external tool on top of this) * we will able to switch between `f18` and `flang-new` for unparsing before an external Fortran compiler is called for code-generation The updated `flang` bash script needs to specify the output file when using the `-fdebug-unparse` action. Both `f18` and `flang-new` have been updated accordingly. These changes were discussed in [1] as a requirement for replacing `f18` with `flang-new`. [1] https://lists.llvm.org/pipermail/flang-dev/2021-April/000677.html Differential Revision: https://reviews.llvm.org/D103177
1 parent d1461d4 commit 93d3219

File tree

5 files changed

+403
-21
lines changed

5 files changed

+403
-21
lines changed

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,12 @@ void DebugUnparseAction::ExecuteAction() {
257257
auto &invoc = this->instance().invocation();
258258
auto &parseTree{instance().parsing().parseTree()};
259259

260+
CompilerInstance &ci = this->instance();
261+
auto os{ci.CreateDefaultOutputFile(
262+
/*Binary=*/false, /*InFile=*/GetCurrentFileOrBufferName())};
263+
260264
// TODO: Options should come from CompilerInvocation
261-
Unparse(llvm::outs(), *parseTree,
265+
Unparse(*os, *parseTree,
262266
/*encoding=*/Fortran::parser::Encoding::UTF_8,
263267
/*capitalizeKeywords=*/true, /*backslashEscapes=*/false,
264268
/*preStatement=*/nullptr,

flang/tools/f18/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ add_custom_target(module_files ALL DEPENDS ${MODULE_FILES})
6363

6464
install(TARGETS f18 DESTINATION bin)
6565

66+
set(FLANG_DEFAULT_DRIVER "flang-new")
67+
if (NOT FLANG_BUILD_NEW_DRIVER)
68+
set(FLANG_DEFAULT_DRIVER "f18")
69+
endif()
70+
6671
# This flang shell script will only work in a POSIX shell.
6772
if (NOT WIN32)
68-
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/flang
69-
DESTINATION ${CMAKE_BINARY_DIR}/bin
70-
FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE)
73+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/flang.in ${CMAKE_BINARY_DIR}/bin/flang @ONLY)
7174
install(PROGRAMS ${CMAKE_BINARY_DIR}/bin/flang DESTINATION bin)
7275
endif()

flang/tools/f18/f18.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,22 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
318318
Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);
319319
}
320320
if (driver.dumpUnparse) {
321-
Unparse(llvm::outs(), parseTree, driver.encoding, true /*capitalize*/,
321+
// Prepare the output stream
322+
std::unique_ptr<llvm::raw_fd_ostream> os;
323+
std::string outputFile = "-";
324+
if (!driver.outputPath.empty()) {
325+
outputFile = driver.outputPath;
326+
}
327+
328+
std::error_code EC;
329+
os.reset(new llvm::raw_fd_ostream(
330+
outputFile, EC, llvm::sys::fs::OF_TextWithCRLF));
331+
if (EC) {
332+
llvm::errs() << EC.message() << "\n";
333+
std::exit(EXIT_FAILURE);
334+
}
335+
336+
Unparse(*os, parseTree, driver.encoding, true /*capitalize*/,
322337
options.features.IsEnabled(
323338
Fortran::common::LanguageFeature::BackslashEscapes),
324339
nullptr /* action before each statement */,

flang/tools/f18/flang

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)