-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSILGenLocalArchetype.cpp
107 lines (86 loc) · 3.56 KB
/
SILGenLocalArchetype.cpp
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
//===--- SILGenLocalArchetype.cpp - Local archetype transform -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the transformation which rewrites captured local
// archetypes into primary archetypes in the enclosing function's generic
// signature.
//
//===----------------------------------------------------------------------===//
#include "SILGen.h"
#include "swift/AST/LocalArchetypeRequirementCollector.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILCloner.h"
using namespace swift;
using namespace swift::Lowering;
namespace {
class LocalArchetypeTransform : public SILCloner<LocalArchetypeTransform> {
friend class SILCloner<LocalArchetypeTransform>;
friend class SILInstructionVisitor<LocalArchetypeTransform>;
GenericSignatureWithCapturedEnvironments sig;
GenericEnvironment *env;
public:
LocalArchetypeTransform(SILFunction *F,
GenericSignatureWithCapturedEnvironments sig)
: SILCloner(*F), env(sig.genericSig.getGenericEnvironment()) {
assert(!sig.capturedEnvs.empty() && "Why are we doing this?");
// The primary archetypes of the old generic environment map to
// primary archetypes of the new generic environment at the same
// index and depth.
Functor.SubsMap = env->getForwardingSubstitutionMap();
// Captured local archetypes map to primary archetypes at higher
// depths.
Functor.BaseGenericSig = sig.baseGenericSig;
Functor.CapturedEnvs = sig.capturedEnvs;
}
void doIt() {
auto &F = getBuilder().getFunction();
// Collect the old basic blocks that we're going to delete.
llvm::SmallVector<SILBasicBlock *, 4> bbs;
for (auto &bb : F)
bbs.push_back(&bb);
// Make F.mapTypeIntoContext() use the new environment.
F.setGenericEnvironment(env);
// Start by cloning the entry block.
auto *origEntryBlock = F.getEntryBlock();
auto *clonedEntryBlock = F.createBasicBlock();
// Clone arguments.
SmallVector<SILValue, 4> entryArgs;
entryArgs.reserve(origEntryBlock->getArguments().size());
for (auto &origArg : origEntryBlock->getArguments()) {
// Remap the argument type into the new generic environment.
SILType mappedType = remapType(origArg->getType());
auto *NewArg = clonedEntryBlock->createFunctionArgument(
mappedType, origArg->getDecl(), true);
NewArg->copyFlags(cast<SILFunctionArgument>(origArg));
entryArgs.push_back(NewArg);
}
// Clone the remaining body.
getBuilder().setInsertionPoint(clonedEntryBlock);
cloneFunctionBody(&F, clonedEntryBlock, entryArgs,
true /*replaceOriginalFunctionInPlace*/);
// Insert the new entry block at the beginning.
F.moveBlockBefore(clonedEntryBlock, F.begin());
// Erase the old basic blocks.
for (auto *bb : bbs) {
bb->removeDeadBlock();
}
}
};
} // end anonymous namespace
void SILGenModule::recontextualizeCapturedLocalArchetypes(
SILFunction *F, GenericSignatureWithCapturedEnvironments sig) {
if (sig.capturedEnvs.empty())
return;
LocalArchetypeTransform(F, sig).doIt();
M.reclaimUnresolvedLocalArchetypeDefinitions();
}