[flang][driver] Add support for -save-temps

This patch adds support for `-save-temps` in `flang-new`, Flang's
compiler driver. The semantics of this option are inherited from Clang.

The file extension for temporary Fortran preprocessed files is set to
`i`. This is identical to what Clang uses for C (or C++) preprocessed
files. I have tried researching what other compilers do here, but I
couldn't find any definitive answers. One GFortran thread [1] suggests
that indeed it is not clear what the right approach should be.

Normally, various phases in Clang/Flang are combined. The `-save-temps`
option works by forcing the compiler to run every phase separately. As
there is no integrated assembler driver in Flang, user will have to use
`-save-temps` together with `-fno-integrated-as`. Otherwise, an
invocation to the integrated assembler would be generated generated,
which is going to fail (i.e. something equivalent to `clang -cc1as` from
Clang).

There are no specific plans for implementing an integrated assembler for
Flang for now. One possible solution would be to share it entirely with
Clang.

Note that on Windows you will get the following error when using
`-fno-integrated-as`:
```bash
  flang-new: error: there is no external assembler that can be used on this platform
```
Unfortunately, I don't have access to a Windows machine to investigate
this. Instead, I marked the tests in this patch as unsupported on
Windows.

[1] https://gcc.gnu.org/bugzilla//show_bug.cgi?id=81615

Differential Revision: https://reviews.llvm.org/D124669
This commit is contained in:
Andrzej Warzynski 2022-04-29 11:48:36 +00:00
parent 988748c077
commit a65afce731
6 changed files with 86 additions and 5 deletions

View File

@ -3995,9 +3995,9 @@ defm openmp_implicit_rpath: BoolFOption<"openmp-implicit-rpath",
NegFlag<SetFalse>>;
def r : Flag<["-"], "r">, Flags<[LinkerInput,NoArgumentUnused]>,
Group<Link_Group>;
def save_temps_EQ : Joined<["-", "--"], "save-temps=">, Flags<[CC1Option, NoXarchOption]>,
def save_temps_EQ : Joined<["-", "--"], "save-temps=">, Flags<[CC1Option, FlangOption, NoXarchOption]>,
HelpText<"Save intermediate compilation results.">;
def save_temps : Flag<["-", "--"], "save-temps">, Flags<[NoXarchOption]>,
def save_temps : Flag<["-", "--"], "save-temps">, Flags<[FlangOption, NoXarchOption]>,
Alias<save_temps_EQ>, AliasArgs<["cwd"]>,
HelpText<"Save intermediate compilation results">;
def save_stats_EQ : Joined<["-", "--"], "save-stats=">, Flags<[NoXarchOption]>,
@ -4125,7 +4125,7 @@ def y : Joined<["-"], "y">;
defm integrated_as : BoolFOption<"integrated-as",
CodeGenOpts<"DisableIntegratedAS">, DefaultFalse,
NegFlag<SetTrue, [CC1Option], "Disable">, PosFlag<SetFalse, [], "Enable">,
NegFlag<SetTrue, [CC1Option, FlangOption], "Disable">, PosFlag<SetFalse, [], "Enable">,
BothFlags<[], " the integrated assembler">>;
def fintegrated_cc1 : Flag<["-"], "fintegrated-cc1">,
@ -4137,7 +4137,7 @@ def fno_integrated_cc1 : Flag<["-"], "fno-integrated-cc1">,
def : Flag<["-"], "integrated-as">, Alias<fintegrated_as>, Flags<[NoXarchOption]>;
def : Flag<["-"], "no-integrated-as">, Alias<fno_integrated_as>,
Flags<[CC1Option, NoXarchOption]>;
Flags<[CC1Option, FlangOption, NoXarchOption]>;
def working_directory : JoinedOrSeparate<["-"], "working-directory">, Flags<[CC1Option]>,
HelpText<"Resolve file paths relative to the specified directory">,

View File

@ -77,7 +77,7 @@ TYPE("c++-module-cpp-output", PP_CXXModule, INVALID, "iim", phases
TYPE("ada", Ada, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("assembler", PP_Asm, INVALID, "s", phases::Assemble, phases::Link)
TYPE("assembler-with-cpp", Asm, PP_Asm, "S", phases::Preprocess, phases::Assemble, phases::Link)
TYPE("f95", PP_Fortran, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("f95", PP_Fortran, INVALID, "i", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("f95-cpp-input", Fortran, PP_Fortran, nullptr, phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("java", Java, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)

View File

@ -44,6 +44,7 @@
! CHECK-NEXT: -flogical-abbreviations Enable logical abbreviations
! CHECK-NEXT: -fno-automatic Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE
! CHECK-NEXT: -fno-color-diagnostics Disable colors in diagnostics
! CHECK-NEXT: -fno-integrated-as Disable the integrated assembler
! CHECK-NEXT: -fopenacc Enable OpenACC
! CHECK-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.
! CHECK-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
@ -58,6 +59,8 @@
! CHECK-NEXT: -print-effective-triple Print the effective target triple
! CHECK-NEXT: -print-target-triple Print the normalized target triple
! CHECK-NEXT: -P Disable linemarker output in -E mode
! CHECK-NEXT: -save-temps=<value> Save intermediate compilation results.
! CHECK-NEXT: -save-temps Save intermediate compilation results
! CHECK-NEXT: -std=<value> Language standard to compile for
! CHECK-NEXT: -S Only run preprocess and compilation steps
! CHECK-NEXT: --target=<value> Generate code for the given target

View File

@ -42,6 +42,7 @@
! HELP-NEXT: -flogical-abbreviations Enable logical abbreviations
! HELP-NEXT: -fno-automatic Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE
! HELP-NEXT: -fno-color-diagnostics Disable colors in diagnostics
! HELP-NEXT: -fno-integrated-as Disable the integrated assembler
! HELP-NEXT: -fopenacc Enable OpenACC
! HELP-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.
! HELP-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
@ -56,6 +57,8 @@
! HELP-NEXT: -print-effective-triple Print the effective target triple
! HELP-NEXT: -print-target-triple Print the normalized target triple
! HELP-NEXT: -P Disable linemarker output in -E mode
! HELP-NEXT: -save-temps=<value> Save intermediate compilation results.
! HELP-NEXT: -save-temps Save intermediate compilation results
! HELP-NEXT: -std=<value> Language standard to compile for
! HELP-NEXT: -S Only run preprocess and compilation steps
! HELP-NEXT: --target=<value> Generate code for the given target

View File

@ -0,0 +1,20 @@
! Tests for the `-fno-integrated-as` flag.
! UNSUPPORTED: system-windows
!--------------------------
! With `-fno-integrated-as`
!--------------------------
! Verify that there _is_ a separate line with an assembler invocation
! RUN: %flang -c -fno-integrated-as %s -### 2>&1 | FileCheck %s
! CHECK-LABEL: "-fc1"
! CHECK-SAME: "-o" "[[assembly_file:.*]].s"
! CHECK-NEXT: "-o" "{{.*}}.o" "[[assembly_file:.*]].s"
!-----------------------------
! Without `-fno-integrated-as`
!-----------------------------
! Verify that there _is no_ separate line with an assembler invocation
! RUN: %flang -c %s -### 2>&1 | FileCheck %s -check-prefix=DEFAULT
! DEFAULT-LABEL: "-fc1"
! DEFAULT-SAME: "-o" "{{.*}}.o" "{{.*}}fno-integrated-as.f90"

View File

@ -0,0 +1,55 @@
! Tests for the `-save-temps` flag. As `flang` does not implement `-fc1as` (i.e. a driver for the integrated assembler), we need to
! use `-fno-integrated-as` here.
! UNSUPPORTED: system-windows
!--------------------------
! Basic case: `-save-temps`
!--------------------------
! RUN: %flang -save-temps -fno-integrated-as %s -### 2>&1 | FileCheck %s
! CHECK: "-o" "save-temps.i"
! CHECK-NEXT: "-o" "save-temps.bc"
! CHECK-NEXT: "-o" "save-temps.s"
! CHECK-NEXT: "-o" "save-temps.o"
! CHECK-NEXT: "-o" "a.out"
!--------------------------
! `-save-temps=cwd`
!--------------------------
! This should work the same as -save-temps above
! RUN: %flang -save-temps=cwd -fno-integrated-as %s -### 2>&1 | FileCheck %s -check-prefix=CWD
! CWD: "-o" "save-temps.i"
! CWD-NEXT: "-o" "save-temps.bc"
! CWD-NEXT: "-o" "save-temps.s"
! CWD-NEXT: "-o" "save-temps.o"
! CWD-NEXT: "-o" "a.out"
!--------------------------
! `-save-temps=obj`
!--------------------------
! Check that temp files are saved in the same directory as the output file
! regardless of whether -o is specified.
! RUN: %flang -save-temps=obj -fno-integrated-as -o obj/dir/a.out %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-OBJ
! CHECK-OBJ: "-o" "obj/dir/save-temps.i"
! CHECK-OBJ-NEXT: "-o" "obj/dir/save-temps.bc"
! CHECK-OBJ-NEXT: "-o" "obj/dir/save-temps.s"
! CHECK-OBJ-NEXT: "-o" "obj/dir/save-temps.o"
! CHECK-OBJ-NEXT: "-o" "obj/dir/a.out"
! RUN: %flang -save-temps=obj -fno-integrated-as %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-OBJ-NOO
! CHECK-OBJ-NOO: "-o" "save-temps.i"
! CHECK-OBJ-NOO-NEXT: "-o" "save-temps.bc"
! CHECK-OBJ-NOO-NEXT: "-o" "save-temps.s"
! CHECK-OBJ-NOO-NEXT: "-o" "save-temps.o"
! CHECK-OBJ-NOO-NEXT: "-o" "a.out"
!--------------------------
! `-S` without `-save-temps`
!--------------------------
! Check for a single `flang -fc1` invocation when NOT using -save-temps.
! RUN: %flang -S %s -### 2>&1 | FileCheck %s -check-prefix=NO-TEMPS
! NO-TEMPS: "-fc1"
! NO-TEMPS-SAME: "-S"
! NO-TEMPS-SAME: "-o" "save-temps.s"