[llvm] [TableGen] Fix operand constraint checking problem. (PR #85859)

Jason Eckhardt via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 19 13:12:12 PDT 2024


https://github.com/nvjle created https://github.com/llvm/llvm-project/pull/85859

Currently operand constraint checks on "$dest = $src" are inadvertently accepting any token that contains "=". This has surprising results, e.g, "$dest != $src" is accepted as a constraint but then treated as "=".

This patch ensures that only exactly the token "=" is accepted.

>From df6882388a0757fd6cd219cf069cf7b99514b68d Mon Sep 17 00:00:00 2001
From: Jason Eckhardt <jeckhardt at nvidia.com>
Date: Tue, 19 Mar 2024 14:58:40 -0500
Subject: [PATCH] [TableGen] Fix operand constraint checking problem.

Currently operand constraint checks on "$dest = $src" are inadvertently
accepting any token that contains "=". This has surprising results, e.g,
"$dest != $src" is accepted as a constraint but then treated as "=".

This patch ensures that only exactly the token "=" is accepted.
---
 llvm/test/TableGen/ConstraintChecking3.td  | 2 +-
 llvm/test/TableGen/ConstraintChecking8.td  | 8 ++++++++
 llvm/test/TableGen/ConstraintChecking9.td  | 8 ++++++++
 llvm/utils/TableGen/CodeGenInstruction.cpp | 3 ++-
 4 files changed, 19 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/TableGen/ConstraintChecking8.td
 create mode 100644 llvm/test/TableGen/ConstraintChecking9.td

diff --git a/llvm/test/TableGen/ConstraintChecking3.td b/llvm/test/TableGen/ConstraintChecking3.td
index 2d5fe6b7ef9699..886e6d52a039fe 100644
--- a/llvm/test/TableGen/ConstraintChecking3.td
+++ b/llvm/test/TableGen/ConstraintChecking3.td
@@ -4,5 +4,5 @@ include "ConstraintChecking.inc"
 
 // (This is illegal because the '=' has to be surrounded by whitespace)
 
-// CHECK: [[FILE]]:[[@LINE+1]]:5: error: Illegal format for tied-to constraint in 'Foo'
+// CHECK: [[FILE]]:[[@LINE+1]]:5: error: Unrecognized constraint '$dest1=$dest2' in 'Foo'
 def Foo : TestInstructionWithConstraints<"$dest1=$dest2">;
diff --git a/llvm/test/TableGen/ConstraintChecking8.td b/llvm/test/TableGen/ConstraintChecking8.td
new file mode 100644
index 00000000000000..52ecd23bf41044
--- /dev/null
+++ b/llvm/test/TableGen/ConstraintChecking8.td
@@ -0,0 +1,8 @@
+// RUN: not llvm-tblgen -gen-asm-writer -I %p -I %p/../../include %s 2>&1 | FileCheck %s -DFILE=%s
+
+include "ConstraintChecking.inc"
+
+// Make sure exactly the token "=" appears.
+
+// CHECK: [[FILE]]:[[@LINE+1]]:5: error: Unrecognized constraint '$dest1 != $src2' in 'Foo'
+def Foo : TestInstructionWithConstraints<"$dest1 != $src2">;
diff --git a/llvm/test/TableGen/ConstraintChecking9.td b/llvm/test/TableGen/ConstraintChecking9.td
new file mode 100644
index 00000000000000..afd6706fdf4900
--- /dev/null
+++ b/llvm/test/TableGen/ConstraintChecking9.td
@@ -0,0 +1,8 @@
+// RUN: not llvm-tblgen -gen-asm-writer -I %p -I %p/../../include %s 2>&1 | FileCheck %s -DFILE=%s
+
+include "ConstraintChecking.inc"
+
+// Make sure exactly the token "=" appears.
+
+// CHECK: [[FILE]]:[[@LINE+1]]:5: error: Unrecognized constraint '$dest1 == $src2' in 'Foo'
+def Foo : TestInstructionWithConstraints<"$dest1 == $src2">;
diff --git a/llvm/utils/TableGen/CodeGenInstruction.cpp b/llvm/utils/TableGen/CodeGenInstruction.cpp
index b00b95da5fc276..44b6f2dded681a 100644
--- a/llvm/utils/TableGen/CodeGenInstruction.cpp
+++ b/llvm/utils/TableGen/CodeGenInstruction.cpp
@@ -325,7 +325,8 @@ static void ParseConstraint(StringRef CStr, CGIOperandList &Ops, Record *Rec) {
 
   // Only other constraint is "TIED_TO" for now.
   StringRef::size_type pos = CStr.find_first_of('=');
-  if (pos == StringRef::npos)
+  if (pos == StringRef::npos || CStr.find_first_of(" \t", pos) != (pos + 1) ||
+      CStr.find_last_of(" \t", pos) != (pos - 1))
     PrintFatalError(Rec->getLoc(), "Unrecognized constraint '" + CStr +
                                        "' in '" + Rec->getName() + "'");
   start = CStr.find_first_not_of(" \t");



More information about the llvm-commits mailing list