r250955 - Enable ARC on the fragile runtime.

John McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 21 15:06:03 PDT 2015


Author: rjmccall
Date: Wed Oct 21 17:06:03 2015
New Revision: 250955

URL: http://llvm.org/viewvc/llvm-project?rev=250955&view=rev
Log:
Enable ARC on the fragile runtime.

This is almost entirely a matter of just flipping a switch.  99% of
the runtime support is available all the way back to when it was
implemented in the non-fragile runtime, i.e. in Lion.  However,
fragile runtimes do not recognize ARC-style ivar layout strings,
which means that accessing __strong or __weak ivars reflectively
(e.g. via object_setIvar) will end up accessing the ivar as if it
were __unsafe_unretained.  Therefore, when using reflective
technologies like KVC, be sure that your paths always refer to a
property.

rdar://23209307

Added:
    cfe/trunk/test/CodeGenObjC/fragile-arc.m
Modified:
    cfe/trunk/include/clang/Basic/ObjCRuntime.h
    cfe/trunk/lib/CodeGen/CGObjCMac.cpp

Modified: cfe/trunk/include/clang/Basic/ObjCRuntime.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/ObjCRuntime.h?rev=250955&r1=250954&r2=250955&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/ObjCRuntime.h (original)
+++ cfe/trunk/include/clang/Basic/ObjCRuntime.h Wed Oct 21 17:06:03 2015
@@ -133,7 +133,9 @@ public:
   /// \brief Does this runtime allow ARC at all?
   bool allowsARC() const {
     switch (getKind()) {
-    case FragileMacOSX: return false;
+    case FragileMacOSX:
+      // No stub library for the fragile runtime.
+      return getVersion() >= VersionTuple(10, 7);
     case MacOSX: return true;
     case iOS: return true;
     case GCC: return false;
@@ -150,7 +152,7 @@ public:
   /// library.
   bool hasNativeARC() const {
     switch (getKind()) {
-    case FragileMacOSX: return false;
+    case FragileMacOSX: return getVersion() >= VersionTuple(10, 7);
     case MacOSX: return getVersion() >= VersionTuple(10, 7);
     case iOS: return getVersion() >= VersionTuple(5);
 

Modified: cfe/trunk/lib/CodeGen/CGObjCMac.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjCMac.cpp?rev=250955&r1=250954&r2=250955&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGObjCMac.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGObjCMac.cpp Wed Oct 21 17:06:03 2015
@@ -3063,7 +3063,8 @@ enum FragileClassFlags {
   FragileABI_Class_Factory                 = 0x00001,
   FragileABI_Class_Meta                    = 0x00002,
   FragileABI_Class_HasCXXStructors         = 0x02000,
-  FragileABI_Class_Hidden                  = 0x20000
+  FragileABI_Class_Hidden                  = 0x20000,
+  FragileABI_Class_CompiledByARC           = 0x04000000
 };
 
 enum NonFragileClassFlags {
@@ -3125,6 +3126,10 @@ void CGObjCMac::GenerateClass(const ObjC
   unsigned Flags = FragileABI_Class_Factory;
   if (ID->hasNonZeroConstructors() || ID->hasDestructors())
     Flags |= FragileABI_Class_HasCXXStructors;
+
+  if (CGM.getLangOpts().ObjCAutoRefCount)
+    Flags |= FragileABI_Class_CompiledByARC;
+
   CharUnits Size =
     CGM.getContext().getASTObjCImplementationLayout(ID).getSize();
 
@@ -4847,7 +4852,7 @@ CGObjCCommonMac::BuildIvarLayout(const O
 
     if (isNonFragileABI()) {
       baseOffset = beginOffset; // InstanceStart
-    } else if (auto superClass = OMD->getSuperClass()) {
+    } else if (auto superClass = OI->getSuperClass()) {
       auto startOffset =
         CGM.getContext().getASTObjCInterfaceLayout(superClass).getSize();
       baseOffset = startOffset.RoundUpToAlignment(CGM.getPointerAlign());

Added: cfe/trunk/test/CodeGenObjC/fragile-arc.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/fragile-arc.m?rev=250955&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenObjC/fragile-arc.m (added)
+++ cfe/trunk/test/CodeGenObjC/fragile-arc.m Wed Oct 21 17:06:03 2015
@@ -0,0 +1,138 @@
+// RUN: %clang_cc1 -triple i386-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime=macosx-fragile-10.10 -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple i386-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime=macosx-fragile-10.10 -o - %s | FileCheck %s -check-prefix=GLOBALS
+
+ at class Opaque;
+
+ at interface Root {
+  Class isa;
+}
+ at end
+
+ at interface A : Root {
+  Opaque *strong;
+  __weak Opaque *weak;
+}
+ at end
+
+// GLOBALS-LABEL @OBJC_METACLASS_A
+//  Strong layout: scan the first word.
+// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private global [2 x i8] c"\01\00"
+//  Weak layout: skip the first word, scan the second word.
+// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private global [2 x i8] c"\11\00"
+
+//  0x04002001
+//     ^ is compiled by ARC (controls interpretation of layouts)
+//        ^ has C++ structors (no distinction for zero-initializable)
+//           ^ factory (always set on non-metaclasses)
+// GLOBALS: @OBJC_CLASS_A = private global {{.*}} i32 67117057
+
+ at implementation A
+// CHECK-LABEL: define internal void @"\01-[A testStrong]"
+// CHECK:      [[SELFVAR:%.*]] = alloca [[A:%.*]]*, align 4
+- (void) testStrong {
+// CHECK:      [[X:%.*]] = alloca [[OPAQUE:%.*]]*, align 4
+// CHECK:      [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
+// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 4
+// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
+// CHECK-NEXT: [[T0:%.*]] = load [[OPAQUE]]*, [[OPAQUE]]** [[IVAR]]
+// CHECK-NEXT: [[T1:%.*]] = bitcast [[OPAQUE]]* [[T0]] to i8*
+// CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retain(i8* [[T1]])
+// CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to [[OPAQUE]]*
+// CHECK-NEXT: store [[OPAQUE]]* [[T3]], [[OPAQUE]]** [[X]]
+  Opaque *x = strong;
+// CHECK-NEXT: [[VALUE:%.*]] = load [[OPAQUE]]*, [[OPAQUE]]** [[X]]
+// CHECK-NEXT: [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
+// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 4
+// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
+// CHECK-NEXT: [[T1:%.*]] = bitcast [[OPAQUE]]* [[VALUE]] to i8*
+// CHECK-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* [[T1]])
+  strong = x;
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[X]] to i8**
+// CHECK-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* null)
+// CHECK-NEXT: ret void
+}
+
+// CHECK-LABEL: define internal void @"\01-[A testWeak]"
+// CHECK:      [[SELFVAR:%.*]] = alloca [[A]]*, align 4
+- (void) testWeak {
+// CHECK:      [[X:%.*]] = alloca [[OPAQUE]]*, align 4
+// CHECK:      [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
+// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 8
+// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
+// CHECK-NEXT: [[T1:%.*]] = call i8* @objc_loadWeakRetained(i8** [[T0]])
+// CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]*
+// CHECK-NEXT: store [[OPAQUE]]* [[T2]], [[OPAQUE]]** [[X]]
+  Opaque *x = weak;
+// CHECK-NEXT: [[VALUE:%.*]] = load [[OPAQUE]]*, [[OPAQUE]]** [[X]]
+// CHECK-NEXT: [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
+// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 8
+// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
+// CHECK-NEXT: [[T1:%.*]] = bitcast [[OPAQUE]]* [[VALUE]] to i8*
+// CHECK-NEXT: call i8* @objc_storeWeak(i8** [[T0]], i8* [[T1]])
+  weak = x;
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[X]] to i8**
+// CHECK-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* null)
+// CHECK-NEXT: ret void
+}
+
+// CHECK-LABEL: define internal void @"\01-[A .cxx_destruct]"
+// CHECK:      [[SELFVAR:%.*]] = alloca [[A]]*, align 4
+// CHECK:      [[SELF:%.*]] = load [[A]]*, [[A]]** [[SELFVAR]]
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
+// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 8
+// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
+// CHECK-NEXT: call void @objc_destroyWeak(i8** [[T0]])
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[SELF]] to i8*
+// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i32 4
+// CHECK-NEXT: [[IVAR:%.*]] = bitcast i8* [[T1]] to [[OPAQUE]]**
+// CHECK-NEXT: [[T0:%.*]] = bitcast [[OPAQUE]]** [[IVAR]] to i8**
+// CHECK-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* null)
+// CHECK-NEXT: ret void
+ at end
+
+// Test case for corner case of ivar layout.
+ at interface B : A {
+  char _b_flag;
+}
+ at end
+
+ at interface C : B {
+  char _c_flag;
+  __unsafe_unretained id c_unsafe[5];
+  id c_strong[4];
+  __weak id c_weak[3];
+  id c_strong2[7];
+}
+ at end
+ at implementation C @end
+
+//  Note that these layouts implicitly start at the end of the previous
+//  class rounded up to pointer alignment.
+// GLOBALS-LABEL: @OBJC_METACLASS_C
+//  Strong layout: skip five, scan four, skip three, scan seven
+//    'T' == 0x54, '7' == 0x37
+// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private global [3 x i8] c"T7\00"
+//  Weak layout: skip nine, scan three
+// GLOBALS: @OBJC_CLASS_NAME_{{.*}} = private global [2 x i8] c"\93\00"
+
+extern void useBlock(void (^block)(void));
+
+//  256 == 0x100 == starts with 1 strong
+// GLOBALS: @__block_descriptor_tmp{{.*}} = internal constant {{.*}}, i32 256 }
+void testBlockLayoutStrong(id x) {
+  useBlock(^{ (void) x; });
+}
+
+//  1   == 0x001 == starts with 1 weak
+// GLOBALS: @__block_descriptor_tmp{{.*}} = internal constant {{.*}}, i32 1 }
+void testBlockLayoutWeak(__weak id x) {
+  useBlock(^{ (void) x; });
+}




More information about the cfe-commits mailing list