|
| 1 | +//===--- OwnershipDumper.cpp ----------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 | +/// \file |
| 14 | +/// |
| 15 | +/// This is a simple utility pass that dumps the ValueOwnershipKind of all |
| 16 | +/// SILValue in a module. It is meant to trigger assertions and verification of |
| 17 | +/// these values. |
| 18 | +/// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +#include "swift/SIL/SILFunction.h" |
| 22 | +#include "swift/SIL/SILInstruction.h" |
| 23 | +#include "swift/SILOptimizer/PassManager/Passes.h" |
| 24 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 25 | + |
| 26 | +using namespace swift; |
| 27 | + |
| 28 | +//===----------------------------------------------------------------------===// |
| 29 | +// Implementation |
| 30 | +//===----------------------------------------------------------------------===// |
| 31 | + |
| 32 | +static void dumpInstruction(SILInstruction &ii) { |
| 33 | + llvm::outs() << "Visiting: " << ii; |
| 34 | + |
| 35 | + auto ops = ii.getAllOperands(); |
| 36 | + if (!ops.empty()) { |
| 37 | + llvm::outs() << "Operand Ownership Map:\n"; |
| 38 | + for (const auto &op : ops) { |
| 39 | + llvm::outs() << "Op #: " << op.getOperandNumber() << "\n" |
| 40 | + << "Ownership Map: " << op.getOwnershipKindMap(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // If the instruction doesn't have any results, bail. |
| 45 | + auto results = ii.getResults(); |
| 46 | + if (!results.empty()) { |
| 47 | + llvm::outs() << "Results Ownership Kinds:\n"; |
| 48 | + for (auto v : results) { |
| 49 | + auto kind = v.getOwnershipKind(); |
| 50 | + llvm::outs() << "Result: " << v; |
| 51 | + llvm::outs() << "Kind: " << kind << "\n"; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +//===----------------------------------------------------------------------===// |
| 57 | +// Top Level Entrypoint |
| 58 | +//===----------------------------------------------------------------------===// |
| 59 | + |
| 60 | +namespace { |
| 61 | + |
| 62 | +class OwnershipDumper : public SILFunctionTransform { |
| 63 | + void run() override { |
| 64 | + SILFunction *f = getFunction(); |
| 65 | + llvm::outs() << "*** Dumping Function: '" << f->getName() << "'\n"; |
| 66 | + for (auto &bb : *f) { |
| 67 | + // We only dump instructions right now. |
| 68 | + for (auto &ii : bb) { |
| 69 | + dumpInstruction(ii); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +} // end anonymous namespace |
| 76 | + |
| 77 | +SILTransform *swift::createOwnershipDumper() { return new OwnershipDumper(); } |
0 commit comments