Skip to content

Commit

Permalink
[OptRemark] Move YAML writing to IR
Browse files Browse the repository at this point in the history
Before the patch this was in Analysis.  Moving it to IR and making it implicit
part of LLVMContext::diagnose allows the full opt-remark facility to be used
outside passes e.g. the pass manager.  Jessica is planning to use this to
report function size after each pass.  The same could be used for time
reports.

Tested with BUILD_SHARED_LIBS=On.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314909 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
anemet committed Oct 4, 2017
1 parent af1c415 commit c5599f6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 98 deletions.
6 changes: 0 additions & 6 deletions include/llvm/Analysis/OptimizationDiagnosticInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,5 @@ class OptimizationRemarkEmitterAnalysis
/// \brief Run the analysis pass over a function and produce BFI.
Result run(Function &F, FunctionAnalysisManager &AM);
};

namespace yaml {
template <> struct MappingTraits<DiagnosticInfoOptimizationBase *> {
static void mapping(IO &io, DiagnosticInfoOptimizationBase *&OptDiag);
};
}
}
#endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
6 changes: 6 additions & 0 deletions include/llvm/IR/DiagnosticInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,12 @@ class DiagnosticInfoUnsupported : public DiagnosticInfoWithLocationBase {
void print(DiagnosticPrinter &DP) const override;
};

namespace yaml {
template <> struct MappingTraits<DiagnosticInfoOptimizationBase *> {
static void mapping(IO &io, DiagnosticInfoOptimizationBase *&OptDiag);
};
} // namespace yaml

} // end namespace llvm

#endif // LLVM_IR_DIAGNOSTICINFO_H
86 changes: 0 additions & 86 deletions lib/Analysis/OptimizationDiagnosticInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,86 +64,6 @@ Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) {
return BFI->getBlockProfileCount(cast<BasicBlock>(V));
}

namespace llvm {
namespace yaml {

void MappingTraits<DiagnosticInfoOptimizationBase *>::mapping(
IO &io, DiagnosticInfoOptimizationBase *&OptDiag) {
assert(io.outputting() && "input not yet implemented");

if (io.mapTag("!Passed",
(OptDiag->getKind() == DK_OptimizationRemark ||
OptDiag->getKind() == DK_MachineOptimizationRemark)))
;
else if (io.mapTag(
"!Missed",
(OptDiag->getKind() == DK_OptimizationRemarkMissed ||
OptDiag->getKind() == DK_MachineOptimizationRemarkMissed)))
;
else if (io.mapTag(
"!Analysis",
(OptDiag->getKind() == DK_OptimizationRemarkAnalysis ||
OptDiag->getKind() == DK_MachineOptimizationRemarkAnalysis)))
;
else if (io.mapTag("!AnalysisFPCommute",
OptDiag->getKind() ==
DK_OptimizationRemarkAnalysisFPCommute))
;
else if (io.mapTag("!AnalysisAliasing",
OptDiag->getKind() ==
DK_OptimizationRemarkAnalysisAliasing))
;
else if (io.mapTag("!Failure", OptDiag->getKind() == DK_OptimizationFailure))
;
else
llvm_unreachable("Unknown remark type");

// These are read-only for now.
DiagnosticLocation DL = OptDiag->getLocation();
StringRef FN =
GlobalValue::dropLLVMManglingEscape(OptDiag->getFunction().getName());

StringRef PassName(OptDiag->PassName);
io.mapRequired("Pass", PassName);
io.mapRequired("Name", OptDiag->RemarkName);
if (!io.outputting() || DL.isValid())
io.mapOptional("DebugLoc", DL);
io.mapRequired("Function", FN);
io.mapOptional("Hotness", OptDiag->Hotness);
io.mapOptional("Args", OptDiag->Args);
}

template <> struct MappingTraits<DiagnosticLocation> {
static void mapping(IO &io, DiagnosticLocation &DL) {
assert(io.outputting() && "input not yet implemented");

StringRef File = DL.getFilename();
unsigned Line = DL.getLine();
unsigned Col = DL.getColumn();

io.mapRequired("File", File);
io.mapRequired("Line", Line);
io.mapRequired("Column", Col);
}

static const bool flow = true;
};

// Implement this as a mapping for now to get proper quotation for the value.
template <> struct MappingTraits<DiagnosticInfoOptimizationBase::Argument> {
static void mapping(IO &io, DiagnosticInfoOptimizationBase::Argument &A) {
assert(io.outputting() && "input not yet implemented");
io.mapRequired(A.Key.data(), A.Val);
if (A.Loc.isValid())
io.mapOptional("DebugLoc", A.Loc);
}
};

} // end namespace yaml
} // end namespace llvm

LLVM_YAML_IS_SEQUENCE_VECTOR(DiagnosticInfoOptimizationBase::Argument)

void OptimizationRemarkEmitter::computeHotness(
DiagnosticInfoIROptimization &OptDiag) {
const Value *V = OptDiag.getCodeRegion();
Expand All @@ -163,12 +83,6 @@ void OptimizationRemarkEmitter::emit(
return;
}

yaml::Output *Out = F->getContext().getDiagnosticsOutputFile();
if (Out) {
// For remarks the << operator takes a reference to a pointer.
auto *P = &OptDiagBase;
*Out << P;
}
F->getContext().diagnose(OptDiag);
}

Expand Down
6 changes: 0 additions & 6 deletions lib/CodeGen/MachineOptimizationRemarkEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ void MachineOptimizationRemarkEmitter::emit(
return;
}

yaml::Output *Out = Ctx.getDiagnosticsOutputFile();
if (Out) {
auto *P = &const_cast<DiagnosticInfoOptimizationBase &>(OptDiagCommon);
*Out << P;
}

Ctx.diagnose(OptDiag);
}

Expand Down
80 changes: 80 additions & 0 deletions lib/IR/DiagnosticInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,83 @@ std::string DiagnosticInfoOptimizationBase::getMsg() const {
OS << Arg.Val;
return OS.str();
}

namespace llvm {
namespace yaml {

void MappingTraits<DiagnosticInfoOptimizationBase *>::mapping(
IO &io, DiagnosticInfoOptimizationBase *&OptDiag) {
assert(io.outputting() && "input not yet implemented");

if (io.mapTag("!Passed",
(OptDiag->getKind() == DK_OptimizationRemark ||
OptDiag->getKind() == DK_MachineOptimizationRemark)))
;
else if (io.mapTag(
"!Missed",
(OptDiag->getKind() == DK_OptimizationRemarkMissed ||
OptDiag->getKind() == DK_MachineOptimizationRemarkMissed)))
;
else if (io.mapTag(
"!Analysis",
(OptDiag->getKind() == DK_OptimizationRemarkAnalysis ||
OptDiag->getKind() == DK_MachineOptimizationRemarkAnalysis)))
;
else if (io.mapTag("!AnalysisFPCommute",
OptDiag->getKind() ==
DK_OptimizationRemarkAnalysisFPCommute))
;
else if (io.mapTag("!AnalysisAliasing",
OptDiag->getKind() ==
DK_OptimizationRemarkAnalysisAliasing))
;
else if (io.mapTag("!Failure", OptDiag->getKind() == DK_OptimizationFailure))
;
else
llvm_unreachable("Unknown remark type");

// These are read-only for now.
DiagnosticLocation DL = OptDiag->getLocation();
StringRef FN =
GlobalValue::dropLLVMManglingEscape(OptDiag->getFunction().getName());

StringRef PassName(OptDiag->PassName);
io.mapRequired("Pass", PassName);
io.mapRequired("Name", OptDiag->RemarkName);
if (!io.outputting() || DL.isValid())
io.mapOptional("DebugLoc", DL);
io.mapRequired("Function", FN);
io.mapOptional("Hotness", OptDiag->Hotness);
io.mapOptional("Args", OptDiag->Args);
}

template <> struct MappingTraits<DiagnosticLocation> {
static void mapping(IO &io, DiagnosticLocation &DL) {
assert(io.outputting() && "input not yet implemented");

StringRef File = DL.getFilename();
unsigned Line = DL.getLine();
unsigned Col = DL.getColumn();

io.mapRequired("File", File);
io.mapRequired("Line", Line);
io.mapRequired("Column", Col);
}

static const bool flow = true;
};

// Implement this as a mapping for now to get proper quotation for the value.
template <> struct MappingTraits<DiagnosticInfoOptimizationBase::Argument> {
static void mapping(IO &io, DiagnosticInfoOptimizationBase::Argument &A) {
assert(io.outputting() && "input not yet implemented");
io.mapRequired(A.Key.data(), A.Val);
if (A.Loc.isValid())
io.mapOptional("DebugLoc", A.Loc);
}
};

} // end namespace yaml
} // end namespace llvm

LLVM_YAML_IS_SEQUENCE_VECTOR(DiagnosticInfoOptimizationBase::Argument)
8 changes: 8 additions & 0 deletions lib/IR/LLVMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
}

void LLVMContext::diagnose(const DiagnosticInfo &DI) {
if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) {
yaml::Output *Out = getDiagnosticsOutputFile();
if (Out) {
// For remarks the << operator takes a reference to a pointer.
auto *P = const_cast<DiagnosticInfoOptimizationBase *>(OptDiagBase);
*Out << P;
}
}
// If there is a report handler, use it.
if (pImpl->DiagHandler &&
(!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
Expand Down

0 comments on commit c5599f6

Please sign in to comment.