From 5b16947343cbf80e265cb352734cd6c742a8d909 Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Mon, 16 Jun 2025 20:58:56 +0100 Subject: [PATCH 1/2] [AsmPrinter] Fix crash when remarks section is unsupported Emit a warning instead of segfault-ing when the current object file format does not have support for emitting a remarks section. --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 12 +++++++++--- llvm/test/CodeGen/X86/remarks-section.ll | 5 +++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index a2c3b50b24670..403963f33b65c 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2383,6 +2383,15 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) { if (!RS.needsSection()) return; + MCSection *RemarksSection = + OutContext.getObjectFileInfo()->getRemarksSection(); + if (!RemarksSection) { + OutContext.reportWarning(SMLoc(), "Current object file format does not " + "support remarks sections. Use the yaml " + "remark format instead."); + return; + } + remarks::RemarkSerializer &RemarkSerializer = RS.getSerializer(); std::optional> Filename; @@ -2400,10 +2409,7 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) { MetaSerializer->emit(); // Switch to the remarks section. - MCSection *RemarksSection = - OutContext.getObjectFileInfo()->getRemarksSection(); OutStreamer->switchSection(RemarksSection); - OutStreamer->emitBinaryData(Buf); } diff --git a/llvm/test/CodeGen/X86/remarks-section.ll b/llvm/test/CodeGen/X86/remarks-section.ll index e67c3579b7593..7d576b0d72e19 100644 --- a/llvm/test/CodeGen/X86/remarks-section.ll +++ b/llvm/test/CodeGen/X86/remarks-section.ll @@ -5,6 +5,8 @@ ; RUN: llc < %s -mtriple=x86_64-darwin --pass-remarks-format=bitstream -remarks-section=false -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-OVERRIDE-BITSTREAM %s ; RUN: llc < %s -mtriple=x86_64-darwin --pass-remarks-format=yaml -remarks-section=true -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-OVERRIDE-YAML %s +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu --pass-remarks-format=bitstream -pass-remarks-output=%/t.yaml 2>&1 | FileCheck --check-prefix=CHECK-LINUX-DEFAULT-BITSTREAM %s + ; CHECK-DARWIN: .section __LLVM,__remarks,regular,debug ; CHECK-DARWIN-NEXT: .byte @@ -22,3 +24,6 @@ define void @func1() { ret void } + +; Currently no ELF support for bitstream remarks +; CHECK-LINUX-DEFAULT-BITSTREAM: warning: Current object file format does not support remarks sections. From 32c3c728a4c875a1300683b0cc3ed9a009c067ce Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Wed, 18 Jun 2025 20:42:29 +0100 Subject: [PATCH 2/2] Update llvm/test/CodeGen/X86/remarks-section.ll Co-authored-by: Jon Roelofs --- llvm/test/CodeGen/X86/remarks-section.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/CodeGen/X86/remarks-section.ll b/llvm/test/CodeGen/X86/remarks-section.ll index 7d576b0d72e19..2611e525aecf8 100644 --- a/llvm/test/CodeGen/X86/remarks-section.ll +++ b/llvm/test/CodeGen/X86/remarks-section.ll @@ -26,4 +26,4 @@ define void @func1() { } ; Currently no ELF support for bitstream remarks -; CHECK-LINUX-DEFAULT-BITSTREAM: warning: Current object file format does not support remarks sections. +; CHECK-LINUX-DEFAULT-BITSTREAM: warning: Current object file format does not support remarks sections. Use the yaml remark format instead.