This is an archive of the discontinued LLVM Phabricator instance.

[CodeView] Add support for local S_CONSTANT records
ClosedPublic

Authored by thieta on Nov 30 2022, 3:00 AM.

Details

Summary

CodeView doesn't have the ability to represent variables
in other ways than as in registers or memory values, but
LLVM very often transforms simple values into constants,
consider this program:

int f () { int i = 123; return i; }

LLVM will transform i into a constant value and just
leave behind a llvm.dbg.value, this can't be represented
as a S_LOCAL record in CodeView. But we can represent it
as a S_CONSTANT record.

This patch checks if the location of a debug value is null,
then we will insert a S_CONSTANT record instead of a S_LOCAL
value with the flag "OptimizedAway".

In lld we then output the S_CONSTANT in the right scope, before
they where always inserted in the global stream, now we check
the scope before inserting it.

This has shown to improve debugging for our developers
internally.

Fixes to llvm/llvm-project#55958

Diff Detail

Event Timeline

thieta created this revision.Nov 30 2022, 3:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 30 2022, 3:00 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
thieta requested review of this revision.Nov 30 2022, 3:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 30 2022, 3:00 AM

Thanks, that's a nice improvement! Out of curiosity does this optimization happen at -O0 too? (when using clang-cl)
Does MSVC generate S_CONSTANT as well for your test-case?

lld/test/COFF/Inputs/pdb-local-constants.s
192

Since we have -gno-codeview-command-line can you leave out this part? It adds unnecessary noise which is not required for this test.

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
1347

s/becasue/because/

1349

'since' repeated twice, not sure if this could be rephrased a bit?

1355

nit: could also be Val.ConstantValue = APSInt(APInt(64, Op.getImm()), false);

2798

unwanted indent?

2800–2801

I would add the extra braces at the end of the for line, to enclose the scope below.

2805

Could this comment fit on the line above? You can also write the entire comment on one line, and then git clang-format reformat it.

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h
107

I think the way to go forward is to use std::optional.

thieta added a comment.Dec 1 2022, 4:18 AM

Thanks, that's a nice improvement! Out of curiosity does this optimization happen at -O0 too? (when using clang-cl)
Does MSVC generate S_CONSTANT as well for your test-case?

In general -O0 doesn't have this problem. I have seen it happen, but unsure what triggers it.

No MSVC doesn't generate S_CONSTANTS for any of my cases. They only use it when you actually do a constant, in most of the cases MSVC doesn't output anything or outputs a S_LOCAL with the optimized away flag set. I tried with /Zo as well and couldn't get it do anything differently.

I even tried to debug several programs with msvc and their default flags, and with /O2 they always had same debugging info as clang-cl.
They do behave better with /O2, mostly because clang really don't want to emit all declarations if they are global and can const propagate some variables.

thieta marked 8 inline comments as done.Dec 1 2022, 4:46 AM
thieta updated this revision to Diff 479246.Dec 1 2022, 4:46 AM

Handled some review feedback

aganea accepted this revision.Dec 2 2022, 1:47 PM
aganea added a subscriber: akhuang.

LGTM, but wait until next week before landing, in case others would like to comment. + @akhuang

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
1346

Bracket on wrong line, please apply git clang-format.

This revision is now accepted and ready to land.Dec 2 2022, 1:47 PM

Looks good to me!

This revision was landed with ongoing or failed builds.Dec 6 2022, 1:34 AM
This revision was automatically updated to reflect the committed changes.