This is an archive of the discontinued LLVM Phabricator instance.

[DebugInfo] Fix assertion for extern void type
ClosedPublic

Authored by yonghong-song on Jun 3 2020, 10:06 PM.

Details

Summary

Commit d77ae1552fc2 ("[DebugInfo] Support to emit debugInfo
for extern variables") added support to emit debuginfo
for extern variables. Currently, only BPF target enables to
emit debuginfo for extern variables.

But if the extern variable has "void" type, the compilation will
fail.

-bash-4.4$ cat t.c 
extern void bla;
void *test() {
  void *x = &bla;
  return x;
}
-bash-4.4$ clang -target bpf -g -O2 -S t.c 
missing global variable type
!1 = distinct !DIGlobalVariable(name: "bla", scope: !2, file: !3, line: 1,
                                isLocal: false, isDefinition: false)
... 
fatal error: error in backend: Broken module found, compilation aborted!
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace,
    preprocessed source, and associated run script.
Stack dump:
...

The IR requires a DIGlobalVariable must have a valid type and the
"void" type does not generate any type, hence the above fatal error.

Note that if the extern variable is defined as "const void", the
compilation will succeed.

-bash-4.4$ cat t.c
extern const void bla;
const void *test() {
  const void *x = &bla;
  return x;
}
-bash-4.4$ clang -target bpf -g -O2 -S t.c
-bash-4.4$ cat t.ll
...
!1 = distinct !DIGlobalVariable(name: "bla", scope: !2, file: !3, line: 1,
                                type: !6, isLocal: false, isDefinition: false)
!6 = !DIDerivedType(tag: DW_TAG_const_type, baseType: null)
...

It might be the expected behavior not to generate debuginfo for
extern "void" variables. But the fatal error (with assert on) is not
nice.

Since currently, "const void extern_var" is supported by the
debug info, it is natural that "void extern_var" should also
be supported. This patch disabled assertion of "void extern_var"
in IR verifier and add proper guarding when emiting potential
null debug info type to dwarf types.

Diff Detail

Event Timeline

yonghong-song created this revision.Jun 3 2020, 10:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 3 2020, 10:06 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
anakryiko accepted this revision.Jun 3 2020, 10:54 PM

For my purposes, having extern var BTF only for extern const void is just fine. Not crashing Clang for extern void is great as well :)

This revision is now accepted and ready to land.Jun 3 2020, 10:54 PM
ast accepted this revision.Jun 4 2020, 10:37 AM

Patches without tests shouldn't be approved - and at least the usual/rough metric I use for patch approval is (most often - unless I'm reviewing the work of the code owner in any area who wants a second opinion, etc) - I approve it if I'd be willing to commit a similar patch without approval (with post-commit review - ie: I'm fairly sure this is the right direction and folks will agree with me, or minor changes can be handled in post-commit iteration). Otherwise "approval" gets muddied as to whether it means "you can now commit this" versus "I think this is good but you should wait for someone more authoritative to say 'yes' before it's committed" (usually folks on the project call out the latter explicitly - sometimes using Phab's "approve" sometimes not).

In any case: I think if we're going to support "const void" we should support "void" too & fix the verifier to allow this & make sure LLVM produces the correct debug info for it (which is probably a DW_TAG_variable without a DW_AT_type attribute (same as a function with a void return type has no DW_AT_type attribute) - and check that GCC does the same thing

dblaikie requested changes to this revision.Jun 4 2020, 10:51 AM
This revision now requires changes to proceed.Jun 4 2020, 10:51 AM

Thanks, @dblaikie

I think if we're going to support "const void" we should support "void" too & fix the verifier to allow this & make sure LLVM produces the correct debug info for it (which is probably a DW_TAG_variable without a DW_AT_type attribute (same as a function with a void return type has no DW_AT_type attribute) - and check that GCC does the same thing.

Your suggestion makes sense, I will try to fix the fatal error according to what you suggested.

yonghong-song edited the summary of this revision. (Show Details)

instead of skipping generating debuginfo for "extern void var_name", do generate debuginfo and skip emitting dwarf types for it.
cc @dblaikie

Herald added a project: Restricted Project. · View Herald TranscriptJun 5 2020, 11:00 PM
yonghong-song edited the summary of this revision. (Show Details)Jun 8 2020, 10:47 AM
dblaikie accepted this revision.Jun 8 2020, 11:20 AM

Looks good - thanks!

This revision is now accepted and ready to land.Jun 8 2020, 11:20 AM
This revision was automatically updated to reflect the committed changes.