Skip to content

Commit cd9c1e1

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 e62dfbe commit cd9c1e1

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
@@ -312,6 +312,9 @@ class IRGenOptions {
312312
/// Print the LLVM inline tree at the end of the LLVM pass pipeline.
313313
unsigned PrintInlineTree : 1;
314314

315+
/// Always recompile the output even the module hash might match.
316+
unsigned AlwaysCompile : 1;
317+
315318
/// Whether we should embed the bitcode file.
316319
IRGenEmbedMode EmbedMode : 2;
317320

@@ -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
@@ -1152,6 +1152,9 @@ def enable_emit_generic_class_ro_t_list :
11521152
def enable_swift_deterministic_check :
11531153
Flag<["-"], "enable-swift-deterministic-check">,
11541154
HelpText<"Check swift compiler output determinisim by run it twice">;
1155+
def always_compile_output_files :
1156+
Flag<["-"], "always-compile-output-files">,
1157+
HelpText<"Always compile output files even it might not change the results">;
11551158

11561159
def experimental_spi_only_imports :
11571160
Flag<["-"], "experimental-spi-only-imports">,

lib/Frontend/CompilerInvocation.cpp

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

22002200
Opts.PrintInlineTree |= Args.hasArg(OPT_print_llvm_inline_tree);
2201+
Opts.AlwaysCompile |= Args.hasArg(OPT_always_compile_output_files);
22012202

22022203
Opts.EnableDynamicReplacementChaining |=
22032204
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
@@ -556,7 +556,7 @@ bool swift::performLLVM(const IRGenOptions &Opts,
556556
ArrayRef<uint8_t> HashData(reinterpret_cast<uint8_t *>(&hash),
557557
sizeof(hash));
558558
if (Opts.OutputKind == IRGenOutputKind::ObjectFile &&
559-
!Opts.PrintInlineTree &&
559+
!Opts.PrintInlineTree && !Opts.AlwaysCompile &&
560560
!needsRecompile(OutputFilename, HashData, HashGlobal, DiagMutex)) {
561561
// The llvm IR did not change. We don't need to re-create the object file.
562562
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 compilation for deterministic check
1114

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

0 commit comments

Comments
 (0)