Skip to content

Commit 7e33e55

Browse files
committed
SIL: a few changes regarding access to a GlobalVariable's static initializer instructions
* add `GlobalVariable.staticInitializerInstructions` to access all initializer instructions of a global * implement `GlobalVariable.staticInitValue` with `GlobalVariable.staticInitializerInstructions` * this requires that `InstructionList.reversed()` works without accessing the parent block of the iterator instruction * allow `Context.erase(instruction:)` to delete instructions from a global's initializer list, which means to handle the case where a deleted instruction has no parent function.
1 parent 89d3326 commit 7e33e55

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public struct InstructionList : CollectionLikeSequence, IteratorProtocol {
9797

9898
public func reversed() -> ReverseInstructionList {
9999
if let inst = currentInstruction {
100-
let lastInst = inst.parentBlock.bridged.getLastInst().instruction
100+
let lastInst = inst.bridged.getLastInstOfParent().instruction
101101
return ReverseInstructionList(first: lastInst)
102102
}
103103
return ReverseInstructionList(first: nil)

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,18 @@ final public class GlobalVariable : CustomStringConvertible, HasShortDescription
4343
return bridged.isAvailableExternally()
4444
}
4545

46+
public var staticInitializerInstructions: InstructionList? {
47+
if let firstStaticInitInst = bridged.getFirstStaticInitInst().instruction {
48+
return InstructionList(first: firstStaticInitInst)
49+
}
50+
return nil
51+
}
52+
4653
public var staticInitValue: SingleValueInstruction? {
47-
bridged.getStaticInitializerValue().instruction as? SingleValueInstruction
54+
if let staticInitInsts = staticInitializerInstructions {
55+
return staticInitInsts.reversed().first! as? SingleValueInstruction
56+
}
57+
return nil
4858
}
4959

5060
/// True if the global's linkage and resilience expansion allow the global

include/swift/SIL/SILBridging.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ struct BridgedGlobalVar {
367367
}
368368

369369
SWIFT_IMPORT_UNSAFE
370-
inline OptionalBridgedInstruction getStaticInitializerValue() const;
370+
inline OptionalBridgedInstruction getFirstStaticInitInst() const;
371371

372372
bool canBeInitializedStatically() const;
373373

@@ -467,6 +467,9 @@ struct BridgedInstruction {
467467
SWIFT_IMPORT_UNSAFE
468468
inline BridgedBasicBlock getParent() const;
469469

470+
SWIFT_IMPORT_UNSAFE
471+
inline BridgedInstruction getLastInstOfParent() const;
472+
470473
bool isDeleted() const {
471474
return getInst()->isDeleted();
472475
}
@@ -1416,11 +1419,12 @@ OptionalBridgedBasicBlock BridgedFunction::getLastBlock() const {
14161419
return {getFunction()->empty() ? nullptr : &*getFunction()->rbegin()};
14171420
}
14181421

1419-
OptionalBridgedInstruction BridgedGlobalVar::getStaticInitializerValue() const {
1420-
if (swift::SILInstruction *inst = getGlobal()->getStaticInitializerValue()) {
1421-
return {inst->asSILNode()};
1422+
OptionalBridgedInstruction BridgedGlobalVar::getFirstStaticInitInst() const {
1423+
if (getGlobal()->begin() == getGlobal()->end()) {
1424+
return {nullptr};
14221425
}
1423-
return {nullptr};
1426+
swift::SILInstruction *firstInst = &*getGlobal()->begin();
1427+
return {firstInst->asSILNode()};
14241428
}
14251429

14261430
BridgedInstruction BridgedMultiValueResult::getParent() const {
@@ -1433,6 +1437,10 @@ BridgedBasicBlock BridgedInstruction::getParent() const {
14331437
return {getInst()->getParent()};
14341438
}
14351439

1440+
inline BridgedInstruction BridgedInstruction::getLastInstOfParent() const {
1441+
return {getInst()->getParent()->back().asSILNode()};
1442+
}
1443+
14361444
BridgedSuccessorArray BridgedInstruction::TermInst_getSuccessors() const {
14371445
auto successors = getAs<swift::TermInst>()->getSuccessors();
14381446
return {{successors.data()}, (SwiftInt)successors.size()};

include/swift/SIL/SILGlobalVariable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SILGlobalVariable
4242
static SwiftMetatype registeredMetatype;
4343

4444
public:
45+
using iterator = SILBasicBlock::iterator;
4546
using const_iterator = SILBasicBlock::const_iterator;
4647

4748
private:
@@ -182,6 +183,8 @@ class SILGlobalVariable
182183

183184
const_iterator begin() const { return StaticInitializerBlock.begin(); }
184185
const_iterator end() const { return StaticInitializerBlock.end(); }
186+
iterator begin() { return StaticInitializerBlock.begin(); }
187+
iterator end() { return StaticInitializerBlock.end(); }
185188

186189
void dropAllReferences() {
187190
StaticInitializerBlock.dropAllReferences();

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,10 @@ void SwiftPassInvocation::eraseInstruction(SILInstruction *inst) {
617617
if (silCombiner) {
618618
silCombiner->eraseInstFromFunction(*inst);
619619
} else {
620-
inst->eraseFromParent();
620+
if (inst->isStaticInitializerInst()) {
621+
inst->getParent()->erase(inst, *getPassManager()->getModule());
622+
} else {
623+
inst->eraseFromParent();
624+
}
621625
}
622626
}

0 commit comments

Comments
 (0)