Skip to content

Commit f6e6b2e

Browse files
committed
Swift SIL: add the possibility to pass a custom location to Builder initializers.
And add the static property `Location.autoGeneratedLocation`
1 parent 2745565 commit f6e6b2e

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/PassContext.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,31 @@ extension Builder {
157157
self.init(insertAt: .before(insPnt), location: insPnt.location, passContext: context._bridged)
158158
}
159159

160-
/// Creates a builder which inserts _after_ `insPnt`, using the location of `insPnt`.
161-
init(after insPnt: Instruction, _ context: PassContext) {
160+
/// Creates a builder which inserts _after_ `insPnt`, using a custom `location`.
161+
init(after insPnt: Instruction, location: Location, _ context: PassContext) {
162162
if let nextInst = insPnt.next {
163-
self.init(insertAt: .before(nextInst), location: insPnt.location, passContext: context._bridged)
163+
self.init(insertAt: .before(nextInst), location: location, passContext: context._bridged)
164164
} else {
165-
self.init(insertAt: .atEndOf(insPnt.block), location: insPnt.location, passContext: context._bridged)
165+
self.init(insertAt: .atEndOf(insPnt.block), location: location, passContext: context._bridged)
166166
}
167167
}
168168

169+
/// Creates a builder which inserts _after_ `insPnt`, using the location of `insPnt`.
170+
init(after insPnt: Instruction, _ context: PassContext) {
171+
self.init(after: insPnt, location: insPnt.location, context)
172+
}
173+
169174
/// Creates a builder which inserts at the end of `block`, using a custom `location`.
170175
init(atEndOf block: BasicBlock, location: Location, _ context: PassContext) {
171176
self.init(insertAt: .atEndOf(block), location: location, passContext: context._bridged)
172177
}
173178

179+
/// Creates a builder which inserts at the begin of `block`, using a custom `location`.
180+
init(atBeginOf block: BasicBlock, location: Location, _ context: PassContext) {
181+
let firstInst = block.instructions.first!
182+
self.init(insertAt: .before(firstInst), location: location, passContext: context._bridged)
183+
}
184+
174185
/// Creates a builder which inserts at the begin of `block`, using the location of the first
175186
/// instruction of `block`.
176187
init(atBeginOf block: BasicBlock, _ context: PassContext) {

SwiftCompilerSources/Sources/SIL/Location.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ import SILBridging
1414

1515
public struct Location {
1616
let bridged: BridgedLocation
17+
18+
public static var autoGeneratedLocation: Location {
19+
Location(bridged: SILLocation_getAutogeneratedLocation())
20+
}
1721
}

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ SwiftInt SILType_getCaseIdxOfEnumType(BridgedType type,
334334

335335
BridgedSubstitutionMap SubstitutionMap_getEmpty();
336336

337+
BridgedLocation SILLocation_getAutogeneratedLocation();
338+
337339
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
338340
SwiftInt SILArgument_isExclusiveIndirectParameter(BridgedArgument argument);
339341

lib/SIL/Utils/SILBridging.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,15 @@ BridgedSubstitutionMap SubstitutionMap_getEmpty() {
529529
return {SubstitutionMap().getOpaqueValue()};
530530
}
531531

532+
//===----------------------------------------------------------------------===//
533+
// SILLocation
534+
//===----------------------------------------------------------------------===//
535+
536+
BridgedLocation SILLocation_getAutogeneratedLocation() {
537+
SILDebugLocation loc;
538+
return *reinterpret_cast<BridgedLocation *>(&loc);
539+
}
540+
532541
//===----------------------------------------------------------------------===//
533542
// SILGlobalVariable
534543
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)