Skip to content

Commit 513ba61

Browse files
committed
[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
1 parent e99abc5 commit 513ba61

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

mlir/include/mlir/IR/OpBase.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,8 @@ class NativeCodeCall<string expr, int returns = 1> {
27982798

27992799
class NativeCodeCallVoid<string expr> : NativeCodeCall<expr, 0>;
28002800

2801-
def ConstantLikeMatcher : NativeCodeCall<"success(matchPattern($_self->getResult(0), m_Constant(&$0)))">;
2801+
def ConstantLikeMatcher : NativeCodeCall<"::mlir::success("
2802+
"::mlir::matchPattern($_self->getResult(0), ::mlir::m_Constant(&$0)))">;
28022803

28032804
//===----------------------------------------------------------------------===//
28042805
// Rewrite directives

mlir/test/mlir-tblgen/rewriter-static-matcher.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def COp : NS_Op<"c_op", []> {
4242
// CHECK: static ::mlir::LogicalResult [[$ATTR_CONSTRAINT:__mlir_ods_local_attr_constraint.*]](
4343
// CHECK-NEXT: {{.*::mlir::Attribute attr}}
4444
// CHECK: static ::mlir::LogicalResult [[$DAG_MATCHER:static_dag_matcher.*]](
45-
// CHECK: if(failed([[$ATTR_CONSTRAINT]]
46-
// CHECK: if(failed([[$TYPE_CONSTRAINT]]
45+
// CHECK: if(::mlir::failed([[$ATTR_CONSTRAINT]]
46+
// CHECK: if(::mlir::failed([[$TYPE_CONSTRAINT]]
4747

48-
// CHECK: if(failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
48+
// CHECK: if(::mlir::failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
4949
def : Pat<(AOp (BOp I32Attr:$attr, I32:$int)),
5050
(AOp $int)>;
5151

52-
// CHECK: if(failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
52+
// CHECK: if(::mlir::failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
5353
def : Pat<(COp $_, (BOp I32Attr:$attr, I32:$int)),
5454
(COp $attr, $int)>;

mlir/tools/mlir-tblgen/RewriterGen.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ void PatternEmitter::emitMatch(DagNode tree, StringRef name, int depth) {
392392

393393
void PatternEmitter::emitStaticMatchCall(DagNode tree, StringRef opName) {
394394
std::string funcName = staticMatcherHelper.getMatcherName(tree);
395-
os << formatv("if(failed({0}(rewriter, {1}, tblgen_ops", funcName, opName);
395+
os << formatv("if(::mlir::failed({0}(rewriter, {1}, tblgen_ops", funcName,
396+
opName);
396397

397398
// TODO(chiahungduan): Add a lookupBoundSymbols() to do the subtree lookup in
398399
// one pass.
@@ -423,8 +424,8 @@ void PatternEmitter::emitStaticMatchCall(DagNode tree, StringRef opName) {
423424
void PatternEmitter::emitStaticVerifierCall(StringRef funcName,
424425
StringRef opName, StringRef arg,
425426
StringRef failureStr) {
426-
os << formatv("if(failed({0}(rewriter, {1}, {2}, {3}))) {{\n", funcName,
427-
opName, arg, failureStr);
427+
os << formatv("if(::mlir::failed({0}(rewriter, {1}, {2}, {3}))) {{\n",
428+
funcName, opName, arg, failureStr);
428429
os.scope().os << "return ::mlir::failure();\n";
429430
os << "}\n";
430431
}
@@ -463,13 +464,13 @@ void PatternEmitter::emitNativeCodeMatch(DagNode tree, StringRef opName,
463464
if (argTree.isEither())
464465
PrintFatalError(loc, "NativeCodeCall cannot have `either` operands");
465466

466-
os << "Value " << argName << ";\n";
467+
os << "::mlir::Value " << argName << ";\n";
467468
} else {
468469
auto leaf = tree.getArgAsLeaf(i);
469470
if (leaf.isAttrMatcher() || leaf.isConstantAttr()) {
470-
os << "Attribute " << argName << ";\n";
471+
os << "::mlir::Attribute " << argName << ";\n";
471472
} else {
472-
os << "Value " << argName << ";\n";
473+
os << "::mlir::Value " << argName << ";\n";
473474
}
474475
}
475476

@@ -490,8 +491,8 @@ void PatternEmitter::emitNativeCodeMatch(DagNode tree, StringRef opName,
490491
tgfmt(fmt, &fmtCtx.addSubst("_loc", locToUse).withSelf(opName.str()),
491492
static_cast<ArrayRef<std::string>>(capture)));
492493

493-
emitMatchCheck(opName, formatv("!failed({0})", nativeCodeCall),
494-
formatv("\"{0} return failure\"", nativeCodeCall));
494+
emitMatchCheck(opName, formatv("!::mlir::failed({0})", nativeCodeCall),
495+
formatv("\"{0} return ::mlir::failure\"", nativeCodeCall));
495496

496497
for (int i = 0, e = tree.getNumArgs() - tail.numDirectives; i != e; ++i) {
497498
auto name = tree.getArgName(i);
@@ -699,8 +700,9 @@ void PatternEmitter::emitEitherOperandMatch(DagNode tree, DagNode eitherArgTree,
699700
llvm::raw_string_ostream tblgenOps(codeBuffer);
700701

701702
std::string lambda = formatv("eitherLambda{0}", depth);
702-
os << formatv("auto {0} = [&](OperandRange v0, OperandRange v1) {{\n",
703-
lambda);
703+
os << formatv(
704+
"auto {0} = [&](::mlir::OperandRange v0, ::mlir::OperandRange v1) {{\n",
705+
lambda);
704706

705707
os.indent();
706708

@@ -744,11 +746,11 @@ void PatternEmitter::emitEitherOperandMatch(DagNode tree, DagNode eitherArgTree,
744746
os << formatv("auto eitherOperand1 = {0}.getODSOperands({1});\n", opName,
745747
operandIndex - 1);
746748

747-
os << formatv("if(failed({0}(eitherOperand0, eitherOperand1)) && "
748-
"failed({0}(eitherOperand1, "
749+
os << formatv("if(::mlir::failed({0}(eitherOperand0, eitherOperand1)) && "
750+
"::mlir::failed({0}(eitherOperand1, "
749751
"eitherOperand0)))\n",
750752
lambda);
751-
os.indent() << "return failure();\n";
753+
os.indent() << "return ::mlir::failure();\n";
752754

753755
os.unindent().unindent() << "}\n";
754756
}
@@ -802,7 +804,7 @@ void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
802804
// through.
803805
if (!StringRef(matcher.getConditionTemplate()).contains("!$_self") &&
804806
StringRef(matcher.getConditionTemplate()).contains("$_self")) {
805-
os << "if (!tblgen_attr) return failure();\n";
807+
os << "if (!tblgen_attr) return ::mlir::failure();\n";
806808
}
807809
}
808810
emitStaticVerifierCall(

0 commit comments

Comments
 (0)