[llvm] r205775 - Add -pass-remarks flag to 'opt'.

Diego Novillo dnovillo at google.com
Tue Apr 8 09:42:38 PDT 2014


Author: dnovillo
Date: Tue Apr  8 11:42:38 2014
New Revision: 205775

URL: http://llvm.org/viewvc/llvm-project?rev=205775&view=rev
Log:
Add -pass-remarks flag to 'opt'.

Summary:
This adds support in 'opt' to filter pass remarks emitted by
optimization passes. A new flag -pass-remarks specifies which
passes should emit a diagnostic when LLVMContext::emitOptimizationRemark
is invoked.

This will allow the front end to simply pass along the regular
expression from its own -Rpass flag when launching the backend.

Depends on D3227.

Reviewers: qcolombet

CC: llvm-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D3291

Added:
    llvm/trunk/test/Other/optimization-remarks-inline.ll
Modified:
    llvm/trunk/lib/IR/LLVMContext.cpp
    llvm/trunk/lib/IR/LLVMContextImpl.cpp
    llvm/trunk/lib/IR/LLVMContextImpl.h

Modified: llvm/trunk/lib/IR/LLVMContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContext.cpp?rev=205775&r1=205774&r2=205775&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContext.cpp (original)
+++ llvm/trunk/lib/IR/LLVMContext.cpp Tue Apr  8 11:42:38 2014
@@ -160,7 +160,8 @@ void LLVMContext::emitOptimizationRemark
                                          const Function &Fn,
                                          const DebugLoc &DLoc,
                                          const Twine &Msg) {
-  diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
+  if (pImpl->optimizationRemarksEnabledFor(PassName))
+    diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
 }
 
 //===----------------------------------------------------------------------===//

Modified: llvm/trunk/lib/IR/LLVMContextImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContextImpl.cpp?rev=205775&r1=205774&r2=205775&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContextImpl.cpp (original)
+++ llvm/trunk/lib/IR/LLVMContextImpl.cpp Tue Apr  8 11:42:38 2014
@@ -15,6 +15,8 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/IR/Attributes.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Regex.h"
 #include <algorithm>
 using namespace llvm;
 
@@ -43,6 +45,59 @@ LLVMContextImpl::LLVMContextImpl(LLVMCon
 }
 
 namespace {
+
+/// \brief Regular expression corresponding to the value given in the
+/// command line flag -pass-remarks. Passes whose name matches this
+/// regexp will emit a diagnostic when calling
+/// LLVMContext::emitOptimizationRemark.
+static Regex *OptimizationRemarkPattern = 0;
+
+/// \brief String to hold all the values passed via -pass-remarks. Every
+/// instance of -pass-remarks on the command line will be concatenated
+/// to this string. Values are stored inside braces and concatenated with
+/// the '|' operator. This implements the expected semantics that multiple
+/// -pass-remarks are additive.
+static std::string OptimizationRemarkExpr;
+
+struct PassRemarksOpt {
+  void operator=(const std::string &Val) const {
+    // Create a regexp object to match pass names for emitOptimizationRemark.
+    if (!Val.empty()) {
+      if (!OptimizationRemarkExpr.empty())
+        OptimizationRemarkExpr += "|";
+      OptimizationRemarkExpr += "(" + Val + ")";
+      delete OptimizationRemarkPattern;
+      OptimizationRemarkPattern = new Regex(OptimizationRemarkExpr);
+      std::string RegexError;
+      if (!OptimizationRemarkPattern->isValid(RegexError))
+        report_fatal_error("Invalid regular expression '" + Val +
+                               "' in -pass-remarks: " + RegexError,
+                           false);
+    }
+  };
+};
+
+static PassRemarksOpt PassRemarksOptLoc;
+
+// -pass-remarks
+//    Command line flag to enable LLVMContext::emitOptimizationRemark()
+//    and LLVMContext::emitOptimizationNote() calls.
+static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
+PassRemarks("pass-remarks", cl::value_desc("pattern"),
+            cl::desc("Enable optimization remarks from passes whose name match "
+                     "the given regular expression"),
+            cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
+            cl::ZeroOrMore);
+}
+
+bool
+LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
+  return OptimizationRemarkPattern &&
+         OptimizationRemarkPattern->match(PassName);
+}
+
+
+namespace {
 struct DropReferences {
   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
   // is a Constant*.

Modified: llvm/trunk/lib/IR/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContextImpl.h?rev=205775&r1=205774&r2=205775&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/IR/LLVMContextImpl.h Tue Apr  8 11:42:38 2014
@@ -368,6 +368,10 @@ public:
   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
   PrefixDataMapTy PrefixDataMap;
 
+  /// \brief Return true if the given pass name should emit optimization
+  /// remarks.
+  bool optimizationRemarksEnabledFor(const char *PassName) const;
+
   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
   

Added: llvm/trunk/test/Other/optimization-remarks-inline.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/optimization-remarks-inline.ll?rev=205775&view=auto
==============================================================================
--- llvm/trunk/test/Other/optimization-remarks-inline.ll (added)
+++ llvm/trunk/test/Other/optimization-remarks-inline.ll Tue Apr  8 11:42:38 2014
@@ -0,0 +1,33 @@
+; RUN: opt < %s -inline -pass-remarks='inline' -S 2>&1 | FileCheck %s
+; RUN: opt < %s -inline -pass-remarks='inl.*' -S 2>&1 | FileCheck %s
+; RUN: opt < %s -inline -pass-remarks='vector' -pass-remarks='inl' -S 2>&1 | FileCheck %s
+; RUN: opt < %s -inline -pass-remarks='vector' -S 2>&1 | FileCheck --check-prefix=REMARKS %s
+; RUN: opt < %s -inline -S 2>&1 | FileCheck --check-prefix=REMARKS %s
+; RUN: not opt < %s -pass-remarks='(' 2>&1 | FileCheck --check-prefix=BAD-REGEXP %s
+
+define i32 @foo(i32 %x, i32 %y) #0 {
+entry:
+  %x.addr = alloca i32, align 4
+  %y.addr = alloca i32, align 4
+  store i32 %x, i32* %x.addr, align 4
+  store i32 %y, i32* %y.addr, align 4
+  %0 = load i32* %x.addr, align 4
+  %1 = load i32* %y.addr, align 4
+  %add = add nsw i32 %0, %1
+  ret i32 %add
+}
+
+define i32 @bar(i32 %j) #0 {
+entry:
+  %j.addr = alloca i32, align 4
+  store i32 %j, i32* %j.addr, align 4
+  %0 = load i32* %j.addr, align 4
+  %1 = load i32* %j.addr, align 4
+  %sub = sub nsw i32 %1, 2
+  %call = call i32 @foo(i32 %0, i32 %sub)
+; CHECK: foo inlined into bar
+; REMARKS-NOT: foo inlined into bar
+  ret i32 %call
+}
+
+; BAD-REGEXP: Invalid regular expression '(' in -pass-remarks:





More information about the llvm-commits mailing list