From 31c307cf96ef1b6a4d0c542d18ebfd7a307ae9b0 Mon Sep 17 00:00:00 2001 From: Amy Huang Date: Fri, 12 Apr 2019 20:25:30 +0000 Subject: [PATCH] Relanding r357928 with fixed debuginfo check. [MS] Add metadata for __declspec(allocator) Original summary: Emit !heapallocsite in the metadata for calls to functions marked with __declspec(allocator). Eventually this will be emitted as S_HEAPALLOCSITE debug info in codeview. Differential Revision: https://reviews.llvm.org/D60237 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@358307 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCall.cpp | 16 ++++++----- lib/CodeGen/CGDebugInfo.cpp | 14 ++++++++++ lib/CodeGen/CGDebugInfo.h | 4 +++ .../debug-info-codeview-heapallocsite.c | 27 +++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test/CodeGen/debug-info-codeview-heapallocsite.c diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index 93421903a0..ed58255231 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -3791,6 +3791,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo); + const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl(); + #ifndef NDEBUG if (!(CallInfo.isVariadic() && CallInfo.getArgStruct())) { // For an inalloca varargs function, we don't expect CallInfo to match the @@ -4279,11 +4281,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, // Apply always_inline to all calls within flatten functions. // FIXME: should this really take priority over __try, below? if (CurCodeDecl && CurCodeDecl->hasAttr() && - !(Callee.getAbstractInfo().getCalleeDecl().getDecl() && - Callee.getAbstractInfo() - .getCalleeDecl() - .getDecl() - ->hasAttr())) { + !(TargetDecl && TargetDecl->hasAttr())) { Attrs = Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex, llvm::Attribute::AlwaysInline); @@ -4367,11 +4365,16 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, // Suppress tail calls if requested. if (llvm::CallInst *Call = dyn_cast(CI)) { - const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl(); if (TargetDecl && TargetDecl->hasAttr()) Call->setTailCallKind(llvm::CallInst::TCK_NoTail); } + // Add metadata for calls to MSAllocator functions + // FIXME: Get the type that the return value is cast to. + if (getDebugInfo() && TargetDecl && + TargetDecl->hasAttr()) + getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy, Loc); + // 4. Finish the call. // If the call doesn't return, finish the basic block and clear the @@ -4528,7 +4531,6 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, } (); // Emit the assume_aligned check on the return value. - const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl(); if (Ret.isScalar() && TargetDecl) { if (const auto *AA = TargetDecl->getAttr()) { llvm::Value *OffsetValue = nullptr; diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index ff48566af2..95ec4fa690 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -1959,6 +1959,20 @@ llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D, return T; } +void CGDebugInfo::addHeapAllocSiteMetadata(llvm::Instruction *CI, + QualType D, + SourceLocation Loc) { + llvm::MDNode *node; + if (D.getTypePtr()->isVoidPointerType()) { + node = llvm::MDNode::get(CGM.getLLVMContext(), None); + } else { + QualType PointeeTy = D.getTypePtr()->getPointeeType(); + node = getOrCreateType(PointeeTy, getOrCreateFile(Loc)); + } + + CI->setMetadata("heapallocsite", node); +} + void CGDebugInfo::completeType(const EnumDecl *ED) { if (DebugKind <= codegenoptions::DebugLineTablesOnly) return; diff --git a/lib/CodeGen/CGDebugInfo.h b/lib/CodeGen/CGDebugInfo.h index 67331b65d3..fd7df08919 100644 --- a/lib/CodeGen/CGDebugInfo.h +++ b/lib/CodeGen/CGDebugInfo.h @@ -476,6 +476,10 @@ public: /// Emit standalone debug info for a type. llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); + /// Add heapallocsite metadata for MSAllocator calls. + void addHeapAllocSiteMetadata(llvm::Instruction *CallSite, QualType Ty, + SourceLocation Loc); + void completeType(const EnumDecl *ED); void completeType(const RecordDecl *RD); void completeRequiredType(const RecordDecl *RD); diff --git a/test/CodeGen/debug-info-codeview-heapallocsite.c b/test/CodeGen/debug-info-codeview-heapallocsite.c new file mode 100644 index 0000000000..a0a7e3b71c --- /dev/null +++ b/test/CodeGen/debug-info-codeview-heapallocsite.c @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -triple x86_64-windows-msvc -debug-info-kind=limited -gcodeview -fdeclspec -S -emit-llvm < %s | FileCheck %s + +struct Foo { + int x; +}; + +__declspec(allocator) void *alloc_void(); +__declspec(allocator) struct Foo *alloc_foo(); + +void call_alloc_void() { + struct Foo *p = (struct Foo*)alloc_void(); +} + +void call_alloc_foo() { + struct Foo *p = alloc_foo(); +} + +// CHECK-LABEL: define {{.*}}void @call_alloc_void +// CHECK: call i8* {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG1:!.*]] + +// CHECK-LABEL: define {{.*}}void @call_alloc_foo +// CHECK: call %struct.Foo* {{.*}}@alloc_foo{{.*}} !heapallocsite [[DBG2:!.*]] + +// CHECK: [[DBG1]] = !{} +// CHECK: [[DBG2]] = distinct !DICompositeType(tag: DW_TAG_structure_type, +// CHECK-SAME: name: "Foo" + -- 2.40.0