1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
//===- PackMetadataMarkerInserter.cpp - Add markers for metadata de/allocs ===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// IRGen initially assembles metadata and wtable packs on the stack. It then
// heapifies those packs.
//
// As an optimization, that heapification can be avoided some of the time:
// whenever the instructions which may result in such allocations are identified
// in functions which do not have instructions that obstruct StackNesting.
//
// Finds all instructions on behalf of which IRGen may emit on-stack pack
// metadata, looking for instructions which obstruct that optimization
//
// No code motion may occur after this pass: alloc_pack_metadata must directly
// precede the instruction on behalf of which metadata will actually be emitted
// (e.g. apply).
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "pack-metadata-dealloc-inserter"
#include "swift/AST/TypeWalker.h"
#include "swift/IRGen/IRGenSILPasses.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/Dominance.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILNode.h"
#include "swift/SIL/StackList.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/StackNesting.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ErrorHandling.h"
using namespace swift;
namespace {
/// Finds and inserts marker instructions indicating where on-stack pack
/// metadata and wtables should be cleaned up.
///
/// Inserts alloc_pack_metadata before such instructions, and inserts
/// dealloc_pack_metadata on the dominance frontier. The client is responsible
/// for ensuring that critical edges have been split before inserting markers.
class Inserter {
SILFunction *function;
StackList<SILInstruction *> instructionsToCleanup;
public:
Inserter(SILFunction *function)
: function(function), instructionsToCleanup(function) {}
enum class FindResult {
None,
Some,
Unhandleable,
};
Inserter::FindResult find();
void insert(DominanceInfo *tree, DeadEndBlocks *deBlocks);
private:
FindResult shouldInsertMarkersForInstruction(SILInstruction *inst);
void insertMarkers(SILInstruction *instruction, DominanceInfo *tree,
DeadEndBlocks *deBlocks);
};
Inserter::FindResult
Inserter::shouldInsertMarkersForInstruction(SILInstruction *inst) {
switch (inst->getKind()) {
case SILInstructionKind::BuiltinInst: {
auto *bi = cast<BuiltinInst>(inst);
// Functions with async lets may use the stack, but that is not encoded in
// SIL. As a result, stack nesting is unable to properly nest async lets
// with on-stack pack metadata. rdar://109850951
if (bi->getBuiltinKind() ==
BuiltinValueKind::StartAsyncLetWithLocalBuffer ||
bi->getBuiltinKind() == BuiltinValueKind::StartAsyncLet)
return Inserter::FindResult::Unhandleable;
LLVM_FALLTHROUGH;
}
default:
return inst->mayRequirePackMetadata(*inst->getFunction())
? FindResult::Some
: FindResult::None;
}
}
void Inserter::insertMarkers(SILInstruction *instruction, DominanceInfo *tree,
DeadEndBlocks *deBlocks) {
SILBuilderWithScope builder(instruction);
auto *apmi = builder.createAllocPackMetadata(CleanupLocation(
RegularLocation::getAutoGeneratedLocation(instruction->getLoc())));
SmallVector<SILBasicBlock *, 4> boundary;
auto *block = apmi->getParent();
computeDominatedBoundaryBlocks(block, tree, boundary);
for (auto *block : boundary) {
if (deBlocks->isDeadEnd(block))
continue;
SILBuilderWithScope builder(&block->back());
builder.createDeallocPackMetadata(
CleanupLocation(RegularLocation::getAutoGeneratedLocation()), apmi);
}
}
/// Find instructions that may entail materializing a metadata pack and those
/// that will prevent stack allocation of metadata.
///
/// Found instructions are recorded in instructionsToCleanup.
Inserter::FindResult Inserter::find() {
auto collected = false;
for (auto &block : *function) {
for (auto &instruction : block) {
auto result = shouldInsertMarkersForInstruction(&instruction);
switch (result) {
case FindResult::None:
continue;
case FindResult::Some:
instructionsToCleanup.push_back(&instruction);
collected = true;
continue;
case FindResult::Unhandleable:
return FindResult::Unhandleable;
}
llvm_unreachable("covered switch");
}
}
return collected ? FindResult::Some : FindResult::None;
}
/// Insert the marker instructions around the instructions previously found.
void Inserter::insert(DominanceInfo *tree, DeadEndBlocks *deBlocks) {
for (auto *instruction : instructionsToCleanup) {
insertMarkers(instruction, tree, deBlocks);
}
}
class PackMetadataMarkerInserter : public SILFunctionTransform {
void run() override {
auto *function = getFunction();
// If on-stack metadata is disabled, this pass does nothing.
auto options = function->getModule().getOptions();
if (!options.EnablePackMetadataStackPromotion)
return;
Inserter inserter(function);
auto found = inserter.find();
switch (found) {
case Inserter::FindResult::None:
return;
case Inserter::FindResult::Unhandleable:
LLVM_DEBUG(llvm::dbgs() << "Found obstructive instructions in "
<< function->getName() << ".\n");
/// The function contains some unhandleable instruction. Record that on
/// the function.
function->setUseStackForPackMetadata(DoNotUseStackForPackMetadata);
return;
case Inserter::FindResult::Some:
LLVM_DEBUG(llvm::dbgs() << "Found potential pack metadata emitters in "
<< function->getName() << ".\n");
break;
}
auto *dominance = getAnalysis<DominanceAnalysis>();
auto *tree = dominance->get(function);
auto split = splitAllCriticalEdges(*function, /*domInfo=*/tree,
/*loopInfo=*/nullptr);
auto *deadEnds = getAnalysis<DeadEndBlocksAnalysis>();
if (split) {
deadEnds->invalidateFunction(function);
}
auto *deBlocks = deadEnds->get(function);
inserter.insert(tree, deBlocks);
auto changes = StackNesting::fixNesting(function);
invalidateAnalysis(
(split || changes == StackNesting::Changes::CFG)
? SILAnalysis::InvalidationKind::BranchesAndInstructions
: SILAnalysis::InvalidationKind::Instructions);
}
};
} // end anonymous namespace
SILTransform *irgen::createPackMetadataMarkerInserter() {
return new PackMetadataMarkerInserter();
}
|