r177086 - Allocate stack storage for .block_descriptor and captured self at -O0.
Adrian Prantl
aprantl at apple.com
Thu Mar 14 10:53:33 PDT 2013
Author: adrian
Date: Thu Mar 14 12:53:33 2013
New Revision: 177086
URL: http://llvm.org/viewvc/llvm-project?rev=177086&view=rev
Log:
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
Added:
cfe/trunk/test/CodeGenObjC/debug-info-block-captured-self.m
Modified:
cfe/trunk/lib/CodeGen/CGBlocks.cpp
cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
cfe/trunk/lib/CodeGen/CGDebugInfo.h
cfe/trunk/lib/CodeGen/CGDecl.cpp
cfe/trunk/test/CodeGenCXX/blocks.cpp
Modified: cfe/trunk/lib/CodeGen/CGBlocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBlocks.cpp?rev=177086&r1=177085&r2=177086&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Thu Mar 14 12:53:33 2013
@@ -1157,10 +1157,24 @@ CodeGenFunction::GenerateBlockFunction(G
// 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(G
CreateMemTemp(variable->getType(), "block.captured-const");
alloca->setAlignment(align);
- Builder.CreateStore(capture.getConstant(), alloca, align);
+ Builder.CreateAlignedStore(capture.getConstant(), alloca, align);
LocalDeclMap[variable] = alloca;
}
Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=177086&r1=177085&r2=177086&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Thu Mar 14 12:53:33 2013
@@ -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::EmitDeclareOfBlockLite
// 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<llvm::Argument>(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<llvm::Argument>(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
Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.h?rev=177086&r1=177085&r2=177086&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.h (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.h Thu Mar 14 12:53:33 2013
@@ -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.
Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=177086&r1=177085&r2=177086&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Thu Mar 14 12:53:33 2013
@@ -1513,17 +1513,29 @@ void CodeGenFunction::EmitParmDecl(const
Arg->setName(D.getName());
+ QualType Ty = D.getType();
+
// Use better IR generation for certain implicit parameters.
if (isa<ImplicitParamDecl>(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
}
}
- QualType Ty = D.getType();
-
llvm::Value *DeclPtr;
// If this is an aggregate or variable sized value, reuse the input pointer.
if (!Ty->isConstantSizeType() ||
Modified: cfe/trunk/test/CodeGenCXX/blocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/blocks.cpp?rev=177086&r1=177085&r2=177086&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/blocks.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/blocks.cpp Thu Mar 14 12:53:33 2013
@@ -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]])
Added: cfe/trunk/test/CodeGenObjC/debug-info-block-captured-self.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/debug-info-block-captured-self.m?rev=177086&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenObjC/debug-info-block-captured-self.m (added)
+++ cfe/trunk/test/CodeGenObjC/debug-info-block-captured-self.m Thu Mar 14 12:53:33 2013
@@ -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.
+ at class T;
+ at interface S
+ at end
+ at interface Mode
+-(int) count;
+ at end
+ at interface Context
+ at end
+ at interface ViewController
+ at property (nonatomic, readwrite, strong) Context *context;
+ at end
+typedef enum {
+ Unknown = 0,
+} State;
+ at interface Main : ViewController
+{
+ T * t1;
+ T * t2;
+}
+ at property(readwrite, nonatomic) State state;
+ at end
+ at 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;
+ }
+ }];
+ }];
+}
+ at 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
More information about the cfe-commits
mailing list