Address review feedback
- When emitting an optimization report diagnostic, the lack of debug
information sometimes causes internal failures. If debug information is
not
present, the diagnostic uses "<unknown>:0:0" as the location string.
- Rename OptimizationReport to OptimizationRemark.
- Only enable line table generation if no debug information had been
requested.
- Add a default remark diagnostic class to enable -Rpass diagnostics.
- Change the diagnostic group for BackendOptimizationRemark to "pass".
- Add DiagnosticIDs::isRemark(). Use it in printDiagnosticOptions to
print "-R" instead of "-W" in the diagnostic message.
- Re-word the help text for -Rpass=.
- In BackendConsumer::OptimizationRemarkHandler, get a SourceLocation
object out of the file name, line and column number. Use that location
in the call to Diags.Report().
- If -Rpass= is used, also enable column information by adding
-dwarf-column-info to the backend command line.
- Add test for -Rpass=. Document OptimizationRemarkHandler.
Hi qcolombet, rsmith,
http://llvm-reviews.chandlerc.com/D3226
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D3226?vs=8246&id=8322#toc
Files:
include/clang/Basic/Diagnostic.td
include/clang/Basic/DiagnosticDriverKinds.td
include/clang/Basic/DiagnosticFrontendKinds.td
include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticIDs.h
include/clang/Driver/Options.td
include/clang/Frontend/CodeGenOptions.h
lib/Basic/DiagnosticIDs.cpp
lib/CodeGen/CodeGenAction.cpp
lib/Driver/Tools.cpp
lib/Frontend/CompilerInvocation.cpp
lib/Frontend/TextDiagnosticPrinter.cpp
test/Frontend/optimization-remark.c
Index: include/clang/Basic/Diagnostic.td
===================================================================
--- include/clang/Basic/Diagnostic.td
+++ include/clang/Basic/Diagnostic.td
@@ -102,6 +102,7 @@
class DefaultWarnShowInSystemHeader {
bit WarningShowInSystemHeader = 1;
}
+class DefaultRemark { DiagMapping DefaultMapping = MAP_REMARK; }
// Definitions for Diagnostics.
include "DiagnosticASTKinds.td"
Index: include/clang/Basic/DiagnosticDriverKinds.td
===================================================================
--- include/clang/Basic/DiagnosticDriverKinds.td
+++ include/clang/Basic/DiagnosticDriverKinds.td
@@ -111,6 +111,8 @@
"unknown or ill-formed Objective-C runtime '%0'">;
def err_drv_emit_llvm_link : Error<
"-emit-llvm cannot be used when linking">;
+def err_drv_optimization_report_pattern : Error<
+ "%0 in '%1'">;
def warn_O4_is_O3 : Warning<"-O4 is equivalent to -O3">, InGroup<Deprecated>;
def warn_drv_optimization_value : Warning<"optimization level '%0' is unsupported; using '%1%2' instead">,
Index: include/clang/Basic/DiagnosticFrontendKinds.td
===================================================================
--- include/clang/Basic/DiagnosticFrontendKinds.td
+++ include/clang/Basic/DiagnosticFrontendKinds.td
@@ -32,6 +32,9 @@
def remark_fe_backend_plugin: Remark<"%0">, CatBackend, InGroup<RemarkBackendPlugin>;
def note_fe_backend_plugin: Note<"%0">, CatBackend;
+def remark_fe_backend_optimization_remark: Remark<"%0">, CatBackend,
+ InGroup<BackendOptimizationRemark>, DefaultRemark;
+
def err_fe_invalid_code_complete_file : Error<
"cannot locate code-completion file %0">, DefaultFatal;
def err_fe_stdout_binary : Error<"unable to change standard output to binary">,
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -662,3 +662,4 @@
def BackendFrameLargerThan : DiagGroup<"frame-larger-than">;
def BackendPlugin : DiagGroup<"backend-plugin">;
def RemarkBackendPlugin : DiagGroup<"remark-backend-plugin">;
+def BackendOptimizationRemark : DiagGroup<"pass">;
Index: include/clang/Basic/DiagnosticIDs.h
===================================================================
--- include/clang/Basic/DiagnosticIDs.h
+++ include/clang/Basic/DiagnosticIDs.h
@@ -154,6 +154,9 @@
/// default.
static bool isDefaultMappingAsError(unsigned DiagID);
+ /// \brief Return true if the specified diagnostic is a Remark.
+ static bool isRemark(unsigned DiagID);
+
/// \brief Determine whether the given built-in diagnostic ID is a Note.
static bool isBuiltinNote(unsigned DiagID);
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -60,6 +60,7 @@
def M_Group : OptionGroup<"<M group>">, Group<CompileOnly_Group>;
def T_Group : OptionGroup<"<T group>">;
def O_Group : OptionGroup<"<O group>">, Group<CompileOnly_Group>;
+def R_Group : OptionGroup<"<R group>">, Group<CompileOnly_Group>;
def W_Group : OptionGroup<"<W group>">, Group<CompileOnly_Group>;
def d_Group : OptionGroup<"<d group>">;
def f_Group : OptionGroup<"<f group>">, Group<CompileOnly_Group>;
@@ -253,7 +254,9 @@
def Qunused_arguments : Flag<["-"], "Qunused-arguments">, Flags<[DriverOption, CoreOption]>,
HelpText<"Don't emit warning for unused driver arguments">;
def Q : Flag<["-"], "Q">;
-def R : Flag<["-"], "R">;
+def Rpass_EQ : Joined<["-"], "Rpass=">, Group<R_Group>, Flags<[CC1Option]>,
+ HelpText<"Report transformations performed by optimization passes whose "
+ "name matches the given POSIX regular expression.">;
def S : Flag<["-"], "S">, Flags<[DriverOption,CC1Option]>, Group<Action_Group>,
HelpText<"Only run preprocess and compilation steps">;
def Tbss : JoinedOrSeparate<["-"], "Tbss">, Group<T_Group>;
Index: include/clang/Frontend/CodeGenOptions.h
===================================================================
--- include/clang/Frontend/CodeGenOptions.h
+++ include/clang/Frontend/CodeGenOptions.h
@@ -16,6 +16,7 @@
#include <string>
#include <vector>
+#include "llvm/Support/Regex.h"
namespace clang {
@@ -145,6 +146,13 @@
/// Name of the profile file to use as input for -fprofile-instr-use
std::string InstrProfileInput;
+ /// Regular expression to select optimizations for which we should enable
+ /// optimization remarks. Transformation passes whose name matches this
+ /// expression (and support this feature), will emit a diagnostic
+ /// whenever they perform a transformation. This is enabled by the
+ /// -Rpass=regexp flag.
+ llvm::Regex *OptimizationRemarkPattern;
+
public:
// Define accessors/mutators for code generation options of enumeration type.
#define CODEGENOPT(Name, Bits, Default)
Index: lib/Basic/DiagnosticIDs.cpp
===================================================================
--- lib/Basic/DiagnosticIDs.cpp
+++ lib/Basic/DiagnosticIDs.cpp
@@ -361,6 +361,11 @@
return GetDefaultDiagMappingInfo(DiagID).getMapping() == diag::MAP_ERROR;
}
+bool DiagnosticIDs::isRemark(unsigned DiagID) {
+ return DiagID < diag::DIAG_UPPER_LIMIT &&
+ getBuiltinDiagClass(DiagID) == CLASS_REMARK;
+}
+
/// getDescription - Given a diagnostic ID, return a description of the
/// issue.
StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
Index: lib/CodeGen/CodeGenAction.cpp
===================================================================
--- lib/CodeGen/CodeGenAction.cpp
+++ lib/CodeGen/CodeGenAction.cpp
@@ -20,6 +20,7 @@
#include "clang/Frontend/FrontendDiagnostic.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
@@ -220,6 +221,12 @@
/// \return True if the diagnostic has been successfully reported, false
/// otherwise.
bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
+ /// \brief Specialized handler for the optimization diagnostic.
+ /// This handler always controls the reporting of the diagnostic.
+ /// If it does not find that the name of the pass in \p D matches
+ /// the regular expression in -Rpass=, it will print nothing.
+ void
+ OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationRemark &D);
};
void BackendConsumer::anchor() {}
@@ -374,6 +381,25 @@
return true;
}
+void BackendConsumer::OptimizationRemarkHandler(
+ const llvm::DiagnosticInfoOptimizationRemark &D) {
+ // Optimization remarks are active only if -Rpass=regexp is given and the
+ // regular expression pattern in 'regexp' matches the name of the pass
+ // name in \p D.
+ if (CodeGenOpts.OptimizationRemarkPattern &&
+ CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName())) {
+ SourceManager &SourceMgr = Context->getSourceManager();
+ FileManager &FileMgr = SourceMgr.getFileManager();
+ StringRef Filename;
+ unsigned Line, Column;
+ D.getLocation(&Filename, &Line, &Column);
+ SourceLocation Loc(SourceMgr.translateFileLineCol(FileMgr.getFile(Filename),
+ Line, Column));
+ Diags.Report(Loc, diag::remark_fe_backend_optimization_remark)
+ << D.getMsg().str();
+ }
+}
+
/// \brief This function is invoked when the backend needs
/// to report something to the user.
void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
@@ -391,6 +417,12 @@
return;
ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
break;
+ case llvm::DK_OptimizationRemark:
+ // Note that optimization remarks are always handled in
+ // OptimizationRemarkHandler. The diagnostic will only be emitted if the
+ // regular expression in -Rpass= matches the name of the pass stored in DI.
+ OptimizationRemarkHandler(cast<DiagnosticInfoOptimizationRemark>(DI));
+ return;
default:
// Plugin IDs are not bound to any value as they are set dynamically.
ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -3387,6 +3387,14 @@
A->render(Args, CmdArgs);
}
+ if (Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) {
+ // -Rpass turns on line table information. Make sure that we also
+ // collect column information to provide more accurate optimization
+ // remarks.
+ CmdArgs.push_back("-dwarf-column-info");
+ A->render(Args, CmdArgs);
+ }
+
if (Args.hasArg(options::OPT_mkernel)) {
if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
CmdArgs.push_back("-fapple-kext");
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -520,6 +520,23 @@
Opts.DependentLibraries = Args.getAllArgValues(OPT_dependent_lib);
+ if (Arg *A = Args.getLastArg(OPT_Rpass_EQ)) {
+ StringRef Val = A->getValue();
+ std::string RegexError;
+ Opts.OptimizationRemarkPattern = new llvm::Regex(Val);
+ if (!Opts.OptimizationRemarkPattern->isValid(RegexError)) {
+ Diags.Report(diag::err_drv_optimization_report_pattern)
+ << RegexError << A->getAsString(Args);
+ delete Opts.OptimizationRemarkPattern;
+ Opts.OptimizationRemarkPattern = 0;
+ } else if (Opts.getDebugInfo() == CodeGenOptions::NoDebugInfo) {
+ // Optimization diagnostics need to extract location information
+ // from the instructions they are emitted on. When -Rpass is used,
+ // make sure that we are at least generating line table information.
+ Opts.setDebugInfo(CodeGenOptions::DebugLineTablesOnly);
+ }
+ }
+
return Success;
}
Index: lib/Frontend/TextDiagnosticPrinter.cpp
===================================================================
--- lib/Frontend/TextDiagnosticPrinter.cpp
+++ lib/Frontend/TextDiagnosticPrinter.cpp
@@ -81,7 +81,8 @@
StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
if (!Opt.empty()) {
- OS << (Started ? "," : " [") << "-W" << Opt;
+ OS << (Started ? "," : " [")
+ << (DiagnosticIDs::isRemark(Info.getID()) ? "-R" : "-W") << Opt;
Started = true;
}
}
Index: test/Frontend/optimization-remark.c
===================================================================
--- /dev/null
+++ test/Frontend/optimization-remark.c
@@ -0,0 +1,11 @@
+// This file tests all the optimization remarks from the optimizers that
+// support them. As new optimizations are modified to emit them, add
+// tests here.
+
+// RUN: %clang -c %s -Rpass=inline -O2 -o /dev/null 2> %t.err
+// RUN: FileCheck < %t.err %s --check-prefix=INLINE
+
+int foo(int x, int y) { return x + y; }
+int bar(int j) { return foo(j, j - 2); }
+
+// INLINE: remark: foo inlined into bar [-Rpass]
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits