Skip to content

Commit 4a9b676

Browse files
authored
Merge pull request #19306 from graydon/one-determined-heart
[Stats] Add SWIFTC_MAXIMUM_DETERMINISM to inhibit parallelism everywhere.
2 parents e55d425 + 5a563f5 commit 4a9b676

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
//
1818
//===----------------------------------------------------------------------===//
1919

20-
#if !(defined(DIAG) || (defined(ERROR) && defined(WARNING) && defined(NOTE)))
21-
# error Must define either DIAG or the set {ERROR,WARNING,NOTE}
20+
#if !(defined(DIAG) || (defined(ERROR) && defined(WARNING) && defined(NOTE) && defined(REMARK)))
21+
# error Must define either DIAG or the set {ERROR,WARNING,NOTE,REMARK}
2222
#endif
2323

2424
#ifndef ERROR
@@ -36,6 +36,11 @@
3636
DIAG(NOTE,ID,Options,Text,Signature)
3737
#endif
3838

39+
#ifndef REMARK
40+
# define REMARK(ID,Options,Text,Signature) \
41+
DIAG(REMARK,ID,Options,Text,Signature)
42+
#endif
43+
3944
ERROR(invalid_diagnostic,none,
4045
"INTERNAL ERROR: this diagnostic should not be produced", ())
4146

@@ -58,6 +63,9 @@ NOTE(brace_stmt_suggest_do,none,
5863
NOTE(while_parsing_as_left_angle_bracket,none,
5964
"while parsing this '<' as a type parameter bracket", ())
6065

66+
// Generic determinism-forcing override.
67+
REMARK(remark_max_determinism_overriding,none,
68+
"SWIFTC_MAXIMUM_DETERMINISM overriding %0", (StringRef))
6169

6270
// FIXME: This is used both as a parse error (a literal "super" outside a
6371
// method) and a type-checker error ("super" in a method of a non-class type).

include/swift/Basic/Statistic.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ class SourceManager;
6868
class Stmt;
6969
class TypeRepr;
7070

71+
// There are a handful of cases where the swift compiler can introduce
72+
// counter-measurement noise via nondeterminism, especially via
73+
// parallelism; inhibiting all such cases reliably using existing avenues
74+
// is a bit tricky and depends both on delicate build-setting management
75+
// and some build-system support that is still pending (see
76+
// rdar://39528362); in the meantime we support an environment variable
77+
// ourselves to request blanket suppression of parallelism (and anything
78+
// else nondeterministic we find).
79+
bool environmentVariableRequestedMaximumDeterminism();
80+
7181
class UnifiedStatsReporter {
7282

7383
public:

lib/Basic/Statistic.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ namespace swift {
5050
using namespace llvm;
5151
using namespace llvm::sys;
5252

53+
bool environmentVariableRequestedMaximumDeterminism() {
54+
if (const char *S = ::getenv("SWIFTC_MAXIMUM_DETERMINISM"))
55+
return (S[0] != '\0');
56+
return false;
57+
}
58+
5359
static int64_t
5460
getChildrenMaxResidentSetSize() {
5561
#if defined(HAVE_GETRUSAGE) && !defined(__HAIKU__)

lib/Driver/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ std::unique_ptr<sys::TaskQueue> Driver::buildTaskQueue(const Compilation &C) {
297297
return nullptr;
298298
}
299299
}
300+
if (environmentVariableRequestedMaximumDeterminism()) {
301+
NumberOfParallelCommands = 1;
302+
Diags.diagnose(SourceLoc(), diag::remark_max_determinism_overriding,
303+
"-j");
304+
}
300305

301306
const bool DriverSkipExecution =
302307
ArgList.hasArg(options::OPT_driver_skip_execution,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,13 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
614614
A->getAsString(Args), A->getValue());
615615
return true;
616616
}
617+
if (environmentVariableRequestedMaximumDeterminism()) {
618+
Opts.NumThreads = 1;
619+
Diags.diagnose(SourceLoc(), diag::remark_max_determinism_overriding,
620+
"-num-threads");
621+
}
617622
}
618-
623+
619624
if (Args.hasArg(OPT_sil_merge_partial_modules))
620625
Opts.MergePartialModules = true;
621626

test/Misc/stats_max_determinism.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: env SWIFTC_MAXIMUM_DETERMINISM=1 %target-swiftc_driver -j 4 -num-threads 10 -c -o %t/out.o %s >%t/out.txt 2>&1
3+
// RUN: %FileCheck -input-file %t/out.txt %s
4+
// CHECK: remark: SWIFTC_MAXIMUM_DETERMINISM overriding -j
5+
// CHECK: remark: SWIFTC_MAXIMUM_DETERMINISM overriding -num-threads
6+
print(1)

0 commit comments

Comments
 (0)