[Mlir-commits] [mlir] [mlir] Init the `TransformsInterfaces` for configuring transformations (PR #99566)

Fabian Mora llvmlistbot at llvm.org
Sun Jul 21 11:47:04 PDT 2024


================
@@ -0,0 +1,77 @@
+//===- TransformsInterfaces.td - Transforms interfaces -----*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Defines interfaces for managing transformations, including populating
+// pattern rewrites.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_INTERFACES_TRANSFORMSINTERFACES_TD
+#define MLIR_INTERFACES_TRANSFORMSINTERFACES_TD
+
+include "mlir/IR/OpBase.td"
+
+//===----------------------------------------------------------------------===//
+// Conversion patterns attribute interface
+//===----------------------------------------------------------------------===//
+
+def ConversionPatternsAttrInterface :
+    AttrInterface<"ConversionPatternsAttrInterface"> {
+  let description = [{
+    This interfaces allows using attributes to configure the dialect conversion
+    infrastructure, this includes:
+     - The conversion target.
+     - The type converter.
+     - The pattern set.
+    
+    The conversion target and type converter are passed through the
+    `ConversionPatternAttrOptions` class. Passing them through this class
+    and by reference allows sub-classing the base option class, allowing
+    specializations like `LLVMConversionPatternAttrOptions` for converting to
+    LLVM.
+  }];
+  let cppNamespace = "::mlir";
+  let methods = [
+    InterfaceMethod<
+      /*desc=*/[{
+        Populate the dialect conversion target, type converter and pattern set.
+      }],
+      /*retTy=*/"void",
+      /*methodName=*/"populateConversionPatterns",
+      /*args=*/(ins "::mlir::ConversionPatternAttrOptions&":$options,
+                    "::mlir::RewritePatternSet&":$patternSet)>
+  ];
+}
----------------
fabianmcg wrote:

Yes.
That's precisely why I pass `ConversionPatternAttrOptions` by reference, to sub-class it and allow interfaces to discern which conversion to populate. But also at the same time, not forcing every conversion to add an interface for it. See for example the `NVVMTargetPatternsAttrInterface::populateConversionPatterns`:
```c++
void NVVMTargetPatternsAttrInterface::populateConversionPatterns(
    Attribute attr, ConversionPatternAttrOptions &options,
    RewritePatternSet &patterns) const {
  auto *llvmOptions = dyn_cast<LLVMConversionPatternAttrOptions>(&options);
  // Bail if the options are invalid.
  if (!llvmOptions)
    return;
  configureGpuToNVVMConversionLegality(options.getConversionTarget());
  configureGpuToNVVMTypeConverter(llvmOptions->getLLVMTypeConverter());
  populateGpuToNVVMConversionPatterns(llvmOptions->getLLVMTypeConverter(),
                                      patterns);
}
```
However, I see 100% the point of having interfaces for each conversion. I can change it to an LLVM specific one. 

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


More information about the Mlir-commits mailing list