[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
morehouse added a comment. This patch caused the Windows sanitizer bot to break: http://lab.llvm.org:8011/builders/sanitizer-windows/builds/44238 Please take a look. FAILED: projects/compiler-rt/lib/fuzzer/tests/FuzzerTestObjects.gtest-all.cc.x86_64.o cmd.exe /C "cd /D C:\b\slave\sanitizer-windows\build\build\stage1\projects\compiler-rt\lib\fuzzer\tests && C:\b\slave\sanitizer-windows\build\build\stage1\.\bin\clang.exe -DWIN32 -D_WINDOWS -Wno-unknown-warning-option -DGTEST_NO_LLVM_RAW_OSTREAM=1 -DGTEST_HAS_RTTI=0 -IC:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest/include -IC:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest -Wno-deprecated-declarations -IC:/b/slave/sanitizer-windows/build/llvm.src/projects/compiler-rt/lib/fuzzer -fno-rtti -O2 -c -o FuzzerTestObjects.gtest-all.cc.x86_64.o C:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest/src/gtest-all.cc" Stack dump: 0.Program arguments: C:\b\slave\sanitizer-windows\build\build\stage1\bin\clang.exe -cc1 -triple x86_64-pc-windows-msvc19.16.27026 -emit-obj -mincremental-linker-compatible -disable-free -main-file-name gtest-all.cc -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -momit-leaf-frame-pointer -coverage-notes-file C:\b\slave\sanitizer-windows\build\build\stage1\projects\compiler-rt\lib\fuzzer\tests\FuzzerTestObjects.gtest-all.cc.x86_64.gcno -resource-dir C:\b\slave\sanitizer-windows\build\build\stage1\lib\clang\9.0.0 -D WIN32 -D _WINDOWS -D GTEST_NO_LLVM_RAW_OSTREAM=1 -D GTEST_HAS_RTTI=0 -I C:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest/include -I C:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest -I C:/b/slave/sanitizer-windows/build/llvm.src/projects/compiler-rt/lib/fuzzer -internal-isystem C:\b\slave\sanitizer-windows\build\build\stage1\lib\clang\9.0.0\include -internal-isystem C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\ATLMFC\include -internal-isystem C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include -internal-isystem C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um -internal-isystem C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt -internal-isystem C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared -internal-isystem C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um -internal-isystem C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt -internal-isystem C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt -O2 -Wno-unknown-warning-option -Wno-deprecated-declarations -fdeprecated-macro -fdebug-compilation-dir C:\b\slave\sanitizer-windows\build\build\stage1\projects\compiler-rt\lib\fuzzer\tests -ferror-limit 19 -fmessage-length 0 -fno-rtti -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.16.27026 -std=c++14 -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-loops -vectorize-slp -o FuzzerTestObjects.gtest-all.cc.x86_64.o -x c++ C:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest/src/gtest-all.cc -faddrsig 1. C:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest\src/gtest-death-test.cc:79:1: current parser token 'namespace' 2. C:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest\src/gtest.cc:149:11: LLVM IR generation of declaration 'testing' 3. C:/b/slave/sanitizer-windows/build/llvm.src/utils/unittest/googletest\src/gtest.cc:361:15: Generating code for declaration 'testing::internal::AssertHelper::AssertHelper' #0 0x7ff761d1de49 clang::CodeGen::CGDebugInfo::addHeapAllocSiteMetadata(class llvm::Instruction *,class clang::QualType,class clang::SourceLocation) c:\b\slave\sanitizer-windows\build\llvm.src\tools\clang\lib\codegen\cgdebuginfo.cpp:1967:0 #1 0x7ff761d9d462 clang::CodeGen::CodeGenFunction::EmitCall(class clang::CodeGen::CGFunctionInfo const &,class clang::CodeGen::CGCallee const &,class clang::CodeGen::ReturnValueSlot,class clang::CodeGen::CallArgList const &,class llvm::CallBase * *,class clang::SourceLocation) c:\b\slave\sanitizer-windows\build\llvm.src\tools\clang\lib\codegen\cgcall.cpp:4392:0 #2 0x7ff76202a144 EmitNewDeleteCall c:\b\slave\sanitizer-windows\build\llvm.src\tools\clang\lib\codegen\cgexprcxx.cpp:1288:0 #3 0x7ff762026a55 clang::CodeGen::CodeGenFunction::EmitCXXNewExpr(class clang::CXXNewExpr const *) c:\b\slave\sanitizer-windows\build\llvm.src\tools\clang\lib\codegen\cgexprcxx.cpp:1617:0 #4 0x7ff761fdf0a6 clang::StmtVisitorBase::Visit c:\b\slave\sanitizer-windows\build\build\stage1\tools\clang\include\clang\ast\stmtnodes.inc:699:0
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
This revision was automatically updated to reflect the committed changes. Closed by commit rL357928: [MS] Add metadata for __declspec(allocator) (authored by akhuang, committed by ). Herald added a project: LLVM. Herald added a subscriber: llvm-commits. Changed prior to commit: https://reviews.llvm.org/D60237?vs=194164=194174#toc Repository: rL LLVM CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 Files: cfe/trunk/lib/CodeGen/CGCall.cpp cfe/trunk/lib/CodeGen/CGDebugInfo.cpp cfe/trunk/lib/CodeGen/CGDebugInfo.h cfe/trunk/test/CodeGen/debug-info-codeview-heapallocsite.c Index: cfe/trunk/test/CodeGen/debug-info-codeview-heapallocsite.c === --- cfe/trunk/test/CodeGen/debug-info-codeview-heapallocsite.c +++ cfe/trunk/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" + Index: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp === --- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp +++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp @@ -1959,6 +1959,20 @@ 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; Index: cfe/trunk/lib/CodeGen/CGCall.cpp === --- cfe/trunk/lib/CodeGen/CGCall.cpp +++ cfe/trunk/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 + // FIXME: Get the type that the return value is cast to. + if (!DisableDebugInfo && 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: cfe/trunk/lib/CodeGen/CGDebugInfo.h === --- cfe/trunk/lib/CodeGen/CGDebugInfo.h +++ cfe/trunk/lib/CodeGen/CGDebugInfo.h @@ -476,6 +476,10 @@ /// Emit standalone debug info for a type. llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
akhuang updated this revision to Diff 194164. akhuang marked 2 inline comments as done. akhuang added a comment. Fixes to test case CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 Files: clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGDebugInfo.h clang/test/CodeGen/debug-info-codeview-heapallocsite.c Index: clang/test/CodeGen/debug-info-codeview-heapallocsite.c === --- /dev/null +++ clang/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" + 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,21 @@ 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; 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 + // FIXME: Get the type that the return value is cast to. + if (!DisableDebugInfo && 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;
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
rnk accepted this revision. rnk added a comment. This revision is now accepted and ready to land. lgtm, with some suggestions to improve the test Comment at: clang/test/CodeGen/debug-info-codeview-heapallocsite.c:16-19 +struct Foo foo_buf[1024]; +__declspec(allocator) struct Foo *alloc_foo() { + return _buf[0]; +} Personally, I think it's nicer to leave the allocator undefined, because then clang doesn't generate IR for it, and FileCheck doesn't have to scan through it. When these tests fail, humans have to generate the IR, read it, and try to understand it, and the less there is, the easier it is to understand. Comment at: clang/test/CodeGen/debug-info-codeview-heapallocsite.c:28 + +// CHECK-LABEL: define {{.*}}%struct.Foo* @alloc_foo +// CHECK: call %struct.Foo* @alloc_foo(){{.*}} !heapallocsite [[DBG2:!.*]] I think you want to look for `call_alloc_foo` in this CHECK. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
akhuang updated this revision to Diff 193792. akhuang marked 3 inline comments as done. akhuang added a comment. -added struct case to test -style fixes CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 Files: clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGDebugInfo.h clang/test/CodeGen/debug-info-codeview-heapallocsite.c Index: clang/test/CodeGen/debug-info-codeview-heapallocsite.c === --- /dev/null +++ clang/test/CodeGen/debug-info-codeview-heapallocsite.c @@ -0,0 +1,34 @@ +// 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) { + return [0]; +} + +void call_alloc() { + char *p = (char*)myalloc(sizeof(char)); +} + +struct Foo { + int x; +}; + +struct Foo foo_buf[1024]; +__declspec(allocator) struct Foo *alloc_foo() { + return _buf[0]; +} + +void call_alloc_foo() { + struct Foo *p = alloc_foo(); +} + +// CHECK-LABEL: define {{.*}}void @call_alloc +// CHECK: call i8* @myalloc(i32 1){{.*}} !heapallocsite [[DBG1:!.*]] + +// CHECK-LABEL: define {{.*}}%struct.Foo* @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" + 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,21 @@ 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; 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 + // FIXME: Get the type that the return value is cast to. + if (!DisableDebugInfo && 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) {
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
rnk added inline comments. Comment at: clang/lib/CodeGen/CGCall.cpp:4382-4383 + // Add metadata for calls to MSAllocator functions + if (!DisableDebugInfo) { +if (TargetDecl && TargetDecl->hasAttr()) + getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy, Loc); I think you can combine the ifs: `!DisableDebugInfo && TargetDecl ...` Comment at: clang/lib/CodeGen/CGDebugInfo.cpp:1965 + SourceLocation Loc) { + // FIXME: use the type that return value is cast to + llvm::MDNode *node; This method takes the type in, so I think it's better to put the FIXME in CGCall.cpp. It should also be capitalized like a complete sentence (https://llvm.org/docs/CodingStandards.html#commenting). Comment at: clang/test/CodeGen/debug-info-codeview-heapallocsite.c:4 +char buf[1024]; +__declspec(allocator) void *myalloc(int s) { + void *p = [0]; I think it would be interesting to add an allocator that returns a pointer to a struct, so something like: ``` struct Foo { int x; }; __declspec(allocator) Foo *alloc_foo(); Foo *call_alloc_foo() { return alloc_foo(); } ``` Then we can check for the right DICompositeType. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
akhuang updated this revision to Diff 193751. akhuang marked 2 inline comments as done. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 Files: clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGDebugInfo.h clang/test/CodeGen/debug-info-codeview-heapallocsite.c 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 = [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:!.*]] = !{} 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/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; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
akhuang marked an inline comment as done. akhuang added inline comments. Comment at: clang/lib/CodeGen/CGAtomic.cpp:1691 } else { -// Build new lvalue for temp address +// Build new lvalue for temp address. Address Ptr = Atomics.materializeRValue(OldRVal); rnk wrote: > I don't have an issue with these changes if you want to make them, but they > should be committed separately. Whoops, these were supposed to be a separate test commit but I guess they ended up in here too. Comment at: clang/lib/CodeGen/CGCall.cpp:4384-4385 +if (TargetDecl && TargetDecl->hasAttr()) + CI->setMetadata("heapallocsite", getDebugInfo()-> + getMSAllocatorMetadata(RetTy, Loc)); + } rnk wrote: > I think we should make CGDebugInfo responsible for calling setMetadata. In > some sense, "heapallocsite" metadata is debug info, so it makes sense that it > would be documented there. Also, if there are other places where we need to > add this metadata, we won't have to duplicate this string literal. > > So, CGDebugInfo should have some new method > `addHeapAllocSiteMetadata(Instruction *CallSite, QualType Ty)`, and that can > call the private getOrCreateType method. Sound good? Makes sense. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
rnk added inline comments. Comment at: clang/lib/CodeGen/CGAtomic.cpp:1691 } else { -// Build new lvalue for temp address +// Build new lvalue for temp address. Address Ptr = Atomics.materializeRValue(OldRVal); I don't have an issue with these changes if you want to make them, but they should be committed separately. Comment at: clang/lib/CodeGen/CGCall.cpp:4384-4385 +if (TargetDecl && TargetDecl->hasAttr()) + CI->setMetadata("heapallocsite", getDebugInfo()-> + getMSAllocatorMetadata(RetTy, Loc)); + } I think we should make CGDebugInfo responsible for calling setMetadata. In some sense, "heapallocsite" metadata is debug info, so it makes sense that it would be documented there. Also, if there are other places where we need to add this metadata, we won't have to duplicate this string literal. So, CGDebugInfo should have some new method `addHeapAllocSiteMetadata(Instruction *CallSite, QualType Ty)`, and that can call the private getOrCreateType method. Sound good? Comment at: clang/test/CodeGen/debug-info-codeview-heapallocsite.c:13 + +// CHECK: !heapallocsite [[DBG_F1:!.*]] +// CHECK: [[DBG_F1:!.*]] = !{} We should expect this test case to grow, so it would be good to add more context around here. Something like: CHECK-LABEL: define{{.*}} @call_alloc CHECK: call i8* @myalloc(i64 1) {{.*}} !heapallocsite ... Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60237/new/ https://reviews.llvm.org/D60237 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D60237: [MS] Add metadata for __declspec(allocator)
akhuang created this revision. akhuang added a reviewer: rnk. Herald added subscribers: cfe-commits, jfb. Herald added a project: clang. 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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D60237 Files: clang/lib/CodeGen/CGAtomic.cpp clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGDebugInfo.h clang/test/CodeGen/debug-info-codeview-heapallocsite.c Index: clang/test/CodeGen/debug-info-codeview-heapallocsite.c === --- /dev/null +++ clang/test/CodeGen/debug-info-codeview-heapallocsite.c @@ -0,0 +1,14 @@ +// 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 = [0]; + return p; +} + +void call_alloc() { + char *p = (char*)myalloc(sizeof(char)); +} + +// CHECK: !heapallocsite [[DBG_F1:!.*]] +// CHECK: [[DBG_F1:!.*]] = !{} Index: clang/lib/CodeGen/CGDebugInfo.h === --- clang/lib/CodeGen/CGDebugInfo.h +++ clang/lib/CodeGen/CGDebugInfo.h @@ -476,6 +476,9 @@ /// Emit standalone debug info for a type. llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); + /// Get debug info for MSAllocator metadata. + llvm::MDNode *getMSAllocatorMetadata(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,20 @@ return T; } +llvm::MDNode *CGDebugInfo::getMSAllocatorMetadata(QualType D, + SourceLocation Loc) { + // FIXME: return 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)); + } + return node; +} + + void CGDebugInfo::completeType(const EnumDecl *ED) { if (DebugKind <= codegenoptions::DebugLineTablesOnly) return; 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,17 @@ // 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()) + CI->setMetadata("heapallocsite", getDebugInfo()-> + getMSAllocatorMetadata(RetTy, Loc)); + } + // 4. Finish the call. // If the call doesn't return, finish the basic block and clear the @@ -4537,7 +4541,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/CGAtomic.cpp === --- clang/lib/CodeGen/CGAtomic.cpp +++ clang/lib/CodeGen/CGAtomic.cpp @@ -1688,7 +1688,7 @@ UpRVal = OldRVal; DesiredLVal = CGF.MakeAddrLValue(DesiredAddr, AtomicLVal.getType()); } else { -// Build new lvalue for temp address +