Skip to content

Commit

Permalink
[NFC] Convert OptimizationRemarkEmitter old emit() calls to new closure
Browse files Browse the repository at this point in the history
parameterized emit() calls

Summary: This is not functional change to adopt new emit() API added in r313691.

Reviewed By: anemet

Subscribers: llvm-commits

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

llvm-svn: 315476
  • Loading branch information
vivekvpandya committed Oct 11, 2017
1 parent 8786798 commit 9590658
Show file tree
Hide file tree
Showing 23 changed files with 533 additions and 373 deletions.
15 changes: 15 additions & 0 deletions llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h
Expand Up @@ -164,6 +164,21 @@ class MachineOptimizationRemarkEmitter {
.getDiagHandlerPtr()->isAnyRemarkEnabled(PassName));
}

/// \brief Take a lambda that returns a remark which will be emitted. Second
/// argument is only used to restrict this to functions.
template <typename T>
void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
// Avoid building the remark unless we know there are at least *some*
// remarks enabled. We can't currently check whether remarks are requested
// for the calling pass since that requires actually building the remark.

if (MF.getFunction()->getContext().getDiagnosticsOutputFile() ||
MF.getFunction()->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled()) {
auto R = RemarkBuilder();
emit(R);
}
}

private:
MachineFunction &MF;

Expand Down
23 changes: 13 additions & 10 deletions llvm/lib/Analysis/InlineCost.cpp
Expand Up @@ -1440,10 +1440,12 @@ bool CallAnalyzer::analyzeBlock(BasicBlock *BB,
if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca ||
HasIndirectBr || HasFrameEscape) {
if (ORE)
ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline",
CandidateCS.getInstruction())
<< NV("Callee", &F)
<< " has uninlinable pattern and cost is not fully computed");
ORE->emit([&]() {
return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline",
CandidateCS.getInstruction())
<< NV("Callee", &F)
<< " has uninlinable pattern and cost is not fully computed";
});
return false;
}

Expand All @@ -1453,12 +1455,13 @@ bool CallAnalyzer::analyzeBlock(BasicBlock *BB,
if (IsCallerRecursive &&
AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller) {
if (ORE)
ORE->emit(
OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline",
CandidateCS.getInstruction())
<< NV("Callee", &F)
<< " is recursive and allocates too much stack space. Cost is "
"not fully computed");
ORE->emit([&]() {
return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline",
CandidateCS.getInstruction())
<< NV("Callee", &F)
<< " is recursive and allocates too much stack space. Cost is "
"not fully computed";
});
return false;
}

Expand Down
16 changes: 9 additions & 7 deletions llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -777,13 +777,15 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
if (Known.Zero.intersects(Known.One)) {
Known.resetAll();

if (Q.ORE) {
auto *CxtI = const_cast<Instruction *>(Q.CxtI);
OptimizationRemarkAnalysis ORA("value-tracking", "BadAssumption", CxtI);
Q.ORE->emit(ORA << "Detected conflicting code assumptions. Program may "
"have undefined behavior, or compiler may have "
"internal error.");
}
if (Q.ORE)
Q.ORE->emit([&]() {
auto *CxtI = const_cast<Instruction *>(Q.CxtI);
return OptimizationRemarkAnalysis("value-tracking", "BadAssumption",
CxtI)
<< "Detected conflicting code assumptions. Program may "
"have undefined behavior, or compiler may have "
"internal error.";
});
}
}

Expand Down
47 changes: 25 additions & 22 deletions llvm/lib/CodeGen/MachineOutliner.cpp
Expand Up @@ -939,29 +939,32 @@ MachineOutliner::findCandidates(SuffixTree &ST, const TargetInstrInfo &TII,
// Emit a remark explaining why we didn't outline this candidate.
std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator> C =
RepeatedSequenceLocs[0];
MachineOptimizationRemarkEmitter MORE(*(C.first->getMF()), nullptr);
MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper",
C.first->getDebugLoc(),
C.first->getParent());
R << "Did not outline " << NV("Length", StringLen) << " instructions"
<< " from " << NV("NumOccurrences", RepeatedSequenceLocs.size())
<< " locations."
<< " Instructions from outlining all occurrences ("
<< NV("OutliningCost", OF.getOutliningCost()) << ")"
<< " >= Unoutlined instruction count ("
<< NV("NotOutliningCost", StringLen * OF.OccurrenceCount) << ")"
<< " (Also found at: ";

// Tell the user the other places the candidate was found.
for (unsigned i = 1, e = RepeatedSequenceLocs.size(); i < e; i++) {
R << NV((Twine("OtherStartLoc") + Twine(i)).str(),
RepeatedSequenceLocs[i].first->getDebugLoc());
if (i != e - 1)
R << ", ";
}
MachineOptimizationRemarkEmitter MORE(
*(C.first->getParent()->getParent()), nullptr);
MORE.emit([&]() {
MachineOptimizationRemarkMissed R(DEBUG_TYPE, "NotOutliningCheaper",
C.first->getDebugLoc(),
C.first->getParent());
R << "Did not outline " << NV("Length", StringLen) << " instructions"
<< " from " << NV("NumOccurrences", RepeatedSequenceLocs.size())
<< " locations."
<< " Instructions from outlining all occurrences ("
<< NV("OutliningCost", OF.getOutliningCost()) << ")"
<< " >= Unoutlined instruction count ("
<< NV("NotOutliningCost", StringLen * OF.OccurrenceCount) << ")"
<< " (Also found at: ";

// Tell the user the other places the candidate was found.
for (unsigned i = 1, e = RepeatedSequenceLocs.size(); i < e; i++) {
R << NV((Twine("OtherStartLoc") + Twine(i)).str(),
RepeatedSequenceLocs[i].first->getDebugLoc());
if (i != e - 1)
R << ", ";
}

R << ")";
MORE.emit(R);
R << ")";
return R;
});

// Move to the next candidate.
continue;
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/CodeGen/PrologEpilogInserter.cpp
Expand Up @@ -960,11 +960,12 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
MFI.setStackSize(StackSize);
NumBytesStackSpace += StackSize;

MachineOptimizationRemarkAnalysis R(
DEBUG_TYPE, "StackSize", Fn.getFunction()->getSubprogram(), &Fn.front());
R << ore::NV("NumStackBytes", StackSize)
<< " stack bytes in function";
ORE->emit(R);
ORE->emit([&]() {
return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
Fn.getFunction()->getSubprogram(),
&Fn.front())
<< ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
});
}

/// insertPrologEpilogCode - Scan the function for modified callee saved
Expand Down
25 changes: 14 additions & 11 deletions llvm/lib/CodeGen/RegAllocGreedy.cpp
Expand Up @@ -2717,17 +2717,20 @@ void RAGreedy::reportNumberOfSplillsReloads(MachineLoop *L, unsigned &Reloads,
if (Reloads || FoldedReloads || Spills || FoldedSpills) {
using namespace ore;

MachineOptimizationRemarkMissed R(DEBUG_TYPE, "LoopSpillReload",
L->getStartLoc(), L->getHeader());
if (Spills)
R << NV("NumSpills", Spills) << " spills ";
if (FoldedSpills)
R << NV("NumFoldedSpills", FoldedSpills) << " folded spills ";
if (Reloads)
R << NV("NumReloads", Reloads) << " reloads ";
if (FoldedReloads)
R << NV("NumFoldedReloads", FoldedReloads) << " folded reloads ";
ORE->emit(R << "generated in loop");
ORE->emit([&]() {
MachineOptimizationRemarkMissed R(DEBUG_TYPE, "LoopSpillReload",
L->getStartLoc(), L->getHeader());
if (Spills)
R << NV("NumSpills", Spills) << " spills ";
if (FoldedSpills)
R << NV("NumFoldedSpills", FoldedSpills) << " folded spills ";
if (Reloads)
R << NV("NumReloads", Reloads) << " reloads ";
if (FoldedReloads)
R << NV("NumFoldedReloads", FoldedReloads) << " folded reloads ";
R << "generated in loop";
return R;
});
}
}

Expand Down
44 changes: 26 additions & 18 deletions llvm/lib/CodeGen/StackProtector.cpp
Expand Up @@ -247,10 +247,12 @@ bool StackProtector::RequiresStackProtector() {
OptimizationRemarkEmitter ORE(F);

if (F->hasFnAttribute(Attribute::StackProtectReq)) {
ORE.emit(OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F)
ORE.emit([&]() {
return OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F)
<< "Stack protection applied to function "
<< ore::NV("Function", F)
<< " due to a function attribute or command-line switch");
<< " due to a function attribute or command-line switch";
});
NeedsProtector = true;
Strong = true; // Use the same heuristic as strong to determine SSPLayout
} else if (F->hasFnAttribute(Attribute::StackProtectStrong))
Expand All @@ -264,29 +266,31 @@ bool StackProtector::RequiresStackProtector() {
for (const Instruction &I : BB) {
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
if (AI->isArrayAllocation()) {
OptimizationRemark Remark(DEBUG_TYPE, "StackProtectorAllocaOrArray",
&I);
Remark
<< "Stack protection applied to function "
<< ore::NV("Function", F)
<< " due to a call to alloca or use of a variable length array";
auto RemarkBuilder = [&]() {
return OptimizationRemark(DEBUG_TYPE, "StackProtectorAllocaOrArray",
&I)
<< "Stack protection applied to function "
<< ore::NV("Function", F)
<< " due to a call to alloca or use of a variable length "
"array";
};
if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
// A call to alloca with size >= SSPBufferSize requires
// stack protectors.
Layout.insert(std::make_pair(AI, SSPLK_LargeArray));
ORE.emit(Remark);
ORE.emit(RemarkBuilder);
NeedsProtector = true;
} else if (Strong) {
// Require protectors for all alloca calls in strong mode.
Layout.insert(std::make_pair(AI, SSPLK_SmallArray));
ORE.emit(Remark);
ORE.emit(RemarkBuilder);
NeedsProtector = true;
}
} else {
// A call to alloca with a variable size requires protectors.
Layout.insert(std::make_pair(AI, SSPLK_LargeArray));
ORE.emit(Remark);
ORE.emit(RemarkBuilder);
NeedsProtector = true;
}
continue;
Expand All @@ -296,23 +300,27 @@ bool StackProtector::RequiresStackProtector() {
if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) {
Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray
: SSPLK_SmallArray));
ORE.emit(OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I)
ORE.emit([&]() {
return OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I)
<< "Stack protection applied to function "
<< ore::NV("Function", F)
<< " due to a stack allocated buffer or struct containing a "
"buffer");
"buffer";
});
NeedsProtector = true;
continue;
}

if (Strong && HasAddressTaken(AI)) {
++NumAddrTaken;
Layout.insert(std::make_pair(AI, SSPLK_AddrOf));
ORE.emit(
OptimizationRemark(DEBUG_TYPE, "StackProtectorAddressTaken", &I)
<< "Stack protection applied to function "
<< ore::NV("Function", F)
<< " due to the address of a local variable being taken");
ORE.emit([&]() {
return OptimizationRemark(DEBUG_TYPE, "StackProtectorAddressTaken",
&I)
<< "Stack protection applied to function "
<< ore::NV("Function", F)
<< " due to the address of a local variable being taken";
});
NeedsProtector = true;
}
}
Expand Down

0 comments on commit 9590658

Please sign in to comment.