Skip to content

Commit 209d5a1

Browse files
committed
[Remarks] Emit the remarks section by default for certain formats
Emit a remarks section by default for the following formats: * bitstream * yaml-strtab while still providing -remarks-section=<bool> to override the defaults.
1 parent a51fc8d commit 209d5a1

File tree

6 files changed

+71
-16
lines changed

6 files changed

+71
-16
lines changed

llvm/docs/Remarks.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,13 @@ This diff file can be displayed using :ref:`opt-viewer.py <optviewerpy>`.
581581
Emitting remark diagnostics in the object file
582582
==============================================
583583

584-
A section containing metadata on remark diagnostics will be emitted when
585-
-remarks-section is passed. The section contains the metadata associated to the
586-
format used to serialize the remarks.
584+
A section containing metadata on remark diagnostics will be emitted for the
585+
following formats:
586+
587+
* ``yaml-strtab``
588+
* ``bitstream``
589+
590+
This can be overridden by using the flag ``-remarks-section=<bool>``.
587591

588592
The section is named:
589593

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class MCTargetOptions;
7070
class MDNode;
7171
class Module;
7272
class raw_ostream;
73+
class RemarkStreamer;
7374
class StackMaps;
7475
class TargetLoweringObjectFile;
7576
class TargetMachine;
@@ -319,7 +320,7 @@ class AsmPrinter : public MachineFunctionPass {
319320

320321
void emitStackSizeSection(const MachineFunction &MF);
321322

322-
void emitRemarksSection(Module &M);
323+
void emitRemarksSection(RemarkStreamer &RS);
323324

324325
enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
325326
CFIMoveType needsCFIMoves() const;

llvm/include/llvm/IR/RemarkStreamer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class RemarkStreamer {
5353
Error setFilter(StringRef Filter);
5454
/// Emit a diagnostic through the streamer.
5555
void emit(const DiagnosticInfoOptimizationBase &Diag);
56+
/// Check if the remarks also need to have associated metadata in a section.
57+
bool needsSection() const;
5658
};
5759

5860
template <typename ThisError>

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ static const char *const CodeViewLineTablesGroupDescription =
146146

147147
STATISTIC(EmittedInsts, "Number of machine instrs printed");
148148

149-
static cl::opt<bool> EnableRemarksSection(
150-
"remarks-section",
151-
cl::desc("Emit a section containing remark diagnostics metadata"),
152-
cl::init(false));
153-
154149
char AsmPrinter::ID = 0;
155150

156151
using gcp_map_type = DenseMap<GCStrategy *, std::unique_ptr<GCMetadataPrinter>>;
@@ -1365,14 +1360,14 @@ void AsmPrinter::emitGlobalIndirectSymbol(Module &M,
13651360
}
13661361
}
13671362

1368-
void AsmPrinter::emitRemarksSection(Module &M) {
1369-
RemarkStreamer *RS = M.getContext().getRemarkStreamer();
1370-
if (!RS)
1363+
void AsmPrinter::emitRemarksSection(RemarkStreamer &RS) {
1364+
if (!RS.needsSection())
13711365
return;
1372-
remarks::RemarkSerializer &RemarkSerializer = RS->getSerializer();
1366+
1367+
remarks::RemarkSerializer &RemarkSerializer = RS.getSerializer();
13731368

13741369
Optional<SmallString<128>> Filename;
1375-
if (Optional<StringRef> FilenameRef = RS->getFilename()) {
1370+
if (Optional<StringRef> FilenameRef = RS.getFilename()) {
13761371
Filename = *FilenameRef;
13771372
sys::fs::make_absolute(*Filename);
13781373
assert(!Filename->empty() && "The filename can't be empty.");
@@ -1427,8 +1422,8 @@ bool AsmPrinter::doFinalization(Module &M) {
14271422
// Emit the remarks section contents.
14281423
// FIXME: Figure out when is the safest time to emit this section. It should
14291424
// not come after debug info.
1430-
if (EnableRemarksSection)
1431-
emitRemarksSection(M);
1425+
if (RemarkStreamer *RS = M.getContext().getRemarkStreamer())
1426+
emitRemarksSection(*RS);
14321427

14331428
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
14341429

llvm/lib/IR/RemarkStreamer.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121

2222
using namespace llvm;
2323

24+
static cl::opt<cl::boolOrDefault> EnableRemarksSection(
25+
"remarks-section",
26+
cl::desc(
27+
"Emit a section containing remark diagnostics metadata. By default, "
28+
"this is enabled for the following formats: yaml-strtab, bitstream."),
29+
cl::init(cl::BOU_UNSET), cl::Hidden);
30+
2431
RemarkStreamer::RemarkStreamer(
2532
std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
2633
Optional<StringRef> FilenameIn)
@@ -104,6 +111,31 @@ void RemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
104111
RemarkSerializer->emit(R);
105112
}
106113

114+
bool RemarkStreamer::needsSection() const {
115+
if (EnableRemarksSection == cl::BOU_TRUE)
116+
return true;
117+
118+
if (EnableRemarksSection == cl::BOU_FALSE)
119+
return false;
120+
121+
assert(EnableRemarksSection == cl::BOU_UNSET);
122+
123+
// We only need a section if we're in separate mode.
124+
if (RemarkSerializer->Mode != remarks::SerializerMode::Separate)
125+
return false;
126+
127+
// Only some formats need a section:
128+
// * bitstream
129+
// * yaml-strtab
130+
switch (RemarkSerializer->SerializerFormat) {
131+
case remarks::Format::YAMLStrTab:
132+
case remarks::Format::Bitstream:
133+
return true;
134+
default:
135+
return false;
136+
}
137+
}
138+
107139
char RemarkSetupFileError::ID = 0;
108140
char RemarkSetupPatternError::ID = 0;
109141
char RemarkSetupFormatError::ID = 0;

llvm/test/CodeGen/X86/remarks-section.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
; RUN: llc < %s -mtriple=x86_64-darwin -remarks-section -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN -DPATH=%/t.yaml %s
33
; RUN: llc < %s -mtriple=x86_64-darwin --pass-remarks-format=yaml-strtab -remarks-section -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-STRTAB -DPATH=%/t.yaml %s
44

5+
; RUN: llc < %s -mtriple=x86_64-darwin -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-DEFAULT %s
6+
; RUN: llc < %s -mtriple=x86_64-darwin --pass-remarks-format=yaml-strtab -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-DEFAULT-YAML-STRTAB %s
7+
; RUN: llc < %s -mtriple=x86_64-darwin --pass-remarks-format=bitstream -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-DEFAULT-BITSTREAM %s
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
9+
; 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
10+
511
; CHECK-LABEL: func1:
612

713
; CHECK: .section .remarks,"e",@progbits
@@ -12,6 +18,21 @@
1218

1319
; CHECK-DARWIN-STRTAB: .section __LLVM,__remarks,regular,debug
1420
; CHECK-DARWIN-STRTAB-NEXT: .byte
21+
22+
; By default, the format is YAML which does not need a section.
23+
; CHECK-DARWIN-DEFAULT-NOT: .section __LLVM,__remarks
24+
25+
; yaml-strtab needs a section.
26+
; CHECK-DARWIN-DEFAULT-YAML-STRTAB: .section __LLVM,__remarks
27+
28+
; bitstream needs a section.
29+
; CHECK-DARWIN-DEFAULT-BITSTREAM: .section __LLVM,__remarks
30+
31+
; -remarks-section should force disable the section.
32+
; CHECK-DARWIN-OVERRIDE-BITSTREAM-NOT: .section __LLVM,__remarks
33+
34+
; -remarks-section should also force enable the section.
35+
; CHECK-DARWIN-OVERRIDE-YAML: .section __LLVM,__remarks
1536
define void @func1() {
1637
ret void
1738
}

0 commit comments

Comments
 (0)