[llvm] [TableGen] Short-circuit !cond operator (PR #208942)

Kevin Bravo via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 11 12:39:00 PDT 2026


https://github.com/kbrav created https://github.com/llvm/llvm-project/pull/208942

Fixes #163213 

TableGen `!cond` operator currently resolves all subsequent conditions and values even if it already found a condition that resolves to `true`. This change short-circuits on the first `true` resolution found iterating from left to right.  It folds a new `CondOpInit` with only the conditions and values that have already been resolved, including the current condition and value.

>From 305f9ebe1d5761cedb880e8687c9cf0bbfc859be Mon Sep 17 00:00:00 2001
From: kbrav <kbravjr at gmail.com>
Date: Sat, 11 Jul 2026 13:37:00 -0400
Subject: [PATCH] [TableGen] Short-circuit !cond operator

---
 llvm/lib/TableGen/Record.cpp     | 11 +++++++++++
 llvm/test/TableGen/true-false.td | 13 +++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index e9d7febf12272..af076b5ae9475 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -2735,6 +2735,17 @@ const Init *CondOpInit::resolveReferences(Resolver &R) const {
     NewConds.push_back(NewCond);
     Changed |= NewCond != Cond;
 
+    // Short-circuit if this cond is true.
+    if (auto *NewCondVal = dyn_cast_or_null<IntInit>(
+            NewCond->convertInitializerTo(IntRecTy::get(getRecordKeeper())))) {
+      if (NewCondVal->getValue()) {
+        NewVals.push_back(Val->resolveReferences(R));
+        Changed = 1;
+        // Don't push the rest of the conds and values.
+        break;
+      }
+    }
+
     const Init *NewVal = Val->resolveReferences(R);
     NewVals.push_back(NewVal);
     Changed |= NewVal != Val;
diff --git a/llvm/test/TableGen/true-false.td b/llvm/test/TableGen/true-false.td
index 5fa5702314489..f4110fab5054b 100644
--- a/llvm/test/TableGen/true-false.td
+++ b/llvm/test/TableGen/true-false.td
@@ -67,13 +67,15 @@ def rec7 {
   bits<3> flags = { true, false, true };
 }
 
-// `!and` and `!or` should be short-circuited such that any of the `!head` or
-// `!tail` on empty list below will never be evaluated.
+// `!and`, `!or`, and '!cond' should be short-circuited such that any of the
+// `!head` or `!tail` on empty list below will never be evaluated.
 // CHECK: def rec8
 // CHECK:   bit v = 0;
 // CHECK:   int v2 = -1;
 // CHECK:   list<int> newSeq = [];
 // CHECK:   list<int> newSeq2 = [];
+// CHECK:   C = 0;
+// CHECK:   D = 1;
 
 class Foo <list<int> seq = []> {
   bit v = !and(false, !head(seq));
@@ -82,6 +84,13 @@ class Foo <list<int> seq = []> {
   bit unresolved = !ne(!find(NAME, "BAR"), -1);
   list<int> newSeq  = !if(!and(false, unresolved), !tail(seq), seq);
   list<int> newSeq2 = !if(!or(-1, unresolved), seq, !tail(seq));
+
+  int C = !cond(!eq(!size(seq), 0): 0,
+                !eq(!size(!tail(seq)), 1) : 1,
+                !eq(!size(!tail(seq)), 2): 2);
+  int D = !cond(!eq(!size(seq), 1): 0,
+                !eq(!size(seq), 0): 1,
+                !eq(!size(!tail(seq)), 2): 2);
 }
 
 def rec8 : Foo<>;



More information about the llvm-commits mailing list