Skip to content

Commit dc38a22

Browse files
committed
Optimizer: convert the AliasInfoDumper and MemBehaviorDumper passes to tests
1 parent edacd4c commit dc38a22

File tree

11 files changed

+261
-160
lines changed

11 files changed

+261
-160
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/PassManager/PassRegistration.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,9 @@ private func registerSwiftPasses() {
148148
registerForSILCombine(EndCOWMutationAddrInst.self, { run(EndCOWMutationAddrInst.self, $0) })
149149

150150
// Test passes
151-
registerPass(aliasInfoDumper, { aliasInfoDumper.run($0) })
152151
registerPass(functionUsesDumper, { functionUsesDumper.run($0) })
153152
registerPass(silPrinterPass, { silPrinterPass.run($0) })
154153
registerPass(deadEndBlockDumper, { deadEndBlockDumper.run($0) })
155-
registerPass(memBehaviorDumper, { memBehaviorDumper.run($0) })
156154
registerPass(rangeDumper, { rangeDumper.run($0) })
157155
registerPass(testInstructionIteration, { testInstructionIteration.run($0) })
158156
registerPass(updateBorrowedFromPass, { updateBorrowedFromPass.run($0) })

SwiftCompilerSources/Sources/Optimizer/TestPasses/AliasInfoDumper.swift

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

SwiftCompilerSources/Sources/Optimizer/TestPasses/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
swift_compiler_sources(Optimizer
1010
FunctionUsesDumper.swift
11-
AliasInfoDumper.swift
1211
DeadEndBlockDumper.swift
13-
MemBehaviorDumper.swift
1412
SILPrinter.swift
1513
RangeDumper.swift
1614
UpdateBorrowedFrom.swift

SwiftCompilerSources/Sources/Optimizer/TestPasses/MemBehaviorDumper.swift

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

SwiftCompilerSources/Sources/Optimizer/Utilities/FunctionTest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public func registerOptimizerTests() {
5454
variableIntroducerTest,
5555
destroyBarrierTest,
5656
escapeInfoTest,
57-
addressEscapeInfoTest
57+
addressEscapeInfoTest,
58+
aliasingTest,
59+
memoryEffectsTest
5860
)
5961

6062
// Finally register the thunk they all call through.

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ PASS(AllocBoxToStack, "allocbox-to-stack",
6363
"stack promotion of box objects")
6464
PASS(CopyToBorrowOptimization, "copy-to-borrow-optimization",
6565
"Convert load [copy] instructions to load_borrow and remove copies of borrowed values")
66-
PASS(AliasInfoDumper, "dump-alias-info",
67-
"Dump Alias Analysis over all Pairs")
6866
PASS(AssumeSingleThreaded, "sil-assume-single-threaded",
6967
"Assume Single-Threaded Environment")
7068
PASS(BooleanLiteralFolding, "boolean-literal-folding",
@@ -110,8 +108,6 @@ PASS(LifetimeDependenceInsertion,
110108
PASS(LifetimeDependenceScopeFixup,
111109
"lifetime-dependence-scope-fixup",
112110
"Fixup scope for lifetime dependence")
113-
PASS(MemBehaviorDumper, "dump-mem-behavior",
114-
"Print SIL Instruction MemBehavior from Alias Analysis over all Pairs")
115111
PASS(MergeCondFails, "merge-cond_fails",
116112
"Merge SIL cond_fail to Eliminate Redundant Overflow Checks")
117113
PASS(ObjCBridgingOptimization, "objc-bridging-opt",
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt %s -dump-alias-info -o /dev/null | %FileCheck %s
1+
// RUN: %target-sil-opt %s -test-runner -o /dev/null | %FileCheck %s
22

33
// General black-box alias analysis tests. White-box unit tests are
44
// specific to the aa-kind, such as basic-aa.sil and typed-access-tb-aa.sil.
@@ -28,6 +28,7 @@ struct OuterStruct {
2828
// CHECK-NEXT: NoAlias
2929
sil shared @testOffsetBehindProjection : $@convention(thin) (@inout MyStruct) -> () {
3030
bb0(%0 : $*MyStruct):
31+
specify_test "aliasing"
3132
%1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
3233
%2 = integer_literal $Builtin.Word, 1
3334
%3 = index_addr %1 : $*Int64, %2 : $Builtin.Word
@@ -46,6 +47,7 @@ bb0(%0 : $*MyStruct):
4647
// CHECK-NEXT: NoAlias
4748
sil shared @testOffsetsBehindProjectionOverlap : $@convention(thin) (@inout OuterStruct) -> () {
4849
bb0(%0 : $*OuterStruct):
50+
specify_test "aliasing"
4951
%1 = struct_element_addr %0 : $*OuterStruct, #OuterStruct.s
5052
%2 = struct_element_addr %1 : $*MyStruct, #MyStruct.i
5153
%3 = load %2 : $*Int64
@@ -70,6 +72,7 @@ bb0(%0 : $*OuterStruct):
7072
// CHECK-NEXT: MayAlias
7173
sil @testIndexOf0 : $@convention(thin) (@inout MyStruct) -> () {
7274
bb0(%0 : $*MyStruct):
75+
specify_test "aliasing"
7376
%1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
7477
%2 = integer_literal $Builtin.Word, 0
7578
%3 = index_addr %0 : $*MyStruct, %2 : $Builtin.Word
@@ -91,6 +94,7 @@ bb0(%0 : $*MyStruct):
9194
// CHECK-NEXT: MayAlias
9295
sil @testUnknownIndex : $@convention(thin) (@inout MyStruct, Builtin.Word) -> () {
9396
bb0(%0 : $*MyStruct, %1 : $Builtin.Word):
97+
specify_test "aliasing"
9498
%2 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
9599
%3 = index_addr %0 : $*MyStruct, %1 : $Builtin.Word
96100
%4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
@@ -127,6 +131,7 @@ bb0(%0 : $*MyStruct, %1 : $Builtin.Word):
127131
// CHECK-NEXT: NoAlias
128132
sil @testVectorBaseAddr : $@convention(thin) (@inout Builtin.FixedArray<10, Int>, Builtin.Word) -> () {
129133
bb0(%0 : $*Builtin.FixedArray<10, Int>, %1 : $Builtin.Word):
134+
specify_test "aliasing"
130135
%2 = vector_base_addr %0
131136
%3 = index_addr %2, %1
132137
%4 = integer_literal $Builtin.Word, 1

0 commit comments

Comments
 (0)