Skip to content

Commit

Permalink
[CodeGen] Only consider innermost cast for !heapallocsite
Browse files Browse the repository at this point in the history
Without opaque pointers, this code determined !heapallocsite based
on the innermost cast of the allocation call. With opaque pointers,
the casts no longer generate an instruction, so the outermost cast
is used. Add an explicit check for nested casts to prevent this.

Differential Revision: https://reviews.llvm.org/D145788
  • Loading branch information
nikic committed May 9, 2023
1 parent 0c852dc commit cac4d7f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {

// Update heapallocsite metadata when there is an explicit pointer cast.
if (auto *CI = dyn_cast<llvm::CallBase>(Src)) {
if (CI->getMetadata("heapallocsite") && isa<ExplicitCastExpr>(CE)) {
if (CI->getMetadata("heapallocsite") && isa<ExplicitCastExpr>(CE) &&
!isa<CastExpr>(E)) {
QualType PointeeType = DestTy->getPointeeType();
if (!PointeeType.isNull())
CGF.getDebugInfo()->addHeapAllocSiteMetadata(CI, PointeeType,
Expand Down
10 changes: 5 additions & 5 deletions clang/test/CodeGen/debug-info-codeview-heapallocsite.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-windows-msvc -debug-info-kind=limited -gcodeview -fdeclspec -S -emit-llvm %s -o - | FileCheck %s
// RUN: %clang_cc1 -triple x86_64-windows-msvc -debug-info-kind=limited -gcodeview -fdeclspec -S -emit-llvm %s -o - | FileCheck %s

struct Foo;
struct Bar;
Expand All @@ -14,10 +14,10 @@ void call_alloc(void) {
}

// CHECK-LABEL: define {{.*}}void @call_alloc
// CHECK: call i8* {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG1:!.*]]
// CHECK: call %struct.Foo* {{.*}}@alloc_foo{{.*}} !heapallocsite [[DBG2:!.*]]
// CHECK: call i8* {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG2]]
// CHECK: call i8* {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG3:!.*]]
// CHECK: call ptr {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG1:!.*]]
// CHECK: call ptr {{.*}}@alloc_foo{{.*}} !heapallocsite [[DBG2:!.*]]
// CHECK: call ptr {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG2]]
// CHECK: call ptr {{.*}}@alloc_void{{.*}} !heapallocsite [[DBG3:!.*]]

// CHECK: [[DBG2]] = !DICompositeType(tag: DW_TAG_structure_type,
// CHECK-SAME: name: "Foo"
Expand Down

0 comments on commit cac4d7f

Please sign in to comment.