|
| 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 | +} |
0 commit comments