Skip to content

Commit 9c9e46d

Browse files
aganeazmodem
authored andcommitted
[Clang] Limit -fintegrated-cc1 to only one TU
As discussed in https://reviews.llvm.org/D74447, this patch disables integrated-cc1 behavior if there's more than one job to be executed. This is meant to limit memory bloating, given that currently jobs don't clean up after execution (-disable-free is always active in cc1 mode). I see this behavior as temporary until release 10.0 ships (to ease merging of this patch), then we'll reevaluate the situation, see if D74447 makes more sense on the long term. Differential Revision: https://reviews.llvm.org/D74490 (cherry picked from commit 20f1abe)
1 parent e1b7335 commit 9c9e46d

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

clang/include/clang/Driver/Job.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ class Command {
5555
/// The list of program arguments which are inputs.
5656
llvm::opt::ArgStringList InputFilenames;
5757

58-
/// Whether to print the input filenames when executing.
59-
bool PrintInputFilenames = false;
60-
6158
/// Response file name, if this command is set to use one, or nullptr
6259
/// otherwise
6360
const char *ResponseFile = nullptr;
@@ -86,6 +83,12 @@ class Command {
8683
void writeResponseFile(raw_ostream &OS) const;
8784

8885
public:
86+
/// Whether to print the input filenames when executing.
87+
bool PrintInputFilenames = false;
88+
89+
/// Whether the command will be executed in this process or not.
90+
bool InProcess = false;
91+
8992
Command(const Action &Source, const Tool &Creator, const char *Executable,
9093
const llvm::opt::ArgStringList &Arguments,
9194
ArrayRef<InputInfo> Inputs);
@@ -128,9 +131,6 @@ class Command {
128131
/// Print a command argument, and optionally quote it.
129132
static void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
130133

131-
/// Set whether to print the input filenames when executing.
132-
void setPrintInputFilenames(bool P) { PrintInputFilenames = P; }
133-
134134
protected:
135135
/// Optionally print the filenames to be compiled
136136
void PrintFileNames() const;
@@ -139,7 +139,9 @@ class Command {
139139
/// Use the CC1 tool callback when available, to avoid creating a new process
140140
class CC1Command : public Command {
141141
public:
142-
using Command::Command;
142+
CC1Command(const Action &Source, const Tool &Creator, const char *Executable,
143+
const llvm::opt::ArgStringList &Arguments,
144+
ArrayRef<InputInfo> Inputs);
143145

144146
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
145147
CrashReportInfo *CrashInfo = nullptr) const override;

clang/lib/Driver/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,6 +3757,11 @@ void Driver::BuildJobs(Compilation &C) const {
37573757
/*TargetDeviceOffloadKind*/ Action::OFK_None);
37583758
}
37593759

3760+
// If we have more than one job, then disable integrated-cc1 for now.
3761+
if (C.getJobs().size() > 1)
3762+
for (auto &J : C.getJobs())
3763+
J.InProcess = false;
3764+
37603765
// If the user passed -Qunused-arguments or there were errors, don't warn
37613766
// about any unused arguments.
37623767
if (Diags.hasErrorOccurred() ||

clang/lib/Driver/Job.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,29 @@ int Command::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
371371
/*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
372372
}
373373

374+
CC1Command::CC1Command(const Action &Source, const Tool &Creator,
375+
const char *Executable,
376+
const llvm::opt::ArgStringList &Arguments,
377+
ArrayRef<InputInfo> Inputs)
378+
: Command(Source, Creator, Executable, Arguments, Inputs) {
379+
InProcess = true;
380+
}
381+
374382
void CC1Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
375383
CrashReportInfo *CrashInfo) const {
376-
OS << " (in-process)\n";
384+
if (InProcess)
385+
OS << " (in-process)\n";
377386
Command::Print(OS, Terminator, Quote, CrashInfo);
378387
}
379388

380-
int CC1Command::Execute(ArrayRef<llvm::Optional<StringRef>> /*Redirects*/,
389+
int CC1Command::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
381390
std::string *ErrMsg, bool *ExecutionFailed) const {
391+
// FIXME: Currently, if there're more than one job, we disable
392+
// -fintegrate-cc1. If we're no longer a integrated-cc1 job, fallback to
393+
// out-of-process execution. See discussion in https://reviews.llvm.org/D74447
394+
if (!InProcess)
395+
return Command::Execute(Redirects, ErrMsg, ExecutionFailed);
396+
382397
PrintFileNames();
383398

384399
SmallVector<const char *, 128> Argv;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6053,7 +6053,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
60536053
if (Output.getType() == types::TY_Object &&
60546054
Args.hasFlag(options::OPT__SLASH_showFilenames,
60556055
options::OPT__SLASH_showFilenames_, false)) {
6056-
C.getJobs().getJobs().back()->setPrintInputFilenames(true);
6056+
C.getJobs().getJobs().back()->PrintInputFilenames = true;
60576057
}
60586058

60596059
if (Arg *A = Args.getLastArg(options::OPT_pg))

clang/test/Driver/cc1-spawnprocess.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1-
// RUN: %clang -fintegrated-cc1 -### %s 2>&1 | FileCheck %s --check-prefix=YES
2-
// RUN: %clang -fno-integrated-cc1 -### %s 2>&1 | FileCheck %s --check-prefix=NO
1+
// RUN: %clang -fintegrated-cc1 -c -### %s 2>&1 | FileCheck %s --check-prefix=YES
2+
// RUN: %clang -fno-integrated-cc1 -c -### %s 2>&1 | FileCheck %s --check-prefix=NO
33

4-
// RUN: %clang -fintegrated-cc1 -fno-integrated-cc1 -### %s 2>&1 \
4+
// RUN: %clang -fintegrated-cc1 -fno-integrated-cc1 -c -### %s 2>&1 \
55
// RUN: | FileCheck %s --check-prefix=NO
6-
// RUN: %clang -fno-integrated-cc1 -fintegrated-cc1 -### %s 2>&1 \
6+
// RUN: %clang -fno-integrated-cc1 -fintegrated-cc1 -c -### %s 2>&1 \
77
// RUN: | FileCheck %s --check-prefix=YES
88

9-
// RUN: %clang_cl -fintegrated-cc1 -### -- %s 2>&1 \
9+
// RUN: %clang_cl -fintegrated-cc1 -c -### -- %s 2>&1 \
1010
// RUN: | FileCheck %s --check-prefix=YES
11-
// RUN: %clang_cl -fno-integrated-cc1 -### -- %s 2>&1 \
11+
// RUN: %clang_cl -fno-integrated-cc1 -c -### -- %s 2>&1 \
1212
// RUN: | FileCheck %s --check-prefix=NO
1313

1414
// RUN: env CCC_OVERRIDE_OPTIONS=+-fintegrated-cc1 \
15-
// RUN: %clang -fintegrated-cc1 -### %s 2>&1 \
15+
// RUN: %clang -fintegrated-cc1 -c -### %s 2>&1 \
1616
// RUN: | FileCheck %s --check-prefix=YES
1717
// RUN: env CCC_OVERRIDE_OPTIONS=+-fno-integrated-cc1 \
18-
// RUN: %clang -fintegrated-cc1 -### %s 2>&1 \
18+
// RUN: %clang -fintegrated-cc1 -c -### %s 2>&1 \
1919
// RUN: | FileCheck %s --check-prefix=NO
2020

2121
// YES: (in-process)
2222
// NO-NOT: (in-process)
23+
24+
// The following tests ensure that only one integrated-cc1 is executed.
25+
26+
// Only one TU, one job, thus integrated-cc1 is enabled.
27+
// RUN: %clang -fintegrated-cc1 -c %s -### 2>&1 | FileCheck %s --check-prefix=YES
28+
29+
// Only one TU, but we're linking, two jobs, thus integrated-cc1 is disabled.
30+
// RUN: %clang -fintegrated-cc1 %s -### 2>&1 | FileCheck %s --check-prefix=NO
31+
32+
// RUN: echo 'int main() { return f() + g(); }' > %t1.cpp
33+
// RUN: echo 'int f() { return 1; }' > %t2.cpp
34+
// RUN: echo 'int g() { return 2; }' > %t3.cpp
35+
36+
// Three jobs, thus integrated-cc1 is disabled.
37+
// RUN: %clang -fintegrated-cc1 -c %t1.cpp %t2.cpp %t3.cpp -### 2>&1 | FileCheck %s --check-prefix=NO

0 commit comments

Comments
 (0)