Skip to content

Commit 0d0334f

Browse files
committed
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 llvm-svn: 358307
1 parent 65132e2 commit 0d0334f

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,6 +3791,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
37913791

37923792
llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo);
37933793

3794+
const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
3795+
37943796
#ifndef NDEBUG
37953797
if (!(CallInfo.isVariadic() && CallInfo.getArgStruct())) {
37963798
// For an inalloca varargs function, we don't expect CallInfo to match the
@@ -4279,11 +4281,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
42794281
// Apply always_inline to all calls within flatten functions.
42804282
// FIXME: should this really take priority over __try, below?
42814283
if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
4282-
!(Callee.getAbstractInfo().getCalleeDecl().getDecl() &&
4283-
Callee.getAbstractInfo()
4284-
.getCalleeDecl()
4285-
.getDecl()
4286-
->hasAttr<NoInlineAttr>())) {
4284+
!(TargetDecl && TargetDecl->hasAttr<NoInlineAttr>())) {
42874285
Attrs =
42884286
Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
42894287
llvm::Attribute::AlwaysInline);
@@ -4367,11 +4365,16 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
43674365

43684366
// Suppress tail calls if requested.
43694367
if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
4370-
const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
43714368
if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
43724369
Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
43734370
}
43744371

4372+
// Add metadata for calls to MSAllocator functions
4373+
// FIXME: Get the type that the return value is cast to.
4374+
if (getDebugInfo() && TargetDecl &&
4375+
TargetDecl->hasAttr<MSAllocatorAttr>())
4376+
getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy, Loc);
4377+
43754378
// 4. Finish the call.
43764379

43774380
// If the call doesn't return, finish the basic block and clear the
@@ -4528,7 +4531,6 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
45284531
} ();
45294532

45304533
// Emit the assume_aligned check on the return value.
4531-
const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
45324534
if (Ret.isScalar() && TargetDecl) {
45334535
if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) {
45344536
llvm::Value *OffsetValue = nullptr;

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,20 @@ llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
19591959
return T;
19601960
}
19611961

1962+
void CGDebugInfo::addHeapAllocSiteMetadata(llvm::Instruction *CI,
1963+
QualType D,
1964+
SourceLocation Loc) {
1965+
llvm::MDNode *node;
1966+
if (D.getTypePtr()->isVoidPointerType()) {
1967+
node = llvm::MDNode::get(CGM.getLLVMContext(), None);
1968+
} else {
1969+
QualType PointeeTy = D.getTypePtr()->getPointeeType();
1970+
node = getOrCreateType(PointeeTy, getOrCreateFile(Loc));
1971+
}
1972+
1973+
CI->setMetadata("heapallocsite", node);
1974+
}
1975+
19621976
void CGDebugInfo::completeType(const EnumDecl *ED) {
19631977
if (DebugKind <= codegenoptions::DebugLineTablesOnly)
19641978
return;

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ class CGDebugInfo {
476476
/// Emit standalone debug info for a type.
477477
llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);
478478

479+
/// Add heapallocsite metadata for MSAllocator calls.
480+
void addHeapAllocSiteMetadata(llvm::Instruction *CallSite, QualType Ty,
481+
SourceLocation Loc);
482+
479483
void completeType(const EnumDecl *ED);
480484
void completeType(const RecordDecl *RD);
481485
void completeRequiredType(const RecordDecl *RD);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -triple x86_64-windows-msvc -debug-info-kind=limited -gcodeview -fdeclspec -S -emit-llvm < %s | FileCheck %s
2+
3+
struct Foo {
4+
int x;
5+
};
6+
7+
__declspec(allocator) void *alloc_void();
8+
__declspec(allocator) struct Foo *alloc_foo();
9+
10+
void call_alloc_void() {
11+
struct Foo *p = (struct Foo*)alloc_void();
12+
}
13+
14+
void call_alloc_foo() {
15+
struct Foo *p = alloc_foo();
16+
}
17+
18+
// CHECK-LABEL: define {{.*}}void @call_alloc_void
19+
// CHECK: call i8* {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG1:!.*]]
20+
21+
// CHECK-LABEL: define {{.*}}void @call_alloc_foo
22+
// CHECK: call %struct.Foo* {{.*}}@alloc_foo{{.*}} !heapallocsite [[DBG2:!.*]]
23+
24+
// CHECK: [[DBG1]] = !{}
25+
// CHECK: [[DBG2]] = distinct !DICompositeType(tag: DW_TAG_structure_type,
26+
// CHECK-SAME: name: "Foo"
27+

0 commit comments

Comments
 (0)