Skip to content

Commit 06b9271

Browse files
authored
[SILOpt] Represent _TrivialStride pre-specializations with vector types (#70938)
rdar://121071710 Currently it uses builtin integers, which round up to the next power of 2, which is not what we want here. Instead it should use builtin vectors of uint8 and a number of elements equal to the stride in bytes.
1 parent c80fbae commit 06b9271

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

include/swift/AST/LayoutConstraint.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ class LayoutConstraintInfo
157157
return 8*8;
158158
}
159159

160+
unsigned getTrivialStride() const {
161+
assert(isTrivialStride());
162+
return (SizeInBits + 7) / 8;
163+
}
164+
160165
unsigned getTrivialStrideInBits() const {
161166
assert(isTrivialStride());
162167
return SizeInBits;

lib/AST/GenericSignature.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,12 @@ GenericSignature GenericSignature::typeErased(ArrayRef<Type> typeErasedParams) c
528528
Requirement(RequirementKind::SameType, req.getFirstType(),
529529
CanType(BuiltinIntegerType::get(bitWidth, C))));
530530
} else if (layout->isTrivialStride()) {
531-
unsigned bitWidth = layout->getTrivialStrideInBits();
532531
requirementsErased.push_back(
533532
Requirement(RequirementKind::SameType, req.getFirstType(),
534-
CanType(BuiltinIntegerType::get(bitWidth, C))));
533+
CanType(BuiltinVectorType::get(
534+
Ptr->getASTContext(),
535+
BuiltinIntegerType::get(8, Ptr->getASTContext()),
536+
layout->getTrivialStride()))));
535537
} else {
536538
requirementsErased.push_back(req);
537539
}

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,9 +3053,10 @@ bool usePrespecialized(
30533053
stride = irgen::Size(1);
30543054

30553055
if (stride.getValueInBits() == layout->getTrivialStrideInBits()) {
3056-
newSubs.push_back(CanType(
3057-
BuiltinIntegerType::get(layout->getTrivialStrideInBits(),
3058-
genericParam->getASTContext())));
3056+
newSubs.push_back(CanType(BuiltinVectorType::get(
3057+
genericParam->getASTContext(),
3058+
BuiltinIntegerType::get(8, genericParam->getASTContext()),
3059+
layout->getTrivialStride())));
30593060
}
30603061
}
30613062
} else {

test/SILOptimizer/Inputs/pre_specialized_module_layouts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SomeClass {
1212
@_specialize(exported: true, where @_noMetadata T : _Class)
1313
@_specialize(exported: true, where @_noMetadata T : _BridgeObject)
1414
@_specialize(exported: true, where @_noMetadata T : _Trivial(64))
15-
@_specialize(exported: true, where @_noMetadata T : _TrivialStride(128))
15+
@_specialize(exported: true, where @_noMetadata T : _TrivialStride(96))
1616
@_specialize(exported: true, availability: macOS 10.50, *; where T == SomeData)
1717
public func publicPrespecialized<T>(_ t: T) {
1818
}

test/SILOptimizer/pre_specialize_layouts.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public struct TwoInt32 {
3636
let y: Int32 = 0
3737
}
3838

39-
public struct Stride128 {
40-
let x: Int64 = 0
41-
let y: Bool = false
39+
public struct Stride96 {
40+
let x: Int32 = 0
41+
let y: Int32 = 0
42+
let z: Bool = false
4243
}
4344

4445
// Make sure we generate the public pre-specialized entry points.
@@ -83,8 +84,8 @@ internal func testEmitIntoClient<T>(t: T) {
8384
print(t)
8485
}
8586

86-
// OPT: sil @$s22pre_specialize_layouts28usePrespecializedEntryPoints13wrapperStruct11overaligned5array9stride128yAA016ReferenceWrapperI0V_AA011OveralignedmnI0VSaySiGAA9Stride128VtF : $@convention(thin) (@guaranteed ReferenceWrapperStruct, @guaranteed OveralignedReferenceWrapperStruct, @guaranteed Array<Int>, Stride128) -> () {
87-
// OPT: bb0([[P1:%.*]] : $ReferenceWrapperStruct, [[P2:%.*]] : $OveralignedReferenceWrapperStruct, [[P3:%.*]] : $Array<Int>, [[P4:%.*]] : $Stride128):
87+
// OPT: sil @$s22pre_specialize_layouts28usePrespecializedEntryPoints13wrapperStruct11overaligned5array8stride96yAA016ReferenceWrapperI0V_AA011OveralignedmnI0VSaySiGAA8Stride96VtF : $@convention(thin) (@guaranteed ReferenceWrapperStruct, @guaranteed OveralignedReferenceWrapperStruct, @guaranteed Array<Int>, Stride96) -> () {
88+
// OPT: bb0([[P1:%.*]] : $ReferenceWrapperStruct, [[P2:%.*]] : $OveralignedReferenceWrapperStruct, [[P3:%.*]] : $Array<Int>, [[P4:%.*]] : $Stride96):
8889
// OPT: [[F1:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFSi_Ts5 : $@convention(thin) (Int) -> ()
8990
// OPT: apply [[F1]]
9091
// OPT: [[F2:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFSd_Ts5 : $@convention(thin) (Double) -> ()
@@ -106,16 +107,16 @@ internal func testEmitIntoClient<T>(t: T) {
106107
// OPT-macosx: [[F8:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFBb_Ts5 : $@convention(thin) (@guaranteed Builtin.BridgeObject) -> ()
107108
// OPT-macosx: [[A4:%.*]] = unchecked_bitwise_cast [[P3]] : $Array<Int> to $Builtin.BridgeObject
108109
// OPT-macosx: apply [[F8]]([[A4]]) : $@convention(thin) (@guaranteed Builtin.BridgeObject) -> ()
109-
// OPT: [[F10:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFBi128__Ts5 : $@convention(thin) (Builtin.Int128) -> ()
110-
// OPT: [[A6:%.*]] = unchecked_trivial_bit_cast [[P4]] : $Stride128 to $Builtin.Int128
111-
// OPT: apply [[F10]]([[A6]]) : $@convention(thin) (Builtin.Int128) -> ()
110+
// OPT: [[F10:%.*]] = function_ref @$s30pre_specialized_module_layouts20publicPrespecializedyyxlFBi8_Bv12__Ts5 : $@convention(thin) (Builtin.Vec12xInt8) -> ()
111+
// OPT: [[A6:%.*]] = unchecked_trivial_bit_cast [[P4]] : $Stride96 to $Builtin.Vec12xInt8
112+
// OPT: apply [[F10]]([[A6]]) : $@convention(thin) (Builtin.Vec12xInt8) -> ()
112113
// OPT: [[F3:%.*]] = function_ref @$s30pre_specialized_module_layouts36internalEmitIntoClientPrespecializedyyxlFSi_Ts5 : $@convention(thin) (Int) -> ()
113114
// OPT: apply [[F3]]
114115
// OPT: [[F4:%.*]] = function_ref @$s30pre_specialized_module_layouts36internalEmitIntoClientPrespecializedyyxlFSd_Ts5 : $@convention(thin) (Double) -> ()
115116
// OPT: apply [[F4]]
116117
// OPT: [[F5:%.*]] = function_ref @$s30pre_specialized_module_layouts16useInternalThingyyxlFSi_Tg5
117118
// OPT: apply [[F5]]({{.*}}) : $@convention(thin) (Int) -> ()
118-
// OPT: } // end sil function '$s22pre_specialize_layouts28usePrespecializedEntryPoints13wrapperStruct11overaligned5array9stride128yAA016ReferenceWrapperI0V_AA011OveralignedmnI0VSaySiGAA9Stride128VtF'
119+
// OPT: } // end sil function '$s22pre_specialize_layouts28usePrespecializedEntryPoints13wrapperStruct11overaligned5array8stride96yAA016ReferenceWrapperI0V_AA011OveralignedmnI0VSaySiGAA8Stride96VtF'
119120

120121
// OPT: sil {{.*}} @$s30pre_specialized_module_layouts16useInternalThingyyxlFSi_Tg5 : $@convention(thin) (Int) -> () {
121122
// OPT: [[F1:%.*]] = function_ref @$s30pre_specialized_module_layouts14InternalThing2V7computexyFSi_Ts5 : $@convention(method) (InternalThing2<Int>) -> Int
@@ -160,7 +161,7 @@ internal func testEmitIntoClient<T>(t: T) {
160161
// OPT: [[R10:%.*]] = unchecked_addr_cast [[R9]] : $*AnyObject to $*SomeClass
161162
// OPT: } // end sil function '$s30pre_specialized_module_layouts16useInternalThingyyxlFAA9SomeClassC_Tg5'
162163

163-
public func usePrespecializedEntryPoints(wrapperStruct: ReferenceWrapperStruct, overaligned: OveralignedReferenceWrapperStruct, array: [Int], stride128: Stride128) {
164+
public func usePrespecializedEntryPoints(wrapperStruct: ReferenceWrapperStruct, overaligned: OveralignedReferenceWrapperStruct, array: [Int], stride96: Stride96) {
164165
publicPrespecialized(1)
165166
publicPrespecialized(1.0)
166167
publicPrespecialized(UInt64(1))
@@ -171,7 +172,7 @@ public func usePrespecializedEntryPoints(wrapperStruct: ReferenceWrapperStruct,
171172
// should not apply _Class specialization for overaligned struct
172173
publicPrespecialized(overaligned)
173174
publicPrespecialized(array)
174-
publicPrespecialized(stride128)
175+
publicPrespecialized(stride96)
175176
useInternalEmitIntoClientPrespecialized(2)
176177
useInternalEmitIntoClientPrespecialized(2.0)
177178
useInternalThing(2)

0 commit comments

Comments
 (0)