Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line-level debug info #4247

Merged
merged 2 commits into from
Aug 26, 2024

Conversation

dwblaikie
Copy link
Contributor

Seems to work with lldb ( https://pastebin.com/igKkNECm ), though gdb has /some/ trouble with the paths (they aren't complete - just using the filename directly, not providing the working directory - might be some quick hacks that can help there).

common/vlog.h Outdated Show resolved Hide resolved
Seems to work with lldb ( https://pastebin.com/igKkNECm ), though gdb
has /some/ trouble with the paths (they aren't complete - just using the
filename directly, not providing the working directory - might be some
quick hacks that can help there).
Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

toolchain/lower/handle.cpp Outdated Show resolved Hide resolved
Comment on lines +101 to +103
builder_.SetCurrentDebugLocation(
llvm::DILocation::get(builder_.getContext(), loc.line_number,
loc.column_number, di_subprogram_));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, this is assuming the locations are valid and in the current file. Should that be checked? For example, loc.line_number <= 0 or loc.column_number <= 0 would indicate unknown. You can see some of this in FormatSnippet

Doing something on the file could just be a TODO.

Not sure how much we test multi-file lowering today, but might still be worth adding some resilience and/or todos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm - 0 is an OK value for the debug info (it means "we can't assign this instruction to any particular source line") and I think we use 0 for invalid column number too. Is it just 0 for invalid, or are really <= 0? (carrying some extra information about kinds of invalidity or something in this?)

What causes invalid values but successful code generation? (happy to add some test coverage)

The multi-file issue is due to the unimpleented/trivially implemented context callback passed to the ConvertLoc call?

Copy link
Contributor

@jonmeow jonmeow Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm - 0 is an OK value for the debug info (it means "we can't assign this instruction to any particular source line") and I think we use 0 for invalid column number too. Is it just 0 for invalid, or are really <= 0? (carrying some extra information about kinds of invalidity or something in this?)

What causes invalid values but successful code generation? (happy to add some test coverage)

-1 is used for "unknown", it's the default value for both (https://github.com/carbon-language/carbon-lang/blob/trunk/toolchain/diagnostics/diagnostic.h#L61-L64).

The default value is sometimes used, for example here:
https://github.com/carbon-language/carbon-lang/blob/trunk/toolchain/parse/tree_node_diagnostic_converter.h#L48-L51

I know this comes up in check:
https://github.com/search?q=repo%3Acarbon-language%2Fcarbon-lang+path%3Atestdata%2F+%22.carbon%3A+%22&type=code

I'm not sure what test coverage would look like. However, it might still be good to handle it since it's allowed by the API call.

The multi-file issue is due to the unimpleented/trivially implemented context callback passed to the ConvertLoc call?

No, this is just what the location converter is up to. A location can be either a node_id or a import_ir_id. In the latter case, it's referencing an imported instruction. Again, not sure it'll happen right now. OTOH, probably will start coming up as complexity increases, e.g. for template expansions or maybe inlining code.

Not sure why you're looking at the context though... To be sure, the DiagnosticLoc has a field "filename" for this. You'd need to ensure that everything you're using is a Parse::Node in order to be guaranteed it's in the file. e.g., something like:

auto loc_id = insts().GetLocId(inst_id);
if (loc_id.is_valid() && loc_id.is_node_id()) {
   // This is in the current file.
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, an option is to just leave a guardrail, e.g.:

CARBON_CHECK(loc.filename == sem_ir.filename() && loc.line_number >= 1 && loc.column_number >= 1) << "We currently only support lowering instructions in the current file";

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, you could also look at changing line_number to be 0-defaulted, to help DebugInfo. I think I'd chosen -1 in order to avoid accidents with 0 versus 1 as the first correct value: although comments don't specify, I think 1 is where it starts, but a bug could lead to 0 and it'd stick out a little more.

But if you change that, do add comments specifying why the values are chosen that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, line: 4294967295 does appear in the .carbon testdata files as a result of this change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out, I hadn't been looking the right way to notice that.

@jonmeow
Copy link
Contributor

jonmeow commented Aug 26, 2024

I'm going to go ahead and merge since this is basically good, and send a separate PR for the safety check.

@jonmeow jonmeow added this pull request to the merge queue Aug 26, 2024
Merged via the queue into carbon-language:trunk with commit 0ae2a39 Aug 26, 2024
8 checks passed
@dwblaikie dwblaikie deleted the debug_info_source_locations branch August 26, 2024 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants