[Mlir-commits] [mlir] 407b351 - [mlir][linalg] Add ods-gen helper to simplify the build methods.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Apr 20 06:19:35 PDT 2022


Author: gysit
Date: 2022-04-20T13:14:38Z
New Revision: 407b351da2a144eb230a0c989b5cc513e0b91a12

URL: https://github.com/llvm/llvm-project/commit/407b351da2a144eb230a0c989b5cc513e0b91a12
DIFF: https://github.com/llvm/llvm-project/commit/407b351da2a144eb230a0c989b5cc513e0b91a12.diff

LOG: [mlir][linalg] Add ods-gen helper to simplify the build methods.

Add a helper used to implement the build methods generated by ods-gen. The change reduces code size and compilation time since all structured op builders use the same build method. The change reduces the LinalgOps.cpp compilation time from 10.2s to 9.8s (debug build).

Depends On D123987

Reviewed By: nicolasvasilache

Differential Revision: https://reviews.llvm.org/D124003

Added: 
    

Modified: 
    mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
    mlir/test/mlir-linalg-ods-gen/test-linalg-ods-yaml-gen.yaml
    mlir/tools/mlir-linalg-ods-gen/mlir-linalg-ods-yaml-gen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
index c41c2245d55c2..1aea2c2fc12f9 100644
--- a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
+++ b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
@@ -82,16 +82,35 @@ static void fillStructuredOpRegion(OpBuilder &opBuilder, Region &region,
   // iterator_types is an auto-generated method.
 }
 
-/// Create the region and fill the block of a structured operation given
-/// `inputTypes` and `outputTypes` as well as a `regionBuilder`.
-void createAndFillStructuredOpRegion(OpBuilder &opBuilder,
-                                     OperationState &result,
-                                     TypeRange inputTypes,
-                                     TypeRange outputTypes,
-                                     RegionBuilderFn regionBuilder) {
-  Region &region = *result.addRegion();
-  fillStructuredOpRegion(opBuilder, region, inputTypes, outputTypes,
-                         result.attributes.getAttrs(), regionBuilder);
+/// Creates a structured operation given `inputs`, `outputs`, and `attributes`.
+/// The result types are derived automatically if `resultTensorTypes` is none.
+/// The body of the operation is filled using `regionBuilder`. All ods-gen
+/// created structured operations use the method to implement their builders.
+static void buildStructuredOp(OpBuilder &b, OperationState &state,
+                              llvm::Optional<TypeRange> resultTensorTypes,
+                              ValueRange inputs, ValueRange outputs,
+                              ArrayRef<NamedAttribute> attributes,
+                              RegionBuilderFn regionBuilder) {
+  // Derive the result types if needed.
+  SmallVector<Type> derivedResultTypes =
+      resultTensorTypes.getValueOr(TypeRange());
+  if (!resultTensorTypes.hasValue())
+    copy_if(outputs.getTypes(), std::back_inserter(derivedResultTypes),
+            [](Type type) { return type.isa<RankedTensorType>(); });
+
+  state.addOperands(inputs);
+  state.addOperands(outputs);
+  state.addTypes(derivedResultTypes);
+  state.addAttributes(attributes);
+  state.addAttribute(
+      "operand_segment_sizes",
+      b.getI32VectorAttr({static_cast<int32_t>(inputs.size()),
+                          static_cast<int32_t>(outputs.size())}));
+
+  // Create and fill the region of the structured operation.
+  Region &region = *state.addRegion();
+  fillStructuredOpRegion(b, region, TypeRange(inputs), TypeRange(outputs),
+                         state.attributes.getAttrs(), regionBuilder);
 }
 
 /// Common parsing used for both named structured ops created by ods-gen and by

diff  --git a/mlir/test/mlir-linalg-ods-gen/test-linalg-ods-yaml-gen.yaml b/mlir/test/mlir-linalg-ods-gen/test-linalg-ods-yaml-gen.yaml
index 628e04a282a88..e8e447351252b 100644
--- a/mlir/test/mlir-linalg-ods-gen/test-linalg-ods-yaml-gen.yaml
+++ b/mlir/test/mlir-linalg-ods-gen/test-linalg-ods-yaml-gen.yaml
@@ -81,22 +81,8 @@ structured_op: !LinalgStructuredOpConfig
 #  ODS-NEXT:       "ValueRange":$outputs, "Attribute":$cast,
 #  ODS-NEXT:       CArg<"ArrayRef<NamedAttribute>", "{}">:$attributes),
 
-#       ODS:    $_state.addOperands(inputs);
-#  ODS-NEXT:    $_state.addOperands(outputs);
-#  ODS-NEXT:    $_state.addTypes(resultTensorTypes);
-#  ODS-NEXT:    $_state.addAttribute("cast", cast)
-#  ODS-NEXT:    $_state.addAttributes(attributes);
-#  ODS-NEXT:    $_state.addAttribute(
-#  ODS-NEXT:      "operand_segment_sizes",
-#  ODS-NEXT:      $_builder.getI32VectorAttr({
-#  ODS-NEXT:        static_cast<int32_t>(inputs.size()),
-#  ODS-NEXT:        static_cast<int32_t>(outputs.size())}));
-#  ODS-NEXT:    createAndFillStructuredOpRegion(
-#  ODS-NEXT:      $_builder,
-#  ODS-NEXT:      $_state,
-#  ODS-NEXT:      TypeRange(inputs),
-#  ODS-NEXT:      TypeRange(outputs),
-#  ODS-NEXT:      Test1Op::getRegionBuilder()
+#       ODS:    buildStructuredOp($_builder, $_state, resultTensorTypes,
+#  ODS-NEXT:      attributes, Test1Op::getRegionBuilder())
 
 
 # IMPL-LABEL:  void Test1Op::regionBuilder(ImplicitLocOpBuilder &b,

diff  --git a/mlir/tools/mlir-linalg-ods-gen/mlir-linalg-ods-yaml-gen.cpp b/mlir/tools/mlir-linalg-ods-gen/mlir-linalg-ods-yaml-gen.cpp
index a7900d6123c3a..e6fdc11231a6e 100644
--- a/mlir/tools/mlir-linalg-ods-gen/mlir-linalg-ods-yaml-gen.cpp
+++ b/mlir/tools/mlir-linalg-ods-gen/mlir-linalg-ods-yaml-gen.cpp
@@ -524,46 +524,16 @@ def {0} : LinalgStructuredBase_Op<"{1}", !listconcat([AttrSizedOperandSegments],
       (ins "ValueRange":$inputs, "ValueRange":$outputs,
             CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
       [{{
-        $_state.addOperands(inputs);
-        $_state.addOperands(outputs);
-        SmallVector<Type> resultTensorTypes;
-        copy_if(outputs.getTypes(),
-                std::back_inserter(resultTensorTypes),
-                [](Type type) {{ return type.isa<RankedTensorType>(); });
-        $_state.addTypes(resultTensorTypes);
-        $_state.addAttribute(
-          "operand_segment_sizes",
-          $_builder.getI32VectorAttr({{
-            static_cast<int32_t>(inputs.size()),
-            static_cast<int32_t>(outputs.size())}));
-        $_state.addAttributes(attributes);
-        createAndFillStructuredOpRegion(
-          $_builder,
-          $_state,
-          TypeRange(inputs),
-          TypeRange(outputs),
-          {0}::getRegionBuilder());
+        buildStructuredOp($_builder, $_state, llvm::None, inputs, outputs,
+          attributes, {0}::getRegionBuilder());
       }]>,
       OpBuilder<
       (ins "TypeRange":$resultTensorTypes, "ValueRange":$inputs,
             "ValueRange":$outputs,
             CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
       [{{
-        $_state.addOperands(inputs);
-        $_state.addOperands(outputs);
-        $_state.addTypes(resultTensorTypes);
-        $_state.addAttributes(attributes);
-        $_state.addAttribute(
-          "operand_segment_sizes",
-          $_builder.getI32VectorAttr({{
-            static_cast<int32_t>(inputs.size()),
-            static_cast<int32_t>(outputs.size())}));
-        createAndFillStructuredOpRegion(
-          $_builder,
-          $_state,
-          TypeRange(inputs),
-          TypeRange(outputs),
-          {0}::getRegionBuilder());
+        buildStructuredOp($_builder, $_state, resultTensorTypes,
+          inputs, outputs, attributes, {0}::getRegionBuilder());
       }]>,
       OpBuilder<
       (ins "TypeRange":$resultTensorTypes, "ValueRange":$operands,
@@ -610,22 +580,9 @@ static const char structuredOpBuilderFormat[] = R"FMT(
        "ValueRange":$outputs, {1},
        CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
   [{{
-    $_state.addOperands(inputs);
-    $_state.addOperands(outputs);
-    $_state.addTypes(resultTensorTypes);
     {2}
-    $_state.addAttributes(attributes);
-    $_state.addAttribute(
-      "operand_segment_sizes",
-      $_builder.getI32VectorAttr({{
-        static_cast<int32_t>(inputs.size()),
-        static_cast<int32_t>(outputs.size())}));
-    createAndFillStructuredOpRegion(
-      $_builder,
-      $_state,
-      TypeRange(inputs),
-      TypeRange(outputs),
-      {0}::getRegionBuilder());
+    buildStructuredOp($_builder, $_state, resultTensorTypes, inputs, outputs,
+      attributes, {0}::getRegionBuilder());
   }]>
 )FMT";
 


        


More information about the Mlir-commits mailing list