Copy the block to the heap before passing it to the callee in case the
block escapes in the callee.
rdar://problem/
55683462
Differential Revision: https://reviews.llvm.org/D71431
``Block_copy``. The optimizer may remove such copies when it sees that the
result is used only as an argument to a call.
+When a block pointer type is converted to a non-block pointer type (such as
+``id``), ``Block_copy`` is called. This is necessary because a block allocated
+on the stack won't get copied to the heap when the non-block pointer escapes.
+A block pointer is implicitly converted to ``id`` when it is passed to a
+function as a variadic argument.
+
.. _arc.misc.exceptions:
Exceptions
for (Expr *A : Args.slice(ArgIx)) {
ExprResult Arg = DefaultVariadicArgumentPromotion(A, CallType, FDecl);
Invalid |= Arg.isInvalid();
+ // Copy blocks to the heap.
+ if (A->getType()->isBlockPointerType())
+ maybeExtendBlockObject(Arg);
AllArgs.push_back(Arg.get());
}
}
test20_callee(^{ (void)x; });
}
+// CHECK-LABEL: define void @test21(
+// CHECK: %[[V6:.*]] = call i8* @llvm.objc.retainBlock(
+// CHECK: %[[V7:.*]] = bitcast i8* %[[V6]] to void ()*
+// CHECK: call void (i32, ...) @test21_callee(i32 1, void ()* %[[V7]]),
+
+void test21_callee(int n, ...);
+void test21(id x) {
+ test21_callee(1, ^{ (void)x; });
+}
+
// CHECK: attributes [[NUW]] = { nounwind }
// CHECK-UNOPT: attributes [[NUW]] = { nounwind }