diff mlir/lib/Target/LLVMIR/DebugTranslation.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children c4bab56944e8
line wrap: on
line diff
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp	Mon May 25 11:55:54 2020 +0900
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp	Tue Jun 08 06:07:14 2021 +0900
@@ -32,7 +32,7 @@
   if (!module->walk(interruptIfValidLocation).wasInterrupted())
     return;
 
-  // TODO(riverriddle) Several parts of this are incorrect. Different source
+  // TODO: Several parts of this are incorrect. Different source
   // languages may interpret different parts of the debug information
   // differently. Frontends will also want to pipe in various information, like
   // flags. This is fine for now as we only emit line-table information and not
@@ -48,6 +48,17 @@
   if (!llvmModule.getModuleFlag(debugVersionKey))
     llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
                              llvm::DEBUG_METADATA_VERSION);
+
+  if (auto targetTripleAttr =
+          module->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) {
+    auto targetTriple =
+        llvm::Triple(targetTripleAttr.cast<StringAttr>().getValue());
+    if (targetTriple.isKnownWindowsMSVCEnvironment()) {
+      // Dwarf debugging files will be generated by default, unless "CodeView"
+      // is set explicitly. Windows/MSVC should use CodeView instead.
+      llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1);
+    }
+  }
 }
 
 /// Finalize the translation of debug information.
@@ -71,11 +82,24 @@
   if (!compileUnit || !func.walk(interruptIfValidLocation).wasInterrupted())
     return;
 
+  // If we are to create debug info for the function, we need to ensure that all
+  // inlinable calls in it are with debug info, otherwise the LLVM verifier will
+  // complain. For now, be more restricted and treat all calls as inlinable.
+  const bool hasCallWithoutDebugInfo =
+      func.walk([](LLVM::CallOp call) {
+            return call.getLoc().isa<UnknownLoc>() ? WalkResult::interrupt()
+                                                   : WalkResult::advance();
+          })
+          .wasInterrupted();
+  if (hasCallWithoutDebugInfo)
+    return;
+
   FileLineColLoc fileLoc = extractFileLoc(func.getLoc());
-  auto *file = translateFile(fileLoc ? fileLoc.getFilename() : "<unknown>");
+  auto *file =
+      translateFile(fileLoc ? fileLoc.getFilename().strref() : "<unknown>");
   unsigned line = fileLoc ? fileLoc.getLine() : 0;
 
-  // TODO(riverriddle) This is the bare essentials for now. We will likely end
+  // TODO: This is the bare essentials for now. We will likely end
   // up with wrapper metadata around LLVMs metadata in the future, so this
   // doesn't need to be smart until then.
   llvm::DISubroutineType *type =
@@ -115,26 +139,19 @@
     return existingIt->second;
 
   const llvm::DILocation *llvmLoc = nullptr;
-  switch (loc->getKind()) {
-  case StandardAttributes::CallSiteLocation: {
-    auto callLoc = loc.dyn_cast<CallSiteLoc>();
-
+  if (auto callLoc = loc.dyn_cast<CallSiteLoc>()) {
     // For callsites, the caller is fed as the inlinedAt for the callee.
     const auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
     llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc);
-    break;
-  }
-  case StandardAttributes::FileLineColLocation: {
-    auto fileLoc = loc.dyn_cast<FileLineColLoc>();
+
+  } else if (auto fileLoc = loc.dyn_cast<FileLineColLoc>()) {
     auto *file = translateFile(fileLoc.getFilename());
     auto *fileScope = builder.createLexicalBlockFile(scope, file);
     llvmLoc = llvm::DILocation::get(llvmCtx, fileLoc.getLine(),
                                     fileLoc.getColumn(), fileScope,
                                     const_cast<llvm::DILocation *>(inlinedAt));
-    break;
-  }
-  case StandardAttributes::FusedLocation: {
-    auto fusedLoc = loc.dyn_cast<FusedLoc>();
+
+  } else if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
     ArrayRef<Location> locations = fusedLoc.getLocations();
 
     // For fused locations, merge each of the nodes.
@@ -143,18 +160,17 @@
       llvmLoc = llvm::DILocation::getMergedLocation(
           llvmLoc, translateLoc(locIt, scope, inlinedAt));
     }
-    break;
-  }
-  case StandardAttributes::NameLocation:
+
+  } else if (auto nameLoc = loc.dyn_cast<NameLoc>()) {
     llvmLoc = translateLoc(loc.cast<NameLoc>().getChildLoc(), scope, inlinedAt);
-    break;
-  case StandardAttributes::OpaqueLocation:
+
+  } else if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>()) {
     llvmLoc = translateLoc(loc.cast<OpaqueLoc>().getFallbackLocation(), scope,
                            inlinedAt);
-    break;
-  default:
+  } else {
     llvm_unreachable("unknown location kind");
   }
+
   locationToLoc.try_emplace(std::make_pair(loc, scope), llvmLoc);
   return llvmLoc;
 }