Skip to content

Commit a65afce

Browse files
committed
[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
1 parent 988748c commit a65afce

File tree

6 files changed

+86
-5
lines changed

6 files changed

+86
-5
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,9 +3995,9 @@ defm openmp_implicit_rpath: BoolFOption<"openmp-implicit-rpath",
39953995
NegFlag<SetFalse>>;
39963996
def r : Flag<["-"], "r">, Flags<[LinkerInput,NoArgumentUnused]>,
39973997
Group<Link_Group>;
3998-
def save_temps_EQ : Joined<["-", "--"], "save-temps=">, Flags<[CC1Option, NoXarchOption]>,
3998+
def save_temps_EQ : Joined<["-", "--"], "save-temps=">, Flags<[CC1Option, FlangOption, NoXarchOption]>,
39993999
HelpText<"Save intermediate compilation results.">;
4000-
def save_temps : Flag<["-", "--"], "save-temps">, Flags<[NoXarchOption]>,
4000+
def save_temps : Flag<["-", "--"], "save-temps">, Flags<[FlangOption, NoXarchOption]>,
40014001
Alias<save_temps_EQ>, AliasArgs<["cwd"]>,
40024002
HelpText<"Save intermediate compilation results">;
40034003
def save_stats_EQ : Joined<["-", "--"], "save-stats=">, Flags<[NoXarchOption]>,
@@ -4125,7 +4125,7 @@ def y : Joined<["-"], "y">;
41254125

41264126
defm integrated_as : BoolFOption<"integrated-as",
41274127
CodeGenOpts<"DisableIntegratedAS">, DefaultFalse,
4128-
NegFlag<SetTrue, [CC1Option], "Disable">, PosFlag<SetFalse, [], "Enable">,
4128+
NegFlag<SetTrue, [CC1Option, FlangOption], "Disable">, PosFlag<SetFalse, [], "Enable">,
41294129
BothFlags<[], " the integrated assembler">>;
41304130

41314131
def fintegrated_cc1 : Flag<["-"], "fintegrated-cc1">,
@@ -4137,7 +4137,7 @@ def fno_integrated_cc1 : Flag<["-"], "fno-integrated-cc1">,
41374137

41384138
def : Flag<["-"], "integrated-as">, Alias<fintegrated_as>, Flags<[NoXarchOption]>;
41394139
def : Flag<["-"], "no-integrated-as">, Alias<fno_integrated_as>,
4140-
Flags<[CC1Option, NoXarchOption]>;
4140+
Flags<[CC1Option, FlangOption, NoXarchOption]>;
41414141

41424142
def working_directory : JoinedOrSeparate<["-"], "working-directory">, Flags<[CC1Option]>,
41434143
HelpText<"Resolve file paths relative to the specified directory">,

clang/include/clang/Driver/Types.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ TYPE("c++-module-cpp-output", PP_CXXModule, INVALID, "iim", phases
7777
TYPE("ada", Ada, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
7878
TYPE("assembler", PP_Asm, INVALID, "s", phases::Assemble, phases::Link)
7979
TYPE("assembler-with-cpp", Asm, PP_Asm, "S", phases::Preprocess, phases::Assemble, phases::Link)
80-
TYPE("f95", PP_Fortran, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
80+
TYPE("f95", PP_Fortran, INVALID, "i", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
8181
TYPE("f95-cpp-input", Fortran, PP_Fortran, nullptr, phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
8282
TYPE("java", Java, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
8383

flang/test/Driver/driver-help-hidden.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
! CHECK-NEXT: -flogical-abbreviations Enable logical abbreviations
4545
! CHECK-NEXT: -fno-automatic Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE
4646
! CHECK-NEXT: -fno-color-diagnostics Disable colors in diagnostics
47+
! CHECK-NEXT: -fno-integrated-as Disable the integrated assembler
4748
! CHECK-NEXT: -fopenacc Enable OpenACC
4849
! CHECK-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.
4950
! CHECK-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
@@ -58,6 +59,8 @@
5859
! CHECK-NEXT: -print-effective-triple Print the effective target triple
5960
! CHECK-NEXT: -print-target-triple Print the normalized target triple
6061
! CHECK-NEXT: -P Disable linemarker output in -E mode
62+
! CHECK-NEXT: -save-temps=<value> Save intermediate compilation results.
63+
! CHECK-NEXT: -save-temps Save intermediate compilation results
6164
! CHECK-NEXT: -std=<value> Language standard to compile for
6265
! CHECK-NEXT: -S Only run preprocess and compilation steps
6366
! CHECK-NEXT: --target=<value> Generate code for the given target

flang/test/Driver/driver-help.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
! HELP-NEXT: -flogical-abbreviations Enable logical abbreviations
4343
! HELP-NEXT: -fno-automatic Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE
4444
! HELP-NEXT: -fno-color-diagnostics Disable colors in diagnostics
45+
! HELP-NEXT: -fno-integrated-as Disable the integrated assembler
4546
! HELP-NEXT: -fopenacc Enable OpenACC
4647
! HELP-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.
4748
! HELP-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
@@ -56,6 +57,8 @@
5657
! HELP-NEXT: -print-effective-triple Print the effective target triple
5758
! HELP-NEXT: -print-target-triple Print the normalized target triple
5859
! HELP-NEXT: -P Disable linemarker output in -E mode
60+
! HELP-NEXT: -save-temps=<value> Save intermediate compilation results.
61+
! HELP-NEXT: -save-temps Save intermediate compilation results
5962
! HELP-NEXT: -std=<value> Language standard to compile for
6063
! HELP-NEXT: -S Only run preprocess and compilation steps
6164
! HELP-NEXT: --target=<value> Generate code for the given target
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! Tests for the `-fno-integrated-as` flag.
2+
3+
! UNSUPPORTED: system-windows
4+
5+
!--------------------------
6+
! With `-fno-integrated-as`
7+
!--------------------------
8+
! Verify that there _is_ a separate line with an assembler invocation
9+
! RUN: %flang -c -fno-integrated-as %s -### 2>&1 | FileCheck %s
10+
! CHECK-LABEL: "-fc1"
11+
! CHECK-SAME: "-o" "[[assembly_file:.*]].s"
12+
! CHECK-NEXT: "-o" "{{.*}}.o" "[[assembly_file:.*]].s"
13+
14+
!-----------------------------
15+
! Without `-fno-integrated-as`
16+
!-----------------------------
17+
! Verify that there _is no_ separate line with an assembler invocation
18+
! RUN: %flang -c %s -### 2>&1 | FileCheck %s -check-prefix=DEFAULT
19+
! DEFAULT-LABEL: "-fc1"
20+
! DEFAULT-SAME: "-o" "{{.*}}.o" "{{.*}}fno-integrated-as.f90"

flang/test/Driver/save-temps.f90

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
! Tests for the `-save-temps` flag. As `flang` does not implement `-fc1as` (i.e. a driver for the integrated assembler), we need to
2+
! use `-fno-integrated-as` here.
3+
4+
! UNSUPPORTED: system-windows
5+
6+
!--------------------------
7+
! Basic case: `-save-temps`
8+
!--------------------------
9+
! RUN: %flang -save-temps -fno-integrated-as %s -### 2>&1 | FileCheck %s
10+
! CHECK: "-o" "save-temps.i"
11+
! CHECK-NEXT: "-o" "save-temps.bc"
12+
! CHECK-NEXT: "-o" "save-temps.s"
13+
! CHECK-NEXT: "-o" "save-temps.o"
14+
! CHECK-NEXT: "-o" "a.out"
15+
16+
!--------------------------
17+
! `-save-temps=cwd`
18+
!--------------------------
19+
! This should work the same as -save-temps above
20+
21+
! RUN: %flang -save-temps=cwd -fno-integrated-as %s -### 2>&1 | FileCheck %s -check-prefix=CWD
22+
! CWD: "-o" "save-temps.i"
23+
! CWD-NEXT: "-o" "save-temps.bc"
24+
! CWD-NEXT: "-o" "save-temps.s"
25+
! CWD-NEXT: "-o" "save-temps.o"
26+
! CWD-NEXT: "-o" "a.out"
27+
28+
!--------------------------
29+
! `-save-temps=obj`
30+
!--------------------------
31+
! Check that temp files are saved in the same directory as the output file
32+
! regardless of whether -o is specified.
33+
34+
! RUN: %flang -save-temps=obj -fno-integrated-as -o obj/dir/a.out %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-OBJ
35+
! CHECK-OBJ: "-o" "obj/dir/save-temps.i"
36+
! CHECK-OBJ-NEXT: "-o" "obj/dir/save-temps.bc"
37+
! CHECK-OBJ-NEXT: "-o" "obj/dir/save-temps.s"
38+
! CHECK-OBJ-NEXT: "-o" "obj/dir/save-temps.o"
39+
! CHECK-OBJ-NEXT: "-o" "obj/dir/a.out"
40+
41+
! RUN: %flang -save-temps=obj -fno-integrated-as %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-OBJ-NOO
42+
! CHECK-OBJ-NOO: "-o" "save-temps.i"
43+
! CHECK-OBJ-NOO-NEXT: "-o" "save-temps.bc"
44+
! CHECK-OBJ-NOO-NEXT: "-o" "save-temps.s"
45+
! CHECK-OBJ-NOO-NEXT: "-o" "save-temps.o"
46+
! CHECK-OBJ-NOO-NEXT: "-o" "a.out"
47+
48+
!--------------------------
49+
! `-S` without `-save-temps`
50+
!--------------------------
51+
! Check for a single `flang -fc1` invocation when NOT using -save-temps.
52+
! RUN: %flang -S %s -### 2>&1 | FileCheck %s -check-prefix=NO-TEMPS
53+
! NO-TEMPS: "-fc1"
54+
! NO-TEMPS-SAME: "-S"
55+
! NO-TEMPS-SAME: "-o" "save-temps.s"

0 commit comments

Comments
 (0)