Skip to content

Commit 74fbae1

Browse files
authored
Merge pull request #70684 from eeckstein/static-objects
ObjectOutliner: support outlining of classes in global let variables
2 parents 81d77a6 + fa3a959 commit 74fbae1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+691
-248
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocVectorLowering.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private func createOutlinedGlobal(
246246
}
247247

248248
let builder = Builder(before: allocVectorBuiltin, context)
249-
let globalAddr = builder.createGlobalAddr(global: outlinedGlobal)
249+
let globalAddr = builder.createGlobalAddr(global: outlinedGlobal, dependencyToken: nil)
250250
let rawVectorPointer = builder.createAddressToPointer(address: globalAddr, pointerType: allocVectorBuiltin.type,
251251
needStackProtection: false)
252252
allocVectorBuiltin.uses.replaceAll(with: rawVectorPointer, context)
@@ -441,17 +441,3 @@ private extension StoreInst {
441441
return false
442442
}
443443
}
444-
445-
private extension Function {
446-
var initializedGlobal: GlobalVariable? {
447-
if !isGlobalInitOnceFunction {
448-
return nil
449-
}
450-
for inst in entryBlock.instructions {
451-
if let allocGlobal = inst as? AllocGlobalInst {
452-
return allocGlobal.global
453-
}
454-
}
455-
return nil
456-
}
457-
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
5757
// Merge such individual stores to a single store of the whole struct.
5858
mergeStores(in: function, context)
5959

60-
guard let (allocInst, storeToGlobal) = getGlobalInitialization(of: function) else {
60+
// The initializer must not contain a `global_value` because `global_value` needs to
61+
// initialize the class metadata at runtime.
62+
guard let (allocInst, storeToGlobal) = getGlobalInitialization(of: function, allowGlobalValue: false) else {
6163
return
6264
}
6365

@@ -81,76 +83,6 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
8183
context.removeTriviallyDeadInstructionsIgnoringDebugUses(in: function)
8284
}
8385

84-
/// Analyses the global initializer function and returns the `alloc_global` and `store`
85-
/// instructions which initialize the global.
86-
///
87-
/// The function's single basic block must contain following code pattern:
88-
/// ```
89-
/// alloc_global @the_global
90-
/// %a = global_addr @the_global
91-
/// %i = some_const_initializer_insts
92-
/// store %i to %a
93-
/// ```
94-
private func getGlobalInitialization(of function: Function) -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? {
95-
96-
guard let block = function.singleBlock else {
97-
return nil
98-
}
99-
100-
var allocInst: AllocGlobalInst? = nil
101-
var globalAddr: GlobalAddrInst? = nil
102-
var store: StoreInst? = nil
103-
104-
for inst in block.instructions {
105-
switch inst {
106-
case is ReturnInst,
107-
is DebugValueInst,
108-
is DebugStepInst,
109-
is BeginAccessInst,
110-
is EndAccessInst:
111-
break
112-
case let agi as AllocGlobalInst:
113-
if allocInst != nil {
114-
return nil
115-
}
116-
allocInst = agi
117-
case let ga as GlobalAddrInst:
118-
if let agi = allocInst, agi.global == ga.global {
119-
globalAddr = ga
120-
}
121-
case let si as StoreInst:
122-
if store != nil {
123-
return nil
124-
}
125-
guard let ga = globalAddr else {
126-
return nil
127-
}
128-
if si.destination != ga {
129-
return nil
130-
}
131-
store = si
132-
default:
133-
if !inst.isValidInStaticInitializerOfGlobal {
134-
return nil
135-
}
136-
}
137-
}
138-
if let store = store {
139-
return (allocInst: allocInst!, storeToGlobal: store)
140-
}
141-
return nil
142-
}
143-
144-
private extension Function {
145-
var singleBlock: BasicBlock? {
146-
let block = entryBlock
147-
if block.next != nil {
148-
return nil
149-
}
150-
return block
151-
}
152-
}
153-
15486
/// Merges stores to individual struct fields to a single store of the whole struct.
15587
///
15688
/// store %element1 to %element1Addr

0 commit comments

Comments
 (0)