Index: clang/lib/CodeGen/CGCall.cpp =================================================================== --- clang/lib/CodeGen/CGCall.cpp +++ clang/lib/CodeGen/CGCall.cpp @@ -3800,6 +3800,8 @@ 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 @@ -4288,11 +4290,7 @@ // 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); @@ -4376,11 +4374,16 @@ // 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 + if (!DisableDebugInfo) { + if (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 @@ -4537,7 +4540,6 @@ } (); // 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; Index: clang/lib/CodeGen/CGDebugInfo.h =================================================================== --- clang/lib/CodeGen/CGDebugInfo.h +++ clang/lib/CodeGen/CGDebugInfo.h @@ -476,6 +476,10 @@ /// 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); Index: clang/lib/CodeGen/CGDebugInfo.cpp =================================================================== --- clang/lib/CodeGen/CGDebugInfo.cpp +++ clang/lib/CodeGen/CGDebugInfo.cpp @@ -1959,6 +1959,22 @@ return T; } +void CGDebugInfo::addHeapAllocSiteMetadata(llvm::Instruction *CI, + QualType D, + SourceLocation Loc) { + // FIXME: use the type that return value is cast to + 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; Index: clang/test/CodeGen/debug-info-codeview-heapallocsite.c =================================================================== --- /dev/null +++ clang/test/CodeGen/debug-info-codeview-heapallocsite.c @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -triple x86_64-windows-msvc -debug-info-kind=limited -gcodeview -fdeclspec -S -emit-llvm < %s | FileCheck %s + +char buf[1024]; +__declspec(allocator) void *myalloc(int s) { + void *p = &buf[0]; + return p; +} + +void call_alloc() { + char *p = (char*)myalloc(sizeof(char)); +} + +// CHECK-LABEL: define {{.*}}void @call_alloc +// CHECK: call i8* @myalloc(i32 1){{.*}} !heapallocsite [[DBG_F1:!.*]] +// CHECK: [[DBG_F1:!.*]] = !{}