File: Test.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (273 lines) | stat: -rw-r--r-- 10,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//===----------- Test.swift - In-IR tests from Swift source ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// TO ADD A NEW TEST, just add a new FunctionTest instance.
// - In the source file containing the functionality you want to test:
//       let myNewTest = 
//       FunctionTest("my_new_test") { function, arguments, context in
//       }
// - In SwiftCompilerSources/Sources/SIL/Test.swift's registerOptimizerTests
//   function, add a new argument to the variadic function:
//       registerFunctionTests(..., myNewTest)
//
//===----------------------------------------------------------------------===//
//
// TO TROUBLESHOOT A NEW TEST, consider the following scenarios:
// - PROBLEM: The test isn't running on PLATFORM and the failure says
//
//                Found no test named my_new_test.
//
//   SOLUTION: Is this a platform one that doesn't use SwiftCompilerSources
//             (e.g. Windows)?  Then add
//
//                 // REQUIRES: swift_in_compiler
//
//             to the test file.
//   EXPLANATION: The tests written within SwiftCompilerSources only get built
//                and registered on platforms where the SwiftCompilerSources are
//                built and used.
//
//===----------------------------------------------------------------------===//
//
// Provides a mechanism for writing tests against compiler code in the context
// of a function.  The goal is to get the same effect as calling a function and
// checking its output.
//
// This is done via the specify_test instruction.  Using one or more instances
// of it in your test case's SIL function, you can specify which test (instance
// of FunctionTest) should be run and what arguments should be provided to it.
// For full details of the specify_test instruction's grammar, see SIL.rst.
//
// The test grabs the arguments it expects out of the TestArguments instance
// it is provided.  It calls some function or functions.  It then prints out
// interesting results.  These results can then be FileCheck'd.
//
// CASE STUDY:
// Here's an example of how it works:
// 0) A source file, NeatUtils.swift contains
//
//    fileprivate func myNeatoUtility(int: Int, value: Value, function: Function) { ... }
//
//    and
//
//    let myNeatoUtilityTest = 
//    FunctionTest("my_neato_utility") { function, arguments, test in
//         // The code here is described in detail below.
//         // See 4).
//         let count = arguments.takeInt()
//         let target = arguments.takeValue()
//         let callee = arguments.takeFunction()
//         // See 5)
//         myNeatoUtility(int: count, value: target, function: callee)
//         // See 6)
//         print(function)
//      }
// 1) A test test/SILOptimizer/interesting_functionality_unit.sil runs the
//    TestRunner pass:
//     // RUN: %target-sil-opt -test-runner %s -o /dev/null 2>&1 | %FileCheck %s
//     // REQUIRES: swift_in_compiler
// 2) A function in interesting_functionality_unit.sil contains the
//    specify_test instruction.
//      sil @f : $() -> () {
//      ...
//      specify_test "my_neato_utility 43 %2 @function[other_fun]"
//      ...
//      }
// 3) TestRunner finds the FunctionTest instance myNeatoUtilityTest registered
//    under the name "my_neato_utility", and calls run() on it, passing the
//    passing first the function, last the FunctionTest instance, AND in the
//    middle, most importantly a TestArguments instance that contains
//
//      (43 : Int, someValue : Value, other_fun : Function)
//
// 4) myNeatoUtilityTest calls takeUInt(), takeValue(), and takeFunction() on
//    the test::Arguments instance.
//      let count = arguments.takeInt()
//      let target = arguments.takeValue()
//      let callee = arguments.takeFunction()
// 5) myNeatoUtilityTest calls myNeatoUtility, passing these values along.
//      myNeatoUtility(int: count, value: target, function: callee)
// 6) myNeatoUtilityTest then dumps out the current function, which was modified
//    in the process.
//      print(function)
// 7) The test file test/SILOptimizer/interesting_functionality_unit.sil matches
// the
//    expected contents of the modified function:
//    // CHECK-LABEL: sil @f
//    // CHECK-NOT:     function_ref @other_fun
//
//===----------------------------------------------------------------------===//

import Basic
import SIL
import SILBridging
import OptimizerBridging

/// The primary interface to in-IR tests.
struct FunctionTest {
  let name: String
  let invocation: FunctionTestInvocation

  public init(_ name: String, invocation: @escaping FunctionTestInvocation) {
    self.name = name
    self.invocation = invocation
  }
}

/// The type of the closure passed to a FunctionTest.
typealias FunctionTestInvocation = @convention(thin) (Function, TestArguments, FunctionPassContext) -> ()

/// Wraps the arguments specified in the specify_test instruction.
public struct TestArguments {
  public var bridged: BridgedTestArguments
  fileprivate init(bridged: BridgedTestArguments) {
    self.bridged = bridged
  }

  public var hasUntaken: Bool { bridged.hasUntaken() }
  public func takeString() -> StringRef { StringRef(bridged: bridged.takeString()) }
  public func takeBool() -> Bool { bridged.takeBool() }
  public func takeInt() -> Int { bridged.takeInt() }
  public func takeOperand() -> Operand { Operand(bridged: bridged.takeOperand()) }
  public func takeValue() -> Value { bridged.takeValue().value }
  public func takeInstruction() -> Instruction { bridged.takeInstruction().instruction }
  public func takeArgument() -> Argument { bridged.takeArgument().argument }
  public func takeBlock() -> BasicBlock { bridged.takeBlock().block }
  public func takeFunction() -> Function { bridged.takeFunction().function }
}

extension BridgedTestArguments {
  public var native: TestArguments { TestArguments(bridged: self) }
}

/// Registration of each test in the SIL module.
public func registerOptimizerTests() {
  // Register each test.
  registerFunctionTests(
    argumentConventionsTest,
    borrowIntroducersTest,
    enclosingValuesTest,
    forwardingDefUseTest,
    forwardingUseDefTest,
    interiorLivenessTest,
    lifetimeDependenceRootTest,
    lifetimeDependenceScopeTest,
    lifetimeDependenceUseTest,
    linearLivenessTest,
    parseTestSpecificationTest,
    variableIntroducerTest
  )

  // Finally register the thunk they all call through.
  registerFunctionTestThunk(functionTestThunk)
}

private func registerFunctionTests(_ tests: FunctionTest...) {
  tests.forEach { registerFunctionTest($0) }
}

private func registerFunctionTest(_ test: FunctionTest) {
  test.name._withBridgedStringRef { ref in
    registerFunctionTest(ref, castToOpaquePointer(fromInvocation: test.invocation))
  }
}

/// The function called by the swift::test::FunctionTest which invokes the
/// actual test function.
///
/// This function is necessary because tests need to be written in terms of
/// native Swift types (Function, TestArguments, BridgedPassContext)
/// rather than their bridged variants, but such a function isn't representable
/// in C++.  This thunk unwraps the bridged types and invokes the real
/// function.
private func functionTestThunk(
  _ erasedInvocation: UnsafeMutableRawPointer,
  _ function: BridgedFunction, 
  _ arguments: BridgedTestArguments, 
  _ passInvocation: BridgedSwiftPassInvocation) {
  let invocation = castToInvocation(fromOpaquePointer: erasedInvocation)
  let context = FunctionPassContext(_bridged: BridgedPassContext(invocation: passInvocation.invocation))
  invocation(function.function, arguments.native, context)
  fflush(stdout)
}

/// Bitcast a thin test closure to void *.
///
/// Needed so that the closure can be represented in C++ for storage in the test
/// registry.
private func castToOpaquePointer(fromInvocation invocation: FunctionTestInvocation) -> UnsafeMutableRawPointer {
  return unsafeBitCast(invocation, to: UnsafeMutableRawPointer.self)
}

/// Bitcast a void * to a thin test closure.
///
/// Needed so that the closure stored in the C++ test registry can be invoked
/// via the functionTestThunk.
private func castToInvocation(fromOpaquePointer erasedInvocation: UnsafeMutableRawPointer) -> FunctionTestInvocation {
  return unsafeBitCast(erasedInvocation, to: FunctionTestInvocation.self)
}

// Arguments:
// - string: list of characters, each of which specifies subsequent arguments
//           - A: (block) argument
//           - F: function
//           - B: block
//           - I: instruction
//           - V: value
//           - O: operand
//           - b: boolean
//           - u: unsigned
//           - s: string
// - ...
// - an argument of the type specified in the initial string
// - ...
// Dumps:
// - for each argument (after the initial string)
//   - its type
//   - something to identify the instance (mostly this means calling dump)
let parseTestSpecificationTest = 
FunctionTest("test_specification_parsing") { function, arguments, context in 
  let expectedFields = arguments.takeString()
  for expectedField in expectedFields.string {
    switch expectedField {
    case "A":
      let argument = arguments.takeArgument()
      print("argument:\n\(argument)")
    case "F":
      let function = arguments.takeFunction()
      print("function: \(function.name)")
    case "B":
      let block = arguments.takeBlock()
      print("block:\n\(block)")
    case "I":
      let instruction = arguments.takeInstruction()
      print("instruction: \(instruction)")
    case "V":
      let value = arguments.takeValue()
      print("value: \(value)")
    case "O":
      let operand = arguments.takeOperand()
      print("operand: \(operand)")
    case "u":
      let u = arguments.takeInt()
      print("uint: \(u)")
    case "b":
      let b = arguments.takeBool()
      print("bool: \(b)")
    case "s":
      let s = arguments.takeString()
      print("string: \(s)")
    default:
      fatalError("unknown field type was expected?!");
    }
  }
}