Skip to content

Commit

Permalink
Fix argument count check for ops with only a variadic argument list.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Thomas Symalla authored and tsymalla-AMD committed May 23, 2024
1 parent 7377ed4 commit ae1b86b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
19 changes: 16 additions & 3 deletions lib/TableGen/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,17 @@ void Operation::emitVerifierMethod(llvm::raw_ostream &out,
fmt.withContext(symbols.chooseName("context"));
fmt.addSubst("_errs", symbols.chooseName("errs"));

bool emitArgCountVerifier = true;
if (m_hasVariadicArgument) {
fmt.addSubst("_comparator", "<");
fmt.addSubst("_at_least", "at least ");
if (getNumFullArguments() == 0) {
// If the only argument in an operation is a variadic argument list,
// getNumFullArguments() will return zero. Thus, prevent us from emitting
// arg_size() < 0 verifier checks.
emitArgCountVerifier = false;
} else {
fmt.addSubst("_comparator", "<");
fmt.addSubst("_at_least", "at least ");
}
} else {
fmt.addSubst("_comparator", "!=");
fmt.addSubst("_at_least", "");
Expand All @@ -477,14 +485,19 @@ void Operation::emitVerifierMethod(llvm::raw_ostream &out,
(void)$_context;
using ::llvm_dialects::printable;
)",
&fmt);

if (emitArgCountVerifier) {
out << tgfmt(R"(
if (arg_size() $_comparator $0) {
$_errs << " wrong number of arguments: " << arg_size()
<< ", expected $_at_least$0\n";
return false;
}
)",
&fmt, getNumFullArguments());
&fmt, getNumFullArguments());
}

Assignment assignment;
Evaluator eval(symbols, assignment, m_system, out, fmt);
Expand Down
44 changes: 19 additions & 25 deletions test/example/generated/ExampleDialect.cpp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ rhs,
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -487,7 +487,7 @@ rhs
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 2) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 2\n";
Expand Down Expand Up @@ -582,7 +582,7 @@ index
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 2) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 2\n";
Expand Down Expand Up @@ -685,7 +685,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -849,7 +849,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 0\n";
Expand Down Expand Up @@ -917,7 +917,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1015,7 +1015,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1184,7 +1184,7 @@ index
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -1302,7 +1302,7 @@ instName_0
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 2) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 2\n";
Expand Down Expand Up @@ -1385,7 +1385,7 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1461,12 +1461,6 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() < 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected at least 0\n";
return false;
}
::llvm::Type * const resultType = getResult()->getType();
(void)resultType;

Expand Down Expand Up @@ -1531,7 +1525,7 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 0\n";
Expand Down Expand Up @@ -1588,7 +1582,7 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 0\n";
Expand Down Expand Up @@ -1645,7 +1639,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1708,7 +1702,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1787,7 +1781,7 @@ initial
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -1879,7 +1873,7 @@ initial
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -1971,7 +1965,7 @@ initial
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -2058,7 +2052,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -2123,7 +2117,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() < 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected at least 1\n";
Expand Down

0 comments on commit ae1b86b

Please sign in to comment.