Skip to content

Commit 08920e3

Browse files
Add an option to force compiling object file output
Add an option to skip module hash checking that checks if swift compiler can skip object file generation because it is going to produce the same result. Always generation object file output when flag is used so we can check for output determinism.
1 parent 4a4fda1 commit 08920e3

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ class IRGenOptions {
315315
/// Print the LLVM inline tree at the end of the LLVM pass pipeline.
316316
unsigned PrintInlineTree : 1;
317317

318+
/// Always recompile the output even the module hash might match.
319+
unsigned AlwaysCompile : 1;
320+
318321
/// Whether we should embed the bitcode file.
319322
IRGenEmbedMode EmbedMode : 2;
320323

@@ -472,7 +475,7 @@ class IRGenOptions {
472475
DisableLLVMOptzns(false), DisableSwiftSpecificLLVMOptzns(false),
473476
Playground(false),
474477
EmitStackPromotionChecks(false), UseSingleModuleLLVMEmission(false),
475-
FunctionSections(false), PrintInlineTree(false),
478+
FunctionSections(false), PrintInlineTree(false), AlwaysCompile(false),
476479
EmbedMode(IRGenEmbedMode::None), LLVMLTOKind(IRGenLLVMLTOKind::None),
477480
SwiftAsyncFramePointer(SwiftAsyncFramePointerKind::Auto),
478481
HasValueNamesSetting(false), ValueNames(false),

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,9 @@ def enable_emit_generic_class_ro_t_list :
11381138
def enable_swift_deterministic_check :
11391139
Flag<["-"], "enable-swift-deterministic-check">,
11401140
HelpText<"Check swift compiler output determinisim by run it twice">;
1141+
def always_compile_output_files :
1142+
Flag<["-"], "always-compile-output-files">,
1143+
HelpText<"Always compile output files even it might not change the results">;
11411144

11421145
def experimental_spi_only_imports :
11431146
Flag<["-"], "experimental-spi-only-imports">,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,7 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
21912191
Opts.UseProfile = ProfileUse ? ProfileUse->getValue() : "";
21922192

21932193
Opts.PrintInlineTree |= Args.hasArg(OPT_print_llvm_inline_tree);
2194+
Opts.AlwaysCompile |= Args.hasArg(OPT_always_compile_output_files);
21942195

21952196
Opts.EnableDynamicReplacementChaining |=
21962197
Args.hasArg(OPT_enable_dynamic_replacement_chaining);

lib/IRGen/IRGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ bool swift::performLLVM(const IRGenOptions &Opts,
743743
ArrayRef<uint8_t> HashData(reinterpret_cast<uint8_t *>(&hash),
744744
sizeof(hash));
745745
if (Opts.OutputKind == IRGenOutputKind::ObjectFile &&
746-
!Opts.PrintInlineTree &&
746+
!Opts.PrintInlineTree && !Opts.AlwaysCompile &&
747747
!needsRecompile(OutputFilename, HashData, HashGlobal, DiagMutex)) {
748748
// The llvm IR did not change. We don't need to re-create the object file.
749749
return false;

test/Frontend/output_determinism_check.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
// RUN: %target-swift-frontend -module-name test -emit-sib -o %t/test.sib -primary-file %s -enable-swift-deterministic-check 2>&1 | %FileCheck %s --check-prefix=SIB_OUTPUT
44

55
/// object files are "not" deterministic because the second run going to match the mod hash and skip code generation.
6-
// RUN: not %target-swift-frontend -module-name test -c -o %t/test.o -primary-file %s -enable-swift-deterministic-check 2>&1 | %FileCheck %s --check-prefix=OBJECT_OUTPUT
6+
// RUN: not %target-swift-frontend -module-name test -c -o %t/test.o -primary-file %s -enable-swift-deterministic-check 2>&1 | %FileCheck %s --check-prefix=OBJECT_MISMATCH
7+
/// object files should match when forcing object generation.
8+
// RUN: %target-swift-frontend -module-name test -c -o %t/test.o -primary-file %s -enable-swift-deterministic-check -always-compile-output-files 2>&1 | %FileCheck %s --check-prefix=OBJECT_OUTPUT
79

810
// MODULE_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.swiftmodule'
911
// SIB_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.sib'
10-
// OBJECT_OUTPUT: error: output file '{{.*}}{{/|\\}}test.o' is missing from second run
12+
// OBJECT_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.o'
13+
// OBJECT_MISMATCH: error: output file '{{.*}}{{/|\\}}test.o' is missing from second run
1114

1215
public var x = 1
16+
public func test() {}

0 commit comments

Comments
 (0)