Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flang][driver] Add support for generating executables
This patch adds 2 missing items required for `flang-new` to be able to generate executables: 1. The Fortran_main runtime library, which implements the main entry point into Fortran's `PROGRAM` in Flang, 2. Extra linker flags to include Fortran runtime libraries (e.g. Fortran_main). Fortran_main is the bridge between object files generated by Flang and the C runtime that takes care of program set-up at system-level. For every Fortran `PROGRAM`, Flang generates the `_QQmain` function. Fortran_main implements the C `main` function that simply calls `_QQmain`. Additionally, "<driver-path>/../lib" directory is added to the list of search directories for libraries. This is where the required runtime libraries are currently located. Note that this the case for the build directory. We haven't considered installation directories/targets yet. With this change, you can generate an executable that will print `hello, world!` as follows: ```bash $ cat hello.f95 PROGRAM HELLO write(*, *) "hello, world!" END PROGRAM HELLO $ flang-new -flang-experimental-exec hello.f95 ./a.out hello, world! ``` NOTE 1: Fortran_main has to be a static library at all times. It invokes `_QQmain`, which is the main entry point generated by Flang for the given input file (you can check this with `flang-new -S hello.f95 -o - | grep "Qmain"`). This means that Fortran_main has an unresolved dependency at build time. The linker will allow this for a static library. However, if Fortran_main was a shared object, then the linker will produce an error: `undefined symbol: `_QQmain`. NOTE 2: When Fortran runtime libraries are generated as shared libraries (excluding Fortran_main, which is always static), you will need to tell the dynamic linker (by e.g. tweaking LD_LIBRARY_PATH) where to look for them when invoking the executables. For example: ```bash LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<flang-build-dir>/lib/ ./a.out ``` NOTE 3: This feature is considered experimental and currently guarded with a flag: `-flang-experimental-exec`. Differential Revision: https://reviews.llvm.org/D122008 [1] https://github.com/flang-compiler/f18-llvm-project CREDITS: Fortran_main was originally written by Eric Schweitz, Jean Perier, Peter Klausler and Steve Scalpone in the fir-dev` branch in [1]. Co-authored-by: Eric Schweitz <eschweitz@nvidia.com> Co-authored-by: Peter Klausler <pklausler@nvidia.com> Co-authored-by: Jean Perier <jperier@nvidia.com> Co-authored-by: Steve Scalpone <sscalpone@nvidia.com
- Loading branch information
1 parent
2fee8dd
commit 97a32d3
Showing
10 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
llvm_add_library(Fortran_main STATIC | ||
Fortran_main.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- runtime/FortranMain/Fortran_main.c --------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "flang/Runtime/main.h" | ||
#include "flang/Runtime/stop.h" | ||
|
||
/* main entry into PROGRAM */ | ||
void _QQmain(); | ||
|
||
/* C main stub */ | ||
int main(int argc, const char *argv[], const char *envp[]) { | ||
RTNAME(ProgramStart)(argc, argv, envp); | ||
_QQmain(); | ||
RTNAME(ProgramEndStatement)(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
! Verify that the Fortran runtime libraries are present in the linker | ||
! invocation. These libraries are added on top of other standard runtime | ||
! libraries that the Clang driver will include. | ||
|
||
! NOTE: The additional linker flags tested here are currently specified in | ||
! clang/lib/Driver/Toolchains/Gnu.cpp. This makes the current implementation GNU | ||
! (Linux) specific. The following line will make sure that this test is skipped | ||
! on Windows. Ideally we should find a more robust way of testing this. | ||
! REQUIRES: shell | ||
! UNSUPPORTED: darwin, macos, system-windows | ||
|
||
!------------ | ||
! RUN COMMAND | ||
!------------ | ||
! Use `--ld-path` so that the linker location (used in the LABEL below) is deterministic. | ||
! RUN: %flang -### -flang-experimental-exec --ld-path=/usr/bin/ld %S/Inputs/hello.f90 2>&1 | FileCheck %s | ||
|
||
!---------------- | ||
! EXPECTED OUTPUT | ||
!---------------- | ||
! Compiler invocation to generate the object file | ||
! CHECK-LABEL: {{.*}} "-emit-obj" | ||
! CHECK-SAME: "-o" "[[object_file:.*]]" {{.*}}Inputs/hello.f90 | ||
|
||
! Linker invocation to generate the executable | ||
! CHECK-LABEL: "/usr/bin/ld" | ||
! CHECK-SAME: "[[object_file]]" | ||
! CHECK-SAME: -lFortran_main | ||
! CHECK-SAME: -lFortranRuntime | ||
! CHECK-SAME: -lFortranDecimal | ||
! CHECK-SAME: -lm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters