Skip to content

Commit a76beff

Browse files
committed
SILGen: use vector_base_addr in InlineArray literals
1 parent 77cda02 commit a76beff

File tree

5 files changed

+74
-41
lines changed

5 files changed

+74
-41
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private func lowerInlineArray(array: InlineArray, _ context: FunctionPassContext
175175
///
176176
private func getInlineArrayInfo(of allocStack: AllocStackInst) -> InlineArray? {
177177
var arrayLoad: LoadInst? = nil
178-
var elementStorage: UncheckedAddrCastInst? = nil
178+
var elementStorage: VectorBaseAddrInst? = nil
179179

180180
for use in allocStack.uses {
181181
switch use.instruction {
@@ -188,11 +188,11 @@ private func getInlineArrayInfo(of allocStack: AllocStackInst) -> InlineArray? {
188188
arrayLoad = load
189189
case is DeallocStackInst:
190190
break
191-
case let addrCastToElement as UncheckedAddrCastInst:
191+
case let baseAddr as VectorBaseAddrInst:
192192
if elementStorage != nil {
193193
return nil
194194
}
195-
elementStorage = addrCastToElement
195+
elementStorage = baseAddr
196196
default:
197197
return nil
198198
}

lib/SILGen/SILGenExpr.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4992,7 +4992,7 @@ static RValue emitInlineArrayLiteral(SILGenFunction &SGF, CollectionExpr *E,
49924992
SGFContext C) {
49934993
ArgumentScope scope(SGF, E);
49944994

4995-
auto iaTy = E->getType()->castTo<BoundGenericType>();
4995+
auto iaTy = E->getType()->castTo<BoundGenericStructType>();
49964996
auto loweredIAType = SGF.getLoweredType(iaTy);
49974997

49984998
// If this is an empty InlineArray literal and it's loadable, then create an
@@ -5007,9 +5007,19 @@ static RValue emitInlineArrayLiteral(SILGenFunction &SGF, CollectionExpr *E,
50075007
auto elementType = iaTy->getGenericArgs()[1]->getCanonicalType();
50085008
auto &eltTL = SGF.getTypeLowering(AbstractionPattern::getOpaque(), elementType);
50095009

5010+
5011+
auto *arrayDecl = cast<StructDecl>(iaTy->getDecl());
5012+
VarDecl *storageProperty = nullptr;
5013+
for (VarDecl *property : arrayDecl->getStoredProperties()) {
5014+
if ((property->getTypeInContext()->is<BuiltinFixedArrayType>())) {
5015+
storageProperty = property;
5016+
break;
5017+
}
5018+
}
5019+
50105020
SILValue alloc = SGF.emitTemporaryAllocation(E, loweredIAType);
5011-
SILValue addr = SGF.B.createUncheckedAddrCast(E, alloc,
5012-
eltTL.getLoweredType().getAddressType());
5021+
SILValue storage = SGF.B.createStructElementAddr(E, alloc, storageProperty);
5022+
SILValue addr = SGF.B.createVectorBaseAddr(E, storage);
50135023

50145024
// Cleanups for any elements that have been initialized so far.
50155025
SmallVector<CleanupHandle, 8> cleanups;

test/SILGen/inlinearray_literal.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func emptyNoncopyable() -> InlineArray<0, Atomic<Int>> {
3737

3838
// CHECK-LABEL: sil{{.*}} @$s19inlinearray_literal7trivials11InlineArrayVy$3_SiGyF : $@convention(thin) () -> InlineArray<4, Int> {
3939
// CHECK: [[SLAB_ALLOC:%.*]] = alloc_stack $InlineArray<4, Int>
40-
// CHECK-NEXT: [[ELEMENT_PTR:%.*]] = unchecked_addr_cast [[SLAB_ALLOC]] to $*Int
40+
// CHECK-NEXT: [[SE:%.*]] = struct_element_addr [[SLAB_ALLOC]], #InlineArray._storage
41+
// CHECK-NEXT: [[ELEMENT_PTR:%.*]] = vector_base_addr [[SE]]
4142
// CHECK-NEXT: [[ELT_0_LITERAL:%.*]] = integer_literal $Builtin.IntLiteral, 1
4243
// CHECK: [[ELT_0:%.*]] = apply {{%.*}}([[ELT_0_LITERAL]], {{%.*}}) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int
4344
// CHECK-NEXT: store [[ELT_0]] to [trivial] [[ELEMENT_PTR]]
@@ -66,7 +67,8 @@ func trivial() -> InlineArray<4, Int> {
6667

6768
// CHECK-LABEL: sil{{.*}} @$s19inlinearray_literal10nontrivials11InlineArrayVy$1_SSGyF : $@convention(thin) () -> @owned InlineArray<2, String> {
6869
// CHECK: [[SLAB_ALLOC:%.*]] = alloc_stack $InlineArray<2, String>
69-
// CHECK-NEXT: [[ELEMENT_PTR:%.*]] = unchecked_addr_cast [[SLAB_ALLOC]] to $*String
70+
// CHECK-NEXT: [[SE:%.*]] = struct_element_addr [[SLAB_ALLOC]], #InlineArray._storage
71+
// CHECK-NEXT: [[ELEMENT_PTR:%.*]] = vector_base_addr [[SE]]
7072
// CHECK-NEXT: [[ELT_0_LITERAL:%.*]] = string_literal utf8 "hello"
7173
// CHECK: [[ELT_0:%.*]] = apply {{%.*}}([[ELT_0_LITERAL]], {{.*}}) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
7274
// CHECK-NEXT: store [[ELT_0]] to [init] [[ELEMENT_PTR]]
@@ -86,7 +88,8 @@ func nontrivial() -> InlineArray<2, String> {
8688
// CHECK-LABEL: sil{{.*}} @$s19inlinearray_literal11noncopyables11InlineArrayVy$1_15Synchronization6AtomicVySiGGyF : $@convention(thin) () -> @out InlineArray<2, Atomic<Int>> {
8789
// CHECK: bb0([[SLAB_RETURN:%.*]] : $*InlineArray<2, Atomic<Int>>):
8890
// CHECK-NEXT: [[SLAB_ALLOC:%.*]] = alloc_stack $InlineArray<2, Atomic<Int>>
89-
// CHECK-NEXT: [[ELEMENT_PTR:%.*]] = unchecked_addr_cast [[SLAB_ALLOC]] to $*Atomic<Int>
91+
// CHECK-NEXT: [[SE:%.*]] = struct_element_addr [[SLAB_ALLOC]], #InlineArray._storage
92+
// CHECK-NEXT: [[ELEMENT_PTR:%.*]] = vector_base_addr [[SE]]
9093
// CHECK: [[ATOMIC_INIT:%.*]] = function_ref @$s15Synchronization6AtomicVyACyxGxcfC
9194
// CHECK-NEXT: [[ELT_0:%.*]] = apply [[ATOMIC_INIT]]<Int>([[ELEMENT_PTR]], {{.*}}) : $@convention(method) <τ_0_0 where τ_0_0 : AtomicRepresentable> (@in τ_0_0, @thin Atomic<τ_0_0>.Type) -> @out Atomic<τ_0_0>
9295
// CHECK: [[ELT_1_OFFSET:%.*]] = integer_literal $Builtin.Word, 1
@@ -109,10 +112,11 @@ func noncopyable() -> InlineArray<2, Atomic<Int>> {
109112

110113
// CHECK-LABEL: sil{{.*}} @$s19inlinearray_literal7closures11InlineArrayVy$0_S2icGyF : $@convention(thin) () -> @owned InlineArray<1, (Int) -> Int> {
111114
// CHECK: [[IA_ALLOC:%.*]] = alloc_stack $InlineArray<1, (Int) -> Int>
112-
// CHECK-NEXT: [[ADDR_CAST:%.*]] = unchecked_addr_cast [[IA_ALLOC]] to $*@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Int, Int>
115+
// CHECK-NEXT: [[SE:%.*]] = struct_element_addr [[IA_ALLOC]], #InlineArray._storage
116+
// CHECK-NEXT: [[ELEMENT_PTR:%.*]] = vector_base_addr [[SE]]
113117
// CHECK: [[FN_REF:%.*]] = function_ref
114118
// CHECK-NEXT: [[THIN_TO_THICK_FN:%.*]] = thin_to_thick_function [[FN_REF]] to $@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Int, Int>
115-
// CHECK-NEXT: store [[THIN_TO_THICK_FN]] to [init] [[ADDR_CAST]]
119+
// CHECK-NEXT: store [[THIN_TO_THICK_FN]] to [init] [[ELEMENT_PTR]]
116120
// CHECK-NEXT: [[IA:%.*]] = load [take] [[IA_ALLOC]]
117121
// CHECK-NEXT: dealloc_stack [[IA_ALLOC]]
118122
// CHECK-NEXT: return [[IA]]

test/SILOptimizer/init_static_globals.sil

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ sil_global [let] @g9 : $TwoFields
107107
// CHECK-NEXT: %3 = struct $Int32 (%2)
108108
// CHECK-NEXT: %4 = integer_literal $Builtin.Int32, 3
109109
// CHECK-NEXT: %5 = struct $Int32 (%4)
110-
// CHECK-NEXT: %initval = vector (%1, %3, %5)
110+
// CHECK-NEXT: %6 = vector (%1, %3, %5)
111+
// CHECK-NEXT: %initval = struct $InlineArray<3, Int32> (%6)
111112
// CHECK-NEXT: }
112113
sil_global [let] @inline_array1 : $InlineArray<3, Int32>
113114

@@ -127,7 +128,8 @@ sil_global [let] @inline_array1 : $InlineArray<3, Int32>
127128
// CHECK-NEXT: %12 = integer_literal $Builtin.Int32, 60
128129
// CHECK-NEXT: %13 = struct $Int32 (%12)
129130
// CHECK-NEXT: %14 = tuple (%11, %13)
130-
// CHECK-NEXT: %initval = vector (%4, %9, %14)
131+
// CHECK-NEXT: %15 = vector (%4, %9, %14)
132+
// CHECK-NEXT: %initval = struct $InlineArray<3, (Int32, Int32)> (%15)
131133
// CHECK-NEXT: }
132134
sil_global [let] @inline_array2 : $InlineArray<3, (Int32, Int32)>
133135

@@ -357,8 +359,8 @@ sil [global_init_once_fn] [ossa] @globalinit_inline_array : $@convention(c) (Bui
357359
bb0(%0 : $Builtin.RawPointer):
358360
alloc_global @inline_array1
359361
%2 = global_addr @inline_array1 : $*InlineArray<3, Int32>
360-
%3 = alloc_stack $InlineArray<3, Int32>
361-
%4 = unchecked_addr_cast %3 to $*Int32
362+
%3 = alloc_stack $Builtin.FixedArray<3, Int32>
363+
%4 = vector_base_addr %3
362364
%5 = integer_literal $Builtin.Int32, 1
363365
%6 = struct $Int32 (%5)
364366
store %6 to [trivial] %4
@@ -373,8 +375,9 @@ bb0(%0 : $Builtin.RawPointer):
373375
%16 = struct $Int32 (%15)
374376
store %16 to [trivial] %14
375377
%18 = load [trivial] %3
378+
%19 = struct $InlineArray<3, Int32> (%18)
376379
dealloc_stack %3
377-
store %18 to [trivial] %2
380+
store %19 to [trivial] %2
378381
%21 = tuple ()
379382
return %21
380383
}
@@ -388,8 +391,8 @@ sil [global_init_once_fn] [ossa] @globalinit_inline_array_of_tuples : $@conventi
388391
bb0(%0 : $Builtin.RawPointer):
389392
alloc_global @inline_array2
390393
%2 = global_addr @inline_array2 : $*InlineArray<3, (Int32, Int32)>
391-
%3 = alloc_stack $InlineArray<3, (Int32, Int32)>
392-
%4 = unchecked_addr_cast %3 to $*(Int32, Int32)
394+
%3 = alloc_stack $Builtin.FixedArray<3, (Int32, Int32)>
395+
%4 = vector_base_addr %3
393396
%5 = tuple_element_addr %4, 0
394397
%6 = tuple_element_addr %4, 1
395398
%7 = integer_literal $Builtin.Int32, 10
@@ -419,8 +422,9 @@ bb0(%0 : $Builtin.RawPointer):
419422
%31 = struct $Int32 (%30)
420423
store %31 to [trivial] %26
421424
%33 = load [trivial] %3
425+
%34 = struct $InlineArray<3, (Int32, Int32)> (%33)
422426
dealloc_stack %3
423-
store %33 to [trivial] %2
427+
store %34 to [trivial] %2
424428
%36 = tuple ()
425429
return %36
426430
}
@@ -434,8 +438,8 @@ sil [global_init_once_fn] [ossa] @no_globalinit_double_store: $@convention(c) (B
434438
bb0(%0 : $Builtin.RawPointer):
435439
alloc_global @inline_array3
436440
%2 = global_addr @inline_array3 : $*InlineArray<2, Int32>
437-
%3 = alloc_stack $InlineArray<2, Int32>
438-
%4 = unchecked_addr_cast %3 to $*Int32
441+
%3 = alloc_stack $Builtin.FixedArray<2, Int32>
442+
%4 = vector_base_addr %3
439443
%5 = integer_literal $Builtin.Int32, 1
440444
%6 = struct $Int32 (%5)
441445
store %6 to [trivial] %4
@@ -446,8 +450,9 @@ bb0(%0 : $Builtin.RawPointer):
446450
store %11 to [trivial] %9
447451
store %6 to [trivial] %9
448452
%18 = load [trivial] %3
453+
%19 = struct $InlineArray<2, Int32> (%18)
449454
dealloc_stack %3
450-
store %18 to [trivial] %2
455+
store %19 to [trivial] %2
451456
%21 = tuple ()
452457
return %21
453458
}
@@ -461,8 +466,8 @@ sil [global_init_once_fn] [ossa] @no_globalinit_extra_load: $@convention(c) (Bui
461466
bb0(%0 : $Builtin.RawPointer):
462467
alloc_global @inline_array4
463468
%2 = global_addr @inline_array4 : $*InlineArray<2, Int32>
464-
%3 = alloc_stack $InlineArray<2, Int32>
465-
%4 = unchecked_addr_cast %3 to $*Int32
469+
%3 = alloc_stack $Builtin.FixedArray<2, Int32>
470+
%4 = vector_base_addr %3
466471
%5 = integer_literal $Builtin.Int32, 1
467472
%6 = struct $Int32 (%5)
468473
store %6 to [trivial] %4
@@ -471,8 +476,9 @@ bb0(%0 : $Builtin.RawPointer):
471476
%10 = load [trivial] %4
472477
store %10 to [trivial] %9
473478
%18 = load [trivial] %3
479+
%19 = struct $InlineArray<2, Int32> (%18)
474480
dealloc_stack %3
475-
store %18 to [trivial] %2
481+
store %19 to [trivial] %2
476482
%21 = tuple ()
477483
return %21
478484
}
@@ -486,8 +492,8 @@ sil [global_init_once_fn] [ossa] @no_globalinit_no_load: $@convention(c) (Builti
486492
bb0(%0 : $Builtin.RawPointer):
487493
alloc_global @gint
488494
%2 = global_addr @gint : $*Int32
489-
%3 = alloc_stack $InlineArray<2, Int32>
490-
%4 = unchecked_addr_cast %3 to $*Int32
495+
%3 = alloc_stack $Builtin.FixedArray<2, Int32>
496+
%4 = vector_base_addr %3
491497
%5 = integer_literal $Builtin.Int32, 1
492498
%6 = struct $Int32 (%5)
493499
store %6 to [trivial] %4
@@ -511,11 +517,12 @@ sil [global_init_once_fn] [ossa] @no_globalinit_empty_inline_array : $@conventio
511517
bb0(%0 : $Builtin.RawPointer):
512518
alloc_global @empty_inline_array
513519
%2 = global_addr @empty_inline_array : $*InlineArray<0, Int32>
514-
%3 = alloc_stack $InlineArray<0, Int32>
515-
%4 = unchecked_addr_cast %3 to $*Int32
520+
%3 = alloc_stack $Builtin.FixedArray<0, Int32>
521+
%4 = vector_base_addr %3
516522
%18 = load [trivial] %3
523+
%19 = struct $InlineArray<0, Int32> (%18)
517524
dealloc_stack %3
518-
store %18 to [trivial] %2
525+
store %19 to [trivial] %2
519526
%21 = tuple ()
520527
return %21
521528
}
@@ -529,15 +536,16 @@ sil [global_init_once_fn] [ossa] @no_globalinit_inline_array_empty_elements : $@
529536
bb0(%0 : $Builtin.RawPointer):
530537
alloc_global @inline_array_empty_elements
531538
%2 = global_addr @inline_array_empty_elements : $*InlineArray<3, ()>
532-
%3 = alloc_stack $InlineArray<3, ()>
533-
%4 = unchecked_addr_cast %3 to $*()
539+
%3 = alloc_stack $Builtin.FixedArray<3, ()>
540+
%4 = vector_base_addr %3
534541
%13 = integer_literal $Builtin.Word, 2
535542
%14 = index_addr %4, %13
536543
%15 = tuple ()
537544
store %15 to [trivial] %14
538545
%18 = load [trivial] %3
546+
%19 = struct $InlineArray<3, ()> (%18)
539547
dealloc_stack %3
540-
store %18 to [trivial] %2
548+
store %19 to [trivial] %2
541549
%21 = tuple ()
542550
return %21
543551
}

test/SILOptimizer/static_inline_arrays.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,65 @@ struct IntByteAndByte {
2525

2626
struct S {
2727
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV6simples11InlineArrayVy$2_SiGvpZ : $InlineArray<3, Int> = {
28-
// CHECK: %initval = vector
28+
// CHECK: [[V:%.*]] = vector
29+
// CHECK: %initval = struct $InlineArray<3, Int> ([[V]])
2930
// CHECK: }
3031
static let simple: InlineArray = [1, 2, 3]
3132

3233
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV12optionalIntss11InlineArrayVy$2_SiSgGvpZ : $InlineArray<3, Optional<Int>> = {
33-
// CHECK: %initval = vector
34+
// CHECK: [[V:%.*]] = vector
35+
// CHECK: %initval = struct $InlineArray<3, Optional<Int>> ([[V]])
3436
// CHECK: }
3537
static let optionalInts: InlineArray<_, Int?> = [10, 20, 30]
3638

3739
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV13optionalArrays06InlineC0Vy$2_SiGSgvpZ : $Optional<InlineArray<3, Int>> = {
3840
// CHECK: [[V:%.*]] = vector
39-
// CHECK: %initval = enum $Optional<InlineArray<3, Int>>, #Optional.some!enumelt, [[V]]
41+
// CHECK: [[A:%.*]] = struct $InlineArray<3, Int> ([[V]])
42+
// CHECK: %initval = enum $Optional<InlineArray<3, Int>>, #Optional.some!enumelt, [[A]]
4043
// CHECK: }
4144
static let optionalArray: InlineArray? = [1, 2, 3]
4245

4346
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV12intBytePairss11InlineArrayVy$2_AA03IntC0VGvpZ : $InlineArray<3, IntByte> = {
4447
// CHECK: [[S0:%.*]] = struct $IntByte
4548
// CHECK: [[S1:%.*]] = struct $IntByte
4649
// CHECK: [[S2:%.*]] = struct $IntByte
47-
// CHECK: %initval = vector ([[S0]], [[S1]], [[S2]])
50+
// CHECK: [[V:%.*]] = vector ([[S0]], [[S1]], [[S2]])
51+
// CHECK: %initval = struct $InlineArray<3, IntByte> ([[V]])
4852
// CHECK: }
4953
static let intBytePairs: InlineArray<_, IntByte> = [IntByte(i: 1, b: 2), IntByte(i: 3, b: 4), IntByte(i: 5, b: 6)]
5054

5155
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV26optionalInlineArrayOfPairss0cD0Vy$2_AA7IntByteVGSgvpZ : $Optional<InlineArray<3, IntByte>> = {
5256
// CHECK: [[V:%.*]] = vector
53-
// CHECK: %initval = enum $Optional<InlineArray<3, IntByte>>, #Optional.some!enumelt, [[V]]
57+
// CHECK: [[A:%.*]] = struct $InlineArray<3, IntByte> ([[V]])
58+
// CHECK: %initval = enum $Optional<InlineArray<3, IntByte>>, #Optional.some!enumelt, [[A]]
5459
// CHECK: }
5560
static let optionalInlineArrayOfPairs: InlineArray<_, IntByte>? = [IntByte(i: 11, b: 12), IntByte(i: 13, b: 14), IntByte(i: 15, b: 16)]
5661

5762
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV6tupless11InlineArrayVy$2_Si_SitGvpZ : $InlineArray<3, (Int, Int)> = {
5863
// CHECK: [[T0:%.*]] = tuple
5964
// CHECK: [[T1:%.*]] = tuple
6065
// CHECK: [[T2:%.*]] = tuple
61-
// CHECK: %initval = vector ([[T0]], [[T1]], [[T2]])
66+
// CHECK: [[V:%.*]] = vector ([[T0]], [[T1]], [[T2]])
67+
// CHECK: %initval = struct $InlineArray<3, (Int, Int)> ([[V]])
6268
// CHECK: }
6369
static let tuples: InlineArray = [(10, 20), (30, 40), (50, 60)]
6470

6571
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV6nesteds11InlineArrayVy$2_AFy$1_SiGGvpZ : $InlineArray<3, InlineArray<2, Int>> = {
6672
// CHECK: [[V0:%.*]] = vector
73+
// CHECK: [[A0:%.*]] = struct $InlineArray<2, Int> ([[V0]])
6774
// CHECK: [[V1:%.*]] = vector
75+
// CHECK: [[A1:%.*]] = struct $InlineArray<2, Int> ([[V1]])
6876
// CHECK: [[V2:%.*]] = vector
69-
// CHECK: %initval = vector ([[V0]], [[V1]], [[V2]])
77+
// CHECK: [[A2:%.*]] = struct $InlineArray<2, Int> ([[V2]])
78+
// CHECK: [[V:%.*]] = vector ([[A0]], [[A1]], [[A2]])
79+
// CHECK: %initval = struct $InlineArray<3, InlineArray<2, Int>> ([[V]])
7080
// CHECK: }
7181
static let nested: InlineArray<3, InlineArray<2, Int>> = [[100, 200], [300, 400], [500, 600]]
7282

7383
// CHECK-LABEL: sil_global hidden [let] @$s4test1SV010intByteAndC0AA03IntcdC0VvpZ : $IntByteAndByte = {
7484
// CHECK: [[V:%.*]] = vector
75-
// CHECK: %initval = struct $IntByteAndByte ([[V]],
85+
// CHECK: [[A:%.*]] = struct $InlineArray<3, IntByte> ([[V]])
86+
// CHECK: %initval = struct $IntByteAndByte ([[A]],
7687
// CHECK: }
7788
static let intByteAndByte = IntByteAndByte(a: [IntByte(i: 1, b: 2), IntByte(i: 3, b: 4), IntByte(i: 5, b: 6)], x: 27)
7889
}

0 commit comments

Comments
 (0)