From a89259574af0e5518dfed8ce0741f549f32d1421 Mon Sep 17 00:00:00 2001 From: Grigory Pastukhov Date: Tue, 8 Jul 2025 16:08:00 -0700 Subject: [PATCH 1/2] [Coroutines] Fix debug info scoping for nested structs in coroutine frames --- llvm/lib/Transforms/Coroutines/CoroFrame.cpp | 4 +- .../coro-split-dbg-nested-struct.ll | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp index 7224a56cd7b8a..fe30c6dc6abe4 100644 --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -639,10 +639,10 @@ static DIType *solveDIType(DIBuilder &Builder, Type *Ty, SmallVector Elements; for (unsigned I = 0; I < StructTy->getNumElements(); I++) { DIType *DITy = solveDIType(Builder, StructTy->getElementType(I), Layout, - Scope, LineNum, DITypeCache); + DIStruct, LineNum, DITypeCache); assert(DITy); Elements.push_back(Builder.createMemberType( - Scope, DITy->getName(), Scope->getFile(), LineNum, + DIStruct, DITy->getName(), DIStruct->getFile(), LineNum, DITy->getSizeInBits(), DITy->getAlignInBits(), Layout.getStructLayout(StructTy)->getElementOffsetInBits(I), llvm::DINode::FlagArtificial, DITy)); diff --git a/llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll b/llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll new file mode 100644 index 0000000000000..7aa1a0fb2c921 --- /dev/null +++ b/llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll @@ -0,0 +1,50 @@ +; RUN: opt < %s -passes='cgscc(coro-split)' -S | FileCheck %s + +; Test that nested structs in coroutine frames have correct debug info scoping. + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Minimal nested struct types that trigger the scoping issue +%"struct.Inner" = type { i32, ptr } +%"struct.Outer" = type { %"struct.Inner", i64 } +%"class.Promise" = type { %"struct.Outer" } + +define void @test_coro_function() presplitcoroutine !dbg !10 { +entry: + %__promise = alloca %"class.Promise", align 8 + %0 = call token @llvm.coro.id(i32 0, ptr %__promise, ptr null, ptr null) + %1 = call ptr @llvm.coro.begin(token %0, ptr null) + %2 = call token @llvm.coro.save(ptr null) + ret void +} + +; The test passes if the debug info is generated without crashing +; CHECK: define void @test_coro_function() + +; Check that frame debug info is generated +; CHECK: ![[FRAME_TYPE:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "{{.*}}.coro_frame_ty" + +; Key validation: Check that nested structs have the correct scope hierarchy +; 1. Promise should be scoped to the frame +; CHECK: ![[PROMISE:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "class_Promise", scope: ![[FRAME_TYPE]] + +; 2. Outer should be scoped to Promise (not the frame!) +; CHECK: ![[OUTER:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "struct_Outer", scope: ![[PROMISE]] + +; 3. Inner should be scoped to Outer (proper nesting) +; CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "struct_Inner", scope: ![[OUTER]] + +declare token @llvm.coro.id(i32, ptr readnone, ptr readonly, ptr) +declare ptr @llvm.coro.begin(token, ptr writeonly) +declare token @llvm.coro.save(ptr) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!9} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug) +!1 = !DIFile(filename: "test.cpp", directory: ".") +!9 = !{i32 2, !"Debug Info Version", i32 3} +!10 = distinct !DISubprogram(name: "test_coro_function", scope: !1, file: !1, line: 1, type: !11, spFlags: DISPFlagDefinition, unit: !0) +!11 = !DISubroutineType(types: !12) +!12 = !{null} From 6901393dea2dbcae1f013241df8ad8e2d5a3ebca Mon Sep 17 00:00:00 2001 From: Grigory Pastukhov Date: Wed, 9 Jul 2025 12:09:10 -0700 Subject: [PATCH 2/2] Addressed reviewer's comments --- .../coro-split-dbg-nested-struct.ll | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll b/llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll index 7aa1a0fb2c921..12dfa16991326 100644 --- a/llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll +++ b/llvm/test/Transforms/Coroutines/coro-split-dbg-nested-struct.ll @@ -2,10 +2,9 @@ ; Test that nested structs in coroutine frames have correct debug info scoping. -target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" -target triple = "x86_64-unknown-linux-gnu" - -; Minimal nested struct types that trigger the scoping issue +; Minimal nested struct types that used to trigger a scoping issue: +; we used to set the wrong `scope` for the `DIDerivedType` member entries of the `DICompositeType` +; as well as the `scope` for `DICompositeType` for the inner struct itself. %"struct.Inner" = type { i32, ptr } %"struct.Outer" = type { %"struct.Inner", i64 } %"class.Promise" = type { %"struct.Outer" } @@ -19,7 +18,6 @@ entry: ret void } -; The test passes if the debug info is generated without crashing ; CHECK: define void @test_coro_function() ; Check that frame debug info is generated @@ -29,11 +27,24 @@ entry: ; 1. Promise should be scoped to the frame ; CHECK: ![[PROMISE:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "class_Promise", scope: ![[FRAME_TYPE]] -; 2. Outer should be scoped to Promise (not the frame!) +; 2. Members of Promise should be scoped to Promise (check this before Outer since it comes first in output) +; CHECK: !DIDerivedType(tag: DW_TAG_member, name: "struct_Outer", scope: ![[PROMISE]] + +; 3. Outer should be scoped to Promise (not the frame!) ; CHECK: ![[OUTER:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "struct_Outer", scope: ![[PROMISE]] -; 3. Inner should be scoped to Outer (proper nesting) -; CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "struct_Inner", scope: ![[OUTER]] +; 4. First Outer member should be scoped to Outer +; CHECK: !DIDerivedType(tag: DW_TAG_member, name: "struct_Inner", scope: ![[OUTER]] + +; 5. Inner should be scoped to Outer (proper nesting) +; CHECK: ![[INNER:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "struct_Inner", scope: ![[OUTER]] + +; 6. Members of Inner should be scoped to Inner +; CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__int_32", scope: ![[INNER]] +; CHECK: !DIDerivedType(tag: DW_TAG_member, name: "PointerType", scope: ![[INNER]] + +; 7. Second Outer member comes after Inner (due to output order) +; CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__int_64", scope: ![[OUTER]] declare token @llvm.coro.id(i32, ptr readnone, ptr readonly, ptr) declare ptr @llvm.coro.begin(token, ptr writeonly)