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
|
//===--- UsePrespecialized.cpp - use pre-specialized functions ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "use-prespecialized"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/Generics.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/SpecializationMangler.h"
#include "llvm/Support/Debug.h"
using namespace swift;
namespace {
static void collectApplyInst(SILFunction &F,
llvm::SmallVectorImpl<ApplySite> &NewApplies) {
// Scan all of the instructions in this function in search of ApplyInsts.
for (auto &BB : F)
for (auto &I : BB)
if (ApplySite AI = ApplySite::isa(&I))
NewApplies.push_back(AI);
}
/// A simple pass which replaces each apply of a generic function by an apply
/// of the corresponding pre-specialized function, if such a pre-specialization
/// exists.
class UsePrespecialized: public SILModuleTransform {
~UsePrespecialized() override { }
void run() override {
auto &M = *getModule();
for (auto &F : M) {
if (replaceByPrespecialized(F)) {
invalidateAnalysis(&F, SILAnalysis::InvalidationKind::FunctionBody);
}
}
}
bool replaceByPrespecialized(SILFunction &F);
};
} // end anonymous namespace
// Analyze the function and replace each apply of
// a generic function by an apply of the corresponding
// pre-specialized function, if such a pre-specialization exists.
bool UsePrespecialized::replaceByPrespecialized(SILFunction &F) {
bool Changed = false;
auto &M = F.getModule();
llvm::SmallVector<ApplySite, 16> NewApplies;
collectApplyInst(F, NewApplies);
for (auto &AI : NewApplies) {
auto *ReferencedF = AI.getReferencedFunctionOrNull();
if (!ReferencedF)
continue;
LLVM_DEBUG(llvm::dbgs() << "Trying to use specialized function for:\n";
AI.getInstruction()->dumpInContext());
// Check if it is a call of a generic function.
// If this is the case, check if there is a specialization
// available for it already and use this specialization
// instead of the generic version.
if (!AI.hasSubstitutions())
continue;
SubstitutionMap Subs = AI.getSubstitutionMap();
// Bail if any generic type parameters are unbound.
// TODO: Remove this limitation once public partial specializations
// are supported and can be provided by other modules.
if (Subs.hasArchetypes())
continue;
ReabstractionInfo ReInfo(M.getSwiftModule(), M.isWholeModule(), AI,
ReferencedF, Subs, IsNotSerialized,
/*ConvertIndirectToDirect=*/ true,
/*dropMetatypeArgs=*/ false);
if (!ReInfo.canBeSpecialized())
continue;
auto SpecType = ReInfo.getSpecializedType();
// Bail if any generic types parameters of the concrete type
// are unbound.
if (SpecType->hasArchetype())
continue;
// Create a name of the specialization. All external pre-specializations
// are serialized without bodies. Thus use IsNotSerialized here.
Mangle::GenericSpecializationMangler NewGenericMangler(ReferencedF,
IsNotSerialized);
std::string ClonedName = NewGenericMangler.mangleReabstracted(Subs,
ReInfo.needAlternativeMangling());
SILFunction *NewF = nullptr;
// If we already have this specialization, reuse it.
auto PrevF = M.lookUpFunction(ClonedName);
if (PrevF) {
LLVM_DEBUG(llvm::dbgs() << "Found a specialization: " << ClonedName
<< "\n");
NewF = PrevF;
}
if (!PrevF || !NewF) {
// Check for the existence of this function in another module without
// loading the function body.
PrevF = lookupPrespecializedSymbol(M, ClonedName);
LLVM_DEBUG(llvm::dbgs() << "Checked if there is a specialization in a "
"different module: "
<< PrevF << "\n");
if (!PrevF)
continue;
assert(PrevF->isExternalDeclaration() &&
"Prespecialized function should be an external declaration");
NewF = PrevF;
}
if (!NewF)
continue;
// An existing specialization was found.
LLVM_DEBUG(llvm::dbgs() << "Found a specialization of "
<< ReferencedF->getName()
<< " : " << NewF->getName() << "\n");
auto NewAI = replaceWithSpecializedFunction(AI, NewF, ReInfo);
switch (AI.getKind()) {
case ApplySiteKind::ApplyInst:
cast<ApplyInst>(AI)->replaceAllUsesWith(cast<ApplyInst>(NewAI));
break;
case ApplySiteKind::PartialApplyInst:
cast<PartialApplyInst>(AI)->replaceAllUsesWith(
cast<PartialApplyInst>(NewAI));
break;
case ApplySiteKind::TryApplyInst:
case ApplySiteKind::BeginApplyInst:
break;
}
recursivelyDeleteTriviallyDeadInstructions(AI.getInstruction(), true);
Changed = true;
}
return Changed;
}
SILTransform *swift::createUsePrespecialized() {
return new UsePrespecialized();
}
|