Skip to content

Commit c49b600

Browse files
committed
[Remarks] Emit remarks for "auto-init" !annotations
Using the !annotation metadata, emit remarks pointing to code added by `-ftrivial-auto-var-init` that survived the optimizer. Example: ``` auto-init.c:4:7: remark: Initialization inserted by -ftrivial-auto-var-init. [-Rpass-missed=annotation-remarks] int buf[1024]; ^ ``` The tests are testing various situations like calls/stores/other instructions, with debug locations, and extra debug information on purpose: more patches will come to improve the reporting to make it more user-friendly, and these tests will show how the reporting evolves. Differential Revision: https://reviews.llvm.org/D97405
1 parent 00b3f2f commit c49b600

File tree

4 files changed

+626
-1
lines changed

4 files changed

+626
-1
lines changed

llvm/lib/Transforms/Scalar/AnnotationRemarks.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,40 @@ using namespace llvm::ore;
2727
#define DEBUG_TYPE "annotation-remarks"
2828
#define REMARK_PASS DEBUG_TYPE
2929

30+
static void tryEmitAutoInitRemark(ArrayRef<Instruction *> Instructions,
31+
OptimizationRemarkEmitter &ORE) {
32+
// For every auto-init annotation generate a separate remark.
33+
for (Instruction *I : Instructions) {
34+
if (!I->hasMetadata(LLVMContext::MD_annotation))
35+
continue;
36+
for (const MDOperand &Op :
37+
I->getMetadata(LLVMContext::MD_annotation)->operands()) {
38+
if (cast<MDString>(Op.get())->getString() != "auto-init")
39+
continue;
40+
41+
ORE.emit(
42+
OptimizationRemarkMissed(REMARK_PASS, "AutoInitUnknownInstruction", I)
43+
<< "Initialization inserted by -ftrivial-auto-var-init.");
44+
}
45+
}
46+
}
47+
3048
static void runImpl(Function &F) {
3149
if (!OptimizationRemarkEmitter::allowExtraAnalysis(F, REMARK_PASS))
3250
return;
3351

52+
// Track all annotated instructions aggregated based on their debug location.
53+
DenseMap<MDNode *, SmallVector<Instruction *, 4>> DebugLoc2Annotated;
54+
3455
OptimizationRemarkEmitter ORE(&F);
35-
// For now, just generate a summary of the annotated instructions.
56+
// First, generate a summary of the annotated instructions.
3657
MapVector<StringRef, unsigned> Mapping;
3758
for (Instruction &I : instructions(F)) {
3859
if (!I.hasMetadata(LLVMContext::MD_annotation))
3960
continue;
61+
auto Iter = DebugLoc2Annotated.insert({I.getDebugLoc().getAsMDNode(), {}});
62+
Iter.first->second.push_back(&I);
63+
4064
for (const MDOperand &Op :
4165
I.getMetadata(LLVMContext::MD_annotation)->operands()) {
4266
auto Iter = Mapping.insert({cast<MDString>(Op.get())->getString(), 0});
@@ -49,6 +73,16 @@ static void runImpl(Function &F) {
4973
ORE.emit(OptimizationRemarkAnalysis(REMARK_PASS, "AnnotationSummary", IP)
5074
<< "Annotated " << NV("count", KV.second) << " instructions with "
5175
<< NV("type", KV.first));
76+
77+
// For each debug location, look for all the instructions with annotations and
78+
// generate more detailed remarks to be displayed at that location.
79+
for (auto &KV : DebugLoc2Annotated) {
80+
// Don't generate remarks with no debug location.
81+
if (!KV.first)
82+
continue;
83+
84+
tryEmitAutoInitRemark(KV.second, ORE);
85+
}
5286
}
5387

5488
namespace {

0 commit comments

Comments
 (0)