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

Improve optimized debug info for escaped variables that live in memory

Open rnk opened this issue 9 years ago • 40 comments

Bugzilla Link 34136
Version trunk
OS Windows NT
CC @adrian-prantl,@chandlerc,@chrisjbris,@dwblaikie,@dpxf,@echristo,@zmodem,@hfinkel,@jmorse,@jimingham,@jrmuizel,@jdm,@lhames,@jasonmolenda,@nico,@OCHyams,@pogo59

Extended Description

Consider:

int getint(void);
void alias(char *);
void crash(void);
void f() {
  int x = getint();
  int y = getint();
  int z = getint();
  alias((char*)&x);
  alias((char*)&y);
  alias((char*)&z);
  crash();
}

InstCombine runs LowerDbgDeclares on this code, and we end up with very poor debug info. We basically say that x, y, and z live in EAX after each call to getint, and they are immediately clobbered, never to be available again.

Those casts to char* also prevent the transform from adding more dbg.values that would extend the range.

This was reduced from http://crbug.com/753934

rnk avatar Aug 09 '17 22:08 rnk

mentioned in issue https://github.com/llvm/llvm-project/issues/47290

OCHyams avatar Nov 27 '21 03:11 OCHyams

I have just reported an issue which seems relevant to this thread: llvm/llvm-bugzilla-archive#48719 .

The bug report shows how we can run into incorrect debug info for an escaped variable when promoting or partially promoting it introduces PHI instructions, combined with SimplifyCFG squashing the PHI block and its preds together.

OCHyams avatar Jan 11 '21 18:01 OCHyams

We intend to start addressing this issue, is anyone currently working on this, are there any objections to our now implementing improvements?

Sounds great. I'm not aware of anyone working on it from our side.

zmodem avatar Jul 20 '20 13:07 zmodem

Some experimental improvemnts to lowerDbgDeclare brought this issue to our interest recently, initially looking at issue llvm/llvm-bugzilla-archive#45667 .

Messges #​25 & and #​26 on this Bug (34136) outline a decent plan to begin improving debuginfo for the escaped variables.

We intend to start addressing this issue, is anyone currently working on this, are there any objections to our now implementing improvements?

chrisjbris avatar Jul 20 '20 13:07 chrisjbris

Filtering iterators helps. We still have cases of -g affecting codegen but all the really egregious cases have been taken care of, at this point.

Eliminating dbg.* instructions is probably a topic for a BoF or something. I'll just point out that when I first looked at dbg.declare, it seemed that it added exactly one item of info to what alloca already had, and that really didn't seem worth having a whole separate instruction. Another kind of metadata on alloca should have worked just as well.

pogo59 avatar Aug 21 '17 20:08 pogo59

P.S. Using instructions to convey debug info is a poor design, period. It causes all kinds of other problems because people writing passes rarely think about the effect on debug info, and we've fixed a bunch of cases where instructions/uses/etc were counted inappropriately. I'd rather be thinking about a design that eliminates all dbg.* instructions.

I agree, but fundamentally we need to represent some placeholder in the instruction stream. I think without radically changing the MI and IR basic block representations, that means we represent variable tracking info with elements in the linked list, regardless of how we name them. The best we can do is probably to just add filtering instruction iterators that skip dbg instructions.

The use-list issues seem like they have been fixed since 2014, when we added ValueAsMetadata / MetadataAsValue, right? I guess this might still be an issue in MIR.

rnk avatar Aug 21 '17 20:08 rnk

Sorry for being late to the party, I've been away a couple of weeks.

There's always a tension between providing "truthful" info and "potentially useful" info. IMO the way to build users' trust in the value of debug info (especially optimized) is to provide truthful info. Half-truths make the debugger into an unreliable tool. (Personally I use debuggers only as a last resort.)

P.S. Using instructions to convey debug info is a poor design, period. It causes all kinds of other problems because people writing passes rarely think about the effect on debug info, and we've fixed a bunch of cases where instructions/uses/etc were counted inappropriately. I'd rather be thinking about a design that eliminates all dbg.* instructions.

pogo59 avatar Aug 21 '17 20:08 pogo59

I'd rather not introduce temporary user-facing options. Could this just be a -Xclang (-cc1) option?

I'd prefer it if users didn't have to know about -Xclang, but I don't care that much.

Since the point of the option is to enable incremental development of the feature until it is ready to become the default, users ideally will never have to interact with the option, because they won't want to turn it on until it's ready for prime time. Driver options are expensive compared to cc1 options, because we typically have to support them indefinitely, so making this (temporary!) option a cc1 option seems right to me.

adrian-prantl avatar Aug 18 '17 16:08 adrian-prantl

  • Add support for memory locations in LiveDebugValues

Yep, I realized I forgot to post that while discussing this in person with Zach.

We also identified two nice-to-have features (these are things that currently don't work either):

  • insert a dbg.value(undef) at the point where a lifetime.end marker is generated (or directly in the stack coloring pass)

It's true, stack coloring needs to add DBG_VALUE MIR instructions on lifetime end. I haven't actually been able to craft a C test case that forces confusing stack colorings to occur, so I don't think it's very high priority. (I tried hard! :)

  • let clang generate allocas for indirectly passed function arguments (e.g., sret)

This works, but I filed llvm/llvm-bugzilla-archive#34227 as an alternative to that.

By describing allocas with dbg.value we have the opportunity to get a more precise start location for variable liveness, but we need to be careful not to regress the DWARF representation at -O0 to a location list, since this would prevent the debugger from letting the user assign new values to the variable (implicit vs. explicit location description). We could represent stack-allocated variables at -O0 with frame index location + a DW_AT_start_scope attribute to achieve this.

Indeed, preserving the -O0 experience is important.

So you want to give users the option to try it?

Yes. We don't have a lot of good tools for evaluating debug info quality beyond deploying changes to users and giving them flags to flip. =/ Adrian said he's trying to upstream his tool to calculate DW_AT_location coverage, but it's still an approximation of quality.

I'd rather not introduce temporary user-facing options. Could this just be a -Xclang (-cc1) option?

I'd prefer it if users didn't have to know about -Xclang, but I don't care that much.

rnk avatar Aug 18 '17 01:08 rnk

instead of dbg.declare. Maybe call it -gdbg-value? Document the new scheme in SourceLevelDebugging.rst.

So you want to give users the option to try it?

I'd rather not introduce temporary user-facing options. Could this just be a -Xclang (-cc1) option?

hfinkel avatar Aug 17 '17 22:08 hfinkel

I think we also had on the list:

  • Add support for memory locations in LiveDebugValues

We also identified two nice-to-have features (these are things that currently don't work either):

  • insert a dbg.value(undef) at the point where a lifetime.end marker is generated (or directly in the stack coloring pass)

  • let clang generate allocas for indirectly passed function arguments (e.g., sret)

We also came to the conclusion that — given the input comes from a well-behaved frontend, and we shall document what exactly that means* — it should never be necessary to insert dbg.values for load or call instructions.

*) A well-behaved frontend produces an alloca for a variable and generates a store to the alloca each time the variable is modified. It further generates a dbg.value describing the variable in the alloca as the one true location for the variable. This contract is what allows us to pay attention to dead store elimination (cf. Reid's second point).

By describing allocas with dbg.value we have the opportunity to get a more precise start location for variable liveness, but we need to be careful not to regress the DWARF representation at -O0 to a location list, since this would prevent the debugger from letting the user assign new values to the variable (implicit vs. explicit location description). We could represent stack-allocated variables at -O0 with frame index location + a DW_AT_start_scope attribute to achieve this.

adrian-prantl avatar Aug 17 '17 22:08 adrian-prantl

Adrian and I sat down and worked out a long term plan to improve things in this area without knowingly introducing any inaccuracies in variable info. For a long time, we've discussed unifying dbg.declare and dbg.value. We can't mix dbg.declare and dbg.value, so to fix this bug, we should standardize everything on dbg.value, and then we don't need instcombine or any other pass to lossily convert dbg.declare to dbg.value. The steps are:

  • Add a flag to clang to always emit dbg.value+DW_OP_deref for local variables instead of dbg.declare. Maybe call it -gdbg-value? Document the new scheme in SourceLevelDebugging.rst.

  • Teach all passes that eliminate dead stores (primarily DSE & instcombine) to insert dbg.value calls when deleting stores to allocas with a dominating dbg.value+deref. This logic would be factored into a shared utility, like salvageDebugInfo. One subtlety is that we need to insert dbg.value+deref calls at the store that kills the store being eliminated, indicating that the value once again lives in memory.

  • Test the flag, flip the default when it's ready, remove the flag in the next release.

In the long run, this will make it so we start printing variable locations as assembly comments at -O0, which is a nice side benefit.

rnk avatar Aug 17 '17 22:08 rnk

Keno wrote an alternative proposal, essentially trying to improve on the inference that instcombine does today: https://gist.github.com/Keno/480b8057df1b7c63c321

I'm proposing that we remove instcombine's conversion of dbg.declare and push that responsibility onto the passes that delete stores.

rnk avatar Aug 16 '17 01:08 rnk

Adrian: Where're you at, philosophically, with all this. Are you of the opinion that no false positive regression is acceptable? Or are some acceptable (with the acknowledgement that they are bugs that should be fixed and should be identified, tracked, have a likely path to resolution, etc) to reduce false negatives ("no location" when there's really an obvious, unambiguous, unavoidable location that should be reported in the DWARF)?

As I said earlier in the thread I strongly believe that we should not knowingly introduce new false positives (unless there is a way to mark them in the debug info as not trustworthy and debuggers will actually surface that to the user). If the user can't trust some of the variable values, every value reported by the debugger becomes not trustworthy.

I also think that this discussion is misguided, because I am convinced that there is a lot of lower-hanging fruit to pick that does not involve watering down the debug info with not-to-be-trusted information. We have lots of documented PRs of optimizations dropping dbg.values that can be fixed without introducing any uncertainty and I think we should focus our attention there.

I am also talking with Reid to work out a plan to fix the issue described in this PR without introducing new false positives — this will need a little more design but it is worth taking the time to solve this correctly. We have a lot of the necessary components already in place.

adrian-prantl avatar Aug 15 '17 20:08 adrian-prantl

Given the different expectations, would adding this behavior behind a (default-off) flag be a short-term option? Then we can improve optimized debug info asynchronously and see if it's possible to get it to be good enough without having to sometimes show invalid information.

I am real uncomfortable with this being an option on the producer of debug info (clang). Now the person who builds a program is deciding how the debugger behaves -- making an assumption about what the debug developer is going to see. The person debugging a binary may have no idea how the program was compiled. The person who is compiling the program may not understand the meaning of the clang flag ("-fpossibly-wrong-but-maybe-really-helpful-debug-info? Yes please!") themselves.

We could make it CodeView-only and not touch the DWARF bits. That should address lldb concerns.

I agree with dblaikie that having two codepaths here long-term seems undesirable, but coming up with a grand unified scheme is going to take time, and ideally we wouldn't have to block switching chrome/win to clang on this work.

nico avatar Aug 15 '17 18:08 nico

Given the different expectations, would adding this behavior behind a (default-off) flag be a short-term option? Then we can improve optimized debug info asynchronously and see if it's possible to get it to be good enough without having to sometimes show invalid information.

I am real uncomfortable with this being an option on the producer of debug info (clang). Now the person who builds a program is deciding how the debugger behaves -- making an assumption about what the debug developer is going to see. The person debugging a binary may have no idea how the program was compiled. The person who is compiling the program may not understand the meaning of the clang flag ("-fpossibly-wrong-but-maybe-really-helpful-debug-info? Yes please!") themselves.

If we want to emit possibly-correct location information in parallel to known-correct location information in the DWARF in a separate list, and add an option to the consumer (lldb) to use possibly-correct location information (with default-off), I'd be OK with that approach. Then the person enabling the use of this in lldb is responsible for their own results.

I don't think MSVC's behavior is a feature to be emulated, though. We've had many cases like this over the years, and in lldb we consider showing incorrect values to the user as our worst class of bugs.

jasonmolenda avatar Aug 15 '17 17:08 jasonmolenda

One thing where this is hurting us a lot in chrome/win land at the moment is minidumps. Minidumps aren't really a thing on non-Windows, which might be why expectations here are so different.

On Windows, when chrome (or what have you) crashes, you can download a minidump from the crash server, load that into your debugger, and then use that to inspect the state of variables. Users are aware that they look at optimized debug info. Someone told me "Misleading information is mitigated by the fact you can ask the debugger where's it's getting its information (dv /v) and if you see stuff in a register, then you know to start distrusting the values."

Why? I thought that minidumps have registers and stack, but lack other memory contents.

With clang, when users ask for the values of things while debugging minidumps, they currently almost always get "no answer" instead of "likely answer is X". This is very different from how things used to work for them, and this makes people unhappy too.

Heh. So we've trained users to expect broken behavior and now they want it back ;) [this is unfortunately common.]

Given the different expectations, would adding this behavior behind a (default-off) flag be a short-term option? Then we can improve optimized debug info asynchronously and see if it's possible to get it to be good enough without having to sometimes show invalid information.

hfinkel avatar Aug 15 '17 17:08 hfinkel

One thing where this is hurting us a lot in chrome/win land at the moment is minidumps. Minidumps aren't really a thing on non-Windows, which might be why expectations here are so different.

On Windows, when chrome (or what have you) crashes, you can download a minidump from the crash server, load that into your debugger, and then use that to inspect the state of variables. Users are aware that they look at optimized debug info. Someone told me "Misleading information is mitigated by the fact you can ask the debugger where's it's getting its information (dv /v) and if you see stuff in a register, then you know to start distrusting the values."

Interestingly it seems like that's the opposite of the likely erroneous information that would happen in the direction discussed here.

Here the issue is that LLVM isn't describing the memory location backing a variable once it copies the value into a register. By adding the stack memory location the likely mistakes are that the value may be stale because a dead store has been eliminated and the value was /only/ in that register (or nowhere at all).

With clang, when users ask for the values of things while debugging minidumps, they currently almost always get "no answer" instead of "likely answer is X". This is very different from how things used to work for them, and this makes people unhappy too.

nod It's clearly a regression for MSVC users switching to Clang, no doubt. And it's clear LLVM can do better - yeah, when "f(&x)" is called, Clang should be describing the location of 'x' without any doubt.

Though I wonder how many false positives ("the value of X is Y" when it's really Z) MSVC has compared to what the proposed direction/fixes have in Clang. Might well be that the users also don't expect as many false positives as this would create in Clang.

Given the different expectations, would adding this behavior behind a (default-off) flag be a short-term option? Then we can improve optimized debug info asynchronously and see if it's possible to get it to be good enough without having to sometimes show invalid information.

Maybe - though the divergence does make me a touch uncomfortable, it does amount to sort of an A/B test with different implementations and users.

I'm not sure exactly where the tradeoffs are of the proposed fixes & what the path to addressing any gaps would be.

Adrian: Where're you at, philosophically, with all this. Are you of the opinion that no false positive regression is acceptable? Or are some acceptable (with the acknowledgement that they are bugs that should be fixed and should be identified, tracked, have a likely path to resolution, etc) to reduce false negatives ("no location" when there's really an obvious, unambiguous, unavoidable location that should be reported in the DWARF)?

If you feel like no false positive regression is acceptable, then yeah - I guess forking the default here with a flag might be the path forward - then as the gaps in the analysis are filled (tracking when a value moves back into memory), eventually the flag can be removed/ignored as the only thing it would be enabling would be the false positives - reintroducing the "unknown values" when it's truly unknown.

dwblaikie avatar Aug 15 '17 17:08 dwblaikie

One thing where this is hurting us a lot in chrome/win land at the moment is minidumps. Minidumps aren't really a thing on non-Windows, which might be why expectations here are so different.

On Windows, when chrome (or what have you) crashes, you can download a minidump from the crash server, load that into your debugger, and then use that to inspect the state of variables. Users are aware that they look at optimized debug info. Someone told me "Misleading information is mitigated by the fact you can ask the debugger where's it's getting its information (dv /v) and if you see stuff in a register, then you know to start distrusting the values."

With clang, when users ask for the values of things while debugging minidumps, they currently almost always get "no answer" instead of "likely answer is X". This is very different from how things used to work for them, and this makes people unhappy too.

Given the different expectations, would adding this behavior behind a (default-off) flag be a short-term option? Then we can improve optimized debug info asynchronously and see if it's possible to get it to be good enough without having to sometimes show invalid information.

nico avatar Aug 15 '17 17:08 nico

To throw two cents in, from the debugger's perspective it's always better to say "I don't know" rather than "this is the value" and give the wrong value. The former is incredibly annoying to anyone working on optimized code -- I work with several groups that have to always build optimized and debugging that code is way harder than it should be today, I fully understand where the pain is coming from. But thinking about the failure modes makes it clear what the right answer is.

If I say "I cannot give you the value of this variable", the dev will either find the value in another stack frame, or they'll resort to reading assembly, find where it is actually still live, and get it. They will curse the debugger for making their lives so hard (I'd prefer they curse the compiler, but that's not what they do, alas.)

If I hand out a value that is wrong, the developer can waste DAYS of time working on the assumption that this code somehow got this impossible value. I've gotten those bug reports -- people who, literally, wasted days because the debugger gave out a value that was not correct. It's so much worse of a failure mode, we've always considered this unacceptable when we've encountered these situations in lldb. The more you work with users of the debugger and their bug reports, the more you realize the importance of getting this right.

The debugger must be conservative in these situations, as frustrating as that is. The debug info must be conservative in these situations. Or if the compiler wants to emit variable locations that are possibly correct, add a different tag in the DWARF for it and debuggers like lldb will be free to ignore it.

Any debug info that ckauns a variable is accessible at a location, and that location does not have the variable value, is buggy debug info.

jasonmolenda avatar Aug 14 '17 21:08 jasonmolenda