Skip to content

Commit 9c2c4ea

Browse files
authored
Merge pull request #79707 from DougGregor/transparent-integer-conversions
[Stdlib performance] Make integer conversion operations transparent
2 parents 829e03c + c2417e0 commit 9c2c4ea

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

lib/SILOptimizer/Mandatory/OSLogOptimization.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,16 @@ static void substituteConstants(FoldState &foldState) {
938938
for (SILValue constantSILValue : foldState.getConstantSILValues()) {
939939
SymbolicValue constantSymbolicVal =
940940
evaluator.lookupConstValue(constantSILValue).value();
941+
CanType instType = constantSILValue->getType().getASTType();
942+
943+
// If the SymbolicValue is a string but the instruction that is folded is
944+
// not String typed, we are tracking a StaticString which is represented as
945+
// a raw pointer. Skip folding StaticString as they are already efficiently
946+
// represented.
947+
if (constantSymbolicVal.getKind() == SymbolicValue::String &&
948+
!instType->isString())
949+
continue;
950+
941951
// Make sure that the symbolic value tracked in the foldState is a constant.
942952
// In the case of ArraySymbolicValue, the array storage could be a non-constant
943953
// if some instruction in the array initialization sequence was not evaluated
@@ -976,7 +986,6 @@ static void substituteConstants(FoldState &foldState) {
976986

977987
SILBuilderWithScope builder(insertionPoint);
978988
SILLocation loc = insertionPoint->getLoc();
979-
CanType instType = constantSILValue->getType().getASTType();
980989
SILValue foldedSILVal = emitCodeForSymbolicValue(
981990
constantSymbolicVal, instType, builder, loc, foldState.stringInfo);
982991

stdlib/public/core/Integers.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,8 +3040,7 @@ extension UnsignedInteger where Self: FixedWidthInteger {
30403040
/// - Parameter source: A value to convert to this type of integer. The value
30413041
/// passed as `source` must be representable in this type.
30423042
@_semantics("optimize.sil.specialize.generic.partial.never")
3043-
@inlinable // FIXME(inline-always)
3044-
@inline(__always)
3043+
@_transparent
30453044
public init<T: BinaryInteger>(_ source: T) {
30463045
// This check is potentially removable by the optimizer
30473046
if T.isSigned {
@@ -3056,8 +3055,7 @@ extension UnsignedInteger where Self: FixedWidthInteger {
30563055
}
30573056

30583057
@_semantics("optimize.sil.specialize.generic.partial.never")
3059-
@inlinable // FIXME(inline-always)
3060-
@inline(__always)
3058+
@_transparent
30613059
public init?<T: BinaryInteger>(exactly source: T) {
30623060
// This check is potentially removable by the optimizer
30633061
if T.isSigned && source < (0 as T) {
@@ -3255,8 +3253,7 @@ extension SignedInteger where Self: FixedWidthInteger {
32553253
/// - Parameter source: A value to convert to this type of integer. The value
32563254
/// passed as `source` must be representable in this type.
32573255
@_semantics("optimize.sil.specialize.generic.partial.never")
3258-
@inlinable // FIXME(inline-always)
3259-
@inline(__always)
3256+
@_transparent
32603257
public init<T: BinaryInteger>(_ source: T) {
32613258
// This check is potentially removable by the optimizer
32623259
if T.isSigned && source.bitWidth > Self.bitWidth {
@@ -3273,8 +3270,7 @@ extension SignedInteger where Self: FixedWidthInteger {
32733270
}
32743271

32753272
@_semantics("optimize.sil.specialize.generic.partial.never")
3276-
@inlinable // FIXME(inline-always)
3277-
@inline(__always)
3273+
@_transparent
32783274
public init?<T: BinaryInteger>(exactly source: T) {
32793275
// This check is potentially removable by the optimizer
32803276
if T.isSigned && source.bitWidth > Self.bitWidth && source < Self.min {

test/IRGen/integer_conversion.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %target-swift-frontend -primary-file %s -emit-ir | %FileCheck %s
2+
// RUN: %target-swift-frontend -primary-file %s -O -emit-ir | %FileCheck %s
3+
// REQUIRES: CPU=x86_64 || CPU=arm64
4+
5+
// https://github.com/swiftlang/swift/issues/78501
6+
public struct PcgRandom {
7+
private var state: UInt64 = 0;
8+
9+
// CHECK-LABEL: define{{.*}}swiftcc i32 @"$s18integer_conversion9PcgRandomV6next32s6UInt32VyF"
10+
public mutating func next32() -> UInt32 {
11+
// CHECK-NOT: sSUss17FixedWidthIntegerRzrlEyxqd__cSzRd__lufC
12+
// CHECK-NOT: sSZss17FixedWidthIntegerRzrlEyxqd__cSzRd__lufC
13+
// CHECK: ret i32
14+
let oldstate : UInt64 = state
15+
state = oldstate &* 6364136223846793005 &+ 1;
16+
let shifted = oldstate >> 18
17+
let xor = shifted ^ oldstate
18+
let xorshifted64 = xor >> 27
19+
let xorshifted = UInt32((xorshifted64 << 32) >> 32)
20+
let rot : UInt32 = UInt32(oldstate >> 59)
21+
let nrot : UInt32 = UInt32(bitPattern: -Int32(rot))
22+
return (xorshifted >> rot) | (xorshifted << (nrot & 31))
23+
}
24+
25+
init() {}
26+
}

test/Interop/Cxx/templates/function-template-silgen.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ import FunctionTemplates
1616
// CHECK: [[ADD_TWO_FN:%.*]] = function_ref @{{_Z18addMixedTypeParamsIiiET_S0_T0_|\?\?\$addMixedTypeParams@HH@@YAHHH@Z}} : $@convention(c) (Int32, Int32) -> Int32
1717
// CHECK: [[C:%.*]] = apply [[ADD_TWO_FN]]([[A]], [[B]]) : $@convention(c) (Int32, Int32) -> Int32
1818

19-
// CHECK: [[C_32_ADDR:%.*]] = alloc_stack $Int32
20-
// CHECK: [[C_32:%.*]] = load [[C_32_ADDR]] : $*Int32
2119
// CHECK: [[ADD_FN:%.*]] = function_ref @{{_Z17addSameTypeParamsIiET_S0_S0_|\?\?\$addSameTypeParams@H@@YAHHH@Z}} : $@convention(c) (Int32, Int32) -> Int32
22-
// CHECK: [[OUT:%.*]] = apply [[ADD_FN]]([[B]], [[C_32]]) : $@convention(c) (Int32, Int32) -> Int32
20+
// CHECK: [[OUT:%.*]] = apply [[ADD_FN]]([[B]], [[C_32:%.*]]) : $@convention(c) (Int32, Int32) -> Int32
2321
// CHECK: return [[OUT]] : $Int32
2422

2523
// CHECK-LABEL: end sil function '$s4main4test1xs5Int32VAE_tF'

test/SILOptimizer/Inputs/constant_evaluable.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ internal func interpretIntTruncations() -> Int8 {
174174
internal func testInvalidIntTruncations(a: Int32) -> Int8 {
175175
return Int8(a)
176176
// CHECK: note: {{.*}}: Not enough bits to represent the passed value
177-
// CHECK: note: operation performed during this call traps
178-
// CHECK: function_ref @$sSZss17FixedWidthIntegerRzrlEyxqd__cSzRd__lufC
177+
// CHECK: note: operation traps
179178
}
180179

181180
@_semantics("test_driver")
@@ -220,8 +219,7 @@ internal func interpretSingedUnsignedConversions() -> UInt32 {
220219
internal func testInvalidSingedUnsignedConversions(a: Int64) -> UInt64 {
221220
return UInt64(a)
222221
// CHECK: note: {{.*}}: Negative value is not representable
223-
// CHECK: note: operation performed during this call traps
224-
// CHECK: function_ref @$sSUss17FixedWidthIntegerRzrlEyxqd__cSzRd__lufC
222+
// CHECK: note: operation traps
225223
}
226224

227225
@_semantics("test_driver")

test/SILOptimizer/constant_evaluable_subset_test_arch64.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ internal func interpretIntTruncations() -> Int16 {
7171
internal func testInvalidIntTruncations(a: Int64) -> Int8 {
7272
return Int8(a)
7373
// CHECK: note: {{.*}} Not enough bits to represent the passed value
74-
// CHECK: note: operation performed during this call traps
75-
// CHECK: function_ref @$sSZss17FixedWidthIntegerRzrlEyxqd__cSzRd__lufC
74+
// CHECK: note: operation traps
7675
}
7776

7877
@_semantics("test_driver")

0 commit comments

Comments
 (0)