diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index ee9ed0d27c26..b2ff1e1dc472 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -900,6 +900,9 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
       eatToEndOfStatement();
   }
 
+  // Make sure we get proper DWARF even for empty files.
+  (void)enabledGenDwarfForAssembly();
+
   getTargetParser().onEndOfFile();
   printPendingErrors();
 
diff --git a/llvm/test/MC/AsmParser/debug-only-comments.s b/llvm/test/MC/AsmParser/debug-only-comments.s
new file mode 100644
index 000000000000..0330653498b4
--- /dev/null
+++ b/llvm/test/MC/AsmParser/debug-only-comments.s
@@ -0,0 +1,6 @@
+        # RUN: llvm-mc -triple i386-linux-gnu -g -dwarf-version 4 < %s | FileCheck %s
+        # RUN: llvm-mc -triple i386-linux-gnu -g -dwarf-version 5 < %s | FileCheck %s
+        # CHECK: .section .debug_info
+        # CHECK: .section .debug_info
+        # CHECK-NOT: .section
+        # CHECK: .ascii "<stdin>"