Skip to content

Commit dc82ed2

Browse files
authored
Fix debug info generation for this (microsoft#5534)
My recent changes to correct AST inaccuracies around `this` objects caused crashes in debug info generation for `this`. This resolves the crashes by making the code behave correctly when `this` is a reference. This has no change to debug info output. The added test case should verify the same output we've always had, but is an important addition since this was not tested in our pre-merge testing. Fixes microsoft#5530
1 parent 3693755 commit dc82ed2

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

tools/clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,11 +1116,12 @@ llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType(
11161116
const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl();
11171117
if (isa<ClassTemplateSpecializationDecl>(RD)) {
11181118
// Create pointer type directly in this case.
1119-
const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
1120-
QualType PointeeTy = ThisPtrTy->getPointeeType();
1119+
// HLSL Change Begin - This is a reference.
1120+
QualType PointeeTy = ThisPtr->getPointeeType();
11211121
unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
11221122
uint64_t Size = CGM.getTarget().getPointerWidth(AS);
1123-
uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
1123+
uint64_t Align = CGM.getContext().getTypeAlign(ThisPtr.getTypePtr());
1124+
// HLSL Change End - This is a reference.
11241125
llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit);
11251126
llvm::DIType *ThisPtrType =
11261127
DBuilder.createPointerType(PointeeType, Size, Align);

tools/clang/test/HLSLFileCheck/hlsl/template/templateStructFunc.hlsl

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// RUN: %dxc -E main -T ps_6_0 -HV 2021 %s | FileCheck %s
1+
// RUN: %dxc -E main -T ps_6_0 -fcgl %s | FileCheck %s
2+
// RUN: %dxc -E main -T ps_6_0 -fcgl -Zi -Qembed_debug %s | FileCheck -check-prefixes=CHECK,DI %s
3+
4+
// The goal of this test is to verify the instantiations of `foo` are correctly
5+
// generated _and_ to verify that the correct debug information is generated for
6+
// the instantiations.
27

3-
// CHECK:define void @main
48

59
template<typename T>
610
struct Test {
711

812
T t;
913
T foo(T t1) {
10-
return sin(t) * cos(t1);
14+
return t * t1;
1115
}
1216

1317
};
@@ -19,3 +23,53 @@ float2 main(float4 a:A) : SV_Target {
1923
t1.t = a.xy;
2024
return t0.foo(a.y) + t1.foo(a.zw);
2125
}
26+
27+
28+
// CHECK: define internal float @"\01?foo@?$Test@{{[A-Z0-9@]+}}"
29+
// CHECK-SAME: (%"struct.Test<float>"* [[this:%.*]], float [[t1:%.*]])
30+
31+
// CHECK: [[t1LocalAddr:%.*]] = alloca float,
32+
// CHECK-NEXT: store float [[t1]], float* [[t1LocalAddr]]
33+
34+
// DI-NEXT: call void @llvm.dbg.declare(metadata float* [[t1LocalAddr]], metadata [[t1FVar:![0-9]+]]
35+
// DI-NEXT: call void @llvm.dbg.declare(metadata %"struct.Test<float>"* [[this]], metadata [[thisFVar:![0-9]+]]
36+
37+
// CHECK-NEXT: [[this_tAddr:%.*]] = getelementptr inbounds %"struct.Test<float>", %"struct.Test<float>"* [[this]], i32 0, i32 0,
38+
// CHECK-NEXT: [[this_t:%.*]] = load float, float* [[this_tAddr]],
39+
// CHECK-NEXT: [[t1Local:%.*]] = load float, float* [[t1LocalAddr]],
40+
// CHECK-NEXT: [[Res:%.*]] = fmul float [[this_t]], [[t1Local]]
41+
// CHECK-NEXT: ret float [[Res]]
42+
43+
44+
// CHECK: define internal <2 x float> @"\01?foo@?$Test@V?$vector{{[A-Za-z0-9@?$]+}}"
45+
// CHECK-SAME: (%"struct.Test<vector<float, 2> >"* [[this:%.*]], <2 x float> [[t1:%.*]])
46+
47+
// CHECK: [[t1LocalAddr:%.*]] = alloca <2 x float>
48+
// CHECK-NEXT: store <2 x float> [[t1]], <2 x float>* [[t1LocalAddr]]
49+
50+
// DI-NEXT: call void @llvm.dbg.declare(metadata <2 x float>* [[t1LocalAddr]], metadata [[t1VVar:![0-9]+]]
51+
// DI-NEXT: call void @llvm.dbg.declare(metadata %"struct.Test<vector<float, 2> >"* [[this]], metadata [[thisVVar:![0-9]+]]
52+
53+
// CHECK-NEXT: [[this_tAddr:%.*]] = getelementptr inbounds %"struct.Test<vector<float, 2> >", %"struct.Test<vector<float, 2> >"* [[this]], i32 0, i32 0
54+
// CHECK-NEXT: [[this_t:%.*]] = load <2 x float>, <2 x float>* [[this_tAddr]]
55+
// CHECK-NEXT: [[t1Local:%.*]] = load <2 x float>, <2 x float>* [[t1LocalAddr]]
56+
// CHECK-NEXT: [[Res:%.*]] = fmul <2 x float> [[this_t]], [[t1Local]]
57+
// CHECK-NEXT ret <2 x float> [[Res]]
58+
59+
60+
// DI: [[DIFile:![0-9]+]] = !DIFile
61+
// DI-DAG: [[DIFloat:![0-9]+]] = !DIBasicType(name: "float", size: 32, align: 32, encoding: DW_ATE_float)
62+
63+
// DI-DAG: [[FSub:![0-9]+]] = !DISubprogram(name: "foo", linkageName: "\01?foo@?$Test@{{[A-Z@0-9]+}}", scope: [[FloatScope:![0-9]+]], file: [[DIFile]], line: {{[0-9]+}}, type: {{![0-9]+}}, isLocal: false, isDefinition: true, scopeLine: {{[0-9]+}}, flags: DIFlagPrototyped, isOptimized: false, function: float (%"struct.Test<float>"*, float)* @"\01?foo@?$Test@{{[A-Z@0-9]+}}",
64+
// DI-DAG: [[DITestF:![0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Test<float>"
65+
66+
// DI-DAG: [[VSub:![0-9]+]] = !DISubprogram(name: "foo", linkageName: "\01?foo@?$Test@V?$vector{{[A-Za-z0-9@?$]+}}", scope: [[VecScope:![0-9]+]], file: [[DIFile]], line: {{[0-9]+}}, type: {{![0-9]+}}, isLocal: false, isDefinition: true, scopeLine: {{[0-9]+}}, flags: DIFlagPrototyped, isOptimized: false, function: <2 x float> (%"struct.Test<vector<float, 2> >"*, <2 x float>)* @"\01?foo@?$Test@V?$vector{{[A-Za-z0-9@?$]+}}",
67+
// DI-DAG: [[DITestV:![0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Test<vector<float, 2> >", file: [[DIFile]], line: {{[0-9]+}}, size: 64, align: 32
68+
69+
// DI-DAG: [[t1FVar]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "t1", arg: 2, scope: [[FSub]], file: [[DIFile]], line: {{[0-9]+}}, type: [[DIFloat]])
70+
// DI-DAG: [[thisFVar]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "this", arg: 1, scope: [[FSub]], type: [[thisFPtr:![0-9]+]])
71+
// DI-DAG: [[thisFPtr]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[DITestF]], size: 32, align: 32)
72+
73+
// DI-DAG: [[t1VVar]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "t1", arg: 2, scope: [[VSub]], file: [[DIFile]]
74+
// DI-DAG: [[thisVVar]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "this", arg: 1, scope: [[VSub]], type: [[thisVPtr:![0-9]+]])
75+
// DI-DAG: [[thisVPtr]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[DITestV]], size: 32, align: 32)

0 commit comments

Comments
 (0)