Skip to content

Commit 887b5ca

Browse files
committed
add a Swift pass which can run unit tests.
The `run-unit-tests` is a "pseudo" pass which is invoked from sil-opt and runs all the unit tests, implemented in Swift. This is done from the `swift-unit-tests.sil` lit test.
1 parent 93f5d9f commit 887b5ca

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ swift_compiler_sources(Optimizer
1111
SILPrinter.swift
1212
MergeCondFails.swift
1313
ReleaseDevirtualizer.swift
14+
RunUnitTests.swift
1415
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===--- UnitTests.swift - A pseudo pass for running the unit tests -------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
/// This pass should only be used by sil-opt to run all the unit tests.
16+
///
17+
let runUnitTests = FunctionPass(name: "run-unit-tests", {
18+
(function: Function, context: PassContext) in
19+
20+
print("--- Run unit tests ---")
21+
})

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ private func registerSwiftPasses() {
5151
registerPass(simplifyStrongRetainPass, { simplifyStrongRetainPass.run($0) })
5252
registerPass(simplifyStrongReleasePass, { simplifyStrongReleasePass.run($0) })
5353
registerPass(assumeSingleThreadedPass, { assumeSingleThreadedPass.run($0) })
54+
registerPass(runUnitTests, { runUnitTests.run($0) })
5455
registerPass(releaseDevirtualizerPass, { releaseDevirtualizerPass.run($0) })
5556
}

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ PASS(SILDebugInfoGenerator, "sil-debuginfo-gen",
368368
"Generate Debug Information with Source Locations into Textual SIL")
369369
PASS(EarlySROA, "early-sroa",
370370
"Scalar Replacement of Aggregate Stack Objects on high-level SIL")
371+
SWIFT_FUNCTION_PASS(RunUnitTests, "run-unit-tests",
372+
"Runs the compiler internal unit tests")
371373
SWIFT_FUNCTION_PASS(SILPrinter, "sil-printer",
372374
"Test pass which prints the SIL of a function")
373375
PASS(SROA, "sroa",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-sil-opt %s -run-unit-tests -o /dev/null
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
// The RunUnitTests is a function pass. Therefore we need one function is this file to trigger the pass run.
6+
sil @trigger_test_run : $@convention(thin) () -> () {
7+
bb0:
8+
%r = tuple()
9+
return %r : $()
10+
}
11+

0 commit comments

Comments
 (0)