[codegen] Store address of indirect arguments on the stack
authorFelipe de Azevedo Piovezan <fpiovezan@apple.com>
Fri, 6 Jan 2023 18:52:22 +0000 (15:52 -0300)
committerFelipe de Azevedo Piovezan <fpiovezan@apple.com>
Mon, 16 Jan 2023 14:14:55 +0000 (11:14 -0300)
commit7e4447a17db4a070f01c8f8a87505a4b2a1b0e3a
tree4824c97920902863512a513e16a658b5ba8b6ccf
parent537cdf92c4344d9d41284b1b49ee8cc7f6bcd40d
[codegen] Store address of indirect arguments on the stack

With codegen prior to this patch, truly indirect arguments -- i.e.
those that are not `byval` -- can have their debug information lost even
at O0. Because indirect arguments are passed by pointer, and this
pointer is likely placed in a register as per the function call ABI,
debug information is lost as soon as the register gets clobbered.

This patch solves the issue by storing the address of the parameter on
the stack, using a similar strategy employed when C++ references are
passed. In other words, this patch changes codegen from:

```
define @foo(ptr %arg) {
   call void @llvm.dbg.declare(%arg, [...], metadata !DIExpression())
```

To:

```
define @foo(ptr %arg) {
   %ptr_storage = alloca ptr
   store ptr %arg, ptr %ptr_storage
   call void @llvm.dbg.declare(%ptr_storage, [...], metadata !DIExpression(DW_OP_deref))
```

Some common cases where this may happen with C or C++ function calls:
  1. "Big enough" trivial structures passed by value under the ARM ABI.
  2. Structures that are non-trivial for the purposes of call (as per
  the Itanium ABI) when passed by value.

A few tests were matching the wrong alloca (matching against the new
alloca, instead of the old one), so they were updated to either match
both allocas or include a `,` right after the alloca type, to prevent
matching against a pointer type.

Differential Revision: https://reviews.llvm.org/D141381
24 files changed:
clang/docs/ReleaseNotes.rst
clang/lib/CodeGen/CGDebugInfo.cpp
clang/lib/CodeGen/CGDebugInfo.h
clang/lib/CodeGen/CGDecl.cpp
clang/test/CodeGen/aarch64-ls64.c
clang/test/CodeGen/atomic-arm64.c
clang/test/CodeGenCXX/amdgcn-func-arg.cpp
clang/test/CodeGenCXX/debug-info.cpp
clang/test/CodeGenCXX/derived-to-base-conv.cpp
clang/test/CodeGenCoroutines/coro-params-exp-namespace.cpp
clang/test/CodeGenCoroutines/coro-params.cpp
clang/test/OpenMP/for_firstprivate_codegen.cpp
clang/test/OpenMP/parallel_firstprivate_codegen.cpp
clang/test/OpenMP/sections_firstprivate_codegen.cpp
clang/test/OpenMP/single_firstprivate_codegen.cpp
clang/test/OpenMP/target_teams_distribute_firstprivate_codegen.cpp
clang/test/OpenMP/target_teams_distribute_parallel_for_firstprivate_codegen.cpp
clang/test/OpenMP/target_teams_distribute_parallel_for_simd_firstprivate_codegen.cpp
clang/test/OpenMP/target_teams_distribute_simd_firstprivate_codegen.cpp
clang/test/OpenMP/teams_distribute_firstprivate_codegen.cpp
clang/test/OpenMP/teams_distribute_parallel_for_firstprivate_codegen.cpp
clang/test/OpenMP/teams_distribute_parallel_for_simd_firstprivate_codegen.cpp
clang/test/OpenMP/teams_distribute_simd_firstprivate_codegen.cpp
clang/test/OpenMP/teams_firstprivate_codegen.cpp