From 836e7c9357b312fd1ee5c90898ce2c81bb384997 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Thu, 14 Mar 2013 17:53:33 +0000 Subject: [PATCH] Allocate stack storage for .block_descriptor and captured self at -O0. This way the register allocator will not optimize away the debug info for captured variables. Fixes rdar://problem/12767564 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177086 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGBlocks.cpp | 18 +++++- lib/CodeGen/CGDebugInfo.cpp | 26 +++++--- lib/CodeGen/CGDebugInfo.h | 3 +- lib/CodeGen/CGDecl.cpp | 16 ++++- test/CodeGenCXX/blocks.cpp | 6 +- .../debug-info-block-captured-self.m | 64 +++++++++++++++++++ 6 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 test/CodeGenObjC/debug-info-block-captured-self.m diff --git a/lib/CodeGen/CGBlocks.cpp b/lib/CodeGen/CGBlocks.cpp index c521bf9472..77e29bd119 100644 --- a/lib/CodeGen/CGBlocks.cpp +++ b/lib/CodeGen/CGBlocks.cpp @@ -1157,10 +1157,24 @@ CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, // There might not be a capture for 'self', but if there is... if (blockInfo.Captures.count(self)) { const CGBlockInfo::Capture &capture = blockInfo.getCapture(self); + llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer, capture.getIndex(), "block.captured-self"); - LocalDeclMap[self] = selfAddr; + + // At -O0 we generate an explicit alloca for self to facilitate debugging. + if (CGM.getCodeGenOpts().OptimizationLevel == 0) { + llvm::Value *load = Builder.CreateLoad(selfAddr); + + // Allocate a stack slot for it, so we can generate debug info for it + llvm::AllocaInst *alloca = CreateTempAlloca(load->getType(), + "block.captured-self.addr"); + unsigned align = getContext().getDeclAlign(self).getQuantity(); + alloca->setAlignment(align); + Builder.CreateAlignedStore(load, alloca, align); + LocalDeclMap[self] = alloca; + } else + LocalDeclMap[self] = selfAddr; } } @@ -1177,7 +1191,7 @@ CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, CreateMemTemp(variable->getType(), "block.captured-const"); alloca->setAlignment(align); - Builder.CreateStore(capture.getConstant(), alloca, align); + Builder.CreateAlignedStore(capture.getConstant(), alloca, align); LocalDeclMap[variable] = alloca; } diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index 4aed943689..6b0edbcafe 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -2680,7 +2680,8 @@ namespace { } void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, - llvm::Value *addr, + llvm::Value *Arg, + llvm::Value *LocalAddr, CGBuilderTy &Builder) { assert(CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo); ASTContext &C = CGM.getContext(); @@ -2807,21 +2808,26 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, // Get overall information about the block. unsigned flags = llvm::DIDescriptor::FlagArtificial; llvm::MDNode *scope = LexicalBlockStack.back(); - StringRef name = ".block_descriptor"; // Create the descriptor for the parameter. llvm::DIVariable debugVar = DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable, llvm::DIDescriptor(scope), - name, tunit, line, type, + Arg->getName(), tunit, line, type, CGM.getLangOpts().Optimize, flags, - cast(addr)->getArgNo() + 1); - - // Insert an llvm.dbg.value into the current block. - llvm::Instruction *declare = - DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar, - Builder.GetInsertBlock()); - declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope)); + cast(Arg)->getArgNo() + 1); + + // Matching the code in EmitParmDecl, depending on optimization level. + llvm::Instruction *Call; + if (LocalAddr) + // Insert an llvm.dbg.value into the current block. + Call = DBuilder.insertDbgValueIntrinsic(LocalAddr, 0, debugVar, + Builder.GetInsertBlock()); + else + // Insert an llvm.dbg.declare into the current block. + Call = DBuilder.insertDeclare(Arg, debugVar, Builder.GetInsertBlock()); + + Call->setDebugLoc(llvm::DebugLoc::get(line, column, scope)); } /// getStaticDataMemberDeclaration - If D is an out-of-class definition of diff --git a/lib/CodeGen/CGDebugInfo.h b/lib/CodeGen/CGDebugInfo.h index 73dd15317e..0d9c92e24d 100644 --- a/lib/CodeGen/CGDebugInfo.h +++ b/lib/CodeGen/CGDebugInfo.h @@ -248,7 +248,8 @@ public: /// llvm.dbg.declare for the block-literal argument to a block /// invocation function. void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, - llvm::Value *addr, + llvm::Value *Arg, + llvm::Value *LocalAddr, CGBuilderTy &Builder); /// EmitGlobalVariable - Emit information about a global variable. diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index f9f48ae9ca..3b33c11b04 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -1513,17 +1513,29 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, Arg->setName(D.getName()); + QualType Ty = D.getType(); + // Use better IR generation for certain implicit parameters. if (isa(D)) { // The only implicit argument a block has is its literal. if (BlockInfo) { LocalDeclMap[&D] = Arg; + llvm::Value *LocalAddr = 0; + if (CGM.getCodeGenOpts().OptimizationLevel == 0) { + // Allocate a stack slot to let debug info survive the RA. + llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), + D.getName() + ".addr"); + Alloc->setAlignment(getContext().getDeclAlign(&D).getQuantity()); + LValue lv = MakeAddrLValue(Alloc, Ty, getContext().getDeclAlign(&D)); + EmitStoreOfScalar(Arg, lv, /* isInitialization */ true); + LocalAddr = Builder.CreateLoad(Alloc); + } if (CGDebugInfo *DI = getDebugInfo()) { if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) { DI->setLocation(D.getLocation()); - DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder); + DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, LocalAddr, Builder); } } @@ -1531,8 +1543,6 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, } } - QualType Ty = D.getType(); - llvm::Value *DeclPtr; // If this is an aggregate or variable sized value, reuse the input pointer. if (!Ty->isConstantSizeType() || diff --git a/test/CodeGenCXX/blocks.cpp b/test/CodeGenCXX/blocks.cpp index 1500c0d698..209213999e 100644 --- a/test/CodeGenCXX/blocks.cpp +++ b/test/CodeGenCXX/blocks.cpp @@ -120,8 +120,10 @@ namespace test4 { } // CHECK: define void @_ZN5test44testEv() // CHECK: define internal void @___ZN5test44testEv_block_invoke - // CHECK: [[TMP:%.*]] = alloca [[A:%.*]], align 1 - // CHECK-NEXT: bitcast i8* + // CHECK: [[TMP:%.*]] = alloca [[A:%.*]], align 1 + // CHECK-NEXT: store i8* [[BLOCKDESC:%.*]], i8** {{.*}}, align 8 + // CHECK-NEXT: load i8* + // CHECK-NEXT: bitcast i8* [[BLOCKDESC]] to <{ i8*, i32, i32, i8*, %struct.__block_descriptor* }>* // CHECK-NEXT: call void @_ZN5test41AC1Ev([[A]]* [[TMP]]) // CHECK-NEXT: call void @_ZN5test43fooENS_1AE([[A]]* [[TMP]]) // CHECK-NEXT: call void @_ZN5test41AD1Ev([[A]]* [[TMP]]) diff --git a/test/CodeGenObjC/debug-info-block-captured-self.m b/test/CodeGenObjC/debug-info-block-captured-self.m new file mode 100644 index 0000000000..6020e84553 --- /dev/null +++ b/test/CodeGenObjC/debug-info-block-captured-self.m @@ -0,0 +1,64 @@ +// RUN: %clang_cc1 -fblocks -g -emit-llvm -triple x86_64-apple-darwin -o - %s | FileCheck %s +// +// Test that debug location is generated for a captured "self" inside +// a block. +// +// This test is split into two parts, this one for the frontend, and +// then llvm/test/DebugInfo/debug-info-block-captured-self.ll to +// ensure that DW_AT_location is generated for the captured self. +@class T; +@interface S +@end +@interface Mode +-(int) count; +@end +@interface Context +@end +@interface ViewController +@property (nonatomic, readwrite, strong) Context *context; +@end +typedef enum { + Unknown = 0, +} State; +@interface Main : ViewController +{ + T * t1; + T * t2; +} +@property(readwrite, nonatomic) State state; +@end +@implementation Main +- (id) initWithContext:(Context *) context +{ + t1 = [self.context withBlock:^(id obj){ + id *mode1; + t2 = [mode1 withBlock:^(id object){ + Mode *mode2 = object; + if ([mode2 count] != 0) { + self.state = 0; + } + }]; + }]; +} +@end +// The important part of this test is that there is a dbg.value +// intrinsic associated with the implicit .block_descriptor argument +// of the block. We also test that this value gets alloca'd, so the +// register llocator won't accidentally kill it. + +// outer block: +// CHECK: define internal void {{.*}}_block_invoke{{.*}} + +// inner block: +// CHECK: define internal void {{.*}}_block_invoke{{.*}} +// CHECK: [[MEM1:%.*]] = alloca i8*, align 8 +// CHECK-NEXT: [[MEM2:%.*]] = alloca i8*, align 8 +// CHECK-NEXT: [[MEM3:%.*]] = alloca %{{.*}}*, align 8 +// CHECK: store i8* %[[BLOCK_DESC:.*]], i8** [[MEM1]], align 8 +// CHECK: [[TMP0:%.*]] = load i8** [[MEM1]] +// CHECK: call void @llvm.dbg.value(metadata !{i8* [[TMP0]]}, i64 0, metadata [[BDMD:![0-9]+]]) +// CHECK: [[TMP1:%.*]] = bitcast +// CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, %0* }>* [[TMP1]] +// CHECK-NEXT: [[TMP3:%.*]] = load %0** [[TMP2]] +// CHECK-NEXT: store {{.*}}* [[TMP3]], %{{.*}}** [[MEM3]], align 8 +// CHECK: [[BDMD]] = metadata {{.*}}.block_descriptor -- 2.40.0