Skip to content

Commit 48bdbc1

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 b6ba3c5 commit 48bdbc1

File tree

15 files changed

+125
-101
lines changed

15 files changed

+125
-101
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,
@@ -472,16 +471,6 @@ class MigrateJobAction : public JobAction {
472471
}
473472
};
474473

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

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
@@ -89,10 +89,10 @@ TYPE("assembler-with-cpp", Asm, PP_Asm, "S", phases
8989
// `PP_TYPE` is set to `PP_Fortran` so that the driver is fine with
9090
// "pre-processing a pre-processed file".
9191
#ifdef ENABLE_CLASSIC_FLANG
92-
TYPE("f77", PP_F_FixedForm, INVALID, "f", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
93-
TYPE("f77-cpp-input", F_FixedForm, PP_F_FixedForm, "F", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
94-
TYPE("f95", PP_F_FreeForm, INVALID, "f95", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
95-
TYPE("f95-cpp-input", F_FreeForm, PP_F_FreeForm, "F95", phases::FortranFrontend, phases::Backend, phases::Assemble, phases::Link)
92+
TYPE("f77", PP_F_FixedForm, INVALID, "f", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
93+
TYPE("f77-cpp-input", F_FixedForm, PP_F_FixedForm, "F", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
94+
TYPE("f95", PP_F_FreeForm, INVALID, "f95", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
95+
TYPE("f95-cpp-input", F_FreeForm, PP_F_FreeForm, "F95", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
9696
#else
9797
TYPE("f95", PP_Fortran, PP_Fortran, "i", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
9898
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";
@@ -379,12 +378,6 @@ void MigrateJobAction::anchor() {}
379378
MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
380379
: JobAction(MigrateJobClass, Input, OutputType) {}
381380

382-
void FortranFrontendJobAction::anchor() {}
383-
384-
FortranFrontendJobAction::FortranFrontendJobAction(Action *Input,
385-
types::ID OutputType)
386-
: JobAction(FortranFrontendJobClass, Input, OutputType) {}
387-
388381
void CompileJobAction::anchor() {}
389382

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

clang/lib/Driver/Driver.cpp

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

397397
// -{E,EP,P,M,MM} only run the preprocessor.
398398
if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) ||
399-
#ifdef ENABLE_CLASSIC_FLANG
400-
(PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
401-
#endif
402399
(PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) ||
403400
(PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
404401
(PhaseArg = DAL.getLastArg(options::OPT__SLASH_P)) ||
405402
CCGenDiagnostics) {
406403
#ifdef ENABLE_CLASSIC_FLANG
407-
// -fsyntax-only or -E stops Fortran compilation after FortranFrontend
408-
if (IsFlangMode() && (DAL.getLastArg(options::OPT_E) ||
409-
DAL.getLastArg(options::OPT_fsyntax_only))) {
410-
FinalPhase = phases::FortranFrontend;
411-
412-
// if not Fortran, fsyntax_only implies 'Compile' is the FinalPhase
413-
} else if (DAL.getLastArg(options::OPT_fsyntax_only)) {
404+
if (IsFlangMode())
414405
FinalPhase = phases::Compile;
415-
416-
// everything else has 'Preprocess' as its FinalPhase
417-
} else {
406+
else
418407
FinalPhase = phases::Preprocess;
419-
}
420408
#else
421409
FinalPhase = phases::Preprocess;
422410
#endif
@@ -430,14 +418,9 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
430418
options::OPT_fmodule_header_EQ))) {
431419
FinalPhase = phases::Precompile;
432420

433-
#ifdef ENABLE_CLASSIC_FLANG
434-
// -{analyze,emit-ast} only run up to the compiler.
435-
} else if ((PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) ||
436-
#else
437421
// -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
438422
} else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
439423
(PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) ||
440-
#endif
441424
(PhaseArg = DAL.getLastArg(options::OPT_print_enabled_extensions)) ||
442425
(PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) ||
443426
(PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) ||
@@ -4288,10 +4271,13 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
42884271
if (InputArg->isClaimed())
42894272
continue;
42904273

4291-
// Fortran input is preprocessed using the frontend.
4292-
if (InitialPhase == phases::FortranFrontend &&
4274+
#ifdef ENABLE_CLASSIC_FLANG
4275+
// If the input is detected as already preprocessed (e.g. has the .f95
4276+
// extension), and the user specifies -E, preprocess the file anyway.
4277+
if (IsFlangMode() && InitialPhase == phases::Compile &&
42934278
FinalPhase == phases::Preprocess)
42944279
continue;
4280+
#endif
42954281

42964282
// Claim here to avoid the more general unused warning.
42974283
InputArg->claim();
@@ -5102,13 +5088,6 @@ Action *Driver::ConstructPhaseAction(
51025088

51035089
return C.MakeAction<PrecompileJobAction>(Input, OutputTy);
51045090
}
5105-
case phases::FortranFrontend: {
5106-
if (Args.hasArg(options::OPT_fsyntax_only))
5107-
return C.MakeAction<FortranFrontendJobAction>(Input,
5108-
types::TY_Nothing);
5109-
return C.MakeAction<FortranFrontendJobAction>(Input,
5110-
types::TY_LLVM_IR);
5111-
}
51125091
case phases::Compile: {
51135092
if (Args.hasArg(options::OPT_fsyntax_only))
51145093
return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing);
@@ -5131,6 +5110,10 @@ Action *Driver::ConstructPhaseAction(
51315110
return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing);
51325111
if (Args.hasArg(options::OPT_extract_api))
51335112
return C.MakeAction<ExtractAPIJobAction>(Input, types::TY_API_INFO);
5113+
#ifdef ENABLE_CLASSIC_FLANG
5114+
if (IsFlangMode())
5115+
return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_IR);
5116+
#endif
51345117
return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC);
51355118
}
51365119
case phases::Backend: {
@@ -5598,6 +5581,10 @@ class ToolSelector final {
55985581
if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
55995582
return nullptr;
56005583

5584+
// Classic Flang is not integrated with the backend.
5585+
if (C.getDriver().IsFlangMode() && !T->hasIntegratedAssembler())
5586+
return nullptr;
5587+
56015588
if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode))
56025589
return nullptr;
56035590

@@ -6897,7 +6884,11 @@ bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
68976884

68986885
// And say "no" if this is not a kind of action flang understands.
68996886
if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) &&
6900-
!isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA))
6887+
!isa<CompileJobAction>(JA)
6888+
#ifndef ENABLE_CLASSIC_FLANG
6889+
&& !isa<BackendJobAction>(JA)
6890+
#endif
6891+
)
69016892
return false;
69026893

69036894
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
@@ -556,13 +556,13 @@ Tool *ToolChain::getClang() const {
556556
}
557557

558558
Tool *ToolChain::getFlang() const {
559-
#ifndef ENABLE_CLASSIC_FLANG
560559
if (!Flang)
561-
Flang.reset(new tools::Flang(*this));
562-
return Flang.get();
560+
#ifdef ENABLE_CLASSIC_FLANG
561+
Flang.reset(new tools::ClassicFlang(*this));
563562
#else
564-
llvm_unreachable("Flang is not supported by this toolchain");
563+
Flang.reset(new tools::Flang(*this));
565564
#endif
565+
return Flang.get();
566566
}
567567

568568
Tool *ToolChain::buildAssembler() const {
@@ -583,16 +583,6 @@ Tool *ToolChain::getAssemble() const {
583583
return Assemble.get();
584584
}
585585

586-
Tool *ToolChain::getFortranFrontend() const {
587-
#ifdef ENABLE_CLASSIC_FLANG
588-
if (!FortranFrontend)
589-
FortranFrontend.reset(new tools::ClassicFlang(*this));
590-
return FortranFrontend.get();
591-
#else
592-
llvm_unreachable("Fortran is not supported by this toolchain");
593-
#endif
594-
}
595-
596586
Tool *ToolChain::getClangAs() const {
597587
if (!Assemble)
598588
Assemble.reset(new tools::ClangAs(*this));
@@ -676,9 +666,6 @@ Tool *ToolChain::getTool(Action::ActionClass AC) const {
676666
return getOffloadPackager();
677667
case Action::LinkerWrapperJobClass:
678668
return getLinkerWrapper();
679-
680-
case Action::FortranFrontendJobClass:
681-
return getFortranFrontend();
682669
}
683670

684671
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)