[PATCH] D81131: [DebugInfo] Fix assertion for extern void type

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 3 22:29:59 PDT 2020


yonghong-song created this revision.
yonghong-song added a reviewer: dblaikie.
yonghong-song added a project: debug-info.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Commit d77ae1552fc2 <https://reviews.llvm.org/rGd77ae1552fc21a9f3877f3ed7e13d631f517c825> ("[DebugInfo] Support to emit debugInfo
for extern variables") added support 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.

The patch simply skipped the debuginfo if the extern variable has 
"void" type. This might be not be the right way to do it. Maybe
we should error out during parsing or sematic analysis stage.
Or maybe we should allow DIGlobalVariable with extern void type.
Your advice/suggestion for the next step is appreciated.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81131

Files:
  clang/lib/CodeGen/CodeGenModule.cpp


Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -4198,16 +4198,23 @@
 }
 
 void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
-  if (CGDebugInfo *DI = getModuleDebugInfo())
-    if (getCodeGenOpts().hasReducedDebugInfo()) {
-      QualType ASTTy = D->getType();
-      llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
-      llvm::PointerType *PTy =
-          llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
-      llvm::Constant *GV = GetOrCreateLLVMGlobal(D->getName(), PTy, D);
-      DI->EmitExternalVariable(
-          cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
-    }
+  if (CGDebugInfo *DI = getModuleDebugInfo()) {
+    if (!getCodeGenOpts().hasReducedDebugInfo())
+      return;
+
+    // The DIGlobalVariable must have a type, skip the generation
+    // if the underlying type is void without any qualifiers.
+    QualType ASTTy = D->getType();
+    if (!ASTTy.hasQualifiers() && ASTTy.getTypePtr()->isVoidType())
+      return;
+
+    llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
+    llvm::PointerType *PTy =
+        llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
+    llvm::Constant *GV = GetOrCreateLLVMGlobal(D->getName(), PTy, D);
+    DI->EmitExternalVariable(
+        cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
+  }
 }
 
 static bool isVarDeclStrongDefinition(const ASTContext &Context,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81131.268362.patch
Type: text/x-patch
Size: 1575 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200604/96447ef4/attachment.bin>


More information about the cfe-commits mailing list