Skip to content

Commit b76047a

Browse files
authored
Merge pull request #80930 from meg-gupta/relandpr
Reland #79707
2 parents 009d612 + d81b5f7 commit b76047a

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

lib/SILOptimizer/Mandatory/OSLogOptimization.cpp

+10-1
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

+4-8
Original file line numberDiff line numberDiff line change
@@ -3042,8 +3042,7 @@ extension UnsignedInteger where Self: FixedWidthInteger {
30423042
/// - Parameter source: A value to convert to this type of integer. The value
30433043
/// passed as `source` must be representable in this type.
30443044
@_semantics("optimize.sil.specialize.generic.partial.never")
3045-
@inlinable // FIXME(inline-always)
3046-
@inline(__always)
3045+
@_transparent
30473046
public init<T: BinaryInteger>(_ source: T) {
30483047
// This check is potentially removable by the optimizer
30493048
if T.isSigned {
@@ -3058,8 +3057,7 @@ extension UnsignedInteger where Self: FixedWidthInteger {
30583057
}
30593058

30603059
@_semantics("optimize.sil.specialize.generic.partial.never")
3061-
@inlinable // FIXME(inline-always)
3062-
@inline(__always)
3060+
@_transparent
30633061
public init?<T: BinaryInteger>(exactly source: T) {
30643062
// This check is potentially removable by the optimizer
30653063
if T.isSigned && source < (0 as T) {
@@ -3257,8 +3255,7 @@ extension SignedInteger where Self: FixedWidthInteger {
32573255
/// - Parameter source: A value to convert to this type of integer. The value
32583256
/// passed as `source` must be representable in this type.
32593257
@_semantics("optimize.sil.specialize.generic.partial.never")
3260-
@inlinable // FIXME(inline-always)
3261-
@inline(__always)
3258+
@_transparent
32623259
public init<T: BinaryInteger>(_ source: T) {
32633260
// This check is potentially removable by the optimizer
32643261
if T.isSigned && source.bitWidth > Self.bitWidth {
@@ -3275,8 +3272,7 @@ extension SignedInteger where Self: FixedWidthInteger {
32753272
}
32763273

32773274
@_semantics("optimize.sil.specialize.generic.partial.never")
3278-
@inlinable // FIXME(inline-always)
3279-
@inline(__always)
3275+
@_transparent
32803276
public init?<T: BinaryInteger>(exactly source: T) {
32813277
// This check is potentially removable by the optimizer
32823278
if T.isSigned && source.bitWidth > Self.bitWidth && source < Self.min {

test/IRGen/integer_conversion.swift

+26
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

+1-3
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

+2-4
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

+1-2
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)