r219696 - CodeGen: correct mangling for blocks

Saleem Abdulrasool abdulras at fb.com
Tue Oct 14 13:55:36 PDT 2014


On Oct 14, 2014, at 10:56 AM, Reid Kleckner <rnk at google.com> wrote:

> On Tue, Oct 14, 2014 at 10:20 AM, Saleem Abdulrasool <compnerd at compnerd.org> wrote:
> Author: compnerd
> Date: Tue Oct 14 12:20:14 2014
> New Revision: 219696
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=219696&view=rev
> Log:
> CodeGen: correct mangling for blocks
> 
> This addresses a regression introduced with SVN r219393.  A block may be
> contained within another block.  In such a scenario, we would end up within a
> BlockDecl, which is not a NamedDecl (as the names are synthesised).  The cast to
> a NamedDecl of the DeclContext would then assert as the types are unrelated.
> 
> Restore the mangling behaviour to that prior to SVN r219393.  If the current
> block is contained within a BlockDecl, walk up to the parent DeclContext,
> recursively, until we have a non-BlockDecl.  This is expected to be a NamedDecl.
> Add in a couple of asserts to ensure that the assumption that we only encounter
> a block within a NamedDecl or a BlockDecl.
> 
> What about nested global blocks or blocks in CapturedDecls?

What do you mean by global blocks here?  I assume you mean blocks at the file scope rather than global blocks in reference to Global vs Stack vs Heap blocks.  If so, yes, those are broken, Ive not fixed that (yet), but I intend to do that soon.  I had these two ready, so I decided to at least improve the state rather than leave it as broken as it is currently.  That was partially the reason that I added the assertion, so that the assertion triggered would be much easier to catch it.

> Also, I don't think we need distinct manglings for blocks in different Ctor variants. We can probably significantly simplify the API if we always mangle the block using the "base" variant of the ctor/dtor.

The change didn’t really touch the ctor/dtor behaviour.  Im afraid that Im not really following you here.

> Added:
>     cfe/trunk/test/CodeGen/mangle-blocks.c
> Modified:
>     cfe/trunk/lib/AST/Mangle.cpp
> 
> Modified: cfe/trunk/lib/AST/Mangle.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Mangle.cpp?rev=219696&r1=219695&r2=219696&view=diff
> ==============================================================================
> --- cfe/trunk/lib/AST/Mangle.cpp (original)
> +++ cfe/trunk/lib/AST/Mangle.cpp Tue Oct 14 12:20:14 2014
> @@ -215,6 +215,12 @@ void MangleContext::mangleBlock(const De
>    if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
>      mangleObjCMethodName(Method, Stream);
>    } else {
> +    assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
> +           "expected a NamedDecl or BlockDecl");
> +    if (isa<BlockDecl>(DC))
> +      for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
> +        (void) getBlockId(cast<BlockDecl>(DC), true);
> +    assert(isa<NamedDecl>(DC) && "expected a NamedDecl");
>      const NamedDecl *ND = cast<NamedDecl>(DC);
>      if (!shouldMangleDeclName(ND) && ND->getIdentifier())
>        Stream << ND->getIdentifier()->getName();
> 
> Added: cfe/trunk/test/CodeGen/mangle-blocks.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/mangle-blocks.c?rev=219696&view=auto
> ==============================================================================
> --- cfe/trunk/test/CodeGen/mangle-blocks.c (added)
> +++ cfe/trunk/test/CodeGen/mangle-blocks.c Tue Oct 14 12:20:14 2014
> @@ -0,0 +1,20 @@
> +// RUN: %clang_cc1 -triple i386-apple-ios -fblocks -emit-llvm -o - %s | FileCheck %s
> +
> +void __assert_rtn(const char *, const char *, int, const char *)
> +    __attribute__ (( noreturn ));
> +void invoke(void (^)(void));
> +
> +void mangle(void) {
> +  invoke(^{ invoke(^{ __assert_rtn(__func__, __FILE__, __LINE__, "mangle"); }); });
> +}
> +
> +// CHECK: @__func__.__mangle_block_invoke_2 = private unnamed_addr constant [24 x i8] c"__mangle_block_invoke_2\00", align 1
> +// CHECK: @.str = private unnamed_addr constant {{.*}}, align 1
> +// CHECK: @.str1 = private unnamed_addr constant [7 x i8] c"mangle\00", align 1
> +
> +// CHECK: define internal void @__mangle_block_invoke(i8* %.block_descriptor)
> +
> +// CHECK: define internal void @__mangle_block_invoke_2(i8* %.block_descriptor){{.*}}{
> +// CHECK:   call void @__assert_rtn(i8* getelementptr inbounds ([24 x i8]* @__func__.__mangle_block_invoke_2, i32 0, i32 0), i8* getelementptr inbounds {{.*}}, i32 8, i8* getelementptr inbounds ([7 x i8]* @.str1, i32 0, i32 0))
> +// CHECK: }
> +
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> https://urldefense.proofpoint.com/v1/url?u=http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits&k=ZVNjlDMF0FElm4dQtryO4A%3D%3D%0A&r=CchYc4lrV44%2BZqxZADw0BQ%3D%3D%0A&m=TdsQO0oNtr%2Flh0a4EEAa2ATgv211LkUJ28UevVrPy3c%3D%0A&s=caeff7a5468ab5d3ebec283bf647edcc474110337a4e60193378e50dba8f6fba

-- 
Saleem Abdulrasool
abdulras (at) fb (dot) com









More information about the cfe-commits mailing list