1 //===--- CGBlocks.cpp - Emit LLVM Code for declarations ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This contains code to emit blocks.
11 //===----------------------------------------------------------------------===//
15 #include "CGDebugInfo.h"
16 #include "CGObjCRuntime.h"
17 #include "CGOpenCLRuntime.h"
18 #include "CodeGenFunction.h"
19 #include "CodeGenModule.h"
20 #include "ConstantEmitter.h"
21 #include "TargetInfo.h"
22 #include "clang/AST/Attr.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/CodeGen/ConstantInitBuilder.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/ScopedPrinter.h"
32 using namespace clang
;
33 using namespace CodeGen
;
35 CGBlockInfo::CGBlockInfo(const BlockDecl
*block
, StringRef name
)
36 : Name(name
), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
37 NoEscape(false), HasCXXObject(false), UsesStret(false),
38 HasCapturedVariableLayout(false), CapturesNonExternalType(false),
39 LocalAddress(Address::invalid()), StructureType(nullptr), Block(block
) {
41 // Skip asm prefix, if any. 'name' is usually taken directly from
42 // the mangled name of the enclosing function.
43 if (!name
.empty() && name
[0] == '\01')
44 name
= name
.substr(1);
47 // Anchor the vtable to this translation unit.
48 BlockByrefHelpers::~BlockByrefHelpers() {}
50 /// Build the given block as a global block.
51 static llvm::Constant
*buildGlobalBlock(CodeGenModule
&CGM
,
52 const CGBlockInfo
&blockInfo
,
53 llvm::Constant
*blockFn
);
55 /// Build the helper function to copy a block.
56 static llvm::Constant
*buildCopyHelper(CodeGenModule
&CGM
,
57 const CGBlockInfo
&blockInfo
) {
58 return CodeGenFunction(CGM
).GenerateCopyHelperFunction(blockInfo
);
61 /// Build the helper function to dispose of a block.
62 static llvm::Constant
*buildDisposeHelper(CodeGenModule
&CGM
,
63 const CGBlockInfo
&blockInfo
) {
64 return CodeGenFunction(CGM
).GenerateDestroyHelperFunction(blockInfo
);
69 enum class CaptureStrKind
{
70 // String for the copy helper.
72 // String for the dispose helper.
74 // Merge the strings for the copy helper and dispose helper.
78 } // end anonymous namespace
80 static std::string
getBlockCaptureStr(const CGBlockInfo::Capture
&Cap
,
81 CaptureStrKind StrKind
,
82 CharUnits BlockAlignment
,
85 static std::string
getBlockDescriptorName(const CGBlockInfo
&BlockInfo
,
87 std::string Name
= "__block_descriptor_";
88 Name
+= llvm::to_string(BlockInfo
.BlockSize
.getQuantity()) + "_";
90 if (BlockInfo
.NeedsCopyDispose
) {
91 if (CGM
.getLangOpts().Exceptions
)
93 if (CGM
.getCodeGenOpts().ObjCAutoRefCountExceptions
)
95 Name
+= llvm::to_string(BlockInfo
.BlockAlign
.getQuantity()) + "_";
97 for (auto &Cap
: BlockInfo
.SortedCaptures
) {
98 if (Cap
.isConstantOrTrivial())
101 Name
+= llvm::to_string(Cap
.getOffset().getQuantity());
103 if (Cap
.CopyKind
== Cap
.DisposeKind
) {
104 // If CopyKind and DisposeKind are the same, merge the capture
106 assert(Cap
.CopyKind
!= BlockCaptureEntityKind::None
&&
107 "shouldn't see BlockCaptureManagedEntity that is None");
108 Name
+= getBlockCaptureStr(Cap
, CaptureStrKind::Merged
,
109 BlockInfo
.BlockAlign
, CGM
);
111 // If CopyKind and DisposeKind are not the same, which can happen when
112 // either Kind is None or the captured object is a __strong block,
113 // concatenate the copy and dispose strings.
114 Name
+= getBlockCaptureStr(Cap
, CaptureStrKind::CopyHelper
,
115 BlockInfo
.BlockAlign
, CGM
);
116 Name
+= getBlockCaptureStr(Cap
, CaptureStrKind::DisposeHelper
,
117 BlockInfo
.BlockAlign
, CGM
);
123 std::string TypeAtEncoding
=
124 CGM
.getContext().getObjCEncodingForBlock(BlockInfo
.getBlockExpr());
125 /// Replace occurrences of '@' with '\1'. '@' is reserved on ELF platforms as
126 /// a separator between symbol name and symbol version.
127 std::replace(TypeAtEncoding
.begin(), TypeAtEncoding
.end(), '@', '\1');
128 Name
+= "e" + llvm::to_string(TypeAtEncoding
.size()) + "_" + TypeAtEncoding
;
129 Name
+= "l" + CGM
.getObjCRuntime().getRCBlockLayoutStr(CGM
, BlockInfo
);
133 /// buildBlockDescriptor - Build the block descriptor meta-data for a block.
134 /// buildBlockDescriptor is accessed from 5th field of the Block_literal
135 /// meta-data and contains stationary information about the block literal.
136 /// Its definition will have 4 (or optionally 6) words.
138 /// struct Block_descriptor {
139 /// unsigned long reserved;
140 /// unsigned long size; // size of Block_literal metadata in bytes.
141 /// void *copy_func_helper_decl; // optional copy helper.
142 /// void *destroy_func_decl; // optional destructor helper.
143 /// void *block_method_encoding_address; // @encode for block literal signature.
144 /// void *block_layout_info; // encoding of captured block variables.
147 static llvm::Constant
*buildBlockDescriptor(CodeGenModule
&CGM
,
148 const CGBlockInfo
&blockInfo
) {
149 ASTContext
&C
= CGM
.getContext();
151 llvm::IntegerType
*ulong
=
152 cast
<llvm::IntegerType
>(CGM
.getTypes().ConvertType(C
.UnsignedLongTy
));
153 llvm::PointerType
*i8p
= nullptr;
154 if (CGM
.getLangOpts().OpenCL
)
155 i8p
= llvm::PointerType::get(
156 CGM
.getLLVMContext(), C
.getTargetAddressSpace(LangAS::opencl_constant
));
160 std::string descName
;
162 // If an equivalent block descriptor global variable exists, return it.
163 if (C
.getLangOpts().ObjC
&&
164 CGM
.getLangOpts().getGC() == LangOptions::NonGC
) {
165 descName
= getBlockDescriptorName(blockInfo
, CGM
);
166 if (llvm::GlobalValue
*desc
= CGM
.getModule().getNamedValue(descName
))
170 // If there isn't an equivalent block descriptor global variable, create a new
172 ConstantInitBuilder
builder(CGM
);
173 auto elements
= builder
.beginStruct();
176 elements
.addInt(ulong
, 0);
179 // FIXME: What is the right way to say this doesn't fit? We should give
180 // a user diagnostic in that case. Better fix would be to change the
182 elements
.addInt(ulong
, blockInfo
.BlockSize
.getQuantity());
184 // Optional copy/dispose helpers.
185 bool hasInternalHelper
= false;
186 if (blockInfo
.NeedsCopyDispose
) {
187 // copy_func_helper_decl
188 llvm::Constant
*copyHelper
= buildCopyHelper(CGM
, blockInfo
);
189 elements
.add(copyHelper
);
192 llvm::Constant
*disposeHelper
= buildDisposeHelper(CGM
, blockInfo
);
193 elements
.add(disposeHelper
);
195 if (cast
<llvm::Function
>(copyHelper
->stripPointerCasts())
196 ->hasInternalLinkage() ||
197 cast
<llvm::Function
>(disposeHelper
->stripPointerCasts())
198 ->hasInternalLinkage())
199 hasInternalHelper
= true;
202 // Signature. Mandatory ObjC-style method descriptor @encode sequence.
203 std::string typeAtEncoding
=
204 CGM
.getContext().getObjCEncodingForBlock(blockInfo
.getBlockExpr());
205 elements
.add(CGM
.GetAddrOfConstantCString(typeAtEncoding
).getPointer());
208 if (C
.getLangOpts().ObjC
) {
209 if (CGM
.getLangOpts().getGC() != LangOptions::NonGC
)
210 elements
.add(CGM
.getObjCRuntime().BuildGCBlockLayout(CGM
, blockInfo
));
212 elements
.add(CGM
.getObjCRuntime().BuildRCBlockLayout(CGM
, blockInfo
));
215 elements
.addNullPointer(i8p
);
217 unsigned AddrSpace
= 0;
218 if (C
.getLangOpts().OpenCL
)
219 AddrSpace
= C
.getTargetAddressSpace(LangAS::opencl_constant
);
221 llvm::GlobalValue::LinkageTypes linkage
;
222 if (descName
.empty()) {
223 linkage
= llvm::GlobalValue::InternalLinkage
;
224 descName
= "__block_descriptor_tmp";
225 } else if (hasInternalHelper
) {
226 // If either the copy helper or the dispose helper has internal linkage,
227 // the block descriptor must have internal linkage too.
228 linkage
= llvm::GlobalValue::InternalLinkage
;
230 linkage
= llvm::GlobalValue::LinkOnceODRLinkage
;
233 llvm::GlobalVariable
*global
=
234 elements
.finishAndCreateGlobal(descName
, CGM
.getPointerAlign(),
235 /*constant*/ true, linkage
, AddrSpace
);
237 if (linkage
== llvm::GlobalValue::LinkOnceODRLinkage
) {
238 if (CGM
.supportsCOMDAT())
239 global
->setComdat(CGM
.getModule().getOrInsertComdat(descName
));
240 global
->setVisibility(llvm::GlobalValue::HiddenVisibility
);
241 global
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
248 Purely notional variadic template describing the layout of a block.
250 template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
251 struct Block_literal {
252 /// Initialized to one of:
253 /// extern void *_NSConcreteStackBlock[];
254 /// extern void *_NSConcreteGlobalBlock[];
256 /// In theory, we could start one off malloc'ed by setting
257 /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
259 /// extern void *_NSConcreteMallocBlock[];
260 struct objc_class *isa;
262 /// These are the flags (with corresponding bit number) that the
263 /// compiler is actually supposed to know about.
264 /// 23. BLOCK_IS_NOESCAPE - indicates that the block is non-escaping
265 /// 25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
266 /// descriptor provides copy and dispose helper functions
267 /// 26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
268 /// object with a nontrivial destructor or copy constructor
269 /// 28. BLOCK_IS_GLOBAL - indicates that the block is allocated
271 /// 29. BLOCK_USE_STRET - indicates that the block function
272 /// uses stret, which objc_msgSend needs to know about
273 /// 30. BLOCK_HAS_SIGNATURE - indicates that the block has an
274 /// @encoded signature string
275 /// And we're not supposed to manipulate these:
276 /// 24. BLOCK_NEEDS_FREE - indicates that the block has been moved
277 /// to malloc'ed memory
278 /// 27. BLOCK_IS_GC - indicates that the block has been moved to
279 /// to GC-allocated memory
280 /// Additionally, the bottom 16 bits are a reference count which
281 /// should be zero on the stack.
284 /// Reserved; should be zero-initialized.
287 /// Function pointer generated from block literal.
288 _ResultType (*invoke)(Block_literal *, _ParamTypes...);
290 /// Block description metadata generated from block literal.
291 struct Block_descriptor *block_descriptor;
293 /// Captured values follow.
294 _CapturesTypes captures...;
299 /// A chunk of data that we actually have to capture in the block.
300 struct BlockLayoutChunk
{
303 const BlockDecl::Capture
*Capture
; // null for 'this'
306 BlockCaptureEntityKind CopyKind
, DisposeKind
;
307 BlockFieldFlags CopyFlags
, DisposeFlags
;
309 BlockLayoutChunk(CharUnits align
, CharUnits size
,
310 const BlockDecl::Capture
*capture
, llvm::Type
*type
,
311 QualType fieldType
, BlockCaptureEntityKind CopyKind
,
312 BlockFieldFlags CopyFlags
,
313 BlockCaptureEntityKind DisposeKind
,
314 BlockFieldFlags DisposeFlags
)
315 : Alignment(align
), Size(size
), Capture(capture
), Type(type
),
316 FieldType(fieldType
), CopyKind(CopyKind
), DisposeKind(DisposeKind
),
317 CopyFlags(CopyFlags
), DisposeFlags(DisposeFlags
) {}
319 /// Tell the block info that this chunk has the given field index.
320 void setIndex(CGBlockInfo
&info
, unsigned index
, CharUnits offset
) {
322 info
.CXXThisIndex
= index
;
323 info
.CXXThisOffset
= offset
;
325 info
.SortedCaptures
.push_back(CGBlockInfo::Capture::makeIndex(
326 index
, offset
, FieldType
, CopyKind
, CopyFlags
, DisposeKind
,
327 DisposeFlags
, Capture
));
331 bool isTrivial() const {
332 return CopyKind
== BlockCaptureEntityKind::None
&&
333 DisposeKind
== BlockCaptureEntityKind::None
;
337 /// Order by 1) all __strong together 2) next, all block together 3) next,
338 /// all byref together 4) next, all __weak together. Preserve descending
339 /// alignment in all situations.
340 bool operator<(const BlockLayoutChunk
&left
, const BlockLayoutChunk
&right
) {
341 if (left
.Alignment
!= right
.Alignment
)
342 return left
.Alignment
> right
.Alignment
;
344 auto getPrefOrder
= [](const BlockLayoutChunk
&chunk
) {
345 switch (chunk
.CopyKind
) {
346 case BlockCaptureEntityKind::ARCStrong
:
348 case BlockCaptureEntityKind::BlockObject
:
349 switch (chunk
.CopyFlags
.getBitMask()) {
350 case BLOCK_FIELD_IS_OBJECT
:
352 case BLOCK_FIELD_IS_BLOCK
:
354 case BLOCK_FIELD_IS_BYREF
:
360 case BlockCaptureEntityKind::ARCWeak
:
368 return getPrefOrder(left
) < getPrefOrder(right
);
370 } // end anonymous namespace
372 static std::pair
<BlockCaptureEntityKind
, BlockFieldFlags
>
373 computeCopyInfoForBlockCapture(const BlockDecl::Capture
&CI
, QualType T
,
374 const LangOptions
&LangOpts
);
376 static std::pair
<BlockCaptureEntityKind
, BlockFieldFlags
>
377 computeDestroyInfoForBlockCapture(const BlockDecl::Capture
&CI
, QualType T
,
378 const LangOptions
&LangOpts
);
380 static void addBlockLayout(CharUnits align
, CharUnits size
,
381 const BlockDecl::Capture
*capture
, llvm::Type
*type
,
383 SmallVectorImpl
<BlockLayoutChunk
> &Layout
,
384 CGBlockInfo
&Info
, CodeGenModule
&CGM
) {
387 Layout
.push_back(BlockLayoutChunk(
388 align
, size
, capture
, type
, fieldType
, BlockCaptureEntityKind::None
,
389 BlockFieldFlags(), BlockCaptureEntityKind::None
, BlockFieldFlags()));
393 const LangOptions
&LangOpts
= CGM
.getLangOpts();
394 BlockCaptureEntityKind CopyKind
, DisposeKind
;
395 BlockFieldFlags CopyFlags
, DisposeFlags
;
397 std::tie(CopyKind
, CopyFlags
) =
398 computeCopyInfoForBlockCapture(*capture
, fieldType
, LangOpts
);
399 std::tie(DisposeKind
, DisposeFlags
) =
400 computeDestroyInfoForBlockCapture(*capture
, fieldType
, LangOpts
);
401 Layout
.push_back(BlockLayoutChunk(align
, size
, capture
, type
, fieldType
,
402 CopyKind
, CopyFlags
, DisposeKind
,
408 if (!Layout
.back().isTrivial())
409 Info
.NeedsCopyDispose
= true;
412 /// Determines if the given type is safe for constant capture in C++.
413 static bool isSafeForCXXConstantCapture(QualType type
) {
414 const RecordType
*recordType
=
415 type
->getBaseElementTypeUnsafe()->getAs
<RecordType
>();
417 // Only records can be unsafe.
418 if (!recordType
) return true;
420 const auto *record
= cast
<CXXRecordDecl
>(recordType
->getDecl());
422 // Maintain semantics for classes with non-trivial dtors or copy ctors.
423 if (!record
->hasTrivialDestructor()) return false;
424 if (record
->hasNonTrivialCopyConstructor()) return false;
426 // Otherwise, we just have to make sure there aren't any mutable
427 // fields that might have changed since initialization.
428 return !record
->hasMutableFields();
431 /// It is illegal to modify a const object after initialization.
432 /// Therefore, if a const object has a constant initializer, we don't
433 /// actually need to keep storage for it in the block; we'll just
434 /// rematerialize it at the start of the block function. This is
435 /// acceptable because we make no promises about address stability of
436 /// captured variables.
437 static llvm::Constant
*tryCaptureAsConstant(CodeGenModule
&CGM
,
438 CodeGenFunction
*CGF
,
439 const VarDecl
*var
) {
440 // Return if this is a function parameter. We shouldn't try to
441 // rematerialize default arguments of function parameters.
442 if (isa
<ParmVarDecl
>(var
))
445 QualType type
= var
->getType();
447 // We can only do this if the variable is const.
448 if (!type
.isConstQualified()) return nullptr;
450 // Furthermore, in C++ we have to worry about mutable fields:
451 // C++ [dcl.type.cv]p4:
452 // Except that any class member declared mutable can be
453 // modified, any attempt to modify a const object during its
454 // lifetime results in undefined behavior.
455 if (CGM
.getLangOpts().CPlusPlus
&& !isSafeForCXXConstantCapture(type
))
458 // If the variable doesn't have any initializer (shouldn't this be
459 // invalid?), it's not clear what we should do. Maybe capture as
461 const Expr
*init
= var
->getInit();
462 if (!init
) return nullptr;
464 return ConstantEmitter(CGM
, CGF
).tryEmitAbstractForInitializer(*var
);
467 /// Get the low bit of a nonzero character count. This is the
468 /// alignment of the nth byte if the 0th byte is universally aligned.
469 static CharUnits
getLowBit(CharUnits v
) {
470 return CharUnits::fromQuantity(v
.getQuantity() & (~v
.getQuantity() + 1));
473 static void initializeForBlockHeader(CodeGenModule
&CGM
, CGBlockInfo
&info
,
474 SmallVectorImpl
<llvm::Type
*> &elementTypes
) {
476 assert(elementTypes
.empty());
477 if (CGM
.getLangOpts().OpenCL
) {
478 // The header is basically 'struct { int; int; generic void *;
479 // custom_fields; }'. Assert that struct is packed.
480 auto GenPtrAlign
= CharUnits::fromQuantity(
481 CGM
.getTarget().getPointerAlign(LangAS::opencl_generic
) / 8);
482 auto GenPtrSize
= CharUnits::fromQuantity(
483 CGM
.getTarget().getPointerWidth(LangAS::opencl_generic
) / 8);
484 assert(CGM
.getIntSize() <= GenPtrSize
);
485 assert(CGM
.getIntAlign() <= GenPtrAlign
);
486 assert((2 * CGM
.getIntSize()).isMultipleOf(GenPtrAlign
));
487 elementTypes
.push_back(CGM
.IntTy
); /* total size */
488 elementTypes
.push_back(CGM
.IntTy
); /* align */
489 elementTypes
.push_back(
490 CGM
.getOpenCLRuntime()
491 .getGenericVoidPointerType()); /* invoke function */
493 2 * CGM
.getIntSize().getQuantity() + GenPtrSize
.getQuantity();
494 unsigned BlockAlign
= GenPtrAlign
.getQuantity();
496 CGM
.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
497 for (auto *I
: Helper
->getCustomFieldTypes()) /* custom fields */ {
498 // TargetOpenCLBlockHelp needs to make sure the struct is packed.
499 // If necessary, add padding fields to the custom fields.
500 unsigned Align
= CGM
.getDataLayout().getABITypeAlign(I
).value();
501 if (BlockAlign
< Align
)
503 assert(Offset
% Align
== 0);
504 Offset
+= CGM
.getDataLayout().getTypeAllocSize(I
);
505 elementTypes
.push_back(I
);
508 info
.BlockAlign
= CharUnits::fromQuantity(BlockAlign
);
509 info
.BlockSize
= CharUnits::fromQuantity(Offset
);
511 // The header is basically 'struct { void *; int; int; void *; void *; }'.
512 // Assert that the struct is packed.
513 assert(CGM
.getIntSize() <= CGM
.getPointerSize());
514 assert(CGM
.getIntAlign() <= CGM
.getPointerAlign());
515 assert((2 * CGM
.getIntSize()).isMultipleOf(CGM
.getPointerAlign()));
516 info
.BlockAlign
= CGM
.getPointerAlign();
517 info
.BlockSize
= 3 * CGM
.getPointerSize() + 2 * CGM
.getIntSize();
518 elementTypes
.push_back(CGM
.VoidPtrTy
);
519 elementTypes
.push_back(CGM
.IntTy
);
520 elementTypes
.push_back(CGM
.IntTy
);
521 elementTypes
.push_back(CGM
.VoidPtrTy
);
522 elementTypes
.push_back(CGM
.getBlockDescriptorType());
526 static QualType
getCaptureFieldType(const CodeGenFunction
&CGF
,
527 const BlockDecl::Capture
&CI
) {
528 const VarDecl
*VD
= CI
.getVariable();
530 // If the variable is captured by an enclosing block or lambda expression,
531 // use the type of the capture field.
532 if (CGF
.BlockInfo
&& CI
.isNested())
533 return CGF
.BlockInfo
->getCapture(VD
).fieldType();
534 if (auto *FD
= CGF
.LambdaCaptureFields
.lookup(VD
))
535 return FD
->getType();
536 // If the captured variable is a non-escaping __block variable, the field
537 // type is the reference type. If the variable is a __block variable that
538 // already has a reference type, the field type is the variable's type.
539 return VD
->isNonEscapingByref() ?
540 CGF
.getContext().getLValueReferenceType(VD
->getType()) : VD
->getType();
543 /// Compute the layout of the given block. Attempts to lay the block
544 /// out with minimal space requirements.
545 static void computeBlockInfo(CodeGenModule
&CGM
, CodeGenFunction
*CGF
,
547 ASTContext
&C
= CGM
.getContext();
548 const BlockDecl
*block
= info
.getBlockDecl();
550 SmallVector
<llvm::Type
*, 8> elementTypes
;
551 initializeForBlockHeader(CGM
, info
, elementTypes
);
552 bool hasNonConstantCustomFields
= false;
553 if (auto *OpenCLHelper
=
554 CGM
.getTargetCodeGenInfo().getTargetOpenCLBlockHelper())
555 hasNonConstantCustomFields
=
556 !OpenCLHelper
->areAllCustomFieldValuesConstant(info
);
557 if (!block
->hasCaptures() && !hasNonConstantCustomFields
) {
559 llvm::StructType::get(CGM
.getLLVMContext(), elementTypes
, true);
560 info
.CanBeGlobal
= true;
563 else if (C
.getLangOpts().ObjC
&&
564 CGM
.getLangOpts().getGC() == LangOptions::NonGC
)
565 info
.HasCapturedVariableLayout
= true;
567 if (block
->doesNotEscape())
568 info
.NoEscape
= true;
570 // Collect the layout chunks.
571 SmallVector
<BlockLayoutChunk
, 16> layout
;
572 layout
.reserve(block
->capturesCXXThis() +
573 (block
->capture_end() - block
->capture_begin()));
575 CharUnits maxFieldAlign
;
578 if (block
->capturesCXXThis()) {
579 assert(CGF
&& CGF
->CurFuncDecl
&& isa
<CXXMethodDecl
>(CGF
->CurFuncDecl
) &&
580 "Can't capture 'this' outside a method");
581 QualType thisType
= cast
<CXXMethodDecl
>(CGF
->CurFuncDecl
)->getThisType();
583 // Theoretically, this could be in a different address space, so
584 // don't assume standard pointer size/align.
585 llvm::Type
*llvmType
= CGM
.getTypes().ConvertType(thisType
);
586 auto TInfo
= CGM
.getContext().getTypeInfoInChars(thisType
);
587 maxFieldAlign
= std::max(maxFieldAlign
, TInfo
.Align
);
589 addBlockLayout(TInfo
.Align
, TInfo
.Width
, nullptr, llvmType
, thisType
,
593 // Next, all the block captures.
594 for (const auto &CI
: block
->captures()) {
595 const VarDecl
*variable
= CI
.getVariable();
597 if (CI
.isEscapingByref()) {
598 // Just use void* instead of a pointer to the byref type.
599 CharUnits align
= CGM
.getPointerAlign();
600 maxFieldAlign
= std::max(maxFieldAlign
, align
);
602 // Since a __block variable cannot be captured by lambdas, its type and
603 // the capture field type should always match.
604 assert(CGF
&& getCaptureFieldType(*CGF
, CI
) == variable
->getType() &&
605 "capture type differs from the variable type");
606 addBlockLayout(align
, CGM
.getPointerSize(), &CI
, CGM
.VoidPtrTy
,
607 variable
->getType(), layout
, info
, CGM
);
611 // Otherwise, build a layout chunk with the size and alignment of
613 if (llvm::Constant
*constant
= tryCaptureAsConstant(CGM
, CGF
, variable
)) {
614 info
.SortedCaptures
.push_back(
615 CGBlockInfo::Capture::makeConstant(constant
, &CI
));
619 QualType VT
= getCaptureFieldType(*CGF
, CI
);
621 if (CGM
.getLangOpts().CPlusPlus
)
622 if (const CXXRecordDecl
*record
= VT
->getAsCXXRecordDecl())
623 if (CI
.hasCopyExpr() || !record
->hasTrivialDestructor()) {
624 info
.HasCXXObject
= true;
625 if (!record
->isExternallyVisible())
626 info
.CapturesNonExternalType
= true;
629 CharUnits size
= C
.getTypeSizeInChars(VT
);
630 CharUnits align
= C
.getDeclAlign(variable
);
632 maxFieldAlign
= std::max(maxFieldAlign
, align
);
634 llvm::Type
*llvmType
=
635 CGM
.getTypes().ConvertTypeForMem(VT
);
637 addBlockLayout(align
, size
, &CI
, llvmType
, VT
, layout
, info
, CGM
);
640 // If that was everything, we're done here.
641 if (layout
.empty()) {
643 llvm::StructType::get(CGM
.getLLVMContext(), elementTypes
, true);
644 info
.CanBeGlobal
= true;
645 info
.buildCaptureMap();
649 // Sort the layout by alignment. We have to use a stable sort here
650 // to get reproducible results. There should probably be an
651 // llvm::array_pod_stable_sort.
652 llvm::stable_sort(layout
);
654 // Needed for blocks layout info.
655 info
.BlockHeaderForcedGapOffset
= info
.BlockSize
;
656 info
.BlockHeaderForcedGapSize
= CharUnits::Zero();
658 CharUnits
&blockSize
= info
.BlockSize
;
659 info
.BlockAlign
= std::max(maxFieldAlign
, info
.BlockAlign
);
661 // Assuming that the first byte in the header is maximally aligned,
662 // get the alignment of the first byte following the header.
663 CharUnits endAlign
= getLowBit(blockSize
);
665 // If the end of the header isn't satisfactorily aligned for the
666 // maximum thing, look for things that are okay with the header-end
667 // alignment, and keep appending them until we get something that's
668 // aligned right. This algorithm is only guaranteed optimal if
669 // that condition is satisfied at some point; otherwise we can get
671 // header // next byte has alignment 4
672 // something_with_size_5; // next byte has alignment 1
673 // something_with_alignment_8;
674 // which has 7 bytes of padding, as opposed to the naive solution
675 // which might have less (?).
676 if (endAlign
< maxFieldAlign
) {
677 SmallVectorImpl
<BlockLayoutChunk
>::iterator
678 li
= layout
.begin() + 1, le
= layout
.end();
680 // Look for something that the header end is already
681 // satisfactorily aligned for.
682 for (; li
!= le
&& endAlign
< li
->Alignment
; ++li
)
685 // If we found something that's naturally aligned for the end of
686 // the header, keep adding things...
688 SmallVectorImpl
<BlockLayoutChunk
>::iterator first
= li
;
689 for (; li
!= le
; ++li
) {
690 assert(endAlign
>= li
->Alignment
);
692 li
->setIndex(info
, elementTypes
.size(), blockSize
);
693 elementTypes
.push_back(li
->Type
);
694 blockSize
+= li
->Size
;
695 endAlign
= getLowBit(blockSize
);
697 // ...until we get to the alignment of the maximum field.
698 if (endAlign
>= maxFieldAlign
) {
703 // Don't re-append everything we just appended.
704 layout
.erase(first
, li
);
708 assert(endAlign
== getLowBit(blockSize
));
710 // At this point, we just have to add padding if the end align still
711 // isn't aligned right.
712 if (endAlign
< maxFieldAlign
) {
713 CharUnits newBlockSize
= blockSize
.alignTo(maxFieldAlign
);
714 CharUnits padding
= newBlockSize
- blockSize
;
716 // If we haven't yet added any fields, remember that there was an
717 // initial gap; this need to go into the block layout bit map.
718 if (blockSize
== info
.BlockHeaderForcedGapOffset
) {
719 info
.BlockHeaderForcedGapSize
= padding
;
722 elementTypes
.push_back(llvm::ArrayType::get(CGM
.Int8Ty
,
723 padding
.getQuantity()));
724 blockSize
= newBlockSize
;
725 endAlign
= getLowBit(blockSize
); // might be > maxFieldAlign
728 assert(endAlign
>= maxFieldAlign
);
729 assert(endAlign
== getLowBit(blockSize
));
730 // Slam everything else on now. This works because they have
731 // strictly decreasing alignment and we expect that size is always a
732 // multiple of alignment.
733 for (SmallVectorImpl
<BlockLayoutChunk
>::iterator
734 li
= layout
.begin(), le
= layout
.end(); li
!= le
; ++li
) {
735 if (endAlign
< li
->Alignment
) {
736 // size may not be multiple of alignment. This can only happen with
737 // an over-aligned variable. We will be adding a padding field to
738 // make the size be multiple of alignment.
739 CharUnits padding
= li
->Alignment
- endAlign
;
740 elementTypes
.push_back(llvm::ArrayType::get(CGM
.Int8Ty
,
741 padding
.getQuantity()));
742 blockSize
+= padding
;
743 endAlign
= getLowBit(blockSize
);
745 assert(endAlign
>= li
->Alignment
);
746 li
->setIndex(info
, elementTypes
.size(), blockSize
);
747 elementTypes
.push_back(li
->Type
);
748 blockSize
+= li
->Size
;
749 endAlign
= getLowBit(blockSize
);
752 info
.buildCaptureMap();
754 llvm::StructType::get(CGM
.getLLVMContext(), elementTypes
, true);
757 /// Emit a block literal expression in the current function.
758 llvm::Value
*CodeGenFunction::EmitBlockLiteral(const BlockExpr
*blockExpr
) {
759 // If the block has no captures, we won't have a pre-computed
761 if (!blockExpr
->getBlockDecl()->hasCaptures())
762 // The block literal is emitted as a global variable, and the block invoke
763 // function has to be extracted from its initializer.
764 if (llvm::Constant
*Block
= CGM
.getAddrOfGlobalBlockIfEmitted(blockExpr
))
767 CGBlockInfo
blockInfo(blockExpr
->getBlockDecl(), CurFn
->getName());
768 computeBlockInfo(CGM
, this, blockInfo
);
769 blockInfo
.BlockExpression
= blockExpr
;
770 if (!blockInfo
.CanBeGlobal
)
771 blockInfo
.LocalAddress
= CreateTempAlloca(blockInfo
.StructureType
,
772 blockInfo
.BlockAlign
, "block");
773 return EmitBlockLiteral(blockInfo
);
776 llvm::Value
*CodeGenFunction::EmitBlockLiteral(const CGBlockInfo
&blockInfo
) {
777 bool IsOpenCL
= CGM
.getContext().getLangOpts().OpenCL
;
779 IsOpenCL
? CGM
.getOpenCLRuntime().getGenericVoidPointerType() : VoidPtrTy
;
780 LangAS GenVoidPtrAddr
= IsOpenCL
? LangAS::opencl_generic
: LangAS::Default
;
781 auto GenVoidPtrSize
= CharUnits::fromQuantity(
782 CGM
.getTarget().getPointerWidth(GenVoidPtrAddr
) / 8);
783 // Using the computed layout, generate the actual block function.
784 bool isLambdaConv
= blockInfo
.getBlockDecl()->isConversionFromLambda();
785 CodeGenFunction BlockCGF
{CGM
, true};
786 BlockCGF
.SanOpts
= SanOpts
;
787 auto *InvokeFn
= BlockCGF
.GenerateBlockFunction(
788 CurGD
, blockInfo
, LocalDeclMap
, isLambdaConv
, blockInfo
.CanBeGlobal
);
789 auto *blockFn
= llvm::ConstantExpr::getPointerCast(InvokeFn
, GenVoidPtrTy
);
791 // If there is nothing to capture, we can emit this as a global block.
792 if (blockInfo
.CanBeGlobal
)
793 return CGM
.getAddrOfGlobalBlockIfEmitted(blockInfo
.BlockExpression
);
795 // Otherwise, we have to emit this as a local block.
797 Address blockAddr
= blockInfo
.LocalAddress
;
798 assert(blockAddr
.isValid() && "block has no address!");
801 llvm::Constant
*descriptor
;
804 // If the block is non-escaping, set field 'isa 'to NSConcreteGlobalBlock
805 // and set the BLOCK_IS_GLOBAL bit of field 'flags'. Copying a non-escaping
806 // block just returns the original block and releasing it is a no-op.
807 llvm::Constant
*blockISA
= blockInfo
.NoEscape
808 ? CGM
.getNSConcreteGlobalBlock()
809 : CGM
.getNSConcreteStackBlock();
812 // Build the block descriptor.
813 descriptor
= buildBlockDescriptor(CGM
, blockInfo
);
815 // Compute the initial on-stack block flags.
816 flags
= BLOCK_HAS_SIGNATURE
;
817 if (blockInfo
.HasCapturedVariableLayout
)
818 flags
|= BLOCK_HAS_EXTENDED_LAYOUT
;
819 if (blockInfo
.NeedsCopyDispose
)
820 flags
|= BLOCK_HAS_COPY_DISPOSE
;
821 if (blockInfo
.HasCXXObject
)
822 flags
|= BLOCK_HAS_CXX_OBJ
;
823 if (blockInfo
.UsesStret
)
824 flags
|= BLOCK_USE_STRET
;
825 if (blockInfo
.NoEscape
)
826 flags
|= BLOCK_IS_NOESCAPE
| BLOCK_IS_GLOBAL
;
829 auto projectField
= [&](unsigned index
, const Twine
&name
) -> Address
{
830 return Builder
.CreateStructGEP(blockAddr
, index
, name
);
832 auto storeField
= [&](llvm::Value
*value
, unsigned index
, const Twine
&name
) {
833 Builder
.CreateStore(value
, projectField(index
, name
));
836 // Initialize the block header.
838 // We assume all the header fields are densely packed.
841 auto addHeaderField
= [&](llvm::Value
*value
, CharUnits size
,
843 storeField(value
, index
, name
);
849 addHeaderField(isa
, getPointerSize(), "block.isa");
850 addHeaderField(llvm::ConstantInt::get(IntTy
, flags
.getBitMask()),
851 getIntSize(), "block.flags");
852 addHeaderField(llvm::ConstantInt::get(IntTy
, 0), getIntSize(),
856 llvm::ConstantInt::get(IntTy
, blockInfo
.BlockSize
.getQuantity()),
857 getIntSize(), "block.size");
859 llvm::ConstantInt::get(IntTy
, blockInfo
.BlockAlign
.getQuantity()),
860 getIntSize(), "block.align");
862 addHeaderField(blockFn
, GenVoidPtrSize
, "block.invoke");
864 addHeaderField(descriptor
, getPointerSize(), "block.descriptor");
865 else if (auto *Helper
=
866 CGM
.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
867 for (auto I
: Helper
->getCustomFieldValues(*this, blockInfo
)) {
870 CharUnits::fromQuantity(
871 CGM
.getDataLayout().getTypeAllocSize(I
.first
->getType())),
877 // Finally, capture all the values into the block.
878 const BlockDecl
*blockDecl
= blockInfo
.getBlockDecl();
881 if (blockDecl
->capturesCXXThis()) {
883 projectField(blockInfo
.CXXThisIndex
, "block.captured-this.addr");
884 Builder
.CreateStore(LoadCXXThis(), addr
);
887 // Next, captured variables.
888 for (const auto &CI
: blockDecl
->captures()) {
889 const VarDecl
*variable
= CI
.getVariable();
890 const CGBlockInfo::Capture
&capture
= blockInfo
.getCapture(variable
);
892 // Ignore constant captures.
893 if (capture
.isConstant()) continue;
895 QualType type
= capture
.fieldType();
897 // This will be a [[type]]*, except that a byref entry will just be
899 Address blockField
= projectField(capture
.getIndex(), "block.captured");
901 // Compute the address of the thing we're going to move into the
903 Address src
= Address::invalid();
905 if (blockDecl
->isConversionFromLambda()) {
906 // The lambda capture in a lambda's conversion-to-block-pointer is
907 // special; we'll simply emit it directly.
908 src
= Address::invalid();
909 } else if (CI
.isEscapingByref()) {
910 if (BlockInfo
&& CI
.isNested()) {
911 // We need to use the capture from the enclosing block.
912 const CGBlockInfo::Capture
&enclosingCapture
=
913 BlockInfo
->getCapture(variable
);
915 // This is a [[type]]*, except that a byref entry will just be an i8**.
916 src
= Builder
.CreateStructGEP(LoadBlockStruct(),
917 enclosingCapture
.getIndex(),
918 "block.capture.addr");
920 auto I
= LocalDeclMap
.find(variable
);
921 assert(I
!= LocalDeclMap
.end());
925 DeclRefExpr
declRef(getContext(), const_cast<VarDecl
*>(variable
),
926 /*RefersToEnclosingVariableOrCapture*/ CI
.isNested(),
927 type
.getNonReferenceType(), VK_LValue
,
929 src
= EmitDeclRefLValue(&declRef
).getAddress(*this);
932 // For byrefs, we just write the pointer to the byref struct into
933 // the block field. There's no need to chase the forwarding
934 // pointer at this point, since we're building something that will
935 // live a shorter life than the stack byref anyway.
936 if (CI
.isEscapingByref()) {
937 // Get a void* that points to the byref struct.
938 llvm::Value
*byrefPointer
;
940 byrefPointer
= Builder
.CreateLoad(src
, "byref.capture");
942 byrefPointer
= src
.getPointer();
944 // Write that void* into the capture field.
945 Builder
.CreateStore(byrefPointer
, blockField
);
947 // If we have a copy constructor, evaluate that into the block field.
948 } else if (const Expr
*copyExpr
= CI
.getCopyExpr()) {
949 if (blockDecl
->isConversionFromLambda()) {
950 // If we have a lambda conversion, emit the expression
951 // directly into the block instead.
953 AggValueSlot::forAddr(blockField
, Qualifiers(),
954 AggValueSlot::IsDestructed
,
955 AggValueSlot::DoesNotNeedGCBarriers
,
956 AggValueSlot::IsNotAliased
,
957 AggValueSlot::DoesNotOverlap
);
958 EmitAggExpr(copyExpr
, Slot
);
960 EmitSynthesizedCXXCopyCtor(blockField
, src
, copyExpr
);
963 // If it's a reference variable, copy the reference into the block field.
964 } else if (type
->isReferenceType()) {
965 Builder
.CreateStore(src
.getPointer(), blockField
);
967 // If type is const-qualified, copy the value into the block field.
968 } else if (type
.isConstQualified() &&
969 type
.getObjCLifetime() == Qualifiers::OCL_Strong
&&
970 CGM
.getCodeGenOpts().OptimizationLevel
!= 0) {
971 llvm::Value
*value
= Builder
.CreateLoad(src
, "captured");
972 Builder
.CreateStore(value
, blockField
);
974 // If this is an ARC __strong block-pointer variable, don't do a
977 // TODO: this can be generalized into the normal initialization logic:
978 // we should never need to do a block-copy when initializing a local
979 // variable, because the local variable's lifetime should be strictly
980 // contained within the stack block's.
981 } else if (type
.getObjCLifetime() == Qualifiers::OCL_Strong
&&
982 type
->isBlockPointerType()) {
983 // Load the block and do a simple retain.
984 llvm::Value
*value
= Builder
.CreateLoad(src
, "block.captured_block");
985 value
= EmitARCRetainNonBlock(value
);
987 // Do a primitive store to the block field.
988 Builder
.CreateStore(value
, blockField
);
990 // Otherwise, fake up a POD copy into the block field.
992 // Fake up a new variable so that EmitScalarInit doesn't think
993 // we're referring to the variable in its own initializer.
994 ImplicitParamDecl
BlockFieldPseudoVar(getContext(), type
,
995 ImplicitParamDecl::Other
);
997 // We use one of these or the other depending on whether the
998 // reference is nested.
999 DeclRefExpr
declRef(getContext(), const_cast<VarDecl
*>(variable
),
1000 /*RefersToEnclosingVariableOrCapture*/ CI
.isNested(),
1001 type
, VK_LValue
, SourceLocation());
1003 ImplicitCastExpr
l2r(ImplicitCastExpr::OnStack
, type
, CK_LValueToRValue
,
1004 &declRef
, VK_PRValue
, FPOptionsOverride());
1005 // FIXME: Pass a specific location for the expr init so that the store is
1006 // attributed to a reasonable location - otherwise it may be attributed to
1007 // locations of subexpressions in the initialization.
1008 EmitExprAsInit(&l2r
, &BlockFieldPseudoVar
,
1009 MakeAddrLValue(blockField
, type
, AlignmentSource::Decl
),
1010 /*captured by init*/ false);
1013 // Push a cleanup for the capture if necessary.
1014 if (!blockInfo
.NoEscape
&& !blockInfo
.NeedsCopyDispose
)
1017 // Ignore __block captures; there's nothing special in the on-stack block
1018 // that we need to do for them.
1022 // Ignore objects that aren't destructed.
1023 QualType::DestructionKind dtorKind
= type
.isDestructedType();
1024 if (dtorKind
== QualType::DK_none
)
1027 CodeGenFunction::Destroyer
*destroyer
;
1029 // Block captures count as local values and have imprecise semantics.
1030 // They also can't be arrays, so need to worry about that.
1032 // For const-qualified captures, emit clang.arc.use to ensure the captured
1033 // object doesn't get released while we are still depending on its validity
1034 // within the block.
1035 if (type
.isConstQualified() &&
1036 type
.getObjCLifetime() == Qualifiers::OCL_Strong
&&
1037 CGM
.getCodeGenOpts().OptimizationLevel
!= 0) {
1038 assert(CGM
.getLangOpts().ObjCAutoRefCount
&&
1039 "expected ObjC ARC to be enabled");
1040 destroyer
= emitARCIntrinsicUse
;
1041 } else if (dtorKind
== QualType::DK_objc_strong_lifetime
) {
1042 destroyer
= destroyARCStrongImprecise
;
1044 destroyer
= getDestroyer(dtorKind
);
1047 CleanupKind cleanupKind
= NormalCleanup
;
1048 bool useArrayEHCleanup
= needsEHCleanup(dtorKind
);
1049 if (useArrayEHCleanup
)
1050 cleanupKind
= NormalAndEHCleanup
;
1052 // Extend the lifetime of the capture to the end of the scope enclosing the
1053 // block expression except when the block decl is in the list of RetExpr's
1054 // cleanup objects, in which case its lifetime ends after the full
1056 auto IsBlockDeclInRetExpr
= [&]() {
1057 auto *EWC
= llvm::dyn_cast_or_null
<ExprWithCleanups
>(RetExpr
);
1059 for (auto &C
: EWC
->getObjects())
1060 if (auto *BD
= C
.dyn_cast
<BlockDecl
*>())
1061 if (BD
== blockDecl
)
1066 if (IsBlockDeclInRetExpr())
1067 pushDestroy(cleanupKind
, blockField
, type
, destroyer
, useArrayEHCleanup
);
1069 pushLifetimeExtendedDestroy(cleanupKind
, blockField
, type
, destroyer
,
1073 // Cast to the converted block-pointer type, which happens (somewhat
1074 // unfortunately) to be a pointer to function type.
1075 llvm::Value
*result
= Builder
.CreatePointerCast(
1076 blockAddr
.getPointer(), ConvertType(blockInfo
.getBlockExpr()->getType()));
1079 CGM
.getOpenCLRuntime().recordBlockInfo(blockInfo
.BlockExpression
, InvokeFn
,
1080 result
, blockInfo
.StructureType
);
1087 llvm::Type
*CodeGenModule::getBlockDescriptorType() {
1088 if (BlockDescriptorType
)
1089 return BlockDescriptorType
;
1091 llvm::Type
*UnsignedLongTy
=
1092 getTypes().ConvertType(getContext().UnsignedLongTy
);
1094 // struct __block_descriptor {
1095 // unsigned long reserved;
1096 // unsigned long block_size;
1098 // // later, the following will be added
1101 // void (*copyHelper)();
1102 // void (*copyHelper)();
1103 // } helpers; // !!! optional
1105 // const char *signature; // the block signature
1106 // const char *layout; // reserved
1108 BlockDescriptorType
= llvm::StructType::create(
1109 "struct.__block_descriptor", UnsignedLongTy
, UnsignedLongTy
);
1111 // Now form a pointer to that.
1112 unsigned AddrSpace
= 0;
1113 if (getLangOpts().OpenCL
)
1114 AddrSpace
= getContext().getTargetAddressSpace(LangAS::opencl_constant
);
1115 BlockDescriptorType
= llvm::PointerType::get(BlockDescriptorType
, AddrSpace
);
1116 return BlockDescriptorType
;
1119 llvm::Type
*CodeGenModule::getGenericBlockLiteralType() {
1120 if (GenericBlockLiteralType
)
1121 return GenericBlockLiteralType
;
1123 llvm::Type
*BlockDescPtrTy
= getBlockDescriptorType();
1125 if (getLangOpts().OpenCL
) {
1126 // struct __opencl_block_literal_generic {
1129 // __generic void *__invoke;
1130 // /* custom fields */
1132 SmallVector
<llvm::Type
*, 8> StructFields(
1133 {IntTy
, IntTy
, getOpenCLRuntime().getGenericVoidPointerType()});
1134 if (auto *Helper
= getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
1135 llvm::append_range(StructFields
, Helper
->getCustomFieldTypes());
1137 GenericBlockLiteralType
= llvm::StructType::create(
1138 StructFields
, "struct.__opencl_block_literal_generic");
1140 // struct __block_literal_generic {
1144 // void (*__invoke)(void *);
1145 // struct __block_descriptor *__descriptor;
1147 GenericBlockLiteralType
=
1148 llvm::StructType::create("struct.__block_literal_generic", VoidPtrTy
,
1149 IntTy
, IntTy
, VoidPtrTy
, BlockDescPtrTy
);
1152 return GenericBlockLiteralType
;
1155 RValue
CodeGenFunction::EmitBlockCallExpr(const CallExpr
*E
,
1156 ReturnValueSlot ReturnValue
) {
1157 const auto *BPT
= E
->getCallee()->getType()->castAs
<BlockPointerType
>();
1158 llvm::Value
*BlockPtr
= EmitScalarExpr(E
->getCallee());
1159 llvm::Type
*GenBlockTy
= CGM
.getGenericBlockLiteralType();
1160 llvm::Value
*Func
= nullptr;
1161 QualType FnType
= BPT
->getPointeeType();
1162 ASTContext
&Ctx
= getContext();
1165 if (getLangOpts().OpenCL
) {
1166 // For OpenCL, BlockPtr is already casted to generic block literal.
1168 // First argument of a block call is a generic block literal casted to
1169 // generic void pointer, i.e. i8 addrspace(4)*
1170 llvm::Type
*GenericVoidPtrTy
=
1171 CGM
.getOpenCLRuntime().getGenericVoidPointerType();
1172 llvm::Value
*BlockDescriptor
= Builder
.CreatePointerCast(
1173 BlockPtr
, GenericVoidPtrTy
);
1174 QualType VoidPtrQualTy
= Ctx
.getPointerType(
1175 Ctx
.getAddrSpaceQualType(Ctx
.VoidTy
, LangAS::opencl_generic
));
1176 Args
.add(RValue::get(BlockDescriptor
), VoidPtrQualTy
);
1177 // And the rest of the arguments.
1178 EmitCallArgs(Args
, FnType
->getAs
<FunctionProtoType
>(), E
->arguments());
1180 // We *can* call the block directly unless it is a function argument.
1181 if (!isa
<ParmVarDecl
>(E
->getCalleeDecl()))
1182 Func
= CGM
.getOpenCLRuntime().getInvokeFunction(E
->getCallee());
1184 llvm::Value
*FuncPtr
= Builder
.CreateStructGEP(GenBlockTy
, BlockPtr
, 2);
1185 Func
= Builder
.CreateAlignedLoad(GenericVoidPtrTy
, FuncPtr
,
1189 // Bitcast the block literal to a generic block literal.
1191 Builder
.CreatePointerCast(BlockPtr
, UnqualPtrTy
, "block.literal");
1192 // Get pointer to the block invoke function
1193 llvm::Value
*FuncPtr
= Builder
.CreateStructGEP(GenBlockTy
, BlockPtr
, 3);
1195 // First argument is a block literal casted to a void pointer
1196 BlockPtr
= Builder
.CreatePointerCast(BlockPtr
, VoidPtrTy
);
1197 Args
.add(RValue::get(BlockPtr
), Ctx
.VoidPtrTy
);
1198 // And the rest of the arguments.
1199 EmitCallArgs(Args
, FnType
->getAs
<FunctionProtoType
>(), E
->arguments());
1201 // Load the function.
1202 Func
= Builder
.CreateAlignedLoad(VoidPtrTy
, FuncPtr
, getPointerAlign());
1205 const FunctionType
*FuncTy
= FnType
->castAs
<FunctionType
>();
1206 const CGFunctionInfo
&FnInfo
=
1207 CGM
.getTypes().arrangeBlockFunctionCall(Args
, FuncTy
);
1209 // Prepare the callee.
1210 CGCallee
Callee(CGCalleeInfo(), Func
);
1212 // And call the block.
1213 return EmitCall(FnInfo
, Callee
, ReturnValue
, Args
);
1216 Address
CodeGenFunction::GetAddrOfBlockDecl(const VarDecl
*variable
) {
1217 assert(BlockInfo
&& "evaluating block ref without block information?");
1218 const CGBlockInfo::Capture
&capture
= BlockInfo
->getCapture(variable
);
1220 // Handle constant captures.
1221 if (capture
.isConstant()) return LocalDeclMap
.find(variable
)->second
;
1223 Address addr
= Builder
.CreateStructGEP(LoadBlockStruct(), capture
.getIndex(),
1224 "block.capture.addr");
1226 if (variable
->isEscapingByref()) {
1227 // addr should be a void** right now. Load, then cast the result
1230 auto &byrefInfo
= getBlockByrefInfo(variable
);
1231 addr
= Address(Builder
.CreateLoad(addr
), byrefInfo
.Type
,
1232 byrefInfo
.ByrefAlignment
);
1234 addr
= emitBlockByrefAddress(addr
, byrefInfo
, /*follow*/ true,
1235 variable
->getName());
1238 assert((!variable
->isNonEscapingByref() ||
1239 capture
.fieldType()->isReferenceType()) &&
1240 "the capture field of a non-escaping variable should have a "
1242 if (capture
.fieldType()->isReferenceType())
1243 addr
= EmitLoadOfReference(MakeAddrLValue(addr
, capture
.fieldType()));
1248 void CodeGenModule::setAddrOfGlobalBlock(const BlockExpr
*BE
,
1249 llvm::Constant
*Addr
) {
1250 bool Ok
= EmittedGlobalBlocks
.insert(std::make_pair(BE
, Addr
)).second
;
1252 assert(Ok
&& "Trying to replace an already-existing global block!");
1256 CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr
*BE
,
1258 if (llvm::Constant
*Block
= getAddrOfGlobalBlockIfEmitted(BE
))
1261 CGBlockInfo
blockInfo(BE
->getBlockDecl(), Name
);
1262 blockInfo
.BlockExpression
= BE
;
1264 // Compute information about the layout, etc., of this block.
1265 computeBlockInfo(*this, nullptr, blockInfo
);
1267 // Using that metadata, generate the actual block function.
1269 CodeGenFunction::DeclMapTy LocalDeclMap
;
1270 CodeGenFunction(*this).GenerateBlockFunction(
1271 GlobalDecl(), blockInfo
, LocalDeclMap
,
1272 /*IsLambdaConversionToBlock*/ false, /*BuildGlobalBlock*/ true);
1275 return getAddrOfGlobalBlockIfEmitted(BE
);
1278 static llvm::Constant
*buildGlobalBlock(CodeGenModule
&CGM
,
1279 const CGBlockInfo
&blockInfo
,
1280 llvm::Constant
*blockFn
) {
1281 assert(blockInfo
.CanBeGlobal
);
1282 // Callers should detect this case on their own: calling this function
1283 // generally requires computing layout information, which is a waste of time
1284 // if we've already emitted this block.
1285 assert(!CGM
.getAddrOfGlobalBlockIfEmitted(blockInfo
.BlockExpression
) &&
1286 "Refusing to re-emit a global block.");
1288 // Generate the constants for the block literal initializer.
1289 ConstantInitBuilder
builder(CGM
);
1290 auto fields
= builder
.beginStruct();
1292 bool IsOpenCL
= CGM
.getLangOpts().OpenCL
;
1293 bool IsWindows
= CGM
.getTarget().getTriple().isOSWindows();
1297 fields
.addNullPointer(CGM
.Int8PtrPtrTy
);
1299 fields
.add(CGM
.getNSConcreteGlobalBlock());
1302 BlockFlags flags
= BLOCK_IS_GLOBAL
| BLOCK_HAS_SIGNATURE
;
1303 if (blockInfo
.UsesStret
)
1304 flags
|= BLOCK_USE_STRET
;
1306 fields
.addInt(CGM
.IntTy
, flags
.getBitMask());
1309 fields
.addInt(CGM
.IntTy
, 0);
1311 fields
.addInt(CGM
.IntTy
, blockInfo
.BlockSize
.getQuantity());
1312 fields
.addInt(CGM
.IntTy
, blockInfo
.BlockAlign
.getQuantity());
1316 fields
.add(blockFn
);
1320 fields
.add(buildBlockDescriptor(CGM
, blockInfo
));
1321 } else if (auto *Helper
=
1322 CGM
.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
1323 for (auto *I
: Helper
->getCustomFieldValues(CGM
, blockInfo
)) {
1328 unsigned AddrSpace
= 0;
1329 if (CGM
.getContext().getLangOpts().OpenCL
)
1330 AddrSpace
= CGM
.getContext().getTargetAddressSpace(LangAS::opencl_global
);
1332 llvm::GlobalVariable
*literal
= fields
.finishAndCreateGlobal(
1333 "__block_literal_global", blockInfo
.BlockAlign
,
1334 /*constant*/ !IsWindows
, llvm::GlobalVariable::InternalLinkage
, AddrSpace
);
1336 literal
->addAttribute("objc_arc_inert");
1338 // Windows does not allow globals to be initialised to point to globals in
1339 // different DLLs. Any such variables must run code to initialise them.
1341 auto *Init
= llvm::Function::Create(llvm::FunctionType::get(CGM
.VoidTy
,
1342 {}), llvm::GlobalValue::InternalLinkage
, ".block_isa_init",
1344 llvm::IRBuilder
<> b(llvm::BasicBlock::Create(CGM
.getLLVMContext(), "entry",
1346 b
.CreateAlignedStore(CGM
.getNSConcreteGlobalBlock(),
1347 b
.CreateStructGEP(literal
->getValueType(), literal
, 0),
1348 CGM
.getPointerAlign().getAsAlign());
1350 // We can't use the normal LLVM global initialisation array, because we
1351 // need to specify that this runs early in library initialisation.
1352 auto *InitVar
= new llvm::GlobalVariable(CGM
.getModule(), Init
->getType(),
1353 /*isConstant*/true, llvm::GlobalValue::InternalLinkage
,
1354 Init
, ".block_isa_init_ptr");
1355 InitVar
->setSection(".CRT$XCLa");
1356 CGM
.addUsedGlobal(InitVar
);
1359 // Return a constant of the appropriately-casted type.
1360 llvm::Type
*RequiredType
=
1361 CGM
.getTypes().ConvertType(blockInfo
.getBlockExpr()->getType());
1362 llvm::Constant
*Result
=
1363 llvm::ConstantExpr::getPointerCast(literal
, RequiredType
);
1364 CGM
.setAddrOfGlobalBlock(blockInfo
.BlockExpression
, Result
);
1365 if (CGM
.getContext().getLangOpts().OpenCL
)
1366 CGM
.getOpenCLRuntime().recordBlockInfo(
1367 blockInfo
.BlockExpression
,
1368 cast
<llvm::Function
>(blockFn
->stripPointerCasts()), Result
,
1369 literal
->getValueType());
1373 void CodeGenFunction::setBlockContextParameter(const ImplicitParamDecl
*D
,
1376 assert(BlockInfo
&& "not emitting prologue of block invocation function?!");
1378 // Allocate a stack slot like for any local variable to guarantee optimal
1379 // debug info at -O0. The mem2reg pass will eliminate it when optimizing.
1380 Address alloc
= CreateMemTemp(D
->getType(), D
->getName() + ".addr");
1381 Builder
.CreateStore(arg
, alloc
);
1382 if (CGDebugInfo
*DI
= getDebugInfo()) {
1383 if (CGM
.getCodeGenOpts().hasReducedDebugInfo()) {
1384 DI
->setLocation(D
->getLocation());
1385 DI
->EmitDeclareOfBlockLiteralArgVariable(
1386 *BlockInfo
, D
->getName(), argNum
,
1387 cast
<llvm::AllocaInst
>(alloc
.getPointer()), Builder
);
1391 SourceLocation StartLoc
= BlockInfo
->getBlockExpr()->getBody()->getBeginLoc();
1392 ApplyDebugLocation
Scope(*this, StartLoc
);
1394 // Instead of messing around with LocalDeclMap, just set the value
1395 // directly as BlockPointer.
1396 BlockPointer
= Builder
.CreatePointerCast(
1398 llvm::PointerType::get(
1400 getContext().getLangOpts().OpenCL
1401 ? getContext().getTargetAddressSpace(LangAS::opencl_generic
)
1406 Address
CodeGenFunction::LoadBlockStruct() {
1407 assert(BlockInfo
&& "not in a block invocation function!");
1408 assert(BlockPointer
&& "no block pointer set!");
1409 return Address(BlockPointer
, BlockInfo
->StructureType
, BlockInfo
->BlockAlign
);
1412 llvm::Function
*CodeGenFunction::GenerateBlockFunction(
1413 GlobalDecl GD
, const CGBlockInfo
&blockInfo
, const DeclMapTy
&ldm
,
1414 bool IsLambdaConversionToBlock
, bool BuildGlobalBlock
) {
1415 const BlockDecl
*blockDecl
= blockInfo
.getBlockDecl();
1419 CurEHLocation
= blockInfo
.getBlockExpr()->getEndLoc();
1421 BlockInfo
= &blockInfo
;
1423 // Arrange for local static and local extern declarations to appear
1424 // to be local to this function as well, in case they're directly
1425 // referenced in a block.
1426 for (DeclMapTy::const_iterator i
= ldm
.begin(), e
= ldm
.end(); i
!= e
; ++i
) {
1427 const auto *var
= dyn_cast
<VarDecl
>(i
->first
);
1428 if (var
&& !var
->hasLocalStorage())
1429 setAddrOfLocalVar(var
, i
->second
);
1432 // Begin building the function declaration.
1434 // Build the argument list.
1435 FunctionArgList args
;
1437 // The first argument is the block pointer. Just take it as a void*
1438 // and cast it later.
1439 QualType selfTy
= getContext().VoidPtrTy
;
1441 // For OpenCL passed block pointer can be private AS local variable or
1442 // global AS program scope variable (for the case with and without captures).
1443 // Generic AS is used therefore to be able to accommodate both private and
1444 // generic AS in one implementation.
1445 if (getLangOpts().OpenCL
)
1446 selfTy
= getContext().getPointerType(getContext().getAddrSpaceQualType(
1447 getContext().VoidTy
, LangAS::opencl_generic
));
1449 IdentifierInfo
*II
= &CGM
.getContext().Idents
.get(".block_descriptor");
1451 ImplicitParamDecl
SelfDecl(getContext(), const_cast<BlockDecl
*>(blockDecl
),
1452 SourceLocation(), II
, selfTy
,
1453 ImplicitParamDecl::ObjCSelf
);
1454 args
.push_back(&SelfDecl
);
1456 // Now add the rest of the parameters.
1457 args
.append(blockDecl
->param_begin(), blockDecl
->param_end());
1459 // Create the function declaration.
1460 const FunctionProtoType
*fnType
= blockInfo
.getBlockExpr()->getFunctionType();
1461 const CGFunctionInfo
&fnInfo
=
1462 CGM
.getTypes().arrangeBlockFunctionDeclaration(fnType
, args
);
1463 if (CGM
.ReturnSlotInterferesWithArgs(fnInfo
))
1464 blockInfo
.UsesStret
= true;
1466 llvm::FunctionType
*fnLLVMType
= CGM
.getTypes().GetFunctionType(fnInfo
);
1468 StringRef name
= CGM
.getBlockMangledName(GD
, blockDecl
);
1469 llvm::Function
*fn
= llvm::Function::Create(
1470 fnLLVMType
, llvm::GlobalValue::InternalLinkage
, name
, &CGM
.getModule());
1471 CGM
.SetInternalFunctionAttributes(blockDecl
, fn
, fnInfo
);
1473 if (BuildGlobalBlock
) {
1474 auto GenVoidPtrTy
= getContext().getLangOpts().OpenCL
1475 ? CGM
.getOpenCLRuntime().getGenericVoidPointerType()
1477 buildGlobalBlock(CGM
, blockInfo
,
1478 llvm::ConstantExpr::getPointerCast(fn
, GenVoidPtrTy
));
1481 // Begin generating the function.
1482 StartFunction(blockDecl
, fnType
->getReturnType(), fn
, fnInfo
, args
,
1483 blockDecl
->getLocation(),
1484 blockInfo
.getBlockExpr()->getBody()->getBeginLoc());
1486 // Okay. Undo some of what StartFunction did.
1488 // At -O0 we generate an explicit alloca for the BlockPointer, so the RA
1489 // won't delete the dbg.declare intrinsics for captured variables.
1490 llvm::Value
*BlockPointerDbgLoc
= BlockPointer
;
1491 if (CGM
.getCodeGenOpts().OptimizationLevel
== 0) {
1492 // Allocate a stack slot for it, so we can point the debugger to it
1493 Address Alloca
= CreateTempAlloca(BlockPointer
->getType(),
1496 // Set the DebugLocation to empty, so the store is recognized as a
1497 // frame setup instruction by llvm::DwarfDebug::beginFunction().
1498 auto NL
= ApplyDebugLocation::CreateEmpty(*this);
1499 Builder
.CreateStore(BlockPointer
, Alloca
);
1500 BlockPointerDbgLoc
= Alloca
.getPointer();
1503 // If we have a C++ 'this' reference, go ahead and force it into
1505 if (blockDecl
->capturesCXXThis()) {
1506 Address addr
= Builder
.CreateStructGEP(
1507 LoadBlockStruct(), blockInfo
.CXXThisIndex
, "block.captured-this");
1508 CXXThisValue
= Builder
.CreateLoad(addr
, "this");
1511 // Also force all the constant captures.
1512 for (const auto &CI
: blockDecl
->captures()) {
1513 const VarDecl
*variable
= CI
.getVariable();
1514 const CGBlockInfo::Capture
&capture
= blockInfo
.getCapture(variable
);
1515 if (!capture
.isConstant()) continue;
1517 CharUnits align
= getContext().getDeclAlign(variable
);
1519 CreateMemTemp(variable
->getType(), align
, "block.captured-const");
1521 Builder
.CreateStore(capture
.getConstant(), alloca
);
1523 setAddrOfLocalVar(variable
, alloca
);
1526 // Save a spot to insert the debug information for all the DeclRefExprs.
1527 llvm::BasicBlock
*entry
= Builder
.GetInsertBlock();
1528 llvm::BasicBlock::iterator entry_ptr
= Builder
.GetInsertPoint();
1531 if (IsLambdaConversionToBlock
)
1532 EmitLambdaBlockInvokeBody();
1534 PGO
.assignRegionCounters(GlobalDecl(blockDecl
), fn
);
1535 incrementProfileCounter(blockDecl
->getBody());
1536 EmitStmt(blockDecl
->getBody());
1539 // Remember where we were...
1540 llvm::BasicBlock
*resume
= Builder
.GetInsertBlock();
1542 // Go back to the entry.
1544 Builder
.SetInsertPoint(entry
, entry_ptr
);
1546 // Emit debug information for all the DeclRefExprs.
1547 // FIXME: also for 'this'
1548 if (CGDebugInfo
*DI
= getDebugInfo()) {
1549 for (const auto &CI
: blockDecl
->captures()) {
1550 const VarDecl
*variable
= CI
.getVariable();
1551 DI
->EmitLocation(Builder
, variable
->getLocation());
1553 if (CGM
.getCodeGenOpts().hasReducedDebugInfo()) {
1554 const CGBlockInfo::Capture
&capture
= blockInfo
.getCapture(variable
);
1555 if (capture
.isConstant()) {
1556 auto addr
= LocalDeclMap
.find(variable
)->second
;
1557 (void)DI
->EmitDeclareOfAutoVariable(variable
, addr
.getPointer(),
1562 DI
->EmitDeclareOfBlockDeclRefVariable(
1563 variable
, BlockPointerDbgLoc
, Builder
, blockInfo
,
1564 entry_ptr
== entry
->end() ? nullptr : &*entry_ptr
);
1567 // Recover location if it was changed in the above loop.
1568 DI
->EmitLocation(Builder
,
1569 cast
<CompoundStmt
>(blockDecl
->getBody())->getRBracLoc());
1572 // And resume where we left off.
1573 if (resume
== nullptr)
1574 Builder
.ClearInsertionPoint();
1576 Builder
.SetInsertPoint(resume
);
1578 FinishFunction(cast
<CompoundStmt
>(blockDecl
->getBody())->getRBracLoc());
1583 static std::pair
<BlockCaptureEntityKind
, BlockFieldFlags
>
1584 computeCopyInfoForBlockCapture(const BlockDecl::Capture
&CI
, QualType T
,
1585 const LangOptions
&LangOpts
) {
1586 if (CI
.getCopyExpr()) {
1587 assert(!CI
.isByRef());
1588 // don't bother computing flags
1589 return std::make_pair(BlockCaptureEntityKind::CXXRecord
, BlockFieldFlags());
1591 BlockFieldFlags Flags
;
1592 if (CI
.isEscapingByref()) {
1593 Flags
= BLOCK_FIELD_IS_BYREF
;
1594 if (T
.isObjCGCWeak())
1595 Flags
|= BLOCK_FIELD_IS_WEAK
;
1596 return std::make_pair(BlockCaptureEntityKind::BlockObject
, Flags
);
1599 Flags
= BLOCK_FIELD_IS_OBJECT
;
1600 bool isBlockPointer
= T
->isBlockPointerType();
1602 Flags
= BLOCK_FIELD_IS_BLOCK
;
1604 switch (T
.isNonTrivialToPrimitiveCopy()) {
1605 case QualType::PCK_Struct
:
1606 return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct
,
1608 case QualType::PCK_ARCWeak
:
1609 // We need to register __weak direct captures with the runtime.
1610 return std::make_pair(BlockCaptureEntityKind::ARCWeak
, Flags
);
1611 case QualType::PCK_ARCStrong
:
1612 // We need to retain the copied value for __strong direct captures.
1613 // If it's a block pointer, we have to copy the block and assign that to
1614 // the destination pointer, so we might as well use _Block_object_assign.
1615 // Otherwise we can avoid that.
1616 return std::make_pair(!isBlockPointer
? BlockCaptureEntityKind::ARCStrong
1617 : BlockCaptureEntityKind::BlockObject
,
1619 case QualType::PCK_Trivial
:
1620 case QualType::PCK_VolatileTrivial
: {
1621 if (!T
->isObjCRetainableType())
1622 // For all other types, the memcpy is fine.
1623 return std::make_pair(BlockCaptureEntityKind::None
, BlockFieldFlags());
1625 // Honor the inert __unsafe_unretained qualifier, which doesn't actually
1626 // make it into the type system.
1627 if (T
->isObjCInertUnsafeUnretainedType())
1628 return std::make_pair(BlockCaptureEntityKind::None
, BlockFieldFlags());
1630 // Special rules for ARC captures:
1631 Qualifiers QS
= T
.getQualifiers();
1633 // Non-ARC captures of retainable pointers are strong and
1634 // therefore require a call to _Block_object_assign.
1635 if (!QS
.getObjCLifetime() && !LangOpts
.ObjCAutoRefCount
)
1636 return std::make_pair(BlockCaptureEntityKind::BlockObject
, Flags
);
1638 // Otherwise the memcpy is fine.
1639 return std::make_pair(BlockCaptureEntityKind::None
, BlockFieldFlags());
1642 llvm_unreachable("after exhaustive PrimitiveCopyKind switch");
1646 /// Release a __block variable.
1647 struct CallBlockRelease final
: EHScopeStack::Cleanup
{
1649 BlockFieldFlags FieldFlags
;
1650 bool LoadBlockVarAddr
, CanThrow
;
1652 CallBlockRelease(Address Addr
, BlockFieldFlags Flags
, bool LoadValue
,
1654 : Addr(Addr
), FieldFlags(Flags
), LoadBlockVarAddr(LoadValue
),
1657 void Emit(CodeGenFunction
&CGF
, Flags flags
) override
{
1658 llvm::Value
*BlockVarAddr
;
1659 if (LoadBlockVarAddr
) {
1660 BlockVarAddr
= CGF
.Builder
.CreateLoad(Addr
);
1662 BlockVarAddr
= Addr
.getPointer();
1665 CGF
.BuildBlockRelease(BlockVarAddr
, FieldFlags
, CanThrow
);
1668 } // end anonymous namespace
1670 /// Check if \p T is a C++ class that has a destructor that can throw.
1671 bool CodeGenFunction::cxxDestructorCanThrow(QualType T
) {
1672 if (const auto *RD
= T
->getAsCXXRecordDecl())
1673 if (const CXXDestructorDecl
*DD
= RD
->getDestructor())
1674 return DD
->getType()->castAs
<FunctionProtoType
>()->canThrow();
1678 // Return a string that has the information about a capture.
1679 static std::string
getBlockCaptureStr(const CGBlockInfo::Capture
&Cap
,
1680 CaptureStrKind StrKind
,
1681 CharUnits BlockAlignment
,
1682 CodeGenModule
&CGM
) {
1684 ASTContext
&Ctx
= CGM
.getContext();
1685 const BlockDecl::Capture
&CI
= *Cap
.Cap
;
1686 QualType CaptureTy
= CI
.getVariable()->getType();
1688 BlockCaptureEntityKind Kind
;
1689 BlockFieldFlags Flags
;
1691 // CaptureStrKind::Merged should be passed only when the operations and the
1692 // flags are the same for copy and dispose.
1693 assert((StrKind
!= CaptureStrKind::Merged
||
1694 (Cap
.CopyKind
== Cap
.DisposeKind
&&
1695 Cap
.CopyFlags
== Cap
.DisposeFlags
)) &&
1696 "different operations and flags");
1698 if (StrKind
== CaptureStrKind::DisposeHelper
) {
1699 Kind
= Cap
.DisposeKind
;
1700 Flags
= Cap
.DisposeFlags
;
1702 Kind
= Cap
.CopyKind
;
1703 Flags
= Cap
.CopyFlags
;
1707 case BlockCaptureEntityKind::CXXRecord
: {
1709 SmallString
<256> TyStr
;
1710 llvm::raw_svector_ostream
Out(TyStr
);
1711 CGM
.getCXXABI().getMangleContext().mangleCanonicalTypeName(CaptureTy
, Out
);
1712 Str
+= llvm::to_string(TyStr
.size()) + TyStr
.c_str();
1715 case BlockCaptureEntityKind::ARCWeak
:
1718 case BlockCaptureEntityKind::ARCStrong
:
1721 case BlockCaptureEntityKind::BlockObject
: {
1722 const VarDecl
*Var
= CI
.getVariable();
1723 unsigned F
= Flags
.getBitMask();
1724 if (F
& BLOCK_FIELD_IS_BYREF
) {
1726 if (F
& BLOCK_FIELD_IS_WEAK
)
1729 // If CaptureStrKind::Merged is passed, check both the copy expression
1730 // and the destructor.
1731 if (StrKind
!= CaptureStrKind::DisposeHelper
) {
1732 if (Ctx
.getBlockVarCopyInit(Var
).canThrow())
1735 if (StrKind
!= CaptureStrKind::CopyHelper
) {
1736 if (CodeGenFunction::cxxDestructorCanThrow(CaptureTy
))
1741 assert((F
& BLOCK_FIELD_IS_OBJECT
) && "unexpected flag value");
1742 if (F
== BLOCK_FIELD_IS_BLOCK
)
1749 case BlockCaptureEntityKind::NonTrivialCStruct
: {
1750 bool IsVolatile
= CaptureTy
.isVolatileQualified();
1751 CharUnits Alignment
= BlockAlignment
.alignmentAtOffset(Cap
.getOffset());
1754 std::string FuncStr
;
1755 if (StrKind
== CaptureStrKind::DisposeHelper
)
1756 FuncStr
= CodeGenFunction::getNonTrivialDestructorStr(
1757 CaptureTy
, Alignment
, IsVolatile
, Ctx
);
1759 // If CaptureStrKind::Merged is passed, use the copy constructor string.
1760 // It has all the information that the destructor string has.
1761 FuncStr
= CodeGenFunction::getNonTrivialCopyConstructorStr(
1762 CaptureTy
, Alignment
, IsVolatile
, Ctx
);
1763 // The underscore is necessary here because non-trivial copy constructor
1764 // and destructor strings can start with a number.
1765 Str
+= llvm::to_string(FuncStr
.size()) + "_" + FuncStr
;
1768 case BlockCaptureEntityKind::None
:
1775 static std::string
getCopyDestroyHelperFuncName(
1776 const SmallVectorImpl
<CGBlockInfo::Capture
> &Captures
,
1777 CharUnits BlockAlignment
, CaptureStrKind StrKind
, CodeGenModule
&CGM
) {
1778 assert((StrKind
== CaptureStrKind::CopyHelper
||
1779 StrKind
== CaptureStrKind::DisposeHelper
) &&
1780 "unexpected CaptureStrKind");
1781 std::string Name
= StrKind
== CaptureStrKind::CopyHelper
1782 ? "__copy_helper_block_"
1783 : "__destroy_helper_block_";
1784 if (CGM
.getLangOpts().Exceptions
)
1786 if (CGM
.getCodeGenOpts().ObjCAutoRefCountExceptions
)
1788 Name
+= llvm::to_string(BlockAlignment
.getQuantity()) + "_";
1790 for (auto &Cap
: Captures
) {
1791 if (Cap
.isConstantOrTrivial())
1793 Name
+= llvm::to_string(Cap
.getOffset().getQuantity());
1794 Name
+= getBlockCaptureStr(Cap
, StrKind
, BlockAlignment
, CGM
);
1800 static void pushCaptureCleanup(BlockCaptureEntityKind CaptureKind
,
1801 Address Field
, QualType CaptureType
,
1802 BlockFieldFlags Flags
, bool ForCopyHelper
,
1803 VarDecl
*Var
, CodeGenFunction
&CGF
) {
1804 bool EHOnly
= ForCopyHelper
;
1806 switch (CaptureKind
) {
1807 case BlockCaptureEntityKind::CXXRecord
:
1808 case BlockCaptureEntityKind::ARCWeak
:
1809 case BlockCaptureEntityKind::NonTrivialCStruct
:
1810 case BlockCaptureEntityKind::ARCStrong
: {
1811 if (CaptureType
.isDestructedType() &&
1812 (!EHOnly
|| CGF
.needsEHCleanup(CaptureType
.isDestructedType()))) {
1813 CodeGenFunction::Destroyer
*Destroyer
=
1814 CaptureKind
== BlockCaptureEntityKind::ARCStrong
1815 ? CodeGenFunction::destroyARCStrongImprecise
1816 : CGF
.getDestroyer(CaptureType
.isDestructedType());
1819 : CGF
.getCleanupKind(CaptureType
.isDestructedType());
1820 CGF
.pushDestroy(Kind
, Field
, CaptureType
, Destroyer
, Kind
& EHCleanup
);
1824 case BlockCaptureEntityKind::BlockObject
: {
1825 if (!EHOnly
|| CGF
.getLangOpts().Exceptions
) {
1826 CleanupKind Kind
= EHOnly
? EHCleanup
: NormalAndEHCleanup
;
1827 // Calls to _Block_object_dispose along the EH path in the copy helper
1828 // function don't throw as newly-copied __block variables always have a
1829 // reference count of 2.
1831 !ForCopyHelper
&& CGF
.cxxDestructorCanThrow(CaptureType
);
1832 CGF
.enterByrefCleanup(Kind
, Field
, Flags
, /*LoadBlockVarAddr*/ true,
1837 case BlockCaptureEntityKind::None
:
1842 static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType
,
1844 const CGFunctionInfo
&FI
,
1845 CodeGenModule
&CGM
) {
1846 if (CapturesNonExternalType
) {
1847 CGM
.SetInternalFunctionAttributes(GlobalDecl(), Fn
, FI
);
1849 Fn
->setVisibility(llvm::GlobalValue::HiddenVisibility
);
1850 Fn
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
1851 CGM
.SetLLVMFunctionAttributes(GlobalDecl(), FI
, Fn
, /*IsThunk=*/false);
1852 CGM
.SetLLVMFunctionAttributesForDefinition(nullptr, Fn
);
1855 /// Generate the copy-helper function for a block closure object:
1856 /// static void block_copy_helper(block_t *dst, block_t *src);
1857 /// The runtime will have previously initialized 'dst' by doing a
1858 /// bit-copy of 'src'.
1860 /// Note that this copies an entire block closure object to the heap;
1861 /// it should not be confused with a 'byref copy helper', which moves
1862 /// the contents of an individual __block variable to the heap.
1864 CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo
&blockInfo
) {
1865 std::string FuncName
= getCopyDestroyHelperFuncName(
1866 blockInfo
.SortedCaptures
, blockInfo
.BlockAlign
,
1867 CaptureStrKind::CopyHelper
, CGM
);
1869 if (llvm::GlobalValue
*Func
= CGM
.getModule().getNamedValue(FuncName
))
1872 ASTContext
&C
= getContext();
1874 QualType ReturnTy
= C
.VoidTy
;
1876 FunctionArgList args
;
1877 ImplicitParamDecl
DstDecl(C
, C
.VoidPtrTy
, ImplicitParamDecl::Other
);
1878 args
.push_back(&DstDecl
);
1879 ImplicitParamDecl
SrcDecl(C
, C
.VoidPtrTy
, ImplicitParamDecl::Other
);
1880 args
.push_back(&SrcDecl
);
1882 const CGFunctionInfo
&FI
=
1883 CGM
.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy
, args
);
1885 // FIXME: it would be nice if these were mergeable with things with
1886 // identical semantics.
1887 llvm::FunctionType
*LTy
= CGM
.getTypes().GetFunctionType(FI
);
1889 llvm::Function
*Fn
=
1890 llvm::Function::Create(LTy
, llvm::GlobalValue::LinkOnceODRLinkage
,
1891 FuncName
, &CGM
.getModule());
1892 if (CGM
.supportsCOMDAT())
1893 Fn
->setComdat(CGM
.getModule().getOrInsertComdat(FuncName
));
1895 SmallVector
<QualType
, 2> ArgTys
;
1896 ArgTys
.push_back(C
.VoidPtrTy
);
1897 ArgTys
.push_back(C
.VoidPtrTy
);
1899 setBlockHelperAttributesVisibility(blockInfo
.CapturesNonExternalType
, Fn
, FI
,
1901 StartFunction(GlobalDecl(), ReturnTy
, Fn
, FI
, args
);
1902 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1904 Address src
= GetAddrOfLocalVar(&SrcDecl
);
1905 src
= Address(Builder
.CreateLoad(src
), blockInfo
.StructureType
,
1906 blockInfo
.BlockAlign
);
1908 Address dst
= GetAddrOfLocalVar(&DstDecl
);
1909 dst
= Address(Builder
.CreateLoad(dst
), blockInfo
.StructureType
,
1910 blockInfo
.BlockAlign
);
1912 for (auto &capture
: blockInfo
.SortedCaptures
) {
1913 if (capture
.isConstantOrTrivial())
1916 const BlockDecl::Capture
&CI
= *capture
.Cap
;
1917 QualType captureType
= CI
.getVariable()->getType();
1918 BlockFieldFlags flags
= capture
.CopyFlags
;
1920 unsigned index
= capture
.getIndex();
1921 Address srcField
= Builder
.CreateStructGEP(src
, index
);
1922 Address dstField
= Builder
.CreateStructGEP(dst
, index
);
1924 switch (capture
.CopyKind
) {
1925 case BlockCaptureEntityKind::CXXRecord
:
1926 // If there's an explicit copy expression, we do that.
1927 assert(CI
.getCopyExpr() && "copy expression for variable is missing");
1928 EmitSynthesizedCXXCopyCtor(dstField
, srcField
, CI
.getCopyExpr());
1930 case BlockCaptureEntityKind::ARCWeak
:
1931 EmitARCCopyWeak(dstField
, srcField
);
1933 case BlockCaptureEntityKind::NonTrivialCStruct
: {
1934 // If this is a C struct that requires non-trivial copy construction,
1935 // emit a call to its copy constructor.
1936 QualType varType
= CI
.getVariable()->getType();
1937 callCStructCopyConstructor(MakeAddrLValue(dstField
, varType
),
1938 MakeAddrLValue(srcField
, varType
));
1941 case BlockCaptureEntityKind::ARCStrong
: {
1942 llvm::Value
*srcValue
= Builder
.CreateLoad(srcField
, "blockcopy.src");
1943 // At -O0, store null into the destination field (so that the
1944 // storeStrong doesn't over-release) and then call storeStrong.
1945 // This is a workaround to not having an initStrong call.
1946 if (CGM
.getCodeGenOpts().OptimizationLevel
== 0) {
1947 auto *ty
= cast
<llvm::PointerType
>(srcValue
->getType());
1948 llvm::Value
*null
= llvm::ConstantPointerNull::get(ty
);
1949 Builder
.CreateStore(null
, dstField
);
1950 EmitARCStoreStrongCall(dstField
, srcValue
, true);
1952 // With optimization enabled, take advantage of the fact that
1953 // the blocks runtime guarantees a memcpy of the block data, and
1954 // just emit a retain of the src field.
1956 EmitARCRetainNonBlock(srcValue
);
1958 // Unless EH cleanup is required, we don't need this anymore, so kill
1959 // it. It's not quite worth the annoyance to avoid creating it in the
1961 if (!needsEHCleanup(captureType
.isDestructedType()))
1962 cast
<llvm::Instruction
>(dstField
.getPointer())->eraseFromParent();
1966 case BlockCaptureEntityKind::BlockObject
: {
1967 llvm::Value
*srcValue
= Builder
.CreateLoad(srcField
, "blockcopy.src");
1968 llvm::Value
*dstAddr
= dstField
.getPointer();
1969 llvm::Value
*args
[] = {
1970 dstAddr
, srcValue
, llvm::ConstantInt::get(Int32Ty
, flags
.getBitMask())
1973 if (CI
.isByRef() && C
.getBlockVarCopyInit(CI
.getVariable()).canThrow())
1974 EmitRuntimeCallOrInvoke(CGM
.getBlockObjectAssign(), args
);
1976 EmitNounwindRuntimeCall(CGM
.getBlockObjectAssign(), args
);
1979 case BlockCaptureEntityKind::None
:
1983 // Ensure that we destroy the copied object if an exception is thrown later
1984 // in the helper function.
1985 pushCaptureCleanup(capture
.CopyKind
, dstField
, captureType
, flags
,
1986 /*ForCopyHelper*/ true, CI
.getVariable(), *this);
1994 static BlockFieldFlags
1995 getBlockFieldFlagsForObjCObjectPointer(const BlockDecl::Capture
&CI
,
1997 BlockFieldFlags Flags
= BLOCK_FIELD_IS_OBJECT
;
1998 if (T
->isBlockPointerType())
1999 Flags
= BLOCK_FIELD_IS_BLOCK
;
2003 static std::pair
<BlockCaptureEntityKind
, BlockFieldFlags
>
2004 computeDestroyInfoForBlockCapture(const BlockDecl::Capture
&CI
, QualType T
,
2005 const LangOptions
&LangOpts
) {
2006 if (CI
.isEscapingByref()) {
2007 BlockFieldFlags Flags
= BLOCK_FIELD_IS_BYREF
;
2008 if (T
.isObjCGCWeak())
2009 Flags
|= BLOCK_FIELD_IS_WEAK
;
2010 return std::make_pair(BlockCaptureEntityKind::BlockObject
, Flags
);
2013 switch (T
.isDestructedType()) {
2014 case QualType::DK_cxx_destructor
:
2015 return std::make_pair(BlockCaptureEntityKind::CXXRecord
, BlockFieldFlags());
2016 case QualType::DK_objc_strong_lifetime
:
2017 // Use objc_storeStrong for __strong direct captures; the
2018 // dynamic tools really like it when we do this.
2019 return std::make_pair(BlockCaptureEntityKind::ARCStrong
,
2020 getBlockFieldFlagsForObjCObjectPointer(CI
, T
));
2021 case QualType::DK_objc_weak_lifetime
:
2022 // Support __weak direct captures.
2023 return std::make_pair(BlockCaptureEntityKind::ARCWeak
,
2024 getBlockFieldFlagsForObjCObjectPointer(CI
, T
));
2025 case QualType::DK_nontrivial_c_struct
:
2026 return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct
,
2028 case QualType::DK_none
: {
2029 // Non-ARC captures are strong, and we need to use _Block_object_dispose.
2030 // But honor the inert __unsafe_unretained qualifier, which doesn't actually
2031 // make it into the type system.
2032 if (T
->isObjCRetainableType() && !T
.getQualifiers().hasObjCLifetime() &&
2033 !LangOpts
.ObjCAutoRefCount
&& !T
->isObjCInertUnsafeUnretainedType())
2034 return std::make_pair(BlockCaptureEntityKind::BlockObject
,
2035 getBlockFieldFlagsForObjCObjectPointer(CI
, T
));
2036 // Otherwise, we have nothing to do.
2037 return std::make_pair(BlockCaptureEntityKind::None
, BlockFieldFlags());
2040 llvm_unreachable("after exhaustive DestructionKind switch");
2043 /// Generate the destroy-helper function for a block closure object:
2044 /// static void block_destroy_helper(block_t *theBlock);
2046 /// Note that this destroys a heap-allocated block closure object;
2047 /// it should not be confused with a 'byref destroy helper', which
2048 /// destroys the heap-allocated contents of an individual __block
2051 CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo
&blockInfo
) {
2052 std::string FuncName
= getCopyDestroyHelperFuncName(
2053 blockInfo
.SortedCaptures
, blockInfo
.BlockAlign
,
2054 CaptureStrKind::DisposeHelper
, CGM
);
2056 if (llvm::GlobalValue
*Func
= CGM
.getModule().getNamedValue(FuncName
))
2059 ASTContext
&C
= getContext();
2061 QualType ReturnTy
= C
.VoidTy
;
2063 FunctionArgList args
;
2064 ImplicitParamDecl
SrcDecl(C
, C
.VoidPtrTy
, ImplicitParamDecl::Other
);
2065 args
.push_back(&SrcDecl
);
2067 const CGFunctionInfo
&FI
=
2068 CGM
.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy
, args
);
2070 // FIXME: We'd like to put these into a mergable by content, with
2071 // internal linkage.
2072 llvm::FunctionType
*LTy
= CGM
.getTypes().GetFunctionType(FI
);
2074 llvm::Function
*Fn
=
2075 llvm::Function::Create(LTy
, llvm::GlobalValue::LinkOnceODRLinkage
,
2076 FuncName
, &CGM
.getModule());
2077 if (CGM
.supportsCOMDAT())
2078 Fn
->setComdat(CGM
.getModule().getOrInsertComdat(FuncName
));
2080 SmallVector
<QualType
, 1> ArgTys
;
2081 ArgTys
.push_back(C
.VoidPtrTy
);
2083 setBlockHelperAttributesVisibility(blockInfo
.CapturesNonExternalType
, Fn
, FI
,
2085 StartFunction(GlobalDecl(), ReturnTy
, Fn
, FI
, args
);
2086 markAsIgnoreThreadCheckingAtRuntime(Fn
);
2088 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
2090 Address src
= GetAddrOfLocalVar(&SrcDecl
);
2091 src
= Address(Builder
.CreateLoad(src
), blockInfo
.StructureType
,
2092 blockInfo
.BlockAlign
);
2094 CodeGenFunction::RunCleanupsScope
cleanups(*this);
2096 for (auto &capture
: blockInfo
.SortedCaptures
) {
2097 if (capture
.isConstantOrTrivial())
2100 const BlockDecl::Capture
&CI
= *capture
.Cap
;
2101 BlockFieldFlags flags
= capture
.DisposeFlags
;
2103 Address srcField
= Builder
.CreateStructGEP(src
, capture
.getIndex());
2105 pushCaptureCleanup(capture
.DisposeKind
, srcField
,
2106 CI
.getVariable()->getType(), flags
,
2107 /*ForCopyHelper*/ false, CI
.getVariable(), *this);
2110 cleanups
.ForceCleanup();
2119 /// Emits the copy/dispose helper functions for a __block object of id type.
2120 class ObjectByrefHelpers final
: public BlockByrefHelpers
{
2121 BlockFieldFlags Flags
;
2124 ObjectByrefHelpers(CharUnits alignment
, BlockFieldFlags flags
)
2125 : BlockByrefHelpers(alignment
), Flags(flags
) {}
2127 void emitCopy(CodeGenFunction
&CGF
, Address destField
,
2128 Address srcField
) override
{
2129 destField
= destField
.withElementType(CGF
.Int8Ty
);
2131 srcField
= srcField
.withElementType(CGF
.Int8PtrTy
);
2132 llvm::Value
*srcValue
= CGF
.Builder
.CreateLoad(srcField
);
2134 unsigned flags
= (Flags
| BLOCK_BYREF_CALLER
).getBitMask();
2136 llvm::Value
*flagsVal
= llvm::ConstantInt::get(CGF
.Int32Ty
, flags
);
2137 llvm::FunctionCallee fn
= CGF
.CGM
.getBlockObjectAssign();
2139 llvm::Value
*args
[] = { destField
.getPointer(), srcValue
, flagsVal
};
2140 CGF
.EmitNounwindRuntimeCall(fn
, args
);
2143 void emitDispose(CodeGenFunction
&CGF
, Address field
) override
{
2144 field
= field
.withElementType(CGF
.Int8PtrTy
);
2145 llvm::Value
*value
= CGF
.Builder
.CreateLoad(field
);
2147 CGF
.BuildBlockRelease(value
, Flags
| BLOCK_BYREF_CALLER
, false);
2150 void profileImpl(llvm::FoldingSetNodeID
&id
) const override
{
2151 id
.AddInteger(Flags
.getBitMask());
2155 /// Emits the copy/dispose helpers for an ARC __block __weak variable.
2156 class ARCWeakByrefHelpers final
: public BlockByrefHelpers
{
2158 ARCWeakByrefHelpers(CharUnits alignment
) : BlockByrefHelpers(alignment
) {}
2160 void emitCopy(CodeGenFunction
&CGF
, Address destField
,
2161 Address srcField
) override
{
2162 CGF
.EmitARCMoveWeak(destField
, srcField
);
2165 void emitDispose(CodeGenFunction
&CGF
, Address field
) override
{
2166 CGF
.EmitARCDestroyWeak(field
);
2169 void profileImpl(llvm::FoldingSetNodeID
&id
) const override
{
2170 // 0 is distinguishable from all pointers and byref flags
2175 /// Emits the copy/dispose helpers for an ARC __block __strong variable
2176 /// that's not of block-pointer type.
2177 class ARCStrongByrefHelpers final
: public BlockByrefHelpers
{
2179 ARCStrongByrefHelpers(CharUnits alignment
) : BlockByrefHelpers(alignment
) {}
2181 void emitCopy(CodeGenFunction
&CGF
, Address destField
,
2182 Address srcField
) override
{
2183 // Do a "move" by copying the value and then zeroing out the old
2186 llvm::Value
*value
= CGF
.Builder
.CreateLoad(srcField
);
2189 llvm::ConstantPointerNull::get(cast
<llvm::PointerType
>(value
->getType()));
2191 if (CGF
.CGM
.getCodeGenOpts().OptimizationLevel
== 0) {
2192 CGF
.Builder
.CreateStore(null
, destField
);
2193 CGF
.EmitARCStoreStrongCall(destField
, value
, /*ignored*/ true);
2194 CGF
.EmitARCStoreStrongCall(srcField
, null
, /*ignored*/ true);
2197 CGF
.Builder
.CreateStore(value
, destField
);
2198 CGF
.Builder
.CreateStore(null
, srcField
);
2201 void emitDispose(CodeGenFunction
&CGF
, Address field
) override
{
2202 CGF
.EmitARCDestroyStrong(field
, ARCImpreciseLifetime
);
2205 void profileImpl(llvm::FoldingSetNodeID
&id
) const override
{
2206 // 1 is distinguishable from all pointers and byref flags
2211 /// Emits the copy/dispose helpers for an ARC __block __strong
2212 /// variable that's of block-pointer type.
2213 class ARCStrongBlockByrefHelpers final
: public BlockByrefHelpers
{
2215 ARCStrongBlockByrefHelpers(CharUnits alignment
)
2216 : BlockByrefHelpers(alignment
) {}
2218 void emitCopy(CodeGenFunction
&CGF
, Address destField
,
2219 Address srcField
) override
{
2220 // Do the copy with objc_retainBlock; that's all that
2221 // _Block_object_assign would do anyway, and we'd have to pass the
2222 // right arguments to make sure it doesn't get no-op'ed.
2223 llvm::Value
*oldValue
= CGF
.Builder
.CreateLoad(srcField
);
2224 llvm::Value
*copy
= CGF
.EmitARCRetainBlock(oldValue
, /*mandatory*/ true);
2225 CGF
.Builder
.CreateStore(copy
, destField
);
2228 void emitDispose(CodeGenFunction
&CGF
, Address field
) override
{
2229 CGF
.EmitARCDestroyStrong(field
, ARCImpreciseLifetime
);
2232 void profileImpl(llvm::FoldingSetNodeID
&id
) const override
{
2233 // 2 is distinguishable from all pointers and byref flags
2238 /// Emits the copy/dispose helpers for a __block variable with a
2239 /// nontrivial copy constructor or destructor.
2240 class CXXByrefHelpers final
: public BlockByrefHelpers
{
2242 const Expr
*CopyExpr
;
2245 CXXByrefHelpers(CharUnits alignment
, QualType type
,
2246 const Expr
*copyExpr
)
2247 : BlockByrefHelpers(alignment
), VarType(type
), CopyExpr(copyExpr
) {}
2249 bool needsCopy() const override
{ return CopyExpr
!= nullptr; }
2250 void emitCopy(CodeGenFunction
&CGF
, Address destField
,
2251 Address srcField
) override
{
2252 if (!CopyExpr
) return;
2253 CGF
.EmitSynthesizedCXXCopyCtor(destField
, srcField
, CopyExpr
);
2256 void emitDispose(CodeGenFunction
&CGF
, Address field
) override
{
2257 EHScopeStack::stable_iterator cleanupDepth
= CGF
.EHStack
.stable_begin();
2258 CGF
.PushDestructorCleanup(VarType
, field
);
2259 CGF
.PopCleanupBlocks(cleanupDepth
);
2262 void profileImpl(llvm::FoldingSetNodeID
&id
) const override
{
2263 id
.AddPointer(VarType
.getCanonicalType().getAsOpaquePtr());
2267 /// Emits the copy/dispose helpers for a __block variable that is a non-trivial
2269 class NonTrivialCStructByrefHelpers final
: public BlockByrefHelpers
{
2273 NonTrivialCStructByrefHelpers(CharUnits alignment
, QualType type
)
2274 : BlockByrefHelpers(alignment
), VarType(type
) {}
2276 void emitCopy(CodeGenFunction
&CGF
, Address destField
,
2277 Address srcField
) override
{
2278 CGF
.callCStructMoveConstructor(CGF
.MakeAddrLValue(destField
, VarType
),
2279 CGF
.MakeAddrLValue(srcField
, VarType
));
2282 bool needsDispose() const override
{
2283 return VarType
.isDestructedType();
2286 void emitDispose(CodeGenFunction
&CGF
, Address field
) override
{
2287 EHScopeStack::stable_iterator cleanupDepth
= CGF
.EHStack
.stable_begin();
2288 CGF
.pushDestroy(VarType
.isDestructedType(), field
, VarType
);
2289 CGF
.PopCleanupBlocks(cleanupDepth
);
2292 void profileImpl(llvm::FoldingSetNodeID
&id
) const override
{
2293 id
.AddPointer(VarType
.getCanonicalType().getAsOpaquePtr());
2296 } // end anonymous namespace
2298 static llvm::Constant
*
2299 generateByrefCopyHelper(CodeGenFunction
&CGF
, const BlockByrefInfo
&byrefInfo
,
2300 BlockByrefHelpers
&generator
) {
2301 ASTContext
&Context
= CGF
.getContext();
2303 QualType ReturnTy
= Context
.VoidTy
;
2305 FunctionArgList args
;
2306 ImplicitParamDecl
Dst(Context
, Context
.VoidPtrTy
, ImplicitParamDecl::Other
);
2307 args
.push_back(&Dst
);
2309 ImplicitParamDecl
Src(Context
, Context
.VoidPtrTy
, ImplicitParamDecl::Other
);
2310 args
.push_back(&Src
);
2312 const CGFunctionInfo
&FI
=
2313 CGF
.CGM
.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy
, args
);
2315 llvm::FunctionType
*LTy
= CGF
.CGM
.getTypes().GetFunctionType(FI
);
2317 // FIXME: We'd like to put these into a mergable by content, with
2318 // internal linkage.
2319 llvm::Function
*Fn
=
2320 llvm::Function::Create(LTy
, llvm::GlobalValue::InternalLinkage
,
2321 "__Block_byref_object_copy_", &CGF
.CGM
.getModule());
2323 SmallVector
<QualType
, 2> ArgTys
;
2324 ArgTys
.push_back(Context
.VoidPtrTy
);
2325 ArgTys
.push_back(Context
.VoidPtrTy
);
2327 CGF
.CGM
.SetInternalFunctionAttributes(GlobalDecl(), Fn
, FI
);
2329 CGF
.StartFunction(GlobalDecl(), ReturnTy
, Fn
, FI
, args
);
2330 // Create a scope with an artificial location for the body of this function.
2331 auto AL
= ApplyDebugLocation::CreateArtificial(CGF
);
2333 if (generator
.needsCopy()) {
2335 Address destField
= CGF
.GetAddrOfLocalVar(&Dst
);
2336 destField
= Address(CGF
.Builder
.CreateLoad(destField
), byrefInfo
.Type
,
2337 byrefInfo
.ByrefAlignment
);
2339 CGF
.emitBlockByrefAddress(destField
, byrefInfo
, false, "dest-object");
2342 Address srcField
= CGF
.GetAddrOfLocalVar(&Src
);
2343 srcField
= Address(CGF
.Builder
.CreateLoad(srcField
), byrefInfo
.Type
,
2344 byrefInfo
.ByrefAlignment
);
2346 CGF
.emitBlockByrefAddress(srcField
, byrefInfo
, false, "src-object");
2348 generator
.emitCopy(CGF
, destField
, srcField
);
2351 CGF
.FinishFunction();
2356 /// Build the copy helper for a __block variable.
2357 static llvm::Constant
*buildByrefCopyHelper(CodeGenModule
&CGM
,
2358 const BlockByrefInfo
&byrefInfo
,
2359 BlockByrefHelpers
&generator
) {
2360 CodeGenFunction
CGF(CGM
);
2361 return generateByrefCopyHelper(CGF
, byrefInfo
, generator
);
2364 /// Generate code for a __block variable's dispose helper.
2365 static llvm::Constant
*
2366 generateByrefDisposeHelper(CodeGenFunction
&CGF
,
2367 const BlockByrefInfo
&byrefInfo
,
2368 BlockByrefHelpers
&generator
) {
2369 ASTContext
&Context
= CGF
.getContext();
2370 QualType R
= Context
.VoidTy
;
2372 FunctionArgList args
;
2373 ImplicitParamDecl
Src(CGF
.getContext(), Context
.VoidPtrTy
,
2374 ImplicitParamDecl::Other
);
2375 args
.push_back(&Src
);
2377 const CGFunctionInfo
&FI
=
2378 CGF
.CGM
.getTypes().arrangeBuiltinFunctionDeclaration(R
, args
);
2380 llvm::FunctionType
*LTy
= CGF
.CGM
.getTypes().GetFunctionType(FI
);
2382 // FIXME: We'd like to put these into a mergable by content, with
2383 // internal linkage.
2384 llvm::Function
*Fn
=
2385 llvm::Function::Create(LTy
, llvm::GlobalValue::InternalLinkage
,
2386 "__Block_byref_object_dispose_",
2387 &CGF
.CGM
.getModule());
2389 SmallVector
<QualType
, 1> ArgTys
;
2390 ArgTys
.push_back(Context
.VoidPtrTy
);
2392 CGF
.CGM
.SetInternalFunctionAttributes(GlobalDecl(), Fn
, FI
);
2394 CGF
.StartFunction(GlobalDecl(), R
, Fn
, FI
, args
);
2395 // Create a scope with an artificial location for the body of this function.
2396 auto AL
= ApplyDebugLocation::CreateArtificial(CGF
);
2398 if (generator
.needsDispose()) {
2399 Address addr
= CGF
.GetAddrOfLocalVar(&Src
);
2400 addr
= Address(CGF
.Builder
.CreateLoad(addr
), byrefInfo
.Type
,
2401 byrefInfo
.ByrefAlignment
);
2402 addr
= CGF
.emitBlockByrefAddress(addr
, byrefInfo
, false, "object");
2404 generator
.emitDispose(CGF
, addr
);
2407 CGF
.FinishFunction();
2412 /// Build the dispose helper for a __block variable.
2413 static llvm::Constant
*buildByrefDisposeHelper(CodeGenModule
&CGM
,
2414 const BlockByrefInfo
&byrefInfo
,
2415 BlockByrefHelpers
&generator
) {
2416 CodeGenFunction
CGF(CGM
);
2417 return generateByrefDisposeHelper(CGF
, byrefInfo
, generator
);
2420 /// Lazily build the copy and dispose helpers for a __block variable
2421 /// with the given information.
2423 static T
*buildByrefHelpers(CodeGenModule
&CGM
, const BlockByrefInfo
&byrefInfo
,
2425 llvm::FoldingSetNodeID id
;
2426 generator
.Profile(id
);
2429 BlockByrefHelpers
*node
2430 = CGM
.ByrefHelpersCache
.FindNodeOrInsertPos(id
, insertPos
);
2431 if (node
) return static_cast<T
*>(node
);
2433 generator
.CopyHelper
= buildByrefCopyHelper(CGM
, byrefInfo
, generator
);
2434 generator
.DisposeHelper
= buildByrefDisposeHelper(CGM
, byrefInfo
, generator
);
2436 T
*copy
= new (CGM
.getContext()) T(std::forward
<T
>(generator
));
2437 CGM
.ByrefHelpersCache
.InsertNode(copy
, insertPos
);
2441 /// Build the copy and dispose helpers for the given __block variable
2442 /// emission. Places the helpers in the global cache. Returns null
2443 /// if no helpers are required.
2445 CodeGenFunction::buildByrefHelpers(llvm::StructType
&byrefType
,
2446 const AutoVarEmission
&emission
) {
2447 const VarDecl
&var
= *emission
.Variable
;
2448 assert(var
.isEscapingByref() &&
2449 "only escaping __block variables need byref helpers");
2451 QualType type
= var
.getType();
2453 auto &byrefInfo
= getBlockByrefInfo(&var
);
2455 // The alignment we care about for the purposes of uniquing byref
2456 // helpers is the alignment of the actual byref value field.
2457 CharUnits valueAlignment
=
2458 byrefInfo
.ByrefAlignment
.alignmentAtOffset(byrefInfo
.FieldOffset
);
2460 if (const CXXRecordDecl
*record
= type
->getAsCXXRecordDecl()) {
2461 const Expr
*copyExpr
=
2462 CGM
.getContext().getBlockVarCopyInit(&var
).getCopyExpr();
2463 if (!copyExpr
&& record
->hasTrivialDestructor()) return nullptr;
2465 return ::buildByrefHelpers(
2466 CGM
, byrefInfo
, CXXByrefHelpers(valueAlignment
, type
, copyExpr
));
2469 // If type is a non-trivial C struct type that is non-trivial to
2470 // destructly move or destroy, build the copy and dispose helpers.
2471 if (type
.isNonTrivialToPrimitiveDestructiveMove() == QualType::PCK_Struct
||
2472 type
.isDestructedType() == QualType::DK_nontrivial_c_struct
)
2473 return ::buildByrefHelpers(
2474 CGM
, byrefInfo
, NonTrivialCStructByrefHelpers(valueAlignment
, type
));
2476 // Otherwise, if we don't have a retainable type, there's nothing to do.
2477 // that the runtime does extra copies.
2478 if (!type
->isObjCRetainableType()) return nullptr;
2480 Qualifiers qs
= type
.getQualifiers();
2482 // If we have lifetime, that dominates.
2483 if (Qualifiers::ObjCLifetime lifetime
= qs
.getObjCLifetime()) {
2485 case Qualifiers::OCL_None
: llvm_unreachable("impossible");
2487 // These are just bits as far as the runtime is concerned.
2488 case Qualifiers::OCL_ExplicitNone
:
2489 case Qualifiers::OCL_Autoreleasing
:
2492 // Tell the runtime that this is ARC __weak, called by the
2494 case Qualifiers::OCL_Weak
:
2495 return ::buildByrefHelpers(CGM
, byrefInfo
,
2496 ARCWeakByrefHelpers(valueAlignment
));
2498 // ARC __strong __block variables need to be retained.
2499 case Qualifiers::OCL_Strong
:
2500 // Block pointers need to be copied, and there's no direct
2501 // transfer possible.
2502 if (type
->isBlockPointerType()) {
2503 return ::buildByrefHelpers(CGM
, byrefInfo
,
2504 ARCStrongBlockByrefHelpers(valueAlignment
));
2506 // Otherwise, we transfer ownership of the retain from the stack
2509 return ::buildByrefHelpers(CGM
, byrefInfo
,
2510 ARCStrongByrefHelpers(valueAlignment
));
2513 llvm_unreachable("fell out of lifetime switch!");
2516 BlockFieldFlags flags
;
2517 if (type
->isBlockPointerType()) {
2518 flags
|= BLOCK_FIELD_IS_BLOCK
;
2519 } else if (CGM
.getContext().isObjCNSObjectType(type
) ||
2520 type
->isObjCObjectPointerType()) {
2521 flags
|= BLOCK_FIELD_IS_OBJECT
;
2526 if (type
.isObjCGCWeak())
2527 flags
|= BLOCK_FIELD_IS_WEAK
;
2529 return ::buildByrefHelpers(CGM
, byrefInfo
,
2530 ObjectByrefHelpers(valueAlignment
, flags
));
2533 Address
CodeGenFunction::emitBlockByrefAddress(Address baseAddr
,
2535 bool followForward
) {
2536 auto &info
= getBlockByrefInfo(var
);
2537 return emitBlockByrefAddress(baseAddr
, info
, followForward
, var
->getName());
2540 Address
CodeGenFunction::emitBlockByrefAddress(Address baseAddr
,
2541 const BlockByrefInfo
&info
,
2543 const llvm::Twine
&name
) {
2544 // Chase the forwarding address if requested.
2545 if (followForward
) {
2546 Address forwardingAddr
= Builder
.CreateStructGEP(baseAddr
, 1, "forwarding");
2547 baseAddr
= Address(Builder
.CreateLoad(forwardingAddr
), info
.Type
,
2548 info
.ByrefAlignment
);
2551 return Builder
.CreateStructGEP(baseAddr
, info
.FieldIndex
, name
);
2554 /// BuildByrefInfo - This routine changes a __block variable declared as T x
2559 /// void *__forwarding;
2560 /// int32_t __flags;
2562 /// void *__copy_helper; // only if needed
2563 /// void *__destroy_helper; // only if needed
2564 /// void *__byref_variable_layout;// only if needed
2565 /// char padding[X]; // only if needed
2569 const BlockByrefInfo
&CodeGenFunction::getBlockByrefInfo(const VarDecl
*D
) {
2570 auto it
= BlockByrefInfos
.find(D
);
2571 if (it
!= BlockByrefInfos
.end())
2574 llvm::StructType
*byrefType
=
2575 llvm::StructType::create(getLLVMContext(),
2576 "struct.__block_byref_" + D
->getNameAsString());
2578 QualType Ty
= D
->getType();
2581 SmallVector
<llvm::Type
*, 8> types
;
2584 types
.push_back(VoidPtrTy
);
2585 size
+= getPointerSize();
2587 // void *__forwarding;
2588 types
.push_back(VoidPtrTy
);
2589 size
+= getPointerSize();
2592 types
.push_back(Int32Ty
);
2593 size
+= CharUnits::fromQuantity(4);
2596 types
.push_back(Int32Ty
);
2597 size
+= CharUnits::fromQuantity(4);
2599 // Note that this must match *exactly* the logic in buildByrefHelpers.
2600 bool hasCopyAndDispose
= getContext().BlockRequiresCopying(Ty
, D
);
2601 if (hasCopyAndDispose
) {
2602 /// void *__copy_helper;
2603 types
.push_back(VoidPtrTy
);
2604 size
+= getPointerSize();
2606 /// void *__destroy_helper;
2607 types
.push_back(VoidPtrTy
);
2608 size
+= getPointerSize();
2611 bool HasByrefExtendedLayout
= false;
2612 Qualifiers::ObjCLifetime Lifetime
= Qualifiers::OCL_None
;
2613 if (getContext().getByrefLifetime(Ty
, Lifetime
, HasByrefExtendedLayout
) &&
2614 HasByrefExtendedLayout
) {
2615 /// void *__byref_variable_layout;
2616 types
.push_back(VoidPtrTy
);
2617 size
+= CharUnits::fromQuantity(PointerSizeInBytes
);
2621 llvm::Type
*varTy
= ConvertTypeForMem(Ty
);
2623 bool packed
= false;
2624 CharUnits varAlign
= getContext().getDeclAlign(D
);
2625 CharUnits varOffset
= size
.alignTo(varAlign
);
2627 // We may have to insert padding.
2628 if (varOffset
!= size
) {
2629 llvm::Type
*paddingTy
=
2630 llvm::ArrayType::get(Int8Ty
, (varOffset
- size
).getQuantity());
2632 types
.push_back(paddingTy
);
2635 // Conversely, we might have to prevent LLVM from inserting padding.
2636 } else if (CGM
.getDataLayout().getABITypeAlign(varTy
) >
2637 uint64_t(varAlign
.getQuantity())) {
2640 types
.push_back(varTy
);
2642 byrefType
->setBody(types
, packed
);
2644 BlockByrefInfo info
;
2645 info
.Type
= byrefType
;
2646 info
.FieldIndex
= types
.size() - 1;
2647 info
.FieldOffset
= varOffset
;
2648 info
.ByrefAlignment
= std::max(varAlign
, getPointerAlign());
2650 auto pair
= BlockByrefInfos
.insert({D
, info
});
2651 assert(pair
.second
&& "info was inserted recursively?");
2652 return pair
.first
->second
;
2655 /// Initialize the structural components of a __block variable, i.e.
2656 /// everything but the actual object.
2657 void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission
&emission
) {
2658 // Find the address of the local.
2659 Address addr
= emission
.Addr
;
2661 // That's an alloca of the byref structure type.
2662 llvm::StructType
*byrefType
= cast
<llvm::StructType
>(addr
.getElementType());
2664 unsigned nextHeaderIndex
= 0;
2665 CharUnits nextHeaderOffset
;
2666 auto storeHeaderField
= [&](llvm::Value
*value
, CharUnits fieldSize
,
2667 const Twine
&name
) {
2668 auto fieldAddr
= Builder
.CreateStructGEP(addr
, nextHeaderIndex
, name
);
2669 Builder
.CreateStore(value
, fieldAddr
);
2672 nextHeaderOffset
+= fieldSize
;
2675 // Build the byref helpers if necessary. This is null if we don't need any.
2676 BlockByrefHelpers
*helpers
= buildByrefHelpers(*byrefType
, emission
);
2678 const VarDecl
&D
= *emission
.Variable
;
2679 QualType type
= D
.getType();
2681 bool HasByrefExtendedLayout
= false;
2682 Qualifiers::ObjCLifetime ByrefLifetime
= Qualifiers::OCL_None
;
2683 bool ByRefHasLifetime
=
2684 getContext().getByrefLifetime(type
, ByrefLifetime
, HasByrefExtendedLayout
);
2688 // Initialize the 'isa', which is just 0 or 1.
2690 if (type
.isObjCGCWeak())
2692 V
= Builder
.CreateIntToPtr(Builder
.getInt32(isa
), Int8PtrTy
, "isa");
2693 storeHeaderField(V
, getPointerSize(), "byref.isa");
2695 // Store the address of the variable into its own forwarding pointer.
2696 storeHeaderField(addr
.getPointer(), getPointerSize(), "byref.forwarding");
2699 // c) the flags field is set to either 0 if no helper functions are
2700 // needed or BLOCK_BYREF_HAS_COPY_DISPOSE if they are,
2702 if (helpers
) flags
|= BLOCK_BYREF_HAS_COPY_DISPOSE
;
2703 if (ByRefHasLifetime
) {
2704 if (HasByrefExtendedLayout
) flags
|= BLOCK_BYREF_LAYOUT_EXTENDED
;
2705 else switch (ByrefLifetime
) {
2706 case Qualifiers::OCL_Strong
:
2707 flags
|= BLOCK_BYREF_LAYOUT_STRONG
;
2709 case Qualifiers::OCL_Weak
:
2710 flags
|= BLOCK_BYREF_LAYOUT_WEAK
;
2712 case Qualifiers::OCL_ExplicitNone
:
2713 flags
|= BLOCK_BYREF_LAYOUT_UNRETAINED
;
2715 case Qualifiers::OCL_None
:
2716 if (!type
->isObjCObjectPointerType() && !type
->isBlockPointerType())
2717 flags
|= BLOCK_BYREF_LAYOUT_NON_OBJECT
;
2722 if (CGM
.getLangOpts().ObjCGCBitmapPrint
) {
2723 printf("\n Inline flag for BYREF variable layout (%d):", flags
.getBitMask());
2724 if (flags
& BLOCK_BYREF_HAS_COPY_DISPOSE
)
2725 printf(" BLOCK_BYREF_HAS_COPY_DISPOSE");
2726 if (flags
& BLOCK_BYREF_LAYOUT_MASK
) {
2727 BlockFlags
ThisFlag(flags
.getBitMask() & BLOCK_BYREF_LAYOUT_MASK
);
2728 if (ThisFlag
== BLOCK_BYREF_LAYOUT_EXTENDED
)
2729 printf(" BLOCK_BYREF_LAYOUT_EXTENDED");
2730 if (ThisFlag
== BLOCK_BYREF_LAYOUT_STRONG
)
2731 printf(" BLOCK_BYREF_LAYOUT_STRONG");
2732 if (ThisFlag
== BLOCK_BYREF_LAYOUT_WEAK
)
2733 printf(" BLOCK_BYREF_LAYOUT_WEAK");
2734 if (ThisFlag
== BLOCK_BYREF_LAYOUT_UNRETAINED
)
2735 printf(" BLOCK_BYREF_LAYOUT_UNRETAINED");
2736 if (ThisFlag
== BLOCK_BYREF_LAYOUT_NON_OBJECT
)
2737 printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT");
2742 storeHeaderField(llvm::ConstantInt::get(IntTy
, flags
.getBitMask()),
2743 getIntSize(), "byref.flags");
2745 CharUnits byrefSize
= CGM
.GetTargetTypeStoreSize(byrefType
);
2746 V
= llvm::ConstantInt::get(IntTy
, byrefSize
.getQuantity());
2747 storeHeaderField(V
, getIntSize(), "byref.size");
2750 storeHeaderField(helpers
->CopyHelper
, getPointerSize(),
2751 "byref.copyHelper");
2752 storeHeaderField(helpers
->DisposeHelper
, getPointerSize(),
2753 "byref.disposeHelper");
2756 if (ByRefHasLifetime
&& HasByrefExtendedLayout
) {
2757 auto layoutInfo
= CGM
.getObjCRuntime().BuildByrefLayout(CGM
, type
);
2758 storeHeaderField(layoutInfo
, getPointerSize(), "byref.layout");
2762 void CodeGenFunction::BuildBlockRelease(llvm::Value
*V
, BlockFieldFlags flags
,
2764 llvm::FunctionCallee F
= CGM
.getBlockObjectDispose();
2765 llvm::Value
*args
[] = {V
,
2766 llvm::ConstantInt::get(Int32Ty
, flags
.getBitMask())};
2769 EmitRuntimeCallOrInvoke(F
, args
);
2771 EmitNounwindRuntimeCall(F
, args
);
2774 void CodeGenFunction::enterByrefCleanup(CleanupKind Kind
, Address Addr
,
2775 BlockFieldFlags Flags
,
2776 bool LoadBlockVarAddr
, bool CanThrow
) {
2777 EHStack
.pushCleanup
<CallBlockRelease
>(Kind
, Addr
, Flags
, LoadBlockVarAddr
,
2781 /// Adjust the declaration of something from the blocks API.
2782 static void configureBlocksRuntimeObject(CodeGenModule
&CGM
,
2783 llvm::Constant
*C
) {
2784 auto *GV
= cast
<llvm::GlobalValue
>(C
->stripPointerCasts());
2786 if (CGM
.getTarget().getTriple().isOSBinFormatCOFF()) {
2787 IdentifierInfo
&II
= CGM
.getContext().Idents
.get(C
->getName());
2788 TranslationUnitDecl
*TUDecl
= CGM
.getContext().getTranslationUnitDecl();
2789 DeclContext
*DC
= TranslationUnitDecl::castToDeclContext(TUDecl
);
2791 assert((isa
<llvm::Function
>(C
->stripPointerCasts()) ||
2792 isa
<llvm::GlobalVariable
>(C
->stripPointerCasts())) &&
2793 "expected Function or GlobalVariable");
2795 const NamedDecl
*ND
= nullptr;
2796 for (const auto *Result
: DC
->lookup(&II
))
2797 if ((ND
= dyn_cast
<FunctionDecl
>(Result
)) ||
2798 (ND
= dyn_cast
<VarDecl
>(Result
)))
2801 // TODO: support static blocks runtime
2802 if (GV
->isDeclaration() && (!ND
|| !ND
->hasAttr
<DLLExportAttr
>())) {
2803 GV
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass
);
2804 GV
->setLinkage(llvm::GlobalValue::ExternalLinkage
);
2806 GV
->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass
);
2807 GV
->setLinkage(llvm::GlobalValue::ExternalLinkage
);
2811 if (CGM
.getLangOpts().BlocksRuntimeOptional
&& GV
->isDeclaration() &&
2812 GV
->hasExternalLinkage())
2813 GV
->setLinkage(llvm::GlobalValue::ExternalWeakLinkage
);
2815 CGM
.setDSOLocal(GV
);
2818 llvm::FunctionCallee
CodeGenModule::getBlockObjectDispose() {
2819 if (BlockObjectDispose
)
2820 return BlockObjectDispose
;
2822 llvm::Type
*args
[] = { Int8PtrTy
, Int32Ty
};
2823 llvm::FunctionType
*fty
2824 = llvm::FunctionType::get(VoidTy
, args
, false);
2825 BlockObjectDispose
= CreateRuntimeFunction(fty
, "_Block_object_dispose");
2826 configureBlocksRuntimeObject(
2827 *this, cast
<llvm::Constant
>(BlockObjectDispose
.getCallee()));
2828 return BlockObjectDispose
;
2831 llvm::FunctionCallee
CodeGenModule::getBlockObjectAssign() {
2832 if (BlockObjectAssign
)
2833 return BlockObjectAssign
;
2835 llvm::Type
*args
[] = { Int8PtrTy
, Int8PtrTy
, Int32Ty
};
2836 llvm::FunctionType
*fty
2837 = llvm::FunctionType::get(VoidTy
, args
, false);
2838 BlockObjectAssign
= CreateRuntimeFunction(fty
, "_Block_object_assign");
2839 configureBlocksRuntimeObject(
2840 *this, cast
<llvm::Constant
>(BlockObjectAssign
.getCallee()));
2841 return BlockObjectAssign
;
2844 llvm::Constant
*CodeGenModule::getNSConcreteGlobalBlock() {
2845 if (NSConcreteGlobalBlock
)
2846 return NSConcreteGlobalBlock
;
2848 NSConcreteGlobalBlock
= GetOrCreateLLVMGlobal(
2849 "_NSConcreteGlobalBlock", Int8PtrTy
, LangAS::Default
, nullptr);
2850 configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock
);
2851 return NSConcreteGlobalBlock
;
2854 llvm::Constant
*CodeGenModule::getNSConcreteStackBlock() {
2855 if (NSConcreteStackBlock
)
2856 return NSConcreteStackBlock
;
2858 NSConcreteStackBlock
= GetOrCreateLLVMGlobal(
2859 "_NSConcreteStackBlock", Int8PtrTy
, LangAS::Default
, nullptr);
2860 configureBlocksRuntimeObject(*this, NSConcreteStackBlock
);
2861 return NSConcreteStackBlock
;