llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[DebugInfo] Lifetime of function arguments is too short in optimized code.

Open llvmbot opened this issue 8 years ago • 40 comments

Bugzilla Link 39715
Resolution FIXED
Resolved on Jul 25, 2020 12:25
Version trunk
OS All
Depends On llvm/llvm-project#44998
Blocks llvm/llvm-project#38116
Reporter LLVM Bugzilla Contributor
CC @asl,@adrian-prantl,@dwblaikie,@djtodoro,@echristo,@fredriss,@JDevlieghere,@jmorse,@jdm,@walkerkd,@pogo59,@rnk,@tbosch,@vedantk

Extended Description

Same test case as before:

attribute((noinline)) int fn1 (long int x, long int y, long int z) { int l = x * 2; int q = y * z; return l + q; }

attribute((noinline)) long int fn2 (long int a, long int b, long int c) { long int q = 2 * a; return fn1 (5, 6, 7); }

int main(void) { return fn2(14, 23, 34); }

(built with -O3 -g). lldb session:

Process 30254 stopped

  • thread #​1, queue = 'com.apple.main-thread', stop reason = step in frame #​0: 0x0000000100000f74 a.out`fn1(x=5, y=6, z=7) at optout.c:4:19 [opt] 1 attribute((noinline)) 2 int fn1 (long int x, long int y, long int z) { 3 int l = x * 2; -> 4 int q = y * z; 5 return l + q; 6 } 7 (lldb) frame var z (long) z = 7 (lldb) n Process 30254 stopped
  • thread #​1, queue = 'com.apple.main-thread', stop reason = step over frame #​0: 0x0000000100000f77 a.out`fn1(x=5, y=6, z=) at optout.c:5:18 [opt] 2 int fn1 (long int x, long int y, long int z) { 3 int l = x * 2; 4 int q = y * z; -> 5 return l + q; 6 } 7 8 attribute((noinline)) long int (lldb) frame var z (long) z =

llvmbot avatar Nov 20 '18 00:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#46848

tbosch avatar Nov 27 '21 03:11 tbosch

mentioned in issue llvm/llvm-project#44998

tbosch avatar Nov 27 '21 03:11 tbosch

Created llvm/llvm-bugzilla-archive#46848 to track the remaining cleanup work. Closing this issue.

tbosch avatar Jul 25 '20 17:07 tbosch

Vedant, while I am trying to land the narrow fix, could you outline the more involved real fix of getting rid of the duplicate DBG_VALUEs for COPYs?

Especially the part about:

try to make up for any loss in location coverage by making generic improvements to LiveDebugValues.

I saw that you pinged me on https://reviews.llvm.org/D80684. Would removing EntryValueCopyBackupKind from EntryDebugValues be the only required fix?

Also, how could I test that the eventual fix doesn't loose location coverage? Do you have specific test cases in mind?

Sorry for the delayed response.

What I had in mind was deleting "LiveInMap" from SelectionDAGISel::runOnMachineFunction (and everything that uses it). I tried this on a lark before commenting on 2020-05-01 and found that this resulted in ~15 or so test failures during check-llvm. Some of these are tests that are being overly strict about what they match -- these can be fixed up by mechanically updating the check lines. But others represent real regressions in location coverage.

AIUI, it should be the job of LiveDebugValues::transferRegisterCopy to track when a copy introduces a backup location for a variable, /not/ the job of the instruction selector. So (and I'm hand-waving a bit here), if we can fix the tests that regress by making transferRegisterCopy more aggressive, that could be a nice, general improvement.

My impression goes in the same way. I think the LiveDebugValues::transferRegisterCopy() should track all these copies.

As for measuring changes in location coverage, you can compile a stage2 clang binary with vs. without your changes, and use llvm-dwarfdump --statistics to collect location coverage stats.

Btw, my understanding of Djordje's comment in https://reviews.llvm.org/D80684 is that EntryValueCopyBackupKind is a hack invented to work around SelectionDAG's insertion of DBG_VALUEs after copies in the entry block. We might be able to delete EntryValueCopyBackupKind after changing SelectionDAG, but not before. Yes. That piece of code from the SelectionDAG violates the premise (before LiveDebugValues) that each DBG_VALUE represents assignment of a variable (and changing of its value potentially); so the EntryValueCopyBackupKind is used to track these copies from SelectionDAG.

djtodoro avatar Jun 04 '20 06:06 djtodoro

Btw, my understanding of Djordje's comment in https://reviews.llvm.org/D80684 is that EntryValueCopyBackupKind is a hack invented to work around SelectionDAG's insertion of DBG_VALUEs after copies in the entry block. We might be able to delete EntryValueCopyBackupKind after changing SelectionDAG, but not before.

vedantk avatar Jun 03 '20 19:06 vedantk

Vedant, while I am trying to land the narrow fix, could you outline the more involved real fix of getting rid of the duplicate DBG_VALUEs for COPYs?

Especially the part about:

try to make up for any loss in location coverage by making generic improvements to LiveDebugValues.

I saw that you pinged me on https://reviews.llvm.org/D80684. Would removing EntryValueCopyBackupKind from EntryDebugValues be the only required fix?

Also, how could I test that the eventual fix doesn't loose location coverage? Do you have specific test cases in mind?

Sorry for the delayed response.

What I had in mind was deleting "LiveInMap" from SelectionDAGISel::runOnMachineFunction (and everything that uses it). I tried this on a lark before commenting on 2020-05-01 and found that this resulted in ~15 or so test failures during check-llvm. Some of these are tests that are being overly strict about what they match -- these can be fixed up by mechanically updating the check lines. But others represent real regressions in location coverage.

AIUI, it should be the job of LiveDebugValues::transferRegisterCopy to track when a copy introduces a backup location for a variable, /not/ the job of the instruction selector. So (and I'm hand-waving a bit here), if we can fix the tests that regress by making transferRegisterCopy more aggressive, that could be a nice, general improvement.

As for measuring changes in location coverage, you can compile a stage2 clang binary with vs. without your changes, and use llvm-dwarfdump --statistics to collect location coverage stats.

vedantk avatar Jun 03 '20 19:06 vedantk

Vedant, while I am trying to land the narrow fix, could you outline the more involved real fix of getting rid of the duplicate DBG_VALUEs for COPYs?

Especially the part about:

try to make up for any loss in location coverage by making generic improvements to LiveDebugValues.

I saw that you pinged me on https://reviews.llvm.org/D80684. Would removing EntryValueCopyBackupKind from EntryDebugValues be the only required fix?

Also, how could I test that the eventual fix doesn't loose location coverage? Do you have specific test cases in mind?

tbosch avatar May 29 '20 19:05 tbosch

Vedant, sorry, this took a bit, but here is the change: https://reviews.llvm.org/D80819

I just took your patch and added a test for it. No other tests seem to have been affected by the change.

Could you have a look? Thanks!

tbosch avatar May 29 '20 17:05 tbosch

@​Tobias Ah, I didn't mean to hijack this thread. If you're still able to drive this forward, I think that'd be great. (Unfortunately I'm not sure I have the bandwidth to do so.)

To summarize things a bit, I see at least two possible solutions, and I suspect folks would be fine with either:

  • A narrow fix in SelectionDAG to add in the missing width check (more or less the patch I attached, plus maybe fixing up a few tests).

  • A more involved / long-term fix where we remove the part of SelectionDAG that tries to place DBG_VALUEs after copies, adjust the ~15 or so tests that fail as a result, and then try to make up for any loss in location coverage by making generic improvements to LiveDebugValues.

vedantk avatar May 01 '20 18:05 vedantk

@​Vedant: Would you like to take over this bug, as you clearly know more about the required fix than I do? Alternatively, I could try to start with your patch and see which tests fail...

tbosch avatar Apr 30 '20 22:04 tbosch

Djordje wrote:

I remember we discussed about removing this when we were implementing the debug entry values support within LiveDebugValues pass. Only deleting that piece of code causes some tests failures and we did not take an in-depth analysis back then.

Ah yeah, I remember, at the time I reckoned the physreg argument location needed to be specified differently to the vreg location for some reason. I think the explanation above is probably closer to the truth,

Yes, I think the LiveDebugValues pass can handle the copies properly. Having that implemented, we can simplify the support for entry values within that pass. These extra DBG_VALUEs (that comes from the SelcetionDAG) before LiveDebugValues volatile the assumption "every dbg_value indicates new value within the MBB for the variable", so we needed to handle it as well.

Removing some of those workarounds would be great, they make up a large portion of LiveDebugValues.

jmorse avatar Apr 29 '20 18:04 jmorse

Sorry, I have missed this, because I have changed my email address.

@​Jeremy

[0] https://github.com/llvm/llvm-project/blob/446082b99f0bd9786d24edd3cd598dc4e5f64371/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp#L572

I remember we discussed about removing this when we were implementing the debug entry values support within LiveDebugValues pass. Only deleting that piece of code causes some tests failures and we did not take an in-depth analysis back then.

@​Vedant

If we can get away with deleting that whole chunk of SelectionDAG, and have a later pass do it right, that seems like the best outcome.

Yes, I think the LiveDebugValues pass can handle the copies properly. Having that implemented, we can simplify the support for entry values within that pass. These extra DBG_VALUEs (that comes from the SelcetionDAG) before LiveDebugValues volatile the assumption "every dbg_value indicates new value within the MBB for the variable", so we needed to handle it as well.

djtodoro avatar Apr 29 '20 05:04 djtodoro

Add the missing width check to SelectionDAG Thanks for finding this Jeremy! I've attached a patch which adds the missing width check to SelectionDAG. This seems to 'fix' the problem :)

Looking through the radar history from [2] (rdar://8412415), I see that the logic to add DBG_VALUEs after copies was added to SelectionDAG in an attempt to address an optimized code debugging regression. The kernel engineers who filed the radar (circa the iOS 4.1 release.. so a lot has changed since then!) didn't seem to think the change was particularly effective.

If we can get away with deleting that whole chunk of SelectionDAG, and have a later pass do it right, that seems like the best outcome.

vedantk avatar Apr 24 '20 23:04 vedantk

"but make the argument value available in the rest of the function in a vreg," if there was more than one block, of course.

jmorse avatar Apr 24 '20 15:04 jmorse

Hi Tobias, thanks for digging into this and all the reproducers!,

The X86DAGToDAGISel pass seems to be creating the DBG_VALUES. And it seems to produce too many COPY operations:

NB: I think (90% confidence) isel deliberately avoids optimising the number of COPYs created, and instead lets the register allocate handle that,

Vedant wrote:

What I'd love to understand is why that DEBUG_VALUE: fn1:z <- $edi gets created in the first place. That just seems wrong. If we can prevent that from happening, we don't need to have later passes clean things up ex post facto

It looks like those DBG_VALUEs, and in fact all the DBG_VALUEs of the argument value, originate from here [0]. dbg.value intrinsics for arguments get picked out of SelectionDAGBuilder in EmitFuncArgumentDbgValue, joined up with the ABIs argument location, and then inserted by [0]. The loop on line 614 is what's generating additional DBG_VALUEs for copies.

Exactly why this code path exists is a bit of a mystery to me; I would imagine it's something to do with past limitations on tracking variable locations. Without this code, SelectionDAG would attach the argument dbg.value to a CopyFromReg(rdi), but make the argument value available in the rest of the function in a vreg, via something like this pseudo-dag: t1 = CopyFromReg(rdi) t2, ch = CopyToReg(t1, %0) If %0 wasn't allocated to $rdi, the variable location would have to follow the copy to reach the rest of the function; and this might have been a serious problem in the past. The commit messages of [1, 2], which add large portions of the code at [0] appear to have that as a motivation.

Today, LiveDebugValues can /probably/ propagate the variable location through such a COPY, although there are various circumstances where it won't.

[0] https://github.com/llvm/llvm-project/blob/446082b99f0bd9786d24edd3cd598dc4e5f64371/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp#L572 [1] https://reviews.llvm.org/rG1b08572a665402ce57771507b3bcbe235ff1ba82 [2] https://reviews.llvm.org/rG99ff76212a0b38ceae924b3a739a8cc6a132624a

jmorse avatar Apr 24 '20 15:04 jmorse

Filed llvm/llvm-project#44998 regarding the lldb bugs.

tbosch avatar Apr 23 '20 22:04 tbosch

Ok sounds good.

Will look into the following then (probably in this order):

  1. Why X86DAGToDAGISel associates the 32bit DBG_VALUE with "z" (DBG_VALUE %1:gr32, $noreg, !"z", ...)
  2. Find a way to remove the DBG_VALUEs when the COPYs are folded (in RegisterCoalescer and VirtRegMap)
  3. Why lldb shows the wrong values for DWARF4

tbosch avatar Apr 22 '20 23:04 tbosch

What I'd love to understand is why that DEBUG_VALUE: fn1:z <- $edi gets created in > the first place. That just seems wrong. If we can prevent that from happening, we don't need to have later passes clean things up ex post facto

As far as I understand it, this DEBUG_VALUE is created like this:

Fn1 takes a long, and the result of the multiplication is stored in an int variable. So X86DAGToDAGISel emits a COPY that uses a register subindex for 32bits, which through the following passes (Simple Register Coalescing, Virtual Register Rewriter) gets inlined into the multiplication as $edi (see the IR dumps above).

So do you refer to the fact that X86DAGToDAGISel emits these extra COPYs? I.e.: DBG_VALUE $rdi, $noreg, !"z", ... %0:gr64 = COPY $rdi

DBG_VALUE %0:gr64, $noreg, !"z", ... %1:gr32 = COPY %0.sub_32bit:gr64

DBG_VALUE %1:gr32, $noreg, !"z", ...

Yes: to be clear, the COPY instructions are fine, but the DBG_VALUE emitted for the copy is bogus (DBG_VALUE %1:gr32, $noreg, !"z", ...).

It's not always true that "z" is equal to the lower 32-bits of "z".

There are probably good reasons for emitting a DBG_VALUE after each COPY; maybe this helps with location availability when the source register gets killed but the copied register stays alive. But I think whatever logic is inserting those DBG_VALUEs is missing a width check.

vedantk avatar Apr 22 '20 23:04 vedantk

Btw, if I change the simplified example to use long in all places, there is no problem either.

tbosch avatar Apr 22 '20 22:04 tbosch

What I'd love to understand is why that DEBUG_VALUE: fn1:z <- $edi gets created in > the first place. That just seems wrong. If we can prevent that from happening, we don't need to have later passes clean things up ex post facto

As far as I understand it, this DEBUG_VALUE is created like this:

Fn1 takes a long, and the result of the multiplication is stored in an int variable. So X86DAGToDAGISel emits a COPY that uses a register subindex for 32bits, which through the following passes (Simple Register Coalescing, Virtual Register Rewriter) gets inlined into the multiplication as $edi (see the IR dumps above).

So do you refer to the fact that X86DAGToDAGISel emits these extra COPYs? I.e.: DBG_VALUE $rdi, $noreg, !"z", ... %0:gr64 = COPY $rdi

DBG_VALUE %0:gr64, $noreg, !"z", ... %1:gr32 = COPY %0.sub_32bit:gr64

DBG_VALUE %1:gr32, $noreg, !"z", ... %2:gr32 = IMUL32rr %1:gr32(tied-def 0), %1:gr32, ...

tbosch avatar Apr 22 '20 22:04 tbosch