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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
// slang-ir-lower-defer.cpp
#include "slang-ir-lower-defer.h"
#include "slang-ir-clone.h"
#include "slang-ir-dominators.h"
#include "slang-ir-inst-pass-base.h"
#include "slang-ir-insts.h"
#include "slang-ir.h"
namespace Slang
{
struct DeferLoweringContext : InstPassBase
{
DiagnosticSink* diagnosticSink;
DeferLoweringContext(IRModule* inModule)
: InstPassBase(inModule)
{
}
void inlineSingleBlockDefer(IRInst* beforeInst, IRBlock* deferBlock, IRBuilder* builder)
{
builder->setInsertBefore(beforeInst);
IRCloneEnv env;
for (IRInst* inst : deferBlock->getChildren())
{
// Copy everything except the terminator; the terminator should only
// be a jump to mergeBlock, which isn't needed after inlining.
if (!as<IRTerminatorInst>(inst))
cloneInst(&env, builder, inst);
}
}
// Returns the new last block.
IRBlock* inlineDefer(
IRInst* beforeInst,
IRBlock* targetBlock,
const List<IRBlock*>& deferBlocks,
IRBlock* mergeBlock,
IRBuilder* builder)
{
// The single-block inlining case is simple, we can just dump the
// instructions at the target position, in the existing block.
if (deferBlocks.getCount() == 1)
{
inlineSingleBlockDefer(beforeInst, deferBlocks.getFirst(), builder);
return targetBlock;
}
// Otherwise, we'll have to splice the blocks in.
IRCloneEnv env;
builder->setInsertAfter(targetBlock);
auto lastBlock = targetBlock;
// Clone blocks first
for (auto block : deferBlocks)
{
auto clonedBlock = builder->createBlock();
builder->addInst(clonedBlock);
env.mapOldValToNew[block] = clonedBlock;
}
// Then, clone instructions, but mapping old blocks to new blocks.
for (auto block : deferBlocks)
{
auto clonedBlock = as<IRBlock>(env.mapOldValToNew.getValue(block));
builder->setInsertInto(clonedBlock);
for (auto inst : block->getChildren())
{
auto endBranch = as<IRUnconditionalBranch>(inst);
if (endBranch && endBranch->getTargetBlock() == mergeBlock)
{
lastBlock = clonedBlock;
}
else
cloneInst(&env, builder, inst);
}
}
// Move old instructions to the last block's end. The last defer block
// shouldn't have a terminator at this point yet.
while (beforeInst)
{
auto nextInst = beforeInst->getNextInst();
beforeInst->insertAtEnd(lastBlock);
beforeInst = nextInst;
}
// Make target block jump to the cloned blocks.
builder->setInsertInto(targetBlock);
auto mainBlock = as<IRBlock>(env.mapOldValToNew.getValue(deferBlocks[0]));
builder->emitBranch(mainBlock);
return lastBlock;
}
HashSet<IRBlock*> findSuccessorBlocks(IRGlobalValueWithCode* func, IRBlock* block)
{
HashSet<IRBlock*> successorBlocksSet;
List<IRBlock*> successorWorkList;
successorWorkList.add(block);
List<IRBlock*> postorder = getPostorder(func);
Index limitIndex = postorder.indexOf(block);
while (successorWorkList.getCount() > 0)
{
IRBlock* predecessor = successorWorkList.getLast();
successorWorkList.removeLast();
if (successorBlocksSet.contains(predecessor))
continue;
Index predecessorIndex = postorder.indexOf(predecessor);
// Does not succeed if it is after the given block in postorder.
if (predecessorIndex > limitIndex)
continue;
successorBlocksSet.add(predecessor);
for (IRBlock* successor : predecessor->getSuccessors())
successorWorkList.add(successor);
}
return successorBlocksSet;
}
void processFunc(IRGlobalValueWithCode* func)
{
// Iterating over `defer` instructions in reverse order allows us to
// expand them in the correct order, including nested `defer`s.
// We also use this to determine scope extents.
List<IRBlock*> reverseBlocks = getReversePostorderOnReverseCFG(func);
List<IRDefer*> unhandledDefers;
for (IRBlock* block : reverseBlocks)
{
for (auto child = block->getLastChild(); child; child = child->getPrevInst())
{
if (auto defer = as<IRDefer>(child))
unhandledDefers.add(defer);
}
}
IRBuilder builder(module);
Dictionary<IRBlock*, IRBlock*> mapOldScopeToNew;
for (IRDefer* defer : unhandledDefers)
{
IRBlock* firstDeferBlock = defer->getDeferBlock();
IRBlock* mergeBlock = defer->getMergeBlock();
IRBlock* scopeEndBlock = defer->getScopeBlock();
mapOldScopeToNew.tryGetValue(scopeEndBlock, scopeEndBlock);
IRBlock* parentBlock = as<IRBlock>(defer->getParent());
// The dominator tree gets invalidated on every iteration, so it's
// necessary to construct it inside the loop.
auto dom = module->findOrCreateDominatorTree(func);
// Enumerate defer block range. That is, all blocks dominated by
// parentBlock and not dominated by mergeBlock.
auto deferDominatedBlocks = dom->getProperlyDominatedBlocks(firstDeferBlock);
List<IRBlock*> deferBlocks;
deferBlocks.add(firstDeferBlock);
for (IRBlock* block : deferDominatedBlocks)
{
if (!dom->properlyDominates(mergeBlock, block) && block != mergeBlock)
deferBlocks.add(block);
}
auto dominatedBlocks = dom->getProperlyDominatedBlocks(mergeBlock);
HashSet<IRBlock*> scopeSuccessorBlocksSet = findSuccessorBlocks(func, scopeEndBlock);
HashSet<IRBlock*> scopeBlocksSet;
scopeBlocksSet.add(mergeBlock);
for (IRBlock* block : dominatedBlocks)
{
if (!scopeSuccessorBlocksSet.contains(block))
scopeBlocksSet.add(block);
}
// All jumps from blocks in scope to blocks out of scope are to be
// preceded by a copy of the deferBlocks.
for (IRBlock* block : scopeBlocksSet)
{
auto terminator = block->getTerminator();
SLANG_ASSERT(terminator);
bool exits = false;
switch (terminator->getOp())
{
case kIROp_Return:
case kIROp_Discard:
case kIROp_Throw:
exits = true;
break;
case kIROp_UnconditionalBranch:
{
auto targetBlock = as<IRBlock>(terminator->getOperand(0));
if (!scopeBlocksSet.contains(targetBlock))
{
exits = true;
}
}
break;
case kIROp_ConditionalBranch:
{
auto trueBlock = as<IRBlock>(terminator->getOperand(1));
auto falseBlock = as<IRBlock>(terminator->getOperand(2));
if (!scopeBlocksSet.contains(trueBlock) ||
!scopeBlocksSet.contains(falseBlock))
{
exits = true;
}
}
break;
default:
break;
}
if (exits)
{ // Duplicate child instructions to the end of this block.
auto newEnd = inlineDefer(terminator, block, deferBlocks, mergeBlock, &builder);
if (newEnd != block)
{
mapOldScopeToNew[block] = newEnd;
}
}
}
// Replace defer with unconditional branch to mergeBlock. Defer
// blocks should now be orphaned, and we can remove them too.
defer->removeAndDeallocate();
builder.setInsertInto(parentBlock);
builder.emitBranch(mergeBlock);
for (IRBlock* deferBlock : deferBlocks)
{
deferBlock->removeAndDeallocate();
}
// Some blocks got removed and added, so mark analysis of the
// function with defer as outdated.
module->invalidateAnalysisForInst(func);
}
}
void processModule()
{
processInstsOfType<IRFunc>(kIROp_Func, [&](IRFunc* func) { processFunc(func); });
}
};
void lowerDefer(IRModule* module, DiagnosticSink* sink)
{
DeferLoweringContext context(module);
context.diagnosticSink = sink;
return context.processModule();
}
} // namespace Slang
|