[llvm] [TableGen] Add a !listflatten operator to TableGen (PR #109346)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 22 10:49:42 PDT 2024


================
@@ -987,6 +987,29 @@ Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
       }
     }
     break;
+
+  case LISTFLATTEN:
+    if (ListInit *LHSList = dyn_cast<ListInit>(LHS)) {
+      ListRecTy *InnerListTy = dyn_cast<ListRecTy>(LHSList->getElementType());
+      // list of non-lists, !listflatten() is a NOP.
+      if (!InnerListTy)
+        return LHS;
+      std::vector<Init *> Flattened;
+      auto CanFlatten = [LHSList, &Flattened]() {
+        // Concatenate elements of all the inner lists.
+        for (Init *InnerInit : LHSList->getValues()) {
+          ListInit *InnerList = dyn_cast<ListInit>(InnerInit);
+          if (!InnerList)
+            return false;
+          for (Init *InnerElem : InnerList->getValues())
+            Flattened.push_back(InnerElem);
+        };
+        return true;
+      }();
----------------
arsenm wrote:

It would be less surprising to move the call down to if (CanFlatten()) than have auto as bool 

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


More information about the llvm-commits mailing list