Skip to content

ObjectOutliner: support outlining of classes in global let variables #70684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private func createOutlinedGlobal(
}

let builder = Builder(before: allocVectorBuiltin, context)
let globalAddr = builder.createGlobalAddr(global: outlinedGlobal)
let globalAddr = builder.createGlobalAddr(global: outlinedGlobal, dependencyToken: nil)
let rawVectorPointer = builder.createAddressToPointer(address: globalAddr, pointerType: allocVectorBuiltin.type,
needStackProtection: false)
allocVectorBuiltin.uses.replaceAll(with: rawVectorPointer, context)
Expand Down Expand Up @@ -441,17 +441,3 @@ private extension StoreInst {
return false
}
}

private extension Function {
var initializedGlobal: GlobalVariable? {
if !isGlobalInitOnceFunction {
return nil
}
for inst in entryBlock.instructions {
if let allocGlobal = inst as? AllocGlobalInst {
return allocGlobal.global
}
}
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
// Merge such individual stores to a single store of the whole struct.
mergeStores(in: function, context)

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

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

/// Analyses the global initializer function and returns the `alloc_global` and `store`
/// instructions which initialize the global.
///
/// The function's single basic block must contain following code pattern:
/// ```
/// alloc_global @the_global
/// %a = global_addr @the_global
/// %i = some_const_initializer_insts
/// store %i to %a
/// ```
private func getGlobalInitialization(of function: Function) -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? {

guard let block = function.singleBlock else {
return nil
}

var allocInst: AllocGlobalInst? = nil
var globalAddr: GlobalAddrInst? = nil
var store: StoreInst? = nil

for inst in block.instructions {
switch inst {
case is ReturnInst,
is DebugValueInst,
is DebugStepInst,
is BeginAccessInst,
is EndAccessInst:
break
case let agi as AllocGlobalInst:
if allocInst != nil {
return nil
}
allocInst = agi
case let ga as GlobalAddrInst:
if let agi = allocInst, agi.global == ga.global {
globalAddr = ga
}
case let si as StoreInst:
if store != nil {
return nil
}
guard let ga = globalAddr else {
return nil
}
if si.destination != ga {
return nil
}
store = si
default:
if !inst.isValidInStaticInitializerOfGlobal {
return nil
}
}
}
if let store = store {
return (allocInst: allocInst!, storeToGlobal: store)
}
return nil
}

private extension Function {
var singleBlock: BasicBlock? {
let block = entryBlock
if block.next != nil {
return nil
}
return block
}
}

/// Merges stores to individual struct fields to a single store of the whole struct.
///
/// store %element1 to %element1Addr
Expand Down
Loading