Skip to content

Commit 8b19bae

Browse files
Merge pull request #58892 from aschwaighofer/canonicalize_noescape_bit_for_thin_functions_5.7
[5.7] Cherry-pick pull request #42478 [DebugInfo] Ignore noescape bit for all @convention(c) pointers
2 parents fa6883a + c81120f commit 8b19bae

24 files changed

+89
-90
lines changed

include/swift/AST/ExtInfo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,6 @@ class ASTExtInfoBuilder {
358358

359359
constexpr Representation getRepresentation() const {
360360
unsigned rawRep = bits & RepresentationMask;
361-
assert(rawRep <= unsigned(Representation::Last) &&
362-
"unexpected SIL representation");
363361
return Representation(rawRep);
364362
}
365363

lib/AST/ASTContext.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/DiagnosticsSema.h"
2525
#include "swift/AST/DistributedDecl.h"
2626
#include "swift/AST/ExistentialLayout.h"
27+
#include "swift/AST/ExtInfo.h"
2728
#include "swift/AST/FileUnit.h"
2829
#include "swift/AST/ForeignAsyncConvention.h"
2930
#include "swift/AST/ForeignErrorConvention.h"
@@ -3683,6 +3684,16 @@ FunctionType *FunctionType::get(ArrayRef<AnyFunctionType::Param> params,
36833684
auto properties = getFunctionRecursiveProperties(params, result, globalActor);
36843685
auto arena = getArena(properties);
36853686

3687+
if (info.hasValue()) {
3688+
// Canonicalize all thin functions to be escaping (to keep compatibility
3689+
// with generic parameters). Note that one can pass SIL-level representation
3690+
// here, so we need additional check for maximum non-SIL value.
3691+
Representation rep = info.getValue().getRepresentation();
3692+
if (rep <= FunctionTypeRepresentation::Last &&
3693+
isThinRepresentation(rep))
3694+
info = info->withNoEscape(false);
3695+
}
3696+
36863697
llvm::FoldingSetNodeID id;
36873698
FunctionType::Profile(id, params, result, info);
36883699

@@ -4100,6 +4111,10 @@ CanSILFunctionType SILFunctionType::get(
41004111
ext = ext.intoBuilder().withClangFunctionType(nullptr).build();
41014112
}
41024113

4114+
// Canonicalize all thin functions to be escaping (to keep compatibility
4115+
// with generic parameters)
4116+
if (isThinRepresentation(ext.getRepresentation()))
4117+
ext = ext.intoBuilder().withNoEscape(false);
41034118

41044119
llvm::FoldingSetNodeID id;
41054120
SILFunctionType::Profile(id, genericSig, ext, coroutineKind, callee, params,

test/AutoDiff/SILOptimizer/activity_analysis.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,12 @@ func testTryApply(_ x: Float) -> Float {
552552
// CHECK: bb0:
553553
// CHECK: [ACTIVE] %0 = argument of bb0 : $Float
554554
// CHECK: [NONE] // function_ref closure #1 in testTryApply(_:)
555-
// CHECK: [NONE] %3 = convert_function %2 :
556-
// CHECK: [NONE] %4 = thin_to_thick_function %3 :
555+
// CHECK: [NONE] %3 = thin_to_thick_function %2 : $@convention(thin) () -> @error Error to $@noescape @callee_guaranteed () -> @error Error
557556
// CHECK: [NONE] // function_ref rethrowing(_:)
558557
// CHECK: bb1:
559-
// CHECK: [NONE] %7 = argument of bb1 : $()
558+
// CHECK: [NONE] %6 = argument of bb1 : $()
560559
// CHECK: bb2:
561-
// CHECK: [NONE] %9 = argument of bb2 : $Error
560+
// CHECK: [NONE] %8 = argument of bb2 : $Error
562561

563562
//===----------------------------------------------------------------------===//
564563
// Coroutine differentiation (`begin_apply`)

test/IRGen/objc_retainAutoreleasedReturnValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public func test(_ dict: NSDictionary) {
3131
// CHECK: notail call i8* @llvm.objc.retainAutoreleasedReturnValue
3232
// CHECK: ret void
3333

34-
// OPT-LABEL: define {{.*}}swiftcc void @"$s34objc_retainAutoreleasedReturnValue4testyySo12NSDictionaryCFyADXEfU_"(%TSo12NSDictionaryC* %0)
34+
// OPT-LABEL: define {{.*}}swiftcc void @"$s34objc_retainAutoreleasedReturnValue10useClosureyySo12NSDictionaryC_yADXEtF09$s34objc_bcd16Value4testyySo12H10CFyADXEfU_Tf1nc_n"(%TSo12NSDictionaryC* %0)
3535
// OPT: entry:
3636
// OPT: call {{.*}}@objc_msgSend
3737
// OPT: notail call i8* @llvm.objc.retainAutoreleasedReturnValue

test/PrintAsObjC/blocks.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
101101
) {
102102
}
103103

104-
// CHECK-NEXT: - (void (* _Nonnull)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(NSInteger, NSInteger)))returnsFunctionPointerThatTakesFunctionPointer SWIFT_WARN_UNUSED_RESULT;
104+
// CHECK-NEXT: - (void (* _Nonnull)(NSInteger (* _Nonnull)(NSInteger, NSInteger)))returnsFunctionPointerThatTakesFunctionPointer SWIFT_WARN_UNUSED_RESULT;
105105
@objc func returnsFunctionPointerThatTakesFunctionPointer() ->
106106
@convention(c) (_ comparator: @convention(c) (_ x: Int, _ y: Int) -> Int) -> Void {
107107
fatalError()

test/PrintAsObjC/cdecl-imports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Foundation
1818
public func fwdDeclaresBee() -> Bee { fatalError() }
1919

2020
// CHECK: @class Hive;
21-
// CHECK-LABEL: void fwd_declares_hive(SWIFT_NOESCAPE Hive * _Nonnull (* _Nonnull bzzz)(Bee * _Nonnull));
21+
// CHECK-LABEL: void fwd_declares_hive(Hive * _Nonnull (* _Nonnull bzzz)(Bee * _Nonnull));
2222

2323
@_cdecl("fwd_declares_hive")
2424
public func fwdDeclaresHive(bzzz: @convention(c) (Bee) -> Hive) { fatalError() }

test/PrintAsObjC/cdecl.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public func block_recurring_nightmare(x: @escaping @convention(block) (@conventi
2626
@_cdecl("foo_bar")
2727
func foo(x: Int, bar y: Int) {}
2828

29-
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_nightmare(SWIFT_NOESCAPE float (* _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
29+
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_nightmare(float (* _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
3030
@_cdecl("function_pointer_nightmare")
3131
func function_pointer_nightmare(x: @convention(c) (Int) -> Float)
3232
-> @convention(c) (CChar) -> Double { return { _ in 0 } }
3333

34-
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_recurring_nightmare(float (* _Nonnull x)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(double))))(SWIFT_NOESCAPE char (* _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
34+
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_recurring_nightmare(float (* _Nonnull x)(NSInteger (* _Nonnull)(double))))(char (* _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
3535
@_cdecl("function_pointer_recurring_nightmare")
3636
public func function_pointer_recurring_nightmare(x: @escaping @convention(c) (@convention(c) (Double) -> Int) -> Float)
3737
-> @convention(c) (@convention(c) (CUnsignedChar) -> CChar) -> Double {

test/SIL/clang-function-type-windows.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import ctypes
66

77
public func f(g: @convention(c, cType: "void (*)(size_t)") (Int) -> ()) { g(0) }
88

9-
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvyE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long long)") @noescape (Int) -> ()) -> () {
10-
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long long)") @noescape (Int) -> ()):
11-
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long long)") @noescape (Int) -> (), let, name "g", argno 1 // id: %1
9+
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvyE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long long)") (Int) -> ()) -> () {
10+
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long long)") (Int) -> ()):
11+
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long long)") (Int) -> (), let, name "g", argno 1 // id: %1

test/SIL/clang-function-types-nonwindows.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import ctypes
77

88
public func f(g: @convention(c, cType: "void (*)(size_t)") (Int) -> ()) { g(0) }
99

10-
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvmE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> ()) -> () {
11-
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> ()):
12-
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> (), let, name "g", argno 1 // id: %1
10+
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvmE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long)") (Int) -> ()) -> () {
11+
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long)") (Int) -> ()):
12+
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long)") (Int) -> (), let, name "g", argno 1 // id: %1

test/SILGen/async_builtins.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public func usesWithUnsafeContinuation() async {
4343
let _: Int = await Builtin.withUnsafeContinuation { c in }
4444

4545
// CHECK: [[FN:%.*]] = function_ref @$s4test26usesWithUnsafeContinuationyyYaFyBcXEfU_ : $@convention(thin) (Builtin.RawUnsafeContinuation) -> ()
46-
// CHECK: [[TMP:%.*]] = convert_function [[FN]] : $@convention(thin) (Builtin.RawUnsafeContinuation) -> () to $@convention(thin) @noescape (Builtin.RawUnsafeContinuation) -> ()
47-
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[TMP]]
46+
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[FN]]
4847
// CHECK: [[BOX:%.*]] = alloc_stack $Int
4948
// CHECK: [[CC:%.*]] = get_async_continuation_addr Int, [[BOX]] : $*Int
5049
// CHECK: apply [[CLOSURE]]([[CC]]) : $@noescape @callee_guaranteed (Builtin.RawUnsafeContinuation) -> ()
@@ -58,8 +57,7 @@ public func usesWithUnsafeContinuation() async {
5857
let _: String = await Builtin.withUnsafeContinuation { c in }
5958

6059
// CHECK: [[FN:%.*]] = function_ref @$s4test26usesWithUnsafeContinuationyyYaFyBcXEfU0_ : $@convention(thin) (Builtin.RawUnsafeContinuation) -> ()
61-
// CHECK: [[TMP:%.*]] = convert_function [[FN]] : $@convention(thin) (Builtin.RawUnsafeContinuation) -> () to $@convention(thin) @noescape (Builtin.RawUnsafeContinuation) -> ()
62-
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[TMP]]
60+
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[FN]]
6361
// CHECK: [[BOX:%.*]] = alloc_stack $String
6462
// CHECK: [[CC:%.*]] = get_async_continuation_addr String, [[BOX]] : $*String
6563
// CHECK: apply [[CLOSURE]]([[CC]]) : $@noescape @callee_guaranteed (Builtin.RawUnsafeContinuation) -> ()
@@ -74,8 +72,7 @@ public func usesWithUnsafeContinuation() async {
7472
let _: Any = await Builtin.withUnsafeContinuation { c in }
7573

7674
// CHECK: [[FN:%.*]] = function_ref @$s4test26usesWithUnsafeContinuationyyYaFyBcXEfU1_ : $@convention(thin) (Builtin.RawUnsafeContinuation) -> ()
77-
// CHECK: [[TMP:%.*]] = convert_function [[FN]] : $@convention(thin) (Builtin.RawUnsafeContinuation) -> () to $@convention(thin) @noescape (Builtin.RawUnsafeContinuation) -> ()
78-
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[TMP]]
75+
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[FN]]
7976
// CHECK: [[BOX:%.*]] = alloc_stack $Any
8077
// CHECK: [[CC:%.*]] = get_async_continuation_addr Any, [[BOX]] : $*Any
8178
// CHECK: apply [[CLOSURE]]([[CC]]) : $@noescape @callee_guaranteed (Builtin.RawUnsafeContinuation) -> ()
@@ -94,8 +91,7 @@ public func usesWithUnsafeThrowingContinuation() async throws {
9491
let _: Int = try await Builtin.withUnsafeThrowingContinuation { c in }
9592

9693
// CHECK: [[FN:%.*]] = function_ref @$s4test34usesWithUnsafeThrowingContinuationyyYaKFyBcXEfU_ : $@convention(thin) (Builtin.RawUnsafeContinuation) -> ()
97-
// CHECK: [[TMP:%.*]] = convert_function [[FN]] : $@convention(thin) (Builtin.RawUnsafeContinuation) -> () to $@convention(thin) @noescape (Builtin.RawUnsafeContinuation) -> ()
98-
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[TMP]]
94+
// CHECK: [[CLOSURE:%.*]] = thin_to_thick_function [[FN]]
9995
// CHECK: [[BOX:%.*]] = alloc_stack $Int
10096
// CHECK: [[CC:%.*]] = get_async_continuation_addr [throws] Int, [[BOX]] : $*Int
10197
// CHECK: apply [[CLOSURE]]([[CC]]) : $@noescape @callee_guaranteed (Builtin.RawUnsafeContinuation) -> ()

0 commit comments

Comments
 (0)