Issue 240457
Summary [clang][DebugInfo] dbg_declare of an NRVO local uses the addrspacecast instead of the alloca
Labels clang
Assignees
Reporter lywei-intel
    On targets where allocas are not in the default address space (AMDGPU), the dbg_declare for an NRVO local refers to the addrspacecast instead of the alloca. 

Source:

```cpp
// big.cpp
struct Big {
  int v[8];
 Big(int a);
  Big(const Big &o);
};
Big make(int a) {
  Big rtn(a);
 return rtn;
}
```

LLVM IR:

```llvm
; ModuleID = 'big.cpp'
source_filename = "big.cpp"
target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
target triple = "amdgcn-amd-amdhsa"

%struct.Big = type { [8 x i32] }

@__oclc_ABI_version = weak_odr hidden local_unnamed_addr addrspace(4) constant i32 500

; Function Attrs: mustprogress noinline optnone
define hidden void @_Z4makei(ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %0, i32 noundef %1) #0 !dbg !9 {
  %3 = alloca ptr, align 8, addrspace(5)
  %4 = alloca i32, align 4, addrspace(5)
  %5 = addrspacecast ptr addrspace(5) %3 to ptr
  %6 = addrspacecast ptr addrspace(5) %4 to ptr
 store ptr %0, ptr %5, align 8
  store i32 %1, ptr %6, align 4
  call void @llvm.dbg.declare(metadata ptr addrspace(5) %4, metadata !15, metadata !DIExpression()), !dbg !16
  call void @llvm.dbg.declare(metadata ptr %5, metadata !17, metadata !DIExpression(DW_OP_deref)), !dbg !18
  ; Expected: call void @llvm.dbg.declare(metadata ptr addrspace(5) %3, metadata !17, metadata !DIExpression(DW_OP_deref)), !dbg !18
  %7 = load i32, ptr %6, align 4, !dbg !19
  call void @_ZN3BigC1Ei(ptr noundef nonnull align 4 dereferenceable(32) %0, i32 noundef %7), !dbg !18
  ret void, !dbg !20
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

declare hidden void @_ZN3BigC1Ei(ptr noundef nonnull align 4 dereferenceable(32), i32 noundef) unnamed_addr #2

attributes #0 = { mustprogress noinline optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7}
!llvm.ident = !{!8}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "Ubuntu clang version 18.1.3 (1ubuntu1)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "big.cpp", directory: "/tmp")
!2 = !{i32 1, !"amdgpu_code_object_version", i32 500}
!3 = !{i32 7, !"Dwarf Version", i32 5}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 1, !"wchar_size", i32 4}
!6 = !{i32 8, !"PIC Level", i32 2}
!7 = !{i32 7, !"frame-pointer", i32 2}
!8 = !{!"Ubuntu clang version 18.1.3 (1ubuntu1)"}
!9 = distinct !DISubprogram(name: "make", linkageName: "_Z4makei", scope: !1, file: !1, line: 6, type: !10, scopeLine: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
!10 = !DISubroutineType(types: !11)
!11 = !{!12, !13}
!12 = !DICompositeType(tag: DW_TAG_structure_type, name: "Big", file: !1, line: 1, size: 256, flags: DIFlagFwdDecl | DIFlagNonTrivial, identifier: "_ZTS3Big")
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!14 = !{}
!15 = !DILocalVariable(name: "a", arg: 1, scope: !9, file: !1, line: 6, type: !13)
!16 = !DILocation(line: 6, column: 14, scope: !9)
!17 = !DILocalVariable(name: "rtn", scope: !9, file: !1, line: 7, type: !12)
!18 = !DILocation(line: 7, column: 7, scope: !9)
!19 = !DILocation(line: 7, column: 11, scope: !9)
!20 = !DILocation(line: 8, column: 3, scope: !9)
```

Command:
```bash
# clang 18.1.3 (also reproduces on trunk, see godbolt link)
clang -target amdgcn-amd-amdhsa -O0 -g -S -emit-llvm -nogpulib big.cpp
```

Reproduced on trunk: https://godbolt.org/z/aYx4n6W66

Backend will lose rtn's location because the declare maps to addrspacecast instead of alloca.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to