diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp index 4e9f254872e4ce..2516dff52efdfa 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(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( + 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(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 00000000000000..06fc51953abfc7 --- /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 -