Skip to content

Commit fd8c3ea

Browse files
authored
Merge pull request #86042 from eeckstein/sil-unit-tests
SwiftCompilerSources: convert all unit-test passes to real tests
2 parents 13c919b + c822615 commit fd8c3ea

File tree

74 files changed

+3345
-2822
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+3345
-2822
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,3 +975,116 @@ private extension BridgedAliasAnalysis {
975975
UnsafeMutableRawPointer(aa)
976976
}
977977
}
978+
979+
//===--------------------------------------------------------------------===//
980+
// Tests
981+
//===--------------------------------------------------------------------===//
982+
983+
/// Prints the memory behavior of relevant instructions in relation to address values of the function.
984+
let aliasingTest = FunctionTest("aliasing") {
985+
function, arguments, context in
986+
987+
let aliasAnalysis = context.aliasAnalysis
988+
989+
print("@\(function.name)")
990+
991+
let values = function.allValues
992+
993+
var pair = 0
994+
for (index1, value1) in values.enumerated() {
995+
for (index2, value2) in values.enumerated() {
996+
if index2 >= index1 {
997+
let result = aliasAnalysis.mayAlias(value1, value2)
998+
precondition(result == aliasAnalysis.mayAlias(value2, value1), "alias analysis not symmetric")
999+
1000+
print("PAIR #\(pair).")
1001+
print(" \(value1)")
1002+
print(" \(value2)")
1003+
if result {
1004+
print(" MayAlias")
1005+
} else if !value1.uses.isEmpty && !value2.uses.isEmpty {
1006+
print(" NoAlias")
1007+
} else {
1008+
print(" noalias?")
1009+
}
1010+
1011+
pair += 1
1012+
}
1013+
}
1014+
}
1015+
}
1016+
1017+
/// Prints the memory behavior of relevant instructions in relation to address values of the function.
1018+
let memoryEffectsTest = FunctionTest("memory_effects") {
1019+
function, arguments, context in
1020+
1021+
let aliasAnalysis = context.aliasAnalysis
1022+
1023+
print("@\(function.name)")
1024+
1025+
let values = function.allValues
1026+
1027+
var currentPair = 0
1028+
for inst in function.instructions where inst.shouldTest {
1029+
1030+
for value in values where value.definingInstruction != inst {
1031+
1032+
if value.type.isAddress {
1033+
let read = inst.mayRead(fromAddress: value, aliasAnalysis)
1034+
let write = inst.mayWrite(toAddress: value, aliasAnalysis)
1035+
print("PAIR #\(currentPair).")
1036+
print(" \(inst)")
1037+
print(" \(value)")
1038+
print(" r=\(read ? 1 : 0),w=\(write ? 1 : 0)")
1039+
currentPair += 1
1040+
}
1041+
}
1042+
}
1043+
print()
1044+
}
1045+
1046+
private extension Instruction {
1047+
var shouldTest: Bool {
1048+
switch self {
1049+
case is ApplySite,
1050+
is EndApplyInst,
1051+
is AbortApplyInst,
1052+
is BeginAccessInst,
1053+
is EndAccessInst,
1054+
is EndCOWMutationInst,
1055+
is EndCOWMutationAddrInst,
1056+
is CopyValueInst,
1057+
is DestroyValueInst,
1058+
is StrongReleaseInst,
1059+
is IsUniqueInst,
1060+
is EndBorrowInst,
1061+
is LoadInst,
1062+
is LoadBorrowInst,
1063+
is StoreInst,
1064+
is CopyAddrInst,
1065+
is BuiltinInst,
1066+
is StoreBorrowInst,
1067+
is MarkDependenceInst,
1068+
is MarkDependenceAddrInst,
1069+
is DebugValueInst,
1070+
is DebugStepInst:
1071+
return true
1072+
default:
1073+
return false
1074+
}
1075+
}
1076+
}
1077+
1078+
private extension Function {
1079+
var allValues: [Value] {
1080+
var values: [Value] = []
1081+
for block in blocks {
1082+
values.append(contentsOf: block.arguments.map { $0 })
1083+
for inst in block.instructions {
1084+
values.append(contentsOf: inst.results)
1085+
}
1086+
}
1087+
return values
1088+
}
1089+
}
1090+

SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ add_subdirectory(InstructionSimplification)
1717
add_subdirectory(PassManager)
1818
add_subdirectory(ModulePasses)
1919
add_subdirectory(FunctionPasses)
20-
add_subdirectory(TestPasses)
2120
add_subdirectory(Utilities)

SwiftCompilerSources/Sources/Optimizer/DataStructures/DeadEndBlocks.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,19 @@ struct DeadEndBlocks : CustomStringConvertible, NoReflectionChildren {
5151
worklist.deinitialize()
5252
}
5353
}
54+
55+
//===--------------------------------------------------------------------===//
56+
// Tests
57+
//===--------------------------------------------------------------------===//
58+
59+
let deadEndBlockTest = FunctionTest("deadendblocks") {
60+
function, arguments, context in
61+
62+
print("Function \(function.name)")
63+
64+
var deadEndBlocks = DeadEndBlocks(function: function, context)
65+
print(deadEndBlocks)
66+
defer { deadEndBlocks.deinitialize() }
67+
68+
print("end function \(function.name)")
69+
}

SwiftCompilerSources/Sources/Optimizer/DataStructures/FunctionUses.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,22 @@ struct FunctionUses {
162162
uses[function, default: FirstUse(of: function)].hasUnknownUses = true
163163
}
164164
}
165+
166+
//===--------------------------------------------------------------------===//
167+
// Tests
168+
//===--------------------------------------------------------------------===//
169+
170+
let functionUsesTest = ModuleTest("function-uses") {
171+
(context: ModulePassContext) in
172+
173+
var functionUses = FunctionUses()
174+
functionUses.collect(context: context)
175+
176+
for function in context.functions {
177+
let uses = functionUses.getUses(of: function)
178+
179+
print("Uses of \(function.name)")
180+
print(uses)
181+
print("End function \(function.name)\n")
182+
}
183+
}

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public func initializeSwiftModules() {
2323
registerOptimizerTests()
2424
}
2525

26-
private func registerPass(
26+
func registerPass(
2727
_ pass: ModulePass,
2828
_ runFn: @escaping (@convention(c) (BridgedContext) -> ())) {
2929
pass.name._withBridgedStringRef { nameStr in
@@ -146,19 +146,6 @@ private func registerSwiftPasses() {
146146
registerForSILCombine(ApplyInst.self, { run(ApplyInst.self, $0) })
147147
registerForSILCombine(TryApplyInst.self, { run(TryApplyInst.self, $0) })
148148
registerForSILCombine(EndCOWMutationAddrInst.self, { run(EndCOWMutationAddrInst.self, $0) })
149-
150-
// Test passes
151-
registerPass(aliasInfoDumper, { aliasInfoDumper.run($0) })
152-
registerPass(functionUsesDumper, { functionUsesDumper.run($0) })
153-
registerPass(silPrinterPass, { silPrinterPass.run($0) })
154-
registerPass(escapeInfoDumper, { escapeInfoDumper.run($0) })
155-
registerPass(addressEscapeInfoDumper, { addressEscapeInfoDumper.run($0) })
156-
registerPass(accessDumper, { accessDumper.run($0) })
157-
registerPass(deadEndBlockDumper, { deadEndBlockDumper.run($0) })
158-
registerPass(memBehaviorDumper, { memBehaviorDumper.run($0) })
159-
registerPass(rangeDumper, { rangeDumper.run($0) })
160-
registerPass(testInstructionIteration, { testInstructionIteration.run($0) })
161-
registerPass(updateBorrowedFromPass, { updateBorrowedFromPass.run($0) })
162149
}
163150

164151
private func registerSwiftAnalyses() {

SwiftCompilerSources/Sources/Optimizer/TestPasses/AccessDumper.swift

Lines changed: 0 additions & 126 deletions
This file was deleted.

SwiftCompilerSources/Sources/Optimizer/TestPasses/AliasInfoDumper.swift

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)