Skip to content

Commit 3b9787f

Browse files
authored
Merge pull request #35845 from atrick/5.4-fix-runtime-readnone
Remove ReadNone attribute from runtime functions
2 parents 0c5ee32 + 346eba8 commit 3b9787f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// This file defines x-macros used for metaprogramming with the set of
1414
// runtime functions.
1515
//
16+
// Runtime functions that read from object arguments cannot be marked
17+
// ReadNone. Otherwise the objects may be freed before the runtime call.
18+
//
1619
//===----------------------------------------------------------------------===//
1720

1821
/// FUNCTION(Id, Name, CC, Availability, ReturnTys, ArgTys, Attrs)
@@ -56,10 +59,11 @@ FUNCTION(DeallocBox, swift_deallocBox, C_CC, AlwaysAvailable,
5659
ARGS(RefCountedPtrTy),
5760
ATTRS(NoUnwind))
5861

62+
// swift_projectBox reads object metadata so cannot be marked ReadNone.
5963
FUNCTION(ProjectBox, swift_projectBox, C_CC, AlwaysAvailable,
6064
RETURNS(OpaquePtrTy),
6165
ARGS(RefCountedPtrTy),
62-
ATTRS(NoUnwind, ReadNone))
66+
ATTRS(NoUnwind, ReadOnly, ArgMemOnly))
6367

6468
FUNCTION(AllocEmptyBox, swift_allocEmptyBox, C_CC, AlwaysAvailable,
6569
RETURNS(RefCountedPtrTy),
@@ -795,11 +799,12 @@ FUNCTION(GetObjCClassFromMetadata, swift_getObjCClassFromMetadata,
795799
ATTRS(NoUnwind, ReadNone))
796800

797801
// Metadata *swift_getObjCClassFromObject(id object);
802+
// This reads object metadata so cannot be marked ReadNone.
798803
FUNCTION(GetObjCClassFromObject, swift_getObjCClassFromObject,
799804
C_CC, AlwaysAvailable,
800805
RETURNS(ObjCClassPtrTy),
801806
ARGS(ObjCPtrTy),
802-
ATTRS(NoUnwind, ReadNone))
807+
ATTRS(NoUnwind, ReadOnly, ArgMemOnly))
803808

804809
// MetadataResponse swift_getTupleTypeMetadata(MetadataRequest request,
805810
// TupleTypeFlags flags,

test/SILOptimizer/llvm_arc.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %target-swift-frontend -O -emit-ir %s | %FileCheck %s
2+
//
3+
// Test LLVM ARC Optimization
4+
5+
// REQUIRES: objc_interop
6+
7+
sil_stage canonical
8+
9+
import Builtin
10+
11+
typealias AnyObject = Builtin.AnyObject
12+
13+
sil @getObject : $@convention(thin) <T where T : AnyObject> () -> T
14+
15+
// Test swift_getObjCClassFromObject side-effects. It reads the object
16+
// argument. A release cannot be hoisted above it.
17+
//
18+
// CHECK-LABEL: define {{.*}}swiftcc %objc_class* @testGetObjCClassFromObjectSideEffect(%swift.type* %T)
19+
// CHECK: entry:
20+
// CHECK-NEXT: %0 = tail call swiftcc %objc_object* @getObject(%swift.type* %T)
21+
// CHECK-NEXT: %.Type = tail call %objc_class* @swift_getObjCClassFromObject(%objc_object* %0)
22+
// CHECK-NEXT: tail call void @swift_unknownObjectRelease(%objc_object* %0)
23+
// CHECK: ret %objc_class* %.Type
24+
// CHECK: }
25+
sil @testGetObjCClassFromObjectSideEffect : $@convention(thin) <T where T : AnyObject> () -> @objc_metatype T.Type {
26+
bb0:
27+
%f = function_ref @getObject : $@convention(thin) <τ_0_0 where τ_0_0 : AnyObject> () -> τ_0_0
28+
%obj = apply %f<T>() : $@convention(thin) <τ_0_0 where τ_0_0 : AnyObject> () -> τ_0_0
29+
%mt = value_metatype $@objc_metatype T.Type, %obj : $T
30+
// Do not hoist this release.
31+
strong_release %obj : $T
32+
return %mt : $@objc_metatype T.Type
33+
}

0 commit comments

Comments
 (0)