// RUN: %target-sil-opt %s -alloc-vector-lowering -enable-experimental-feature FixedArrays | %FileCheck %s // REQUIRES: swift_in_compiler sil_stage canonical import Builtin import Swift public struct FixedArray: ~Copyable { @_hasStorage let buffer: UnsafeMutablePointer { get } @_hasStorage public let capacity: Int64 { get } @_hasStorage internal var _count: Int64 { get set } public struct _Literal { @_hasStorage let array: [T] { get } typealias ArrayLiteralElement = T } } sil_global @global_fixed_array : $FixedArray sil_global @global_fixed_array2 : $UnsafeMutablePointer // CHECK-LABEL: sil_global private @global_init_with_array_initializationTv_ : $Int64 = { // CHECK-NEXT: %0 = integer_literal $Builtin.Int64, 1 // CHECK-NEXT: %1 = struct $Int64 (%0 : $Builtin.Int64) // CHECK-NEXT: %2 = integer_literal $Builtin.Int64, 2 // CHECK-NEXT: %3 = struct $Int64 (%2 : $Builtin.Int64) // CHECK-NEXT: %initval = vector (%1 : $Int64, %3 : $Int64) // CHECK-LABEL: sil_global private @global_init_uninitializedTv_ : $Int64 = { // CHECK-NEXT: %0 = integer_literal $Builtin.Word, 10 // CHECK-NEXT: %initval = alloc_vector $Int64, %0 : $Builtin.Word sil [_semantics "array.uninitialized_intrinsic"] @alloc_uninitialized_array : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer) sil [_semantics "array.finalize_intrinsic"] @finalize_array : $@convention(thin) (@owned Array) -> @owned Array sil [_semantics "array.get_count"] @array_count : $@convention(method) <τ_0_0> (@guaranteed Array<τ_0_0>) -> Int64 sil [_semantics "array.copy_into_vector"] @array_copy_into_vector : $@convention(method) (UnsafeMutablePointer, @guaranteed Array) -> () // CHECK-LABEL: sil [ossa] @stack_allocated : // CHECK: [[C:%.*]] = integer_literal $Builtin.Word, 10 // CHECK: [[V:%.*]] = alloc_vector $Int64, [[C]] // CHECK: bb1: // CHECK: dealloc_stack {{.*}} : $*Builtin.RawPointer // CHECK: dealloc_stack [[V]] : $*Int64 // CHECK: bb2: // CHECK: dealloc_stack {{.*}} : $*Builtin.RawPointer // CHECK: dealloc_stack [[V]] : $*Int64 // CHECK: bb3: // CHECK: } // end sil function 'stack_allocated' sil [ossa] @stack_allocated : $@convention(thin) () -> () { %8 = integer_literal $Builtin.Word, 10 %9 = metatype $@thin Int64.Type %10 = builtin "allocVector"(%9 : $@thin Int64.Type, %8 : $Builtin.Word) : $Builtin.RawPointer %11 = alloc_stack $Builtin.RawPointer store %10 to [trivial] %11 : $*Builtin.RawPointer cond_br undef, bb1, bb2 bb1: dealloc_stack %11 : $*Builtin.RawPointer br bb3 bb2: dealloc_stack %11 : $*Builtin.RawPointer br bb3 bb3: %r = tuple() return %r : $() } // CHECK-LABEL: sil [global_init_once_fn] [ossa] @global_init_with_array_initialization : // CHECK: [[G:%.*]] = global_addr @global_init_with_array_initializationTv_ // CHECK: [[P:%.*]] = address_to_pointer [[G]] // CHECK: [[UMP:%.*]] = struct $UnsafeMutablePointer ([[P]] : $Builtin.RawPointer) // CHECK: [[BA:%.*]] = struct_element_addr {{%.*}} : $*FixedArray, #FixedArray.buffer // CHECK: store [[UMP]] to [trivial] [[BA]] // CHECK: } // end sil function 'global_init_with_array_initialization' sil [global_init_once_fn] [ossa] @global_init_with_array_initialization : $@convention(thin) () -> () { bb0: alloc_global @global_fixed_array %2 = global_addr @global_fixed_array : $*FixedArray %3 = integer_literal $Builtin.Word, 2 %4 = function_ref @alloc_uninitialized_array : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer) %5 = apply %4(%3) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer) (%6, %7) = destructure_tuple %5 : $(Array, Builtin.RawPointer) %8 = mark_dependence %7 : $Builtin.RawPointer on %6 : $Array %9 = pointer_to_address %8 : $Builtin.RawPointer to [strict] $*Int64 %10 = integer_literal $Builtin.Int64, 1 %11 = struct $Int64 (%10 : $Builtin.Int64) store %11 to [trivial] %9 : $*Int64 %13 = integer_literal $Builtin.Word, 1 %14 = index_addr %9 : $*Int64, %13 : $Builtin.Word %15 = integer_literal $Builtin.Int64, 2 %16 = struct $Int64 (%15 : $Builtin.Int64) store %16 to [trivial] %14 : $*Int64 %23 = function_ref @finalize_array : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0> %24 = apply %23(%6) : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0> %25 = copy_value %24 : $Array %34 = struct $FixedArray._Literal (%25 : $Array) destroy_value %34 : $FixedArray._Literal %36 = alloc_stack [lexical] $FixedArray %37 = function_ref @array_count : $@convention(method) <τ_0_0> (@guaranteed Array<τ_0_0>) -> Int64 %38 = apply %37(%24) : $@convention(method) <τ_0_0> (@guaranteed Array<τ_0_0>) -> Int64 %39 = begin_access [modify] [static] %36 : $*FixedArray %40 = struct_element_addr %39 : $*FixedArray, #FixedArray._count store %38 to [trivial] %40 : $*Int64 end_access %39 : $*FixedArray %43 = begin_access [modify] [static] %36 : $*FixedArray %44 = struct_element_addr %43 : $*FixedArray, #FixedArray.capacity store %38 to [trivial] %44 : $*Int64 end_access %43 : $*FixedArray %47 = begin_borrow %24 : $Array %48 = function_ref @array_count : $@convention(method) <τ_0_0> (@guaranteed Array<τ_0_0>) -> Int64 %49 = apply %48(%47) : $@convention(method) <τ_0_0> (@guaranteed Array<τ_0_0>) -> Int64 %50 = struct_extract %49 : $Int64, #Int64._value %52 = metatype $@thin Int64.Type %53 = builtin "allocVector"(%52 : $@thin Int64.Type, %50 : $Builtin.Int64) : $Builtin.RawPointer %54 = struct $UnsafeMutablePointer (%53 : $Builtin.RawPointer) %55 = function_ref @array_copy_into_vector : $@convention(method) <τ_0_0> (UnsafeMutablePointer<τ_0_0>, @guaranteed Array<τ_0_0>) -> () %56 = apply %55(%54, %47) : $@convention(method) <τ_0_0> (UnsafeMutablePointer<τ_0_0>, @guaranteed Array<τ_0_0>) -> () end_borrow %47 : $Array %58 = begin_access [modify] [static] %36 : $*FixedArray %59 = struct_element_addr %58 : $*FixedArray, #FixedArray.buffer store %54 to [trivial] %59 : $*UnsafeMutablePointer end_access %58 : $*FixedArray %62 = load [take] %36 : $*FixedArray destroy_value %24 : $Array dealloc_stack %36 : $*FixedArray store %62 to [init] %2 : $*FixedArray %67 = tuple () return %67 : $() } // CHECK-LABEL: sil [global_init_once_fn] [ossa] @global_init_uninitialized : // CHECK: [[A:%.*]] = alloc_stack $UnsafeMutablePointer // CHECK: [[G:%.*]] = global_addr @global_init_uninitializedTv_ // CHECK: [[P:%.*]] = address_to_pointer [[G]] // CHECK: [[UMP:%.*]] = struct $UnsafeMutablePointer ([[P]] : $Builtin.RawPointer) // CHECK: store [[UMP]] to [trivial] [[A]] // CHECK: } // end sil function 'global_init_uninitialized' sil [global_init_once_fn] [ossa] @global_init_uninitialized : $@convention(thin) () -> () { bb0: alloc_global @global_fixed_array2 %2 = global_addr @global_fixed_array2 : $*UnsafeMutablePointer %5 = alloc_stack $UnsafeMutablePointer %10 = integer_literal $Builtin.Word, 10 %11 = metatype $@thin Int64.Type %12 = builtin "allocVector"(%11 : $@thin Int64.Type, %10 : $Builtin.Word) : $Builtin.RawPointer %13 = struct $UnsafeMutablePointer (%12 : $Builtin.RawPointer) store %13 to [trivial] %5 : $*UnsafeMutablePointer %24 = load [trivial] %5 : $*UnsafeMutablePointer dealloc_stack %5 : $*UnsafeMutablePointer store %24 to [trivial] %2 : $*UnsafeMutablePointer %27 = tuple () return %27 : $() }