[llvm] [ConstraintSystem] Build constraint rows in sparse form (NFC). (PR #215735)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 13:11:25 PDT 2026


================
@@ -29,26 +30,39 @@ class ConstraintSystem {
         : Coefficient(Coefficient), Id(Id) {}
   };
 
-  static int64_t getConstPart(const Entry &E) {
-    if (E.Id == 0)
-      return E.Coefficient;
-    return 0;
+  /// A single constraint, storing only entries with non-zero coefficients,
+  /// ordered by increasing variable index.
+  using RowTy = SmallVector<Entry, 8>;
+
+private:
+  /// Returns the coefficient of the variable with index \p Id in \p R, which
+  /// must be the last entry of \p R if present, and 0 otherwise.
+  static int64_t getLastCoefficient(ArrayRef<Entry> R, uint16_t Id) {
+    if (R.empty() || R.back().Id != Id)
+      return 0;
+    return R.back().Coefficient;
   }
 
-  static int64_t getLastCoefficient(ArrayRef<Entry> Row, uint16_t Id) {
-    if (Row.empty())
+  /// Returns true if \p R does not have an entry for any variable, i.e. it is
+  /// of the form 'c >= 0'.
+  static bool isConstantOnly(ArrayRef<Entry> R) {
+    return R.empty() || (R.size() == 1 && R.front().Id == 0);
+  }
+
+  /// Returns the constant part of \p R, which is 0 if \p R does not have an
+  /// entry for it.
+  static int64_t getConstant(ArrayRef<Entry> R) {
+    if (R.empty() || R.front().Id != 0)
       return 0;
-    if (Row.back().Id == Id)
-      return Row.back().Coefficient;
-    return 0;
+    return R.front().Coefficient;
   }
 
   size_t NumVariables = 0;
----------------
fhahn wrote:

Yes it was a bit confusing, and there was not a clear separation. I tried to clarify and make sure they only the number of variables is stored.

Note that the rows passed to negate need the constant part, but I added some extra asserts.

https://github.com/llvm/llvm-project/pull/215735


More information about the llvm-commits mailing list