diff --git a/SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift b/SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift index 805f2f2952415..feee0016ff518 100644 --- a/SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift +++ b/SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift @@ -59,16 +59,25 @@ let objectOutliner = FunctionPass(name: "object-outliner") { return } - for inst in function.instructions { - if let ari = inst as? AllocRefInstBase { - if !context.continueWithNextSubpassRun(for: inst) { + var allocRefs = Stack(context) + defer { allocRefs.deinitialize() } + + allocRefs.append(contentsOf: function.instructions.lazy.compactMap { $0 as? AllocRefInstBase }) + + // Try multiple iterations to handle multi-dimensional arrays. + var changed: Bool + repeat { + changed = false + for ari in allocRefs where !ari.isDeleted { + if !context.continueWithNextSubpassRun(for: ari) { return } if let globalValue = optimizeObjectAllocation(allocRef: ari, context) { optimizeFindStringCall(stringArray: globalValue, context) + changed = true } } - } + } while changed } private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: FunctionPassContext) -> GlobalValueInst? { diff --git a/test/SILOptimizer/readonly_arrays.swift b/test/SILOptimizer/readonly_arrays.swift index 85197abf910a0..509d206281164 100644 --- a/test/SILOptimizer/readonly_arrays.swift +++ b/test/SILOptimizer/readonly_arrays.swift @@ -17,7 +17,7 @@ // CHECK-DAG: @"$s4test3StrV9staticLet_WZTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} @_swiftImmortalRefCount // CHECK-DAG: @"$s4test3StrV9staticVar_WZTv_r" = {{.*}} constant {{.*}} @"$ss20__StaticArrayStorageCN", {{.*}} @_swiftImmortalRefCount // CHECK-DAG: @"$s4test3StrV9staticVarSaySiGvpZ" = global %TSa <{ %Ts12_ArrayBufferV <{ %Ts14_BridgeStorageV <{ ptr @"$s4test3StrV9staticVar_WZTv_r" }> }> }> -// CHECK-NOT: swift_initStaticObject +// CHECK-DAG: @"$s4test3StrV14twoDimensionalSaySaySiGGvpZ" = global %TSa <{ %Ts12_ArrayBufferV <{ %Ts14_BridgeStorageV <{ ptr @"$s4test3StrV14twoDimensional_WZTv{{[0-9]*}}_r" }> }> }>, align 8 // UNSUPPORTED: use_os_stdlib @@ -28,6 +28,7 @@ public struct Str { public static let staticLet = [ 200, 201, 202 ] public static var staticVar = [ 300, 301, 302 ] + public static var twoDimensional = [[1, 2], [3, 4], [5, 6]] } @inline(never) @@ -72,6 +73,9 @@ print(Str.staticLet) // CHECK-OUTPUT: [300, 301, 302] print(Str.staticVar) +// CHECK-OUTPUT{LITERAL}: [[1, 2], [3, 4], [5, 6]] +print(Str.twoDimensional) + // CHECK-OUTPUT-NEXT: 11 print(arrayLookup(1))