Skip to content

Commit

Permalink
[flang][driver] Extend the flang bash script to act as a driver
Browse files Browse the repository at this point in the history
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
  • Loading branch information
banach-space committed Jul 1, 2021
1 parent b122ff7 commit e77191c
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 21 deletions.
6 changes: 5 additions & 1 deletion flang/lib/Frontend/FrontendActions.cpp
Expand Up @@ -257,8 +257,12 @@ void DebugUnparseAction::ExecuteAction() {
auto &invoc = this->instance().invocation();
auto &parseTree{instance().parsing().parseTree()};

CompilerInstance &ci = this->instance();
auto os{ci.CreateDefaultOutputFile(
/*Binary=*/false, /*InFile=*/GetCurrentFileOrBufferName())};

// TODO: Options should come from CompilerInvocation
Unparse(llvm::outs(), *parseTree,
Unparse(*os, *parseTree,
/*encoding=*/Fortran::parser::Encoding::UTF_8,
/*capitalizeKeywords=*/true, /*backslashEscapes=*/false,
/*preStatement=*/nullptr,
Expand Down
9 changes: 6 additions & 3 deletions flang/tools/f18/CMakeLists.txt
Expand Up @@ -62,10 +62,13 @@ add_custom_target(module_files ALL DEPENDS ${MODULE_FILES})

install(TARGETS f18 DESTINATION bin)

set(FLANG_DEFAULT_DRIVER "flang-new")
if (NOT FLANG_BUILD_NEW_DRIVER)
set(FLANG_DEFAULT_DRIVER "f18")
endif()

# This flang shell script will only work in a POSIX shell.
if (NOT WIN32)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/flang
DESTINATION ${CMAKE_BINARY_DIR}/bin
FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/flang.in ${CMAKE_BINARY_DIR}/bin/flang @ONLY)
install(PROGRAMS ${CMAKE_BINARY_DIR}/bin/flang DESTINATION bin)
endif()
17 changes: 16 additions & 1 deletion flang/tools/f18/f18.cpp
Expand Up @@ -319,7 +319,22 @@ std::string CompileFortran(std::string path, Fortran::parser::Options options,
Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);
}
if (driver.dumpUnparse) {
Unparse(llvm::outs(), parseTree, driver.encoding, true /*capitalize*/,
// Prepare the output stream
std::unique_ptr<llvm::raw_fd_ostream> os;
std::string outputFile = "-";
if (!driver.outputPath.empty()) {
outputFile = driver.outputPath;
}

std::error_code EC;
os.reset(new llvm::raw_fd_ostream(
outputFile, EC, llvm::sys::fs::OF_TextWithCRLF));
if (EC) {
llvm::errs() << EC.message() << "\n";
std::exit(EXIT_FAILURE);
}

Unparse(*os, parseTree, driver.encoding, true /*capitalize*/,
options.features.IsEnabled(
Fortran::common::LanguageFeature::BackslashEscapes),
nullptr /* action before each statement */,
Expand Down
16 changes: 0 additions & 16 deletions flang/tools/f18/flang

This file was deleted.

0 comments on commit e77191c

Please sign in to comment.