Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 541e8e7

Browse files
authored
[mlir] Execute same operand name constraints before user constraints. (#162526)
For a pattern like this: Pat<(MyOp $x, $x), (...), [(MyCheck $x)]>; The old implementation generates: Pat<(MyOp $x0, $x1), (...), [(MyCheck $x0), ($x0 == $x1)]>; This is not very straightforward, because the $x name appears in the source pattern; it's attempting to assume equality check will be performed as part of the source pattern matching. This commit moves the equality checks before the other constraints, i.e.: Pat<(MyOp $x0, $x1), (...), [($x0 == $x1), (MyCheck $x0)]>;
1 parent 2a33654 commit 541e8e7

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

mlir/tools/mlir-tblgen/RewriterGen.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,32 @@ void PatternEmitter::emitMatchLogic(DagNode tree, StringRef opName) {
10241024
int depth = 0;
10251025
emitMatch(tree, opName, depth);
10261026

1027+
// Some of the operands could be bound to the same symbol name, we need
1028+
// to enforce equality constraint on those.
1029+
// This has to happen before user provided constraints, which may assume the
1030+
// same name checks are already performed, since in the pattern source code
1031+
// the user provided constraints appear later.
1032+
// TODO: we should be able to emit equality checks early
1033+
// and short circuit unnecessary work if vars are not equal.
1034+
for (auto symbolInfoIt = symbolInfoMap.begin();
1035+
symbolInfoIt != symbolInfoMap.end();) {
1036+
auto range = symbolInfoMap.getRangeOfEqualElements(symbolInfoIt->first);
1037+
auto startRange = range.first;
1038+
auto endRange = range.second;
1039+
1040+
auto firstOperand = symbolInfoIt->second.getVarName(symbolInfoIt->first);
1041+
for (++startRange; startRange != endRange; ++startRange) {
1042+
auto secondOperand = startRange->second.getVarName(symbolInfoIt->first);
1043+
emitMatchCheck(
1044+
opName,
1045+
formatv("*{0}.begin() == *{1}.begin()", firstOperand, secondOperand),
1046+
formatv("\"Operands '{0}' and '{1}' must be equal\"", firstOperand,
1047+
secondOperand));
1048+
}
1049+
1050+
symbolInfoIt = endRange;
1051+
}
1052+
10271053
for (auto &appliedConstraint : pattern.getConstraints()) {
10281054
auto &constraint = appliedConstraint.constraint;
10291055
auto &entities = appliedConstraint.entities;
@@ -1068,29 +1094,6 @@ void PatternEmitter::emitMatchLogic(DagNode tree, StringRef opName) {
10681094
}
10691095
}
10701096

1071-
// Some of the operands could be bound to the same symbol name, we need
1072-
// to enforce equality constraint on those.
1073-
// TODO: we should be able to emit equality checks early
1074-
// and short circuit unnecessary work if vars are not equal.
1075-
for (auto symbolInfoIt = symbolInfoMap.begin();
1076-
symbolInfoIt != symbolInfoMap.end();) {
1077-
auto range = symbolInfoMap.getRangeOfEqualElements(symbolInfoIt->first);
1078-
auto startRange = range.first;
1079-
auto endRange = range.second;
1080-
1081-
auto firstOperand = symbolInfoIt->second.getVarName(symbolInfoIt->first);
1082-
for (++startRange; startRange != endRange; ++startRange) {
1083-
auto secondOperand = startRange->second.getVarName(symbolInfoIt->first);
1084-
emitMatchCheck(
1085-
opName,
1086-
formatv("*{0}.begin() == *{1}.begin()", firstOperand, secondOperand),
1087-
formatv("\"Operands '{0}' and '{1}' must be equal\"", firstOperand,
1088-
secondOperand));
1089-
}
1090-
1091-
symbolInfoIt = endRange;
1092-
}
1093-
10941097
LLVM_DEBUG(llvm::dbgs() << "--- done emitting match logic ---\n");
10951098
}
10961099

0 commit comments

Comments
 (0)