Skip to content

Commit 7a9e3ef

Browse files
committed
[mlir] Fix crash in RewriterGen when a TypeConstraint is not given an argument
The code assumes that a TypeConstraint in the additional constraints list specifies precisely one argument. If the user were to not specify any, it'd result in a crash. If given more than one, the additional ones were ignored. This patch fixes the crash and disallows user errors by adding a check that a single argument is supplied to the TypeConstraint Differential Revision: https://reviews.llvm.org/D118763
1 parent 67a9f82 commit 7a9e3ef

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mlir/test/mlir-tblgen/rewriter-errors.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// RUN: not mlir-tblgen -gen-rewriters -I %S/../../include -DERROR3 %s 2>&1 | FileCheck --check-prefix=ERROR3 %s
44
// RUN: not mlir-tblgen -gen-rewriters -I %S/../../include -DERROR4 %s 2>&1 | FileCheck --check-prefix=ERROR4 %s
55
// RUN: not mlir-tblgen -gen-rewriters -I %S/../../include -DERROR5 %s 2>&1 | FileCheck --check-prefix=ERROR5 %s
6+
// RUN: not mlir-tblgen -gen-rewriters -I %S/../../include -DERROR6 %s 2>&1 | FileCheck --check-prefix=ERROR6 %s
7+
// RUN: not mlir-tblgen -gen-rewriters -I %S/../../include -DERROR7 %s 2>&1 | FileCheck --check-prefix=ERROR7 %s
68

79
include "mlir/IR/OpBase.td"
810

@@ -49,3 +51,15 @@ def : Pat<(OpB $val, AnyI32Attr:$attr), (OpA (OpA $val, $val, (returnType (OpA $
4951
// ERROR5: [[@LINE+1]]:1: error: Cannot specify explicit return types in an op
5052
def : Pat<(OpB $val, AnyI32Attr:$attr), (OpA $val, $val, (returnType "someType()"))>;
5153
#endif
54+
55+
#ifdef ERROR6
56+
// Check that type constraint has one argument
57+
// ERROR6: [[@LINE+1]]:1: error: type constraint requires exactly one argument
58+
def : Pat<(OpB:$result $val, $attr), (OpA $val, $val), [(AnyInteger:$result)]>;
59+
#endif
60+
61+
#ifdef ERROR7
62+
// Check that type constraint has one argument
63+
// ERROR7: [[@LINE+1]]:1: error: type constraint requires exactly one argument
64+
def : Pat<(OpB:$opB $val, $attr), (OpA $val, $val), [(AnyInteger $opB, $val)]>;
65+
#endif

mlir/tools/mlir-tblgen/RewriterGen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,9 @@ void PatternEmitter::emitMatchLogic(DagNode tree, StringRef opName) {
853853

854854
auto condition = constraint.getConditionTemplate();
855855
if (isa<TypeConstraint>(constraint)) {
856+
if (entities.size() != 1)
857+
PrintFatalError(loc, "type constraint requires exactly one argument");
858+
856859
auto self = formatv("({0}.getType())",
857860
symbolInfoMap.getValueAndRangeUse(entities.front()));
858861
emitMatchCheck(

0 commit comments

Comments
 (0)