Skip to content

Commit ccc691c

Browse files
committed
ObjectOutliner: try outline alloc_refs in multiple iterations to handle multi-dimensional arrays
This allows to completely statically allocate multi-dimensional global arrays, like ``` var x = [[1, 2], [3, 4], [5, 6]] ```
1 parent a30b014 commit ccc691c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,25 @@ let objectOutliner = FunctionPass(name: "object-outliner") {
5959
return
6060
}
6161

62-
for inst in function.instructions {
63-
if let ari = inst as? AllocRefInstBase {
64-
if !context.continueWithNextSubpassRun(for: inst) {
62+
var allocRefs = Stack<AllocRefInstBase>(context)
63+
defer { allocRefs.deinitialize() }
64+
65+
allocRefs.append(contentsOf: function.instructions.lazy.compactMap { $0 as? AllocRefInstBase })
66+
67+
// Try multiple iterations to handle multi-dimensional arrays.
68+
var changed: Bool
69+
repeat {
70+
changed = false
71+
for ari in allocRefs where !ari.isDeleted {
72+
if !context.continueWithNextSubpassRun(for: ari) {
6573
return
6674
}
6775
if let globalValue = optimizeObjectAllocation(allocRef: ari, context) {
6876
optimizeFindStringCall(stringArray: globalValue, context)
77+
changed = true
6978
}
7079
}
71-
}
80+
} while changed
7281
}
7382

7483
private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: FunctionPassContext) -> GlobalValueInst? {

test/SILOptimizer/readonly_arrays.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// CHECK-DAG: @"$s4test3StrV9staticLet_WZTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} @_swiftImmortalRefCount
1818
// CHECK-DAG: @"$s4test3StrV9staticVar_WZTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} @_swiftImmortalRefCount
1919
// CHECK-DAG: @"$s4test3StrV9staticVarSaySiGvpZ" = global %TSa <{ %Ts12_ArrayBufferV <{ %Ts14_BridgeStorageV <{ ptr @"$s4test3StrV9staticVar_WZTv_r" }> }> }>
20-
// CHECK-NOT: swift_initStaticObject
20+
// CHECK-DAG: @"$s4test3StrV14twoDimensionalSaySaySiGGvpZ" = global %TSa <{ %Ts12_ArrayBufferV <{ %Ts14_BridgeStorageV <{ ptr @"$s4test3StrV14twoDimensional_WZTv{{[0-9]*}}_r" }> }> }>, align 8
2121

2222
// UNSUPPORTED: use_os_stdlib
2323

@@ -28,6 +28,7 @@
2828
public struct Str {
2929
public static let staticLet = [ 200, 201, 202 ]
3030
public static var staticVar = [ 300, 301, 302 ]
31+
public static var twoDimensional = [[1, 2], [3, 4], [5, 6]]
3132
}
3233

3334
@inline(never)
@@ -72,6 +73,9 @@ print(Str.staticLet)
7273
// CHECK-OUTPUT: [300, 301, 302]
7374
print(Str.staticVar)
7475

76+
// CHECK-OUTPUT{LITERAL}: [[1, 2], [3, 4], [5, 6]]
77+
print(Str.twoDimensional)
78+
7579
// CHECK-OUTPUT-NEXT: 11
7680
print(arrayLookup(1))
7781

0 commit comments

Comments
 (0)