[llvm] [SandboxVec] Add pass to create Regions from metadata. Generalize SandboxVec pass pipelines. (PR #112288)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 14 18:07:09 PDT 2024


================
@@ -50,40 +50,130 @@ class PassManager : public ParentPass {
   }
 
   using CreatePassFunc =
-      std::function<std::unique_ptr<ContainedPass>(StringRef)>;
+      std::function<std::unique_ptr<ContainedPass>(StringRef, StringRef)>;
 
   /// Parses \p Pipeline as a comma-separated sequence of pass names and sets
   /// the pass pipeline, using \p CreatePass to instantiate passes by name.
   ///
-  /// After calling this function, the PassManager contains only the specified
-  /// pipeline, any previously added passes are cleared.
+  /// Passes can have arguments, for example:
+  ///   "pass1<arg1,arg2>,pass2,pass3<arg3,arg4>"
+  ///
+  /// The arguments between angle brackets are treated as a mostly opaque string
+  /// and each pass is responsible for parsing its arguments. The exception to
+  /// this are nested angle brackets, which must match pair-wise to allow
+  /// arguments to contain nested pipelines, like:
+  ///
+  ///   "pass1<subpass1,subpass2<arg1,arg2>,subpass3>"
+  ///
+  /// An empty args string is treated the same as no args, so "pass" and
+  /// "pass<>" are equivalent.
   void setPassPipeline(StringRef Pipeline, CreatePassFunc CreatePass) {
     static constexpr const char EndToken = '\0';
+    static constexpr const char BeginArgsToken = '<';
+    static constexpr const char EndArgsToken = '>';
     static constexpr const char PassDelimToken = ',';
 
     assert(Passes.empty() &&
            "setPassPipeline called on a non-empty sandboxir::PassManager");
-    // Add EndToken to the end to ease parsing.
-    std::string PipelineStr = std::string(Pipeline) + EndToken;
-    int FlagBeginIdx = 0;
 
-    for (auto [Idx, C] : enumerate(PipelineStr)) {
-      // Keep moving Idx until we find the end of the pass name.
-      bool FoundDelim = C == EndToken || C == PassDelimToken;
-      if (!FoundDelim)
-        continue;
-      unsigned Sz = Idx - FlagBeginIdx;
-      std::string PassName(&PipelineStr[FlagBeginIdx], Sz);
-      FlagBeginIdx = Idx + 1;
+    // Accept an empty pipeline as a special case. This can be useful, for
+    // example, to test conversion to SandboxIR without running any passes on
+    // it.
+    if (Pipeline.empty())
+      return;
 
+    // Add EndToken to the end to ease parsing.
+    std::string PipelineStr = std::string(Pipeline) + EndToken;
+    Pipeline = StringRef(PipelineStr);
+
+    enum {
+      ScanName,  // reading a pass name
+      ScanArgs,  // reading a list of args
+      ArgsEnded, // read the last '>' in an args list, must read delimiter next
+    } State;
+    State = ScanName;
+    int PassBeginIdx = 0;
+    int ArgsBeginIdx;
+    StringRef PassName;
+    StringRef PassArgs;
----------------
vporpo wrote:

Same, line 148, `StringRef PassArgs = Pipeline.slice(...)` ?

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


More information about the llvm-commits mailing list