Skip to content

Commit 633cc34

Browse files
authored
Merge pull request #60512 from Angelogeb/accessutils-pr
Adds a set of utilities and concepts (AccessBase, AccessPath and AccessStoragePath) to identify and analyze formal accesses in SIL.
2 parents 74f97d4 + 7a20bc3 commit 633cc34

File tree

13 files changed

+1042
-24
lines changed

13 files changed

+1042
-24
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- AccessDumper.swift - Dump access information --------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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+
/// Dumps access information for memory accesses (`load` and `store`)
16+
/// instructions.
17+
///
18+
/// This pass is used for testing `AccessUtils`.
19+
let accessDumper = FunctionPass(name: "dump-access", {
20+
(function: Function, context: PassContext) in
21+
print("Accesses for \(function.name)")
22+
23+
var apw = AccessPathWalker()
24+
var arw = AccessStoragePathVisitor()
25+
for block in function.blocks {
26+
for instr in block.instructions {
27+
switch instr {
28+
case let st as StoreInst:
29+
printAccessInfo(st.destinationOperand.value, &apw, &arw, context)
30+
case let load as LoadInst:
31+
printAccessInfo(load.operand, &apw, &arw, context)
32+
default:
33+
break
34+
}
35+
}
36+
}
37+
38+
print("End accesses for \(function.name)")
39+
})
40+
41+
private struct AccessStoragePathVisitor : AccessStoragePathWalker {
42+
var walkUpCache = WalkerCache<Path>()
43+
mutating func visit(access: AccessStoragePath) {
44+
print(" Storage: \(access.storage)")
45+
print(" Path: \"\(access.path)\"")
46+
}
47+
}
48+
49+
private func printAccessInfo(_ value: Value, _ apw: inout AccessPathWalker, _ aspw: inout AccessStoragePathVisitor,
50+
_ ctx: PassContext) {
51+
print("Value: \(value)")
52+
let (ap, scope) = apw.getAccessPathWithScope(of: value)
53+
if let scope = scope {
54+
switch scope {
55+
case let .scope(ba):
56+
print(" Scope: \(ba)")
57+
case .base(_):
58+
print(" Scope: base")
59+
}
60+
}
61+
62+
if let ap = ap {
63+
print(" Base: \(ap.base)")
64+
print(" Path: \"\(ap.projectionPath)\"")
65+
66+
aspw.getAccessStorage(for: ap)
67+
}
68+
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
swift_compiler_sources(Optimizer
1010
AssumeSingleThreaded.swift
11+
AccessDumper.swift
1112
ComputeEffects.swift
1213
EscapeInfoDumper.swift
1314
ObjCBridgingOptimization.swift

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private func registerSwiftPasses() {
4242
registerPass(mergeCondFailsPass, { mergeCondFailsPass.run($0) })
4343
registerPass(escapeInfoDumper, { escapeInfoDumper.run($0) })
4444
registerPass(addressEscapeInfoDumper, { addressEscapeInfoDumper.run($0) })
45+
registerPass(accessDumper, { accessDumper.run($0) })
4546
registerPass(computeEffects, { computeEffects.run($0) })
4647
registerPass(objCBridgingOptimization, { objCBridgingOptimization.run($0) })
4748
registerPass(stackPromotion, { stackPromotion.run($0) })

0 commit comments

Comments
 (0)