[Mlir-commits] [mlir] [MLIR][Transform] Allow ApplyRegisteredPassOp to take options as a param (PR #142683)

Rolf Morel llvmlistbot at llvm.org
Fri Jun 6 03:05:57 PDT 2025


================
@@ -766,17 +772,53 @@ void transform::ApplyLoopInvariantCodeMotionOp::getEffects(
 // ApplyRegisteredPassOp
 //===----------------------------------------------------------------------===//
 
-DiagnosedSilenceableFailure transform::ApplyRegisteredPassOp::applyToOne(
-    transform::TransformRewriter &rewriter, Operation *target,
-    ApplyToEachResultList &results, transform::TransformState &state) {
-  // Make sure that this transform is not applied to itself. Modifying the
-  // transform IR while it is being interpreted is generally dangerous. Even
-  // more so when applying passes because they may perform a wide range of IR
-  // modifications.
-  DiagnosedSilenceableFailure payloadCheck =
-      ensurePayloadIsSeparateFromTransform(*this, target);
-  if (!payloadCheck.succeeded())
-    return payloadCheck;
+void transform::ApplyRegisteredPassOp::getEffects(
+    SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  consumesHandle(getTargetMutable(), effects);
+  onlyReadsHandle(getDynamicOptionsMutable(), effects);
+  producesHandle(getOperation()->getOpResults(), effects);
+  modifiesPayload(effects);
+}
+
+DiagnosedSilenceableFailure
+transform::ApplyRegisteredPassOp::apply(transform::TransformRewriter &rewriter,
+                                        transform::TransformResults &results,
+                                        transform::TransformState &state) {
+  // Obtain a single options-string from options passed statically as
+  // string attributes as well as "dynamically" through params.
+  std::string options;
+  OperandRange dynamicOptions = getDynamicOptions();
+  size_t dynamicOptionsIdx = 0;
+  for (auto [idx, optionAttr] : llvm::enumerate(getOptions())) {
+    if (idx > 0)
+      options += " "; // Interleave options seperator.
+
+    if (auto strAttr = dyn_cast<StringAttr>(optionAttr)) {
+      options += strAttr.getValue();
+    } else if (isa<UnitAttr>(optionAttr)) {
+      assert(dynamicOptionsIdx < dynamicOptions.size() &&
+             "number of dynamic option markers (UnitAttr) in options ArrayAttr "
+             "should be the same as the number of options passed as params");
+      ArrayRef<Attribute> dynamicOption =
+          state.getParams(dynamicOptions[dynamicOptionsIdx++]);
+      if (dynamicOption.size() != 1)
+        return emitSilenceableError() << "options passed as a param must have "
+                                         "a single value associated, param "
+                                      << dynamicOptionsIdx - 1 << " associates "
+                                      << dynamicOption.size();
+
+      if (auto dynamicOptionStr = dyn_cast<StringAttr>(dynamicOption[0])) {
+        options += dynamicOptionStr.getValue();
+      } else {
+        return emitSilenceableError()
+               << "options passed as a param must be a string, got "
+               << dynamicOption[0];
+      }
+    } else {
+      llvm_unreachable(
----------------
rolfmorel wrote:

It depends. Can these methods assume that they run on verified IR, yes or no?

As it stands, the verifier is the one which checks this invariant. In case it holds the other methods can rely on it. In case the invariant is false, it prints an error.

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


More information about the Mlir-commits mailing list