Skip to content

Commit ae1b86b

Browse files
Thomas Symallatsymalla-AMD
authored andcommitted
Fix argument count check for ops with only a variadic argument list.
We don't count variadic arguments in the `getNumFullArguments()` method since they can be zero. Not properly checking the return value of this function can however lead to a `arg_size() < 0` check in the generated verifier method. This happens if we only have a single variadic argument list argument.
1 parent 7377ed4 commit ae1b86b

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

lib/TableGen/Operations.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,17 @@ void Operation::emitVerifierMethod(llvm::raw_ostream &out,
463463
fmt.withContext(symbols.chooseName("context"));
464464
fmt.addSubst("_errs", symbols.chooseName("errs"));
465465

466+
bool emitArgCountVerifier = true;
466467
if (m_hasVariadicArgument) {
467-
fmt.addSubst("_comparator", "<");
468-
fmt.addSubst("_at_least", "at least ");
468+
if (getNumFullArguments() == 0) {
469+
// If the only argument in an operation is a variadic argument list,
470+
// getNumFullArguments() will return zero. Thus, prevent us from emitting
471+
// arg_size() < 0 verifier checks.
472+
emitArgCountVerifier = false;
473+
} else {
474+
fmt.addSubst("_comparator", "<");
475+
fmt.addSubst("_at_least", "at least ");
476+
}
469477
} else {
470478
fmt.addSubst("_comparator", "!=");
471479
fmt.addSubst("_at_least", "");
@@ -477,14 +485,19 @@ void Operation::emitVerifierMethod(llvm::raw_ostream &out,
477485
(void)$_context;
478486
479487
using ::llvm_dialects::printable;
488+
)",
489+
&fmt);
480490

491+
if (emitArgCountVerifier) {
492+
out << tgfmt(R"(
481493
if (arg_size() $_comparator $0) {
482494
$_errs << " wrong number of arguments: " << arg_size()
483495
<< ", expected $_at_least$0\n";
484496
return false;
485497
}
486498
)",
487-
&fmt, getNumFullArguments());
499+
&fmt, getNumFullArguments());
500+
}
488501

489502
Assignment assignment;
490503
Evaluator eval(symbols, assignment, m_system, out, fmt);

test/example/generated/ExampleDialect.cpp.inc

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ rhs,
368368
(void)context;
369369

370370
using ::llvm_dialects::printable;
371-
371+
372372
if (arg_size() != 3) {
373373
errs << " wrong number of arguments: " << arg_size()
374374
<< ", expected 3\n";
@@ -487,7 +487,7 @@ rhs
487487
(void)context;
488488

489489
using ::llvm_dialects::printable;
490-
490+
491491
if (arg_size() != 2) {
492492
errs << " wrong number of arguments: " << arg_size()
493493
<< ", expected 2\n";
@@ -582,7 +582,7 @@ index
582582
(void)context;
583583

584584
using ::llvm_dialects::printable;
585-
585+
586586
if (arg_size() != 2) {
587587
errs << " wrong number of arguments: " << arg_size()
588588
<< ", expected 2\n";
@@ -685,7 +685,7 @@ source
685685
(void)context;
686686

687687
using ::llvm_dialects::printable;
688-
688+
689689
if (arg_size() != 1) {
690690
errs << " wrong number of arguments: " << arg_size()
691691
<< ", expected 1\n";
@@ -849,7 +849,7 @@ source
849849
(void)context;
850850

851851
using ::llvm_dialects::printable;
852-
852+
853853
if (arg_size() != 0) {
854854
errs << " wrong number of arguments: " << arg_size()
855855
<< ", expected 0\n";
@@ -917,7 +917,7 @@ source
917917
(void)context;
918918

919919
using ::llvm_dialects::printable;
920-
920+
921921
if (arg_size() != 1) {
922922
errs << " wrong number of arguments: " << arg_size()
923923
<< ", expected 1\n";
@@ -1015,7 +1015,7 @@ source
10151015
(void)context;
10161016

10171017
using ::llvm_dialects::printable;
1018-
1018+
10191019
if (arg_size() != 1) {
10201020
errs << " wrong number of arguments: " << arg_size()
10211021
<< ", expected 1\n";
@@ -1184,7 +1184,7 @@ index
11841184
(void)context;
11851185

11861186
using ::llvm_dialects::printable;
1187-
1187+
11881188
if (arg_size() != 3) {
11891189
errs << " wrong number of arguments: " << arg_size()
11901190
<< ", expected 3\n";
@@ -1302,7 +1302,7 @@ instName_0
13021302
(void)context;
13031303

13041304
using ::llvm_dialects::printable;
1305-
1305+
13061306
if (arg_size() != 2) {
13071307
errs << " wrong number of arguments: " << arg_size()
13081308
<< ", expected 2\n";
@@ -1385,7 +1385,7 @@ instName
13851385
(void)context;
13861386

13871387
using ::llvm_dialects::printable;
1388-
1388+
13891389
if (arg_size() != 1) {
13901390
errs << " wrong number of arguments: " << arg_size()
13911391
<< ", expected 1\n";
@@ -1461,12 +1461,6 @@ instName
14611461
(void)context;
14621462

14631463
using ::llvm_dialects::printable;
1464-
1465-
if (arg_size() < 0) {
1466-
errs << " wrong number of arguments: " << arg_size()
1467-
<< ", expected at least 0\n";
1468-
return false;
1469-
}
14701464
::llvm::Type * const resultType = getResult()->getType();
14711465
(void)resultType;
14721466

@@ -1531,7 +1525,7 @@ instName
15311525
(void)context;
15321526

15331527
using ::llvm_dialects::printable;
1534-
1528+
15351529
if (arg_size() != 0) {
15361530
errs << " wrong number of arguments: " << arg_size()
15371531
<< ", expected 0\n";
@@ -1588,7 +1582,7 @@ instName
15881582
(void)context;
15891583

15901584
using ::llvm_dialects::printable;
1591-
1585+
15921586
if (arg_size() != 0) {
15931587
errs << " wrong number of arguments: " << arg_size()
15941588
<< ", expected 0\n";
@@ -1645,7 +1639,7 @@ data
16451639
(void)context;
16461640

16471641
using ::llvm_dialects::printable;
1648-
1642+
16491643
if (arg_size() != 1) {
16501644
errs << " wrong number of arguments: " << arg_size()
16511645
<< ", expected 1\n";
@@ -1708,7 +1702,7 @@ data
17081702
(void)context;
17091703

17101704
using ::llvm_dialects::printable;
1711-
1705+
17121706
if (arg_size() != 1) {
17131707
errs << " wrong number of arguments: " << arg_size()
17141708
<< ", expected 1\n";
@@ -1787,7 +1781,7 @@ initial
17871781
(void)context;
17881782

17891783
using ::llvm_dialects::printable;
1790-
1784+
17911785
if (arg_size() != 3) {
17921786
errs << " wrong number of arguments: " << arg_size()
17931787
<< ", expected 3\n";
@@ -1879,7 +1873,7 @@ initial
18791873
(void)context;
18801874

18811875
using ::llvm_dialects::printable;
1882-
1876+
18831877
if (arg_size() != 3) {
18841878
errs << " wrong number of arguments: " << arg_size()
18851879
<< ", expected 3\n";
@@ -1971,7 +1965,7 @@ initial
19711965
(void)context;
19721966

19731967
using ::llvm_dialects::printable;
1974-
1968+
19751969
if (arg_size() != 3) {
19761970
errs << " wrong number of arguments: " << arg_size()
19771971
<< ", expected 3\n";
@@ -2058,7 +2052,7 @@ data
20582052
(void)context;
20592053

20602054
using ::llvm_dialects::printable;
2061-
2055+
20622056
if (arg_size() != 1) {
20632057
errs << " wrong number of arguments: " << arg_size()
20642058
<< ", expected 1\n";
@@ -2123,7 +2117,7 @@ data
21232117
(void)context;
21242118

21252119
using ::llvm_dialects::printable;
2126-
2120+
21272121
if (arg_size() < 1) {
21282122
errs << " wrong number of arguments: " << arg_size()
21292123
<< ", expected at least 1\n";

0 commit comments

Comments
 (0)