Skip to content

Commit e5cdb6c

Browse files
committed
[flang][driver] Add support for -c and -emit-obj
This patch adds a frontend action for emitting object files. While Flang does not support code-generation, this action remains a placeholder. This patch simply provides glue-code to connect the compiler driver with the appropriate frontend action. The new action is triggered with the `-c` compiler driver flag, i.e. `flang-new -c`. This is then translated to `flang-new -fc1 -emit-obj`, so `-emit-obj` has to be marked as supported as well. As code-generation is not available yet, `flang-new -c` results in a driver error: ``` error: code-generation is not available yet ``` Hopefully this will help communicating the level of available functionality within Flang. The definition of `emit-obj` is updated so that it can be shared between Clang and Flang. As the original definition was enclosed within a Clang-specific TableGen `let` statement, it is extracted into a new `let` statement. That felt like the cleanest option. I also commented out `-triple` in Flang::ConstructJob and updated some comments there. This is similar to https://reviews.llvm.org/D93027. I wanted to make sure that it's clear that we can't support `-triple` until we have code-generation. However, once code-generation is available we _will need_ `-triple`. As this patch adds `-emit-obj`, the emit-obj.f90 becomes irrelevant and is deleted. Instead, phases.f90 is added to demonstrate that users can control compilation phases (indeed, `-c` is a phase control flag). Reviewed By: SouraVX, clementval Differential Revision: https://reviews.llvm.org/D93301
1 parent a828fb4 commit e5cdb6c

File tree

12 files changed

+81
-26
lines changed

12 files changed

+81
-26
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ def current__version : JoinedOrSeparate<["-"], "current_version">;
857857
def cxx_isystem : JoinedOrSeparate<["-"], "cxx-isystem">, Group<clang_i_Group>,
858858
HelpText<"Add directory to the C++ SYSTEM include search path">, Flags<[CC1Option]>,
859859
MetaVarName<"<directory>">;
860-
def c : Flag<["-"], "c">, Flags<[NoXarchOption]>, Group<Action_Group>,
860+
def c : Flag<["-"], "c">, Flags<[NoXarchOption, FlangOption]>, Group<Action_Group>,
861861
HelpText<"Only run preprocess, compile, and assemble steps">;
862862
def fconvergent_functions : Flag<["-"], "fconvergent-functions">, Group<f_Group>, Flags<[CC1Option]>,
863863
HelpText<"Assume functions may be convergent">;
@@ -4854,8 +4854,6 @@ def emit_llvm_only : Flag<["-"], "emit-llvm-only">,
48544854
HelpText<"Build ASTs and convert to LLVM, discarding output">;
48554855
def emit_codegen_only : Flag<["-"], "emit-codegen-only">,
48564856
HelpText<"Generate machine code, but discard output">;
4857-
def emit_obj : Flag<["-"], "emit-obj">,
4858-
HelpText<"Emit native object files">;
48594857
def rewrite_test : Flag<["-"], "rewrite-test">,
48604858
HelpText<"Rewriter playground">;
48614859
def rewrite_macros : Flag<["-"], "rewrite-macros">,
@@ -5229,6 +5227,18 @@ def fsycl_is_device : Flag<["-"], "fsycl-is-device">,
52295227

52305228
} // let Flags = [CC1Option, NoDriverOption]
52315229

5230+
//===----------------------------------------------------------------------===//
5231+
// Frontend Options - cc1 + fc1
5232+
//===----------------------------------------------------------------------===//
5233+
let Flags = [CC1Option, FC1Option, NoDriverOption] in {
5234+
let Group = Action_Group in {
5235+
5236+
def emit_obj : Flag<["-"], "emit-obj">,
5237+
HelpText<"Emit native object files">;
5238+
5239+
} // let Group = Action_Group
5240+
} // let Flags = [CC1Option, FC1Option, NoDriverOption]
5241+
52325242
//===----------------------------------------------------------------------===//
52335243
// cc1as-only Options
52345244
//===----------------------------------------------------------------------===//

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
2828
const InputInfo &Output, const InputInfoList &Inputs,
2929
const ArgList &Args, const char *LinkingOutput) const {
3030
const auto &TC = getToolChain();
31-
const llvm::Triple &Triple = TC.getEffectiveTriple();
32-
const std::string &TripleStr = Triple.getTriple();
31+
// TODO: Once code-generation is available, this will need to be commented
32+
// out.
33+
// const llvm::Triple &Triple = TC.getEffectiveTriple();
34+
// const std::string &TripleStr = Triple.getTriple();
3335

3436
ArgStringList CmdArgs;
3537

38+
// Invoke ourselves in -fc1 mode.
3639
CmdArgs.push_back("-fc1");
3740

38-
// TODO: Eventually all actions will require a triple (e.g. `-triple
39-
// aarch64-unknown-linux-gnu`). However, `-triple` is currently not supported
40-
// by `flang-new -fc1`, so we only add it selectively to actions that we
41-
// don't support/execute just yet.
41+
// TODO: Once code-generation is available, this will need to be commented
42+
// out.
43+
// Add the "effective" target triple.
44+
// CmdArgs.push_back("-triple");
45+
// CmdArgs.push_back(Args.MakeArgString(TripleStr));
46+
4247
if (isa<PreprocessJobAction>(JA)) {
4348
if (C.getArgs().hasArg(options::OPT_test_io))
4449
CmdArgs.push_back("-test-io");
@@ -61,8 +66,6 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
6166
assert(false && "Unexpected output type!");
6267
}
6368
} else if (isa<AssembleJobAction>(JA)) {
64-
CmdArgs.push_back("-triple");
65-
CmdArgs.push_back(Args.MakeArgString(TripleStr));
6669
CmdArgs.push_back("-emit-obj");
6770
} else {
6871
assert(false && "Unexpected action class for Flang tool.");

flang/include/flang/Frontend/FrontendActions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class ParseSyntaxOnlyAction : public FrontendAction {
2929
void ExecuteAction() override;
3030
};
3131

32+
class EmitObjAction : public FrontendAction {
33+
void ExecuteAction() override;
34+
};
35+
3236
} // namespace Fortran::frontend
3337

3438
#endif // LLVM_FLANG_FRONTEND_FRONTENDACTIONS_H

flang/include/flang/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ enum ActionKind {
2828
/// -fsyntax-only
2929
ParseSyntaxOnly,
3030

31+
/// Emit a .o file.
32+
EmitObj,
33+
3134
/// TODO: RunPreprocessor, EmitLLVM, EmitLLVMOnly,
3235
/// EmitCodeGenOnly, EmitAssembly, (...)
3336
};

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ static InputKind ParseFrontendArgs(FrontendOptions &opts,
9999
case clang::driver::options::OPT_fsyntax_only:
100100
opts.programAction_ = ParseSyntaxOnly;
101101
break;
102+
case clang::driver::options::OPT_emit_obj:
103+
opts.programAction_ = EmitObj;
104+
break;
102105

103106
// TODO:
104-
// case clang::driver::options::OPT_emit_obj:
105107
// case calng::driver::options::OPT_emit_llvm:
106108
// case clang::driver::options::OPT_emit_llvm_only:
107109
// case clang::driver::options::OPT_emit_codegen_only:

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ void ParseSyntaxOnlyAction::ExecuteAction() {
111111
ci.diagnostics().Report(DiagID) << GetCurrentFileOrBufferName();
112112
}
113113
}
114+
115+
void EmitObjAction::ExecuteAction() {
116+
CompilerInstance &ci = this->instance();
117+
unsigned DiagID = ci.diagnostics().getCustomDiagID(
118+
clang::DiagnosticsEngine::Error, "code-generation is not available yet");
119+
ci.diagnostics().Report(DiagID);
120+
}

flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static std::unique_ptr<FrontendAction> CreateFrontendBaseAction(
3434
break;
3535
case ParseSyntaxOnly:
3636
return std::make_unique<ParseSyntaxOnlyAction>();
37+
case EmitObj:
38+
return std::make_unique<EmitObjAction>();
3739
break;
3840
default:
3941
break;

flang/test/Flang-Driver/code-gen.f90

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
! RUN: not %flang-new %s 2>&1 | FileCheck %s --check-prefix=ERROR
2+
! RUN: not %flang-new -c %s 2>&1 | FileCheck %s --check-prefix=ERROR
3+
! RUN: not %flang-new -emit-obj %s 2>&1 | FileCheck %s --check-prefix=ERROR
4+
! RUN: not %flang-new -fc1 -emit-obj %s 2>&1 | FileCheck %s --check-prefix=ERROR
5+
6+
! REQUIRES: new-flang-driver
7+
8+
! Although code-generation is not yet available, we do have frontend actions
9+
! that correspond to `-c` and `-emit-obj`. For now these actions are just a
10+
! placeholder and running them leads to a driver error. This test makes sure
11+
! that these actions are indeed run (rather than `-c` or `-emit-obj` being
12+
! rejected earlier).
13+
! TODO: Replace this file with a proper test once code-generation is available.
14+
15+
! ERROR: code-generation is not available yet

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
! CHECK-EMPTY:
2020
! CHECK-NEXT:OPTIONS:
2121
! CHECK-NEXT: -### Print (but do not run) the commands to run for this compilation
22+
! CHECK-NEXT: -c Only run preprocess, compile, and assemble steps
2223
! CHECK-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
2324
! CHECK-NEXT: -E Only run the preprocessor
2425
! CHECK-NEXT: -fcolor-diagnostics Enable colors in diagnostics

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
! HELP-EMPTY:
2020
! HELP-NEXT:OPTIONS:
2121
! HELP-NEXT: -### Print (but do not run) the commands to run for this compilation
22+
! HELP-NEXT: -c Only run preprocess, compile, and assemble steps
2223
! HELP-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
2324
! HELP-NEXT: -E Only run the preprocessor
2425
! HELP-NEXT: -fcolor-diagnostics Enable colors in diagnostics
@@ -35,6 +36,7 @@
3536
! HELP-FC1-EMPTY:
3637
! HELP-FC1-NEXT:OPTIONS:
3738
! HELP-FC1-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
39+
! HELP-FC1-NEXT: -emit-obj Emit native object files
3840
! HELP-FC1-NEXT: -E Only run the preprocessor
3941
! HELP-FC1-NEXT: -help Display available options
4042
! HELP-FC1-NEXT: -o <file> Write output to <file>

flang/test/Flang-Driver/emit-obj.f90

Lines changed: 0 additions & 14 deletions
This file was deleted.

flang/test/Flang-Driver/phases.f90

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! RUN: %flang-new -E -ccc-print-phases %s 2>&1 | FileCheck %s --check-prefix=PP
2+
! RUN: %flang-new -fsyntax-only -ccc-print-phases %s 2>&1 | FileCheck %s --check-prefix=COMPILE
3+
! RUN: %flang-new -c -ccc-print-phases %s 2>&1 | FileCheck %s --check-prefix=EMIT_OBJ
4+
5+
! REQUIRES: new-flang-driver
6+
7+
! This test verifies the phase control in Flang compiler driver.
8+
9+
! PP: +- 0: input, "{{.*}}phases.f90", f95-cpp-input
10+
! PP-NEXT: 1: preprocessor, {0}, f95
11+
12+
! COMPILE: +- 0: input, "{{.*}}phases.f90", f95-cpp-input
13+
! COMPILE-NEXT: 1: preprocessor, {0}, f95
14+
! COMPILE-NEXT: 2: compiler, {1}, none
15+
16+
! EMIT_OBJ: +- 0: input, "{{.*}}phases.f90", f95-cpp-input
17+
! EMIT_OBJ-NEXT: 1: preprocessor, {0}, f95
18+
! EMIT_OBJ-NEXT: 2: compiler, {1}, ir
19+
! EMIT_OBJ-NEXT: +- 3: backend, {2}, assembler
20+
! EMIT_OBJ-NEXT: 4: assembler, {3}, object

0 commit comments

Comments
 (0)