[llvm] d7681d9 - [NVPTX] Avoid a crash when 'llc' is called with '-filetype=null'

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 22 05:49:52 PDT 2022


Author: Igor Kudrin
Date: 2022-03-22T16:46:47+04:00
New Revision: d7681d9f77e05fb6321757b933d2eadd943b069d

URL: https://github.com/llvm/llvm-project/commit/d7681d9f77e05fb6321757b933d2eadd943b069d
DIFF: https://github.com/llvm/llvm-project/commit/d7681d9f77e05fb6321757b933d2eadd943b069d.diff

LOG: [NVPTX] Avoid a crash when 'llc' is called with '-filetype=null'

For '-filetype=null', 'NVPTXTargetStreamer' is not created, so the
return value of 'OutStreamer->getTargetStreamer()' should be checked
before calling the methods.

Differential Revision: https://reviews.llvm.org/D122001

Added: 
    llvm/test/CodeGen/NVPTX/filetype-null.ll

Modified: 
    llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 4e9f254872e4c..2516dff52efdf 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -888,17 +888,18 @@ bool NVPTXAsmPrinter::doFinalization(Module &M) {
 
   clearAnnotationCache(&M);
 
-  // Close the last emitted section
-  if (HasDebugInfo) {
-    static_cast<NVPTXTargetStreamer *>(OutStreamer->getTargetStreamer())
-        ->closeLastSection();
-    // Emit empty .debug_loc section for better support of the empty files.
-    OutStreamer->emitRawText("\t.section\t.debug_loc\t{\t}");
-  }
+  if (auto *TS = static_cast<NVPTXTargetStreamer *>(
+          OutStreamer->getTargetStreamer())) {
+    // Close the last emitted section
+    if (HasDebugInfo) {
+      TS->closeLastSection();
+      // Emit empty .debug_loc section for better support of the empty files.
+      OutStreamer->emitRawText("\t.section\t.debug_loc\t{\t}");
+    }
 
-  // Output last DWARF .file directives, if any.
-  static_cast<NVPTXTargetStreamer *>(OutStreamer->getTargetStreamer())
-      ->outputDwarfFileDirectives();
+    // Output last DWARF .file directives, if any.
+    TS->outputDwarfFileDirectives();
+  }
 
   return ret;
 

diff  --git a/llvm/test/CodeGen/NVPTX/filetype-null.ll b/llvm/test/CodeGen/NVPTX/filetype-null.ll
new file mode 100644
index 0000000000000..06fc51953abfc
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/filetype-null.ll
@@ -0,0 +1,4 @@
+; Check that 'llc' does not crash if '-filetype=null' is used.
+
+; RUN: llc %s -filetype=null -march=nvptx -o -
+; RUN: llc %s -filetype=null -march=nvptx64 -o -


        


More information about the llvm-commits mailing list