Skip to content

Commit 4d1a0c9

Browse files
committed
Reduce downstream delta
This commit is motivated by reducing the merge burden by shrinking the diff between llvm upstream and classic-flang-llvm-project. Outside of Flang, Fortran code is fed through the Compile phase, and the appropriate tooling is picked up through ToolChain::SelectTool. Classic Flang introduced a FortranFrontend, but these days this seems unnecessary. Fortran can go through the same machinery as everything else. * Use the Preprocess phase to preprocess Fortran code. This phase is always combined with the Compile phase. * Use the Compile phase to lower Fortran code to LLVM IR, and use the Backend phase to compile and optimize the IR. These phases are never combined. * Remove FortranFrontendJobClass. * Remove FortranFrontend tool (instead it's just the Flang tool, which in Classic Flang mode is Classic Flang). * Update tests which inspect the output of the Classic Flang tooling, and ensures that the driver does the right thing for various types of inputs. Based on a patch from Peter Waller <[email protected]>.
1 parent bea6f6d commit 4d1a0c9

File tree

15 files changed

+125
-102
lines changed

15 files changed

+125
-102
lines changed

clang/include/clang/Driver/Action.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class Action {
6262
AnalyzeJobClass,
6363
MigrateJobClass,
6464
CompileJobClass,
65-
FortranFrontendJobClass,
6665
BackendJobClass,
6766
AssembleJobClass,
6867
LinkJobClass,
@@ -471,16 +470,6 @@ class MigrateJobAction : public JobAction {
471470
}
472471
};
473472

474-
class FortranFrontendJobAction : public JobAction {
475-
void anchor() override;
476-
public:
477-
FortranFrontendJobAction(Action *Input, types::ID OutputType);
478-
479-
static bool classof(const Action *A) {
480-
return A->getKind() == FortranFrontendJobClass;
481-
}
482-
};
483-
484473
class CompileJobAction : public JobAction {
485474
void anchor() override;
486475

clang/include/clang/Driver/Phases.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace phases {
1717
enum ID {
1818
Preprocess,
1919
Precompile,
20-
FortranFrontend,
2120
Compile,
2221
Backend,
2322
Assemble,

clang/include/clang/Driver/ToolChain.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ class ToolChain {
159159

160160
mutable std::unique_ptr<Tool> Clang;
161161
mutable std::unique_ptr<Tool> Flang;
162-
mutable std::unique_ptr<Tool> FortranFrontend;
163162
mutable std::unique_ptr<Tool> Assemble;
164163
mutable std::unique_ptr<Tool> Link;
165164
mutable std::unique_ptr<Tool> StaticLibTool;
@@ -170,7 +169,6 @@ class ToolChain {
170169

171170
Tool *getClang() const;
172171
Tool *getFlang() const;
173-
Tool *getFortranFrontend() const;
174172
Tool *getAssemble() const;
175173
Tool *getLink() const;
176174
Tool *getStaticLibTool() const;

clang/include/clang/Driver/Types.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ TYPE("ada", Ada, INVALID, nullptr, phases
8080
TYPE("assembler", PP_Asm, INVALID, "s", phases::Assemble, phases::Link)
8181
TYPE("assembler-with-cpp", Asm, PP_Asm, "S", phases::Preprocess, phases::Assemble, phases::Link)
8282
#ifdef ENABLE_CLASSIC_FLANG
83-
TYPE("f77", PP_F_FixedForm, INVALID, "f", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
84-
TYPE("f77-cpp-input", F_FixedForm, PP_F_FixedForm, "F", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
85-
TYPE("f95", PP_F_FreeForm, INVALID, "f95", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
86-
TYPE("f95-cpp-input", F_FreeForm, PP_F_FreeForm, "F95", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
83+
TYPE("f77", PP_F_FixedForm, INVALID, "f", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
84+
TYPE("f77-cpp-input", F_FixedForm, PP_F_FixedForm, "F", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
85+
TYPE("f95", PP_F_FreeForm, INVALID, "f95", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
86+
TYPE("f95-cpp-input", F_FreeForm, PP_F_FreeForm, "F95", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
8787
#else
8888
TYPE("f95", PP_Fortran, INVALID, "i", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
8989
TYPE("f95-cpp-input", Fortran, PP_Fortran, nullptr, phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)

clang/lib/Driver/Action.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const char *Action::getClassName(ActionClass AC) {
2929
return "api-extractor";
3030
case AnalyzeJobClass: return "analyzer";
3131
case MigrateJobClass: return "migrator";
32-
case FortranFrontendJobClass: return "fortran-frontend";
3332
case CompileJobClass: return "compiler";
3433
case BackendJobClass: return "backend";
3534
case AssembleJobClass: return "assembler";
@@ -373,12 +372,6 @@ void MigrateJobAction::anchor() {}
373372
MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
374373
: JobAction(MigrateJobClass, Input, OutputType) {}
375374

376-
void FortranFrontendJobAction::anchor() {}
377-
378-
FortranFrontendJobAction::FortranFrontendJobAction(Action *Input,
379-
types::ID OutputType)
380-
: JobAction(FortranFrontendJobClass, Input, OutputType) {}
381-
382375
void CompileJobAction::anchor() {}
383376

384377
CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)

clang/lib/Driver/Driver.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -346,27 +346,15 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
346346

347347
// -{E,EP,P,M,MM} only run the preprocessor.
348348
if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) ||
349-
#ifdef ENABLE_CLASSIC_FLANG
350-
(PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
351-
#endif
352349
(PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) ||
353350
(PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
354351
(PhaseArg = DAL.getLastArg(options::OPT__SLASH_P)) ||
355352
CCGenDiagnostics) {
356353
#ifdef ENABLE_CLASSIC_FLANG
357-
// -fsyntax-only or -E stops Fortran compilation after FortranFrontend
358-
if (IsFlangMode() && (DAL.getLastArg(options::OPT_E) ||
359-
DAL.getLastArg(options::OPT_fsyntax_only))) {
360-
FinalPhase = phases::FortranFrontend;
361-
362-
// if not Fortran, fsyntax_only implies 'Compile' is the FinalPhase
363-
} else if (DAL.getLastArg(options::OPT_fsyntax_only)) {
354+
if (IsFlangMode())
364355
FinalPhase = phases::Compile;
365-
366-
// everything else has 'Preprocess' as its FinalPhase
367-
} else {
356+
else
368357
FinalPhase = phases::Preprocess;
369-
}
370358
#else
371359
FinalPhase = phases::Preprocess;
372360
#endif
@@ -380,14 +368,9 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
380368
options::OPT_fmodule_header_EQ))) {
381369
FinalPhase = phases::Precompile;
382370

383-
#ifdef ENABLE_CLASSIC_FLANG
384-
// -{analyze,emit-ast} only run up to the compiler.
385-
} else if ((PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) ||
386-
#else
387371
// -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
388372
} else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
389373
(PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) ||
390-
#endif
391374
(PhaseArg = DAL.getLastArg(options::OPT_print_enabled_extensions)) ||
392375
(PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) ||
393376
(PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) ||
@@ -4076,10 +4059,13 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
40764059
if (InputArg->isClaimed())
40774060
continue;
40784061

4079-
// Fortran input is preprocessed using the frontend.
4080-
if (InitialPhase == phases::FortranFrontend &&
4062+
#ifdef ENABLE_CLASSIC_FLANG
4063+
// If the input is detected as already preprocessed (e.g. has the .f95
4064+
// extension), and the user specifies -E, preprocess the file anyway.
4065+
if (IsFlangMode() && InitialPhase == phases::Compile &&
40814066
FinalPhase == phases::Preprocess)
40824067
continue;
4068+
#endif
40834069

40844070
// Claim here to avoid the more general unused warning.
40854071
InputArg->claim();
@@ -4854,13 +4840,6 @@ Action *Driver::ConstructPhaseAction(
48544840

48554841
return C.MakeAction<PrecompileJobAction>(Input, OutputTy);
48564842
}
4857-
case phases::FortranFrontend: {
4858-
if (Args.hasArg(options::OPT_fsyntax_only))
4859-
return C.MakeAction<FortranFrontendJobAction>(Input,
4860-
types::TY_Nothing);
4861-
return C.MakeAction<FortranFrontendJobAction>(Input,
4862-
types::TY_LLVM_IR);
4863-
}
48644843
case phases::Compile: {
48654844
if (Args.hasArg(options::OPT_fsyntax_only))
48664845
return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing);
@@ -4883,6 +4862,10 @@ Action *Driver::ConstructPhaseAction(
48834862
return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing);
48844863
if (Args.hasArg(options::OPT_extract_api))
48854864
return C.MakeAction<ExtractAPIJobAction>(Input, types::TY_API_INFO);
4865+
#ifdef ENABLE_CLASSIC_FLANG
4866+
if (IsFlangMode())
4867+
return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_IR);
4868+
#endif
48864869
return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC);
48874870
}
48884871
case phases::Backend: {
@@ -5349,6 +5332,10 @@ class ToolSelector final {
53495332
if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
53505333
return nullptr;
53515334

5335+
// Classic Flang is not integrated with the backend.
5336+
if (C.getDriver().IsFlangMode() && !T->hasIntegratedAssembler())
5337+
return nullptr;
5338+
53525339
if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode))
53535340
return nullptr;
53545341

@@ -6620,8 +6607,11 @@ bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
66206607
return false;
66216608

66226609
// And say "no" if this is not a kind of action flang understands.
6623-
if (!isa<PreprocessJobAction>(JA) && !isa<CompileJobAction>(JA) &&
6624-
!isa<BackendJobAction>(JA))
6610+
if (!isa<PreprocessJobAction>(JA) && !isa<CompileJobAction>(JA)
6611+
#ifndef ENABLE_CLASSIC_FLANG
6612+
&& !isa<BackendJobAction>(JA)
6613+
#endif
6614+
)
66256615
return false;
66266616

66276617
return true;

clang/lib/Driver/Phases.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const char *phases::getPhaseName(ID Id) {
1616
switch (Id) {
1717
case Preprocess: return "preprocessor";
1818
case Precompile: return "precompiler";
19-
case FortranFrontend: return "fortran-frontend";
2019
case Compile: return "compiler";
2120
case Backend: return "backend";
2221
case Assemble: return "assembler";

clang/lib/Driver/ToolChain.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,13 @@ Tool *ToolChain::getClang() const {
467467
}
468468

469469
Tool *ToolChain::getFlang() const {
470-
#ifndef ENABLE_CLASSIC_FLANG
471470
if (!Flang)
472-
Flang.reset(new tools::Flang(*this));
473-
return Flang.get();
471+
#ifdef ENABLE_CLASSIC_FLANG
472+
Flang.reset(new tools::ClassicFlang(*this));
474473
#else
475-
llvm_unreachable("Flang is not supported by this toolchain");
474+
Flang.reset(new tools::Flang(*this));
476475
#endif
476+
return Flang.get();
477477
}
478478

479479
Tool *ToolChain::buildAssembler() const {
@@ -494,16 +494,6 @@ Tool *ToolChain::getAssemble() const {
494494
return Assemble.get();
495495
}
496496

497-
Tool *ToolChain::getFortranFrontend() const {
498-
#ifdef ENABLE_CLASSIC_FLANG
499-
if (!FortranFrontend)
500-
FortranFrontend.reset(new tools::ClassicFlang(*this));
501-
return FortranFrontend.get();
502-
#else
503-
llvm_unreachable("Fortran is not supported by this toolchain");
504-
#endif
505-
}
506-
507497
Tool *ToolChain::getClangAs() const {
508498
if (!Assemble)
509499
Assemble.reset(new tools::ClangAs(*this));
@@ -587,9 +577,6 @@ Tool *ToolChain::getTool(Action::ActionClass AC) const {
587577
return getOffloadPackager();
588578
case Action::LinkerWrapperJobClass:
589579
return getLinkerWrapper();
590-
591-
case Action::FortranFrontendJobClass:
592-
return getFortranFrontend();
593580
}
594581

595582
llvm_unreachable("Invalid tool kind.");

clang/lib/Driver/ToolChains/ClassicFlang.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class LLVM_LIBRARY_VISIBILITY ClassicFlang : public Tool {
3333

3434
bool hasGoodDiagnostics() const override { return true; }
3535
bool hasIntegratedAssembler() const override { return false; }
36-
bool hasIntegratedCPP() const override { return false; }
36+
bool hasIntegratedCPP() const override { return true; }
3737

3838
void ConstructJob(Compilation &C, const JobAction &JA,
3939
const InputInfo &Output, const InputInfoList &Inputs,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! REQUIRES: classic_flang
2+
3+
! Check that the driver invokes flang1 correctly for fixed-form Fortran code
4+
! which requires preprocessing.
5+
6+
! RUN: %clang --driver-mode=flang -target x86_64-unknown-linux-gnu -c %s -### 2>&1 \
7+
! RUN: | FileCheck %s
8+
! CHECK: "flang1"
9+
! CHECK-SAME: "-preprocess"
10+
! CHECK-SAME: "-nofreeform"
11+
! CHECK-NEXT: "flang2"
12+
! CHECK-NEXT: {{clang.* "-cc1"}}

0 commit comments

Comments
 (0)