Skip to content

Commit 7b2c8f1

Browse files
authored
Merge pull request swiftlang#30074 from eeckstein/globalopt
GlobalOpt: improvements for constant folding global variables
2 parents 65ab041 + 43e8b07 commit 7b2c8f1

File tree

11 files changed

+233
-484
lines changed

11 files changed

+233
-484
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ class SILBuilder {
197197
assert(F && "cannot create this instruction without a function context");
198198
return *F;
199199
}
200+
201+
bool isInsertingIntoGlobal() const { return F == nullptr; }
200202

201203
TypeExpansionContext getTypeExpansionContext() const {
202204
return TypeExpansionContext(getFunction());

include/swift/SIL/SILCloner.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
149149
/// override the implementation via `postProcess`.
150150
void recordClonedInstruction(SILInstruction *Orig, SILInstruction *Cloned) {
151151
asImpl().postProcess(Orig, Cloned);
152-
assert((Orig->getDebugScope() ? Cloned->getDebugScope() != nullptr : true)
152+
assert((!Orig->getDebugScope() || Cloned->getDebugScope() ||
153+
Builder.isInsertingIntoGlobal())
153154
&& "cloned instruction dropped debug scope");
154155
}
155156

@@ -560,7 +561,8 @@ template<typename ImplClass>
560561
void
561562
SILCloner<ImplClass>::postProcess(SILInstruction *orig,
562563
SILInstruction *cloned) {
563-
assert((orig->getDebugScope() ? cloned->getDebugScope()!=nullptr : true) &&
564+
assert((!orig->getDebugScope() || cloned->getDebugScope() ||
565+
Builder.isInsertingIntoGlobal()) &&
564566
"cloned function dropped debug scope");
565567

566568
// It sometimes happens that an instruction with no results gets mapped

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,18 @@ class StaticInitCloner : public SILCloner<StaticInitCloner> {
289289
/// don't have any operands).
290290
llvm::SmallVector<SILInstruction *, 8> readyToClone;
291291

292+
SILInstruction *insertionPoint = nullptr;
293+
292294
public:
293295
StaticInitCloner(SILGlobalVariable *gVar)
294296
: SILCloner<StaticInitCloner>(gVar) {}
295297

298+
StaticInitCloner(SILInstruction *insertionPoint)
299+
: SILCloner<StaticInitCloner>(*insertionPoint->getFunction()),
300+
insertionPoint(insertionPoint) {
301+
Builder.setInsertionPoint(insertionPoint);
302+
}
303+
296304
/// Add \p InitVal and all its operands (transitively) for cloning.
297305
///
298306
/// Note: all init values must are added, before calling clone().
@@ -314,8 +322,16 @@ class StaticInitCloner : public SILCloner<StaticInitCloner> {
314322

315323
protected:
316324
SILLocation remapLocation(SILLocation loc) {
325+
if (insertionPoint)
326+
return insertionPoint->getLoc();
317327
return ArtificialUnreachableLocation();
318328
}
329+
330+
const SILDebugScope *remapScope(const SILDebugScope *DS) {
331+
if (insertionPoint)
332+
return insertionPoint->getDebugScope();
333+
return nullptr;
334+
}
319335
};
320336

321337
} // namespace swift

0 commit comments

Comments
 (0)