Skip to content

Commit 9cebd69

Browse files
authored
Merge pull request swiftlang#62556 from eeckstein/remove-dead-metatype-insts
Performance annotations: make `UnsafeMutableRawBufferPointer.bindMemory` allocation/lock free
2 parents 65dbf83 + fc30ac2 commit 9cebd69

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/SILOptimizer/Transforms/GenericSpecializer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ optimizeInst(SILInstruction *inst, SILOptFunctionBuilder &funcBuilder,
373373
deleter.forceDelete(bi);
374374
return true;
375375
}
376+
if (auto *mti = dyn_cast<MetatypeInst>(inst)) {
377+
// Remove dead `metatype` instructions which only have `debug_value` uses.
378+
// We lose debug info for such type variables, but this is a compromise we
379+
// need to accept to get allocation/lock free code.
380+
if (onlyHaveDebugUses(mti)) {
381+
deleter.forceDeleteWithUsers(mti);
382+
}
383+
}
376384
return false;
377385
}
378386

test/SILOptimizer/performance-annotations.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ func testGlobalWithComplexInit() -> Int {
131131
return Str.s3 // expected-note {{called from here}}
132132
}
133133

134-
func metatypeArg<T>(_ t: T.Type, _ b: Bool) { // expected-error {{Using type 'Int' can cause metadata allocation or locks}}
134+
func metatypeArg<T>(_ t: T.Type, _ b: Bool) {
135135
}
136136

137137
@_noAllocation
138138
func callFuncWithMetatypeArg() {
139-
metatypeArg(Int.self, false) // expected-note {{called from here}}
139+
metatypeArg(Int.self, false)
140140
}
141141

142142
@_noAllocation
@@ -228,3 +228,16 @@ func createEmptyArray() {
228228
_ = [Int]() // expected-error {{ending the lifetime of a value of type}}
229229
}
230230

231+
struct Buffer {
232+
var p: UnsafeMutableRawBufferPointer
233+
234+
func bind<T>(of type: T.Type) -> UnsafeMutableBufferPointer<T> {
235+
self.p.bindMemory(to: T.self)
236+
}
237+
238+
@_noAllocation
239+
func callBind() -> UnsafeMutableBufferPointer<Int> {
240+
return bind(of: Int.self)
241+
}
242+
}
243+

0 commit comments

Comments
 (0)