|
| 1 | +//===--- EscapeInfoDumper.swift - Dumps escape 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 the results of escape analysis. |
| 16 | +/// |
| 17 | +/// Dumps the EscapeInfo query results for all `alloc_stack` instructions in a function. |
| 18 | +/// |
| 19 | +/// This pass is used for testing EscapeInfo. |
| 20 | +let escapeInfoDumper = FunctionPass(name: "dump-escape-info", { |
| 21 | + (function: Function, context: PassContext) in |
| 22 | + |
| 23 | + print("Escape information for \(function.name):") |
| 24 | + |
| 25 | + var escapeInfo = EscapeInfo(calleeAnalysis: context.calleeAnalysis) |
| 26 | + |
| 27 | + for block in function.blocks { |
| 28 | + for inst in block.instructions { |
| 29 | + if let allocRef = inst as? AllocRefInst { |
| 30 | + var results = Set<String>() |
| 31 | + |
| 32 | + let escapes = escapeInfo.isEscaping(object: allocRef, |
| 33 | + visitUse: { op, path, _ in |
| 34 | + if op.instruction is ReturnInst { |
| 35 | + results.insert("return[\(path)]") |
| 36 | + return .ignore |
| 37 | + } |
| 38 | + return .continueWalking |
| 39 | + }, |
| 40 | + visitDef: { def, path, followStores in |
| 41 | + guard let arg = def as? FunctionArgument else { |
| 42 | + return .continueWalkingUp |
| 43 | + } |
| 44 | + results.insert("arg\(arg.index)[\(path)]") |
| 45 | + return .continueWalkingDown |
| 46 | + }) |
| 47 | + |
| 48 | + let res: String |
| 49 | + if escapes { |
| 50 | + res = "global" |
| 51 | + } else if results.isEmpty { |
| 52 | + res = " - " |
| 53 | + } else { |
| 54 | + res = Array(results).sorted().joined(separator: ",") |
| 55 | + } |
| 56 | + print("\(res): \(allocRef)") |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + print("End function \(function.name)\n") |
| 61 | +}) |
| 62 | + |
| 63 | +/// Dumps the results of address-related escape analysis. |
| 64 | +/// |
| 65 | +/// Dumps the EscapeInfo query results for addresses escaping to function calls. |
| 66 | +/// The `fix_lifetime` instruction is used as marker for addresses and values to query. |
| 67 | +/// |
| 68 | +/// This pass is used for testing EscapeInfo. |
| 69 | +let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info", { |
| 70 | + (function: Function, context: PassContext) in |
| 71 | + |
| 72 | + print("Address escape information for \(function.name):") |
| 73 | + |
| 74 | + var valuesToCheck = [Value]() |
| 75 | + var applies = [Instruction]() |
| 76 | + |
| 77 | + for block in function.blocks { |
| 78 | + for inst in block.instructions { |
| 79 | + switch inst { |
| 80 | + case let fli as FixLifetimeInst: |
| 81 | + valuesToCheck.append(fli.operand) |
| 82 | + case is FullApplySite: |
| 83 | + applies.append(inst) |
| 84 | + default: |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + var escapeInfo = EscapeInfo(calleeAnalysis: context.calleeAnalysis) |
| 91 | + |
| 92 | + // test `isEscaping(addressesOf:)` |
| 93 | + for value in valuesToCheck { |
| 94 | + print("value:\(value)") |
| 95 | + for apply in applies { |
| 96 | + let path = AliasAnalysis.getPtrOrAddressPath(for: value) |
| 97 | + let escaping = escapeInfo.isEscaping(addressesOf: value, path: path, |
| 98 | + visitUse: { op, _, _ in |
| 99 | + let user = op.instruction |
| 100 | + if user == apply { |
| 101 | + return .markEscaping |
| 102 | + } |
| 103 | + if user is ReturnInst { |
| 104 | + // Anything which is returned cannot escape to an instruction inside the function. |
| 105 | + return .ignore |
| 106 | + } |
| 107 | + return .continueWalking |
| 108 | + }) |
| 109 | + print(" \(escaping ? "==>" : "- ") \(apply)") |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // test `canReferenceSameField` for each pair of `fix_lifetime`. |
| 114 | + if !valuesToCheck.isEmpty { |
| 115 | + for lhsIdx in 0..<(valuesToCheck.count - 1) { |
| 116 | + for rhsIdx in (lhsIdx + 1) ..< valuesToCheck.count { |
| 117 | + print("pair \(lhsIdx) - \(rhsIdx)") |
| 118 | + let lhs = valuesToCheck[lhsIdx] |
| 119 | + let rhs = valuesToCheck[rhsIdx] |
| 120 | + print(lhs) |
| 121 | + print(rhs) |
| 122 | + if escapeInfo.canReferenceSameField( |
| 123 | + lhs, path: AliasAnalysis.getPtrOrAddressPath(for: lhs), |
| 124 | + rhs, path: AliasAnalysis.getPtrOrAddressPath(for: rhs)) { |
| 125 | + print("may alias") |
| 126 | + } else { |
| 127 | + print("no alias") |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + print("End function \(function.name)\n") |
| 134 | +}) |
0 commit comments