Open pogo59 opened 2 hours ago
@llvm/issue-subscribers-clang-codegen
Author: Paul T Robinson (pogo59)
@llvm/issue-subscribers-debuginfo
Author: Paul T Robinson (pogo59)
Note that fixing the if
issue resulted in a noticeable reduction in object size, as reported here. I'd expect the for
statement fix to cause some reduction as well.
I did not find any similar issues with the while
statement. I think that if/for/while are the only places that implicitly introduce lexical scopes, although C++ has become so huge it's hard to be certain.
I'm filing this issue as a follow-up to PR108300. Ordinarily I'd track this as an internal issue within Sony, but as I'm retiring from Sony effective today, I won't have access to my test case or explanation. I do intend to look at this in the next couple of weeks.
Here's my example. It's the simplest form of the
for
statement; more complicated forms might have additional issues, but let's start small.Compile with
-g -gno-column-info -S -emit-llvm
and we see this IR and location metadata:Observe that the evaluation of the condition uses
!18
as itsDILocation
, while the conditional branch uses!21
. These have the same source location, but different scopes. When it comes time to emit the.loc
directives for the machine instructions (inllvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
), the fact that the branch has a differentDILocation
causes us to generate a new.loc
directive. The fact that the only difference is the scope (which is irrelevant to.loc
) is not considered.Look at the assembly code for the same block:
The
.loc
at.Ltmp1
is emitted because it is at the start of a new basic block. But the.loc
at.Ltmp2
is 100% redundant; it's emitted only because of the scope confusion described above.Arguably we could filter out the redundant
.loc
directives in LLVM, but I claim the root cause is in Clang codegen; see the PR cited above, which fixed the equivalent problem withif
statements simply by correcting how Clang sets up its notion ofDebugLoc
.