[Mlir-commits] [mlir] 513ba61 - [mlir] Fully qualify generated C++ code in RewriterGen.cpp

Markus Böck llvmlistbot at llvm.org
Wed Feb 2 02:58:03 PST 2022


Author: Markus Böck
Date: 2022-02-02T11:57:57+01:00
New Revision: 513ba61ca1a2934fb0ec0483fd03302b3ab14f6b

URL: https://github.com/llvm/llvm-project/commit/513ba61ca1a2934fb0ec0483fd03302b3ab14f6b
DIFF: https://github.com/llvm/llvm-project/commit/513ba61ca1a2934fb0ec0483fd03302b3ab14f6b.diff

LOG: [mlir] Fully qualify generated C++ code in RewriterGen.cpp

By fully qualifying the use of any types and functions from the mlir namespace, users are not required to add using namespace mlir; into the C++ file including the Tablegen output.

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/OpBase.td
    mlir/test/mlir-tblgen/rewriter-static-matcher.td
    mlir/tools/mlir-tblgen/RewriterGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index f931c0b139b44..4d64af296d2fc 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -2798,7 +2798,8 @@ class NativeCodeCall<string expr, int returns = 1> {
 
 class NativeCodeCallVoid<string expr> : NativeCodeCall<expr, 0>;
 
-def ConstantLikeMatcher : NativeCodeCall<"success(matchPattern($_self->getResult(0), m_Constant(&$0)))">;
+def ConstantLikeMatcher : NativeCodeCall<"::mlir::success("
+    "::mlir::matchPattern($_self->getResult(0), ::mlir::m_Constant(&$0)))">;
 
 //===----------------------------------------------------------------------===//
 // Rewrite directives

diff  --git a/mlir/test/mlir-tblgen/rewriter-static-matcher.td b/mlir/test/mlir-tblgen/rewriter-static-matcher.td
index d77ac5af816c5..9445431dee999 100644
--- a/mlir/test/mlir-tblgen/rewriter-static-matcher.td
+++ b/mlir/test/mlir-tblgen/rewriter-static-matcher.td
@@ -42,13 +42,13 @@ def COp : NS_Op<"c_op", []> {
 // CHECK: static ::mlir::LogicalResult [[$ATTR_CONSTRAINT:__mlir_ods_local_attr_constraint.*]](
 // CHECK-NEXT: {{.*::mlir::Attribute attr}}
 // CHECK: static ::mlir::LogicalResult [[$DAG_MATCHER:static_dag_matcher.*]](
-// CHECK: if(failed([[$ATTR_CONSTRAINT]]
-// CHECK: if(failed([[$TYPE_CONSTRAINT]]
+// CHECK: if(::mlir::failed([[$ATTR_CONSTRAINT]]
+// CHECK: if(::mlir::failed([[$TYPE_CONSTRAINT]]
 
-// CHECK: if(failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
+// CHECK: if(::mlir::failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
 def : Pat<(AOp (BOp I32Attr:$attr, I32:$int)),
           (AOp $int)>;
 
-// CHECK: if(failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
+// CHECK: if(::mlir::failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
 def : Pat<(COp $_, (BOp I32Attr:$attr, I32:$int)),
           (COp $attr, $int)>;

diff  --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp
index 4fcc880464c98..38f76c0cd6f54 100644
--- a/mlir/tools/mlir-tblgen/RewriterGen.cpp
+++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp
@@ -392,7 +392,8 @@ void PatternEmitter::emitMatch(DagNode tree, StringRef name, int depth) {
 
 void PatternEmitter::emitStaticMatchCall(DagNode tree, StringRef opName) {
   std::string funcName = staticMatcherHelper.getMatcherName(tree);
-  os << formatv("if(failed({0}(rewriter, {1}, tblgen_ops", funcName, opName);
+  os << formatv("if(::mlir::failed({0}(rewriter, {1}, tblgen_ops", funcName,
+                opName);
 
   // TODO(chiahungduan): Add a lookupBoundSymbols() to do the subtree lookup in
   // one pass.
@@ -423,8 +424,8 @@ void PatternEmitter::emitStaticMatchCall(DagNode tree, StringRef opName) {
 void PatternEmitter::emitStaticVerifierCall(StringRef funcName,
                                             StringRef opName, StringRef arg,
                                             StringRef failureStr) {
-  os << formatv("if(failed({0}(rewriter, {1}, {2}, {3}))) {{\n", funcName,
-                opName, arg, failureStr);
+  os << formatv("if(::mlir::failed({0}(rewriter, {1}, {2}, {3}))) {{\n",
+                funcName, opName, arg, failureStr);
   os.scope().os << "return ::mlir::failure();\n";
   os << "}\n";
 }
@@ -463,13 +464,13 @@ void PatternEmitter::emitNativeCodeMatch(DagNode tree, StringRef opName,
       if (argTree.isEither())
         PrintFatalError(loc, "NativeCodeCall cannot have `either` operands");
 
-      os << "Value " << argName << ";\n";
+      os << "::mlir::Value " << argName << ";\n";
     } else {
       auto leaf = tree.getArgAsLeaf(i);
       if (leaf.isAttrMatcher() || leaf.isConstantAttr()) {
-        os << "Attribute " << argName << ";\n";
+        os << "::mlir::Attribute " << argName << ";\n";
       } else {
-        os << "Value " << argName << ";\n";
+        os << "::mlir::Value " << argName << ";\n";
       }
     }
 
@@ -490,8 +491,8 @@ void PatternEmitter::emitNativeCodeMatch(DagNode tree, StringRef opName,
       tgfmt(fmt, &fmtCtx.addSubst("_loc", locToUse).withSelf(opName.str()),
             static_cast<ArrayRef<std::string>>(capture)));
 
-  emitMatchCheck(opName, formatv("!failed({0})", nativeCodeCall),
-                 formatv("\"{0} return failure\"", nativeCodeCall));
+  emitMatchCheck(opName, formatv("!::mlir::failed({0})", nativeCodeCall),
+                 formatv("\"{0} return ::mlir::failure\"", nativeCodeCall));
 
   for (int i = 0, e = tree.getNumArgs() - tail.numDirectives; i != e; ++i) {
     auto name = tree.getArgName(i);
@@ -699,8 +700,9 @@ void PatternEmitter::emitEitherOperandMatch(DagNode tree, DagNode eitherArgTree,
   llvm::raw_string_ostream tblgenOps(codeBuffer);
 
   std::string lambda = formatv("eitherLambda{0}", depth);
-  os << formatv("auto {0} = [&](OperandRange v0, OperandRange v1) {{\n",
-                lambda);
+  os << formatv(
+      "auto {0} = [&](::mlir::OperandRange v0, ::mlir::OperandRange v1) {{\n",
+      lambda);
 
   os.indent();
 
@@ -744,11 +746,11 @@ void PatternEmitter::emitEitherOperandMatch(DagNode tree, DagNode eitherArgTree,
   os << formatv("auto eitherOperand1 = {0}.getODSOperands({1});\n", opName,
                 operandIndex - 1);
 
-  os << formatv("if(failed({0}(eitherOperand0, eitherOperand1)) && "
-                "failed({0}(eitherOperand1, "
+  os << formatv("if(::mlir::failed({0}(eitherOperand0, eitherOperand1)) && "
+                "::mlir::failed({0}(eitherOperand1, "
                 "eitherOperand0)))\n",
                 lambda);
-  os.indent() << "return failure();\n";
+  os.indent() << "return ::mlir::failure();\n";
 
   os.unindent().unindent() << "}\n";
 }
@@ -802,7 +804,7 @@ void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
       // through.
       if (!StringRef(matcher.getConditionTemplate()).contains("!$_self") &&
           StringRef(matcher.getConditionTemplate()).contains("$_self")) {
-        os << "if (!tblgen_attr) return failure();\n";
+        os << "if (!tblgen_attr) return ::mlir::failure();\n";
       }
     }
     emitStaticVerifierCall(


        


More information about the Mlir-commits mailing list