ahatanak created this revision.
ahatanak added reviewers: steven_wu, dexonsmith, rjmccall.
ahatanak added a project: clang.
Herald added subscribers: jkorous, kristof.beyls, javed.absar, mehdi_amini.

This fixes a bug which causes the ARC contract pass to not insert the assembly 
marker that is needed for the runtime to do the `objc_autoreleaseReturnValue/ 
objc_retainAutoreleasedReturnValue` optimization when compiling on ARM64/ARM 
with LTO enabled. This happens because `IRLinker::linkNamedMDNodes()` adds all 
the operands from the source modules to the metadata's operand list in the 
merged module (see the example below) and ARC contract fails to extract the 
marker string if the metadata has more than one operand.

  clang.arc.retainAutoreleasedReturnValueMarker = !{!0, !0}
  !0 = !{!"mov\09fp, fp\09\09# marker for objc_retainAutoreleaseReturnValue"}

To fix the bug, this patch emits the marker as a module flag.

rdar://problem/49464214


Repository:
  rC Clang

https://reviews.llvm.org/D60302

Files:
  lib/CodeGen/CGObjC.cpp
  test/CodeGenObjC/arc-unsafeclaim.m


Index: test/CodeGenObjC/arc-unsafeclaim.m
===================================================================
--- test/CodeGenObjC/arc-unsafeclaim.m
+++ test/CodeGenObjC/arc-unsafeclaim.m
@@ -231,4 +231,5 @@
 
 // This is always at the end of the module.
 
-// CHECK-OPTIMIZED: !clang.arc.retainAutoreleasedReturnValueMarker = !{!0}
+// CHECK-OPTIMIZED: !llvm.module.flags = !{!0,
+// CHECK-OPTIMIZED: !0 = !{i32 1, 
!"clang.arc.retainAutoreleasedReturnValueMarker", !"mov{{.*}}marker for 
objc_retainAutoreleaseReturnValue"}
Index: lib/CodeGen/CGObjC.cpp
===================================================================
--- lib/CodeGen/CGObjC.cpp
+++ lib/CodeGen/CGObjC.cpp
@@ -2161,14 +2161,10 @@
     // with this marker yet, so leave a breadcrumb for the ARC
     // optimizer to pick up.
     } else {
-      llvm::NamedMDNode *metadata =
-        CGF.CGM.getModule().getOrInsertNamedMetadata(
-                            "clang.arc.retainAutoreleasedReturnValueMarker");
-      assert(metadata->getNumOperands() <= 1);
-      if (metadata->getNumOperands() == 0) {
-        auto &ctx = CGF.getLLVMContext();
-        metadata->addOperand(llvm::MDNode::get(ctx,
-                                     llvm::MDString::get(ctx, assembly)));
+      const char *markerKey = "clang.arc.retainAutoreleasedReturnValueMarker";
+      if (!CGF.CGM.getModule().getModuleFlag(markerKey)) {
+        auto *str = llvm::MDString::get(CGF.getLLVMContext(), assembly);
+        CGF.CGM.getModule().addModuleFlag(llvm::Module::Error, markerKey, str);
       }
     }
   }


Index: test/CodeGenObjC/arc-unsafeclaim.m
===================================================================
--- test/CodeGenObjC/arc-unsafeclaim.m
+++ test/CodeGenObjC/arc-unsafeclaim.m
@@ -231,4 +231,5 @@
 
 // This is always at the end of the module.
 
-// CHECK-OPTIMIZED: !clang.arc.retainAutoreleasedReturnValueMarker = !{!0}
+// CHECK-OPTIMIZED: !llvm.module.flags = !{!0,
+// CHECK-OPTIMIZED: !0 = !{i32 1, !"clang.arc.retainAutoreleasedReturnValueMarker", !"mov{{.*}}marker for objc_retainAutoreleaseReturnValue"}
Index: lib/CodeGen/CGObjC.cpp
===================================================================
--- lib/CodeGen/CGObjC.cpp
+++ lib/CodeGen/CGObjC.cpp
@@ -2161,14 +2161,10 @@
     // with this marker yet, so leave a breadcrumb for the ARC
     // optimizer to pick up.
     } else {
-      llvm::NamedMDNode *metadata =
-        CGF.CGM.getModule().getOrInsertNamedMetadata(
-                            "clang.arc.retainAutoreleasedReturnValueMarker");
-      assert(metadata->getNumOperands() <= 1);
-      if (metadata->getNumOperands() == 0) {
-        auto &ctx = CGF.getLLVMContext();
-        metadata->addOperand(llvm::MDNode::get(ctx,
-                                     llvm::MDString::get(ctx, assembly)));
+      const char *markerKey = "clang.arc.retainAutoreleasedReturnValueMarker";
+      if (!CGF.CGM.getModule().getModuleFlag(markerKey)) {
+        auto *str = llvm::MDString::get(CGF.getLLVMContext(), assembly);
+        CGF.CGM.getModule().addModuleFlag(llvm::Module::Error, markerKey, str);
       }
     }
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to