Skip to content

Commit def3b6f

Browse files
committed
AccessUtils: recognize addressor calls of globals as a "global" access base
Makes e.g. alias analysis more precise in the early stage of the pass pipeline where addressor calls are not inlined, yet.
1 parent ac3e2cb commit def3b6f

File tree

4 files changed

+180
-10
lines changed

4 files changed

+180
-10
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -625,16 +625,6 @@ private struct EscapesToValueVisitor : EscapeVisitor {
625625
}
626626

627627
extension Function {
628-
var globalOfGlobalInitFunction: GlobalVariable? {
629-
if isGlobalInitFunction,
630-
let ret = returnInstruction,
631-
let atp = ret.returnedValue as? AddressToPointerInst,
632-
let ga = atp.address as? GlobalAddrInst {
633-
return ga.global
634-
}
635-
return nil
636-
}
637-
638628
var initializedGlobal: GlobalVariable? {
639629
if !isGlobalInitOnceFunction {
640630
return nil

SwiftCompilerSources/Sources/SIL/Utilities/AccessUtils.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,17 @@ private extension PointerToAddressInst {
434434
}
435435
return walker.result
436436
}
437+
438+
var resultOfGlobalAddressorCall: GlobalVariable? {
439+
if isStrict,
440+
let apply = pointer as? ApplyInst,
441+
let callee = apply.referencedFunction,
442+
let global = callee.globalOfGlobalInitFunction
443+
{
444+
return global
445+
}
446+
return nil
447+
}
437448
}
438449

439450
/// The `EnclosingScope` of an access is the innermost `begin_access`
@@ -507,6 +518,9 @@ private struct AccessPathWalker : AddressUseDefWalker {
507518
if let p2ai = address as? PointerToAddressInst {
508519
if let originatingAddr = p2ai.originatingAddress {
509520
return walkUp(address: originatingAddr, path: path)
521+
} else if let global = p2ai.resultOfGlobalAddressorCall {
522+
self.result = AccessPath(base: .global(global), projectionPath: path.projectionPath)
523+
return .continueWalk
510524
} else {
511525
self.result = AccessPath(base: .pointer(p2ai), projectionPath: path.projectionPath)
512526
return .continueWalk
@@ -628,3 +642,15 @@ extension ValueUseDefWalker where Path == SmallProjectionPath {
628642
}
629643
}
630644
}
645+
646+
extension Function {
647+
public var globalOfGlobalInitFunction: GlobalVariable? {
648+
if isGlobalInitFunction,
649+
let ret = returnInstruction,
650+
let atp = ret.returnedValue as? AddressToPointerInst,
651+
let ga = atp.address as? GlobalAddrInst {
652+
return ga.global
653+
}
654+
return nil
655+
}
656+
}

test/SILOptimizer/accessutils.sil

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,85 @@ bb0:
370370

371371
sil_global @global1 : $Int64
372372
sil_global @global2 : $Int64
373+
sil_global @somePointer : $Builtin.RawPointer
374+
375+
sil [global_init] [ossa] @addressor_of_global1 : $@convention(thin) () -> Builtin.RawPointer {
376+
bb0:
377+
%0 = global_addr @global1 : $*Int64
378+
%1 = address_to_pointer %0 : $*Int64 to $Builtin.RawPointer
379+
return %1 : $Builtin.RawPointer
380+
}
381+
382+
sil [global_init] [ossa] @external_addressor_of_global1 : $@convention(thin) () -> Builtin.RawPointer
383+
384+
sil [global_init] [ossa] @wrong_addressor_of_global1 : $@convention(thin) () -> Builtin.RawPointer {
385+
bb0:
386+
%0 = global_addr @somePointer : $*Builtin.RawPointer
387+
%1 = load [trivial] %0 : $*Builtin.RawPointer
388+
return %1 : $Builtin.RawPointer
389+
}
390+
391+
// CHECK-LABEL: Accesses for testSimpleGlobal
392+
// CHECK-NEXT: Value: %0 = global_addr @global1 : $*Int64
393+
// CHECK-NEXT: Scope: base
394+
// CHECK-NEXT: Base: global - @global1
395+
// CHECK-NEXT: Path: ""
396+
// CHECK-NEXT: no Storage paths
397+
// CHECK-NEXT: End accesses for testSimpleGlobal
398+
sil [ossa] @testSimpleGlobal : $@convention(thin) () -> Int64 {
399+
bb0:
400+
%0 = global_addr @global1 : $*Int64
401+
%1 = load [trivial] %0 : $*Int64
402+
return %1 : $Int64
403+
}
404+
405+
// CHECK-LABEL: Accesses for testGlobalViaAddressor
406+
// CHECK-NEXT: Value: %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
407+
// CHECK-NEXT: Scope: base
408+
// CHECK-NEXT: Base: global - @global1
409+
// CHECK-NEXT: Path: ""
410+
// CHECK-NEXT: no Storage paths
411+
// CHECK-NEXT: End accesses for testGlobalViaAddressor
412+
sil [ossa] @testGlobalViaAddressor : $@convention(thin) () -> Int64 {
413+
bb0:
414+
%0 = function_ref @addressor_of_global1 : $@convention(thin) () -> Builtin.RawPointer
415+
%1 = apply %0() : $@convention(thin) () -> Builtin.RawPointer
416+
%2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
417+
%3 = load [trivial] %2 : $*Int64
418+
return %3 : $Int64
419+
}
420+
421+
// CHECK-LABEL: Accesses for testGlobalViaUnknwonAddressor
422+
// CHECK-NEXT: Value: %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
423+
// CHECK-NEXT: Scope: base
424+
// CHECK-NEXT: Base: pointer - %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
425+
// CHECK-NEXT: Path: ""
426+
// CHECK-NEXT: no Storage paths
427+
// CHECK-NEXT: End accesses for testGlobalViaUnknwonAddressor
428+
sil [ossa] @testGlobalViaUnknwonAddressor : $@convention(thin) () -> Int64 {
429+
bb0:
430+
%0 = function_ref @external_addressor_of_global1 : $@convention(thin) () -> Builtin.RawPointer
431+
%1 = apply %0() : $@convention(thin) () -> Builtin.RawPointer
432+
%2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
433+
%3 = load [trivial] %2 : $*Int64
434+
return %3 : $Int64
435+
}
436+
437+
// CHECK-LABEL: Accesses for testGlobalViaWrongAddressor
438+
// CHECK-NEXT: Value: %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
439+
// CHECK-NEXT: Scope: base
440+
// CHECK-NEXT: Base: pointer - %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
441+
// CHECK-NEXT: Path: ""
442+
// CHECK-NEXT: no Storage paths
443+
// CHECK-NEXT: End accesses for testGlobalViaWrongAddressor
444+
sil [ossa] @testGlobalViaWrongAddressor : $@convention(thin) () -> Int64 {
445+
bb0:
446+
%0 = function_ref @wrong_addressor_of_global1 : $@convention(thin) () -> Builtin.RawPointer
447+
%1 = apply %0() : $@convention(thin) () -> Builtin.RawPointer
448+
%2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*Int64
449+
%3 = load [trivial] %2 : $*Int64
450+
return %3 : $Int64
451+
}
373452

374453
sil @coro : $@yield_once @convention(thin) () -> @yields @inout Int64
375454

test/SILOptimizer/mem-behavior.sil

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,81 @@ bb0:
882882
return %8 : $()
883883
}
884884

885+
sil [global_init] @addressor_of_globalC : $@convention(thin) () -> Builtin.RawPointer {
886+
bb0:
887+
%0 = global_addr @globalC : $*C
888+
%1 = address_to_pointer %0 : $*C to $Builtin.RawPointer
889+
return %1 : $Builtin.RawPointer
890+
}
891+
892+
sil [global_init] @addressor_of_globalCVar : $@convention(thin) () -> Builtin.RawPointer {
893+
bb0:
894+
%0 = global_addr @globalCVar : $*C
895+
%1 = address_to_pointer %0 : $*C to $Builtin.RawPointer
896+
return %1 : $Builtin.RawPointer
897+
}
898+
899+
// CHECK-LABEL: @testGlobalViaAddressorLet
900+
// CHECK: PAIR #2.
901+
// CHECK-NEXT: %5 = apply %4() : $@convention(thin) () -> ()
902+
// CHECK-NEXT: %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*C
903+
// CHECK-NEXT: r=1,w=0
904+
sil @testGlobalViaAddressorLet : $@convention(thin) () -> () {
905+
bb0:
906+
%0 = function_ref @addressor_of_globalC : $@convention(thin) () -> Builtin.RawPointer
907+
%1 = apply %0() : $@convention(thin) () -> Builtin.RawPointer
908+
%2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*C
909+
%3 = load %2 : $*C
910+
%4 = function_ref @nouser_func : $@convention(thin) () -> ()
911+
%5 = apply %4() : $@convention(thin) () -> ()
912+
%6 = function_ref @read_C : $@convention(thin) (@in_guaranteed C) -> ()
913+
%7 = apply %6(%2) : $@convention(thin) (@in_guaranteed C) -> ()
914+
%8 = tuple ()
915+
return %8 : $()
916+
}
917+
918+
// CHECK-LABEL: @testGlobalViaAddressorVar
919+
// CHECK: PAIR #2.
920+
// CHECK-NEXT: %5 = apply %4() : $@convention(thin) () -> ()
921+
// CHECK-NEXT: %2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*C
922+
// CHECK-NEXT: r=1,w=1
923+
sil @testGlobalViaAddressorVar : $@convention(thin) () -> () {
924+
bb0:
925+
%0 = function_ref @addressor_of_globalCVar : $@convention(thin) () -> Builtin.RawPointer
926+
%1 = apply %0() : $@convention(thin) () -> Builtin.RawPointer
927+
%2 = pointer_to_address %1 : $Builtin.RawPointer to [strict] $*C
928+
%3 = load %2 : $*C
929+
%4 = function_ref @nouser_func : $@convention(thin) () -> ()
930+
%5 = apply %4() : $@convention(thin) () -> ()
931+
%8 = tuple ()
932+
return %8 : $()
933+
}
934+
935+
// CHECK-LABEL: @testGlobalAliasing
936+
// CHECK: PAIR #6.
937+
// CHECK-NEXT: store %0 to %3 : $*C
938+
// CHECK-NEXT: %3 = pointer_to_address %2 : $Builtin.RawPointer to [strict] $*C
939+
// CHECK-NEXT: r=0,w=1
940+
// CHECK: PAIR #7.
941+
// CHECK-NEXT: store %0 to %3 : $*C
942+
// CHECK-NEXT: %6 = pointer_to_address %5 : $Builtin.RawPointer to [strict] $*C
943+
// CHECK-NEXT: r=0,w=0
944+
sil @testGlobalAliasing : $@convention(thin) (@guaranteed C) -> () {
945+
bb0(%0 : $C):
946+
%1 = function_ref @addressor_of_globalCVar : $@convention(thin) () -> Builtin.RawPointer
947+
%2 = apply %1() : $@convention(thin) () -> Builtin.RawPointer
948+
%3 = pointer_to_address %2 : $Builtin.RawPointer to [strict] $*C
949+
%4 = function_ref @addressor_of_globalC : $@convention(thin) () -> Builtin.RawPointer
950+
%5 = apply %4() : $@convention(thin) () -> Builtin.RawPointer
951+
%6 = pointer_to_address %5 : $Builtin.RawPointer to [strict] $*C
952+
%7 = global_addr @globalCVar : $*C
953+
store %0 to %3 : $*C
954+
fix_lifetime %3 : $*C
955+
fix_lifetime %6 : $*C
956+
%9 = tuple ()
957+
return %9 : $()
958+
}
959+
885960
// ===-----------------------------------------------------------------------===
886961
// Test the effect of a [deinit] access on a global 'let'
887962
//

0 commit comments

Comments
 (0)