Skip to content

refactor two swift passes #65821

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 1 commit into from
May 10, 2023
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 @@ -48,7 +48,7 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
return
}

guard let (allocInst, storeToGlobal) = function.getGlobalInitialization() else {
guard let (allocInst, storeToGlobal) = getGlobalInitialization(of: function) else {
return
}

Expand All @@ -62,69 +62,69 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
context.erase(instruction: storeToGlobal)
}

private extension 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
/// ```
func getGlobalInitialization() -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? {
/// 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 = singleBlock else {
return nil
}
guard let block = function.singleBlock else {
return nil
}

var allocInst: AllocGlobalInst? = nil
var globalAddr: GlobalAddrInst? = nil
var store: StoreInst? = 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:
break
case let agi as AllocGlobalInst:
if allocInst != nil {
return nil
}
allocInst = agi
case let ga as GlobalAddrInst:
if globalAddr != nil {
return nil
}
guard let agi = allocInst, agi.global == ga.global else {
return nil
}
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
}
for inst in block.instructions {
switch inst {
case is ReturnInst,
is DebugValueInst,
is DebugStepInst:
break
case let agi as AllocGlobalInst:
if allocInst != nil {
return nil
}
allocInst = agi
case let ga as GlobalAddrInst:
if globalAddr != nil {
return nil
}
guard let agi = allocInst, agi.global == ga.global else {
return nil
}
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
}
if let store = store {
return (allocInst: allocInst!, storeToGlobal: store)
}
return nil
}

private extension Function {
var singleBlock: BasicBlock? {
let block = entryBlock
if block.next != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let readOnlyGlobalVariablesPass = ModulePass(name: "read-only-global-variables")
for f in moduleContext.functions {
for inst in f.instructions {
if let gAddr = inst as? GlobalAddrInst {
if gAddr.addressHasWrites {
if findWrites(toAddress: gAddr) {
writtenGlobals.insert(gAddr.global)
}
}
Expand All @@ -43,11 +43,9 @@ let readOnlyGlobalVariablesPass = ModulePass(name: "read-only-global-variables")
}
}

private extension Value {
var addressHasWrites: Bool {
var walker = FindWrites()
return walker.walkDownUses(ofAddress: self, path: UnusedWalkingPath()) == .abortWalk
}
private func findWrites(toAddress: Value) -> Bool {
var walker = FindWrites()
return walker.walkDownUses(ofAddress: toAddress, path: UnusedWalkingPath()) == .abortWalk
}

private struct FindWrites : AddressDefUseWalker {
Expand Down