xref: /llvm-project/mlir/include/mlir/Dialect/Tosa/Transforms/Passes.td (revision cc9e7cb99b63559c5baf7e380287e5658c412370)
1//===-- Passes.td - TOSA pass declarations ----*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the passes for the TOSA Dialect in MLIR.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_DIALECT_TOSA_TRANSFORMS_PASSES
14#define MLIR_DIALECT_TOSA_TRANSFORMS_PASSES
15
16include "mlir/IR/EnumAttr.td"
17include "mlir/Pass/PassBase.td"
18
19def TosaLayerwiseConstantFoldPass : Pass<"tosa-layerwise-constant-fold", "func::FuncOp"> {
20  let summary = "Fold layerwise operations on constant tensors";
21  let description = [{
22    Pass that enables folding of full-layer operations on constant tensors.
23  }];
24
25  let constructor = "createTosaLayerwiseConstantFoldPass()";
26
27  let options = [
28      Option<"aggressiveReduceConstant", "aggressive-reduce-constant", "bool",
29             /*default=*/"false",
30             "Always perform the reduce constant optimization"
31             "May add more tosa.const but would reduce runtime calculations">,
32   ];
33}
34
35def TosaInferShapes : Pass<"tosa-infer-shapes", "func::FuncOp"> {
36  let summary = "Propagate shapes across TOSA operations";
37  let description = [{
38    Pass that uses operand types and propagates shapes to TOSA operations.
39    This includes legalizing rankless and dynamic shapes towards static.
40  }];
41
42  let constructor = "createTosaInferShapesPass()";
43  let dependentDialects = [
44    "func::FuncDialect",
45    "tensor::TensorDialect",
46    "tosa::TosaDialect",
47  ];
48}
49
50def TosaMakeBroadcastable : Pass<"tosa-make-broadcastable", "func::FuncOp"> {
51  let summary = "TOSA rank Reshape to enable Broadcasting";
52  let description = [{
53    Pass that enables broadcast by making all input arrays have the same
54    number of dimensions. Insert RESHAPE operations to prepend dimensions
55    of size one until the number of dimensions is equal. Implements
56    approach similar to step 1 of Numpy 4-step broadcasting:
57    https://numpy.org/doc/stable/reference/ufuncs.html#broadcasting
58  }];
59
60  let constructor = "createTosaMakeBroadcastablePass()";
61}
62
63def TosaOptionalDecompositions
64  : Pass<"tosa-optional-decompositions", "func::FuncOp"> {
65  let summary = "Applies Tosa operations optional decompositions";
66  let description = [{
67    Pass to apply the Tosa operations decompositions
68    exposed as populate functions in include/mlir/Dialect/Tosa/Transforms/Passes.h
69  }];
70
71  let constructor = "tosa::createTosaOptionalDecompositions()";
72}
73
74def TosaProfileType : I32EnumAttr<"TosaProfileEnum", "Tosa profile",
75    [
76      I32EnumAttrCase<"BaseInference", 0, "bi">,
77      I32EnumAttrCase<"MainInference", 1, "mi">,
78      I32EnumAttrCase<"MainTraining", 2, "mt">,
79      I32EnumAttrCase<"Undefined", 3, "none">
80    ]>{
81  let cppNamespace = "mlir::tosa";
82}
83
84def TosaLevelType : I32EnumAttr<"TosaLevelEnum", "Tosa level",
85    [
86      I32EnumAttrCase<"None", 0, "none">,
87      I32EnumAttrCase<"EightK", 1, "8k">,
88    ]>{
89  let cppNamespace = "mlir::tosa";
90}
91
92def TosaValidation : Pass<"tosa-validate", "mlir::ModuleOp"> {
93  let summary = "Validates TOSA dialect";
94  let description = [{
95    This pass validates if input TOSA operations match the specification for given
96    criteria, e.g. TOSA profile.
97  }];
98
99  let options = [
100      ListOption<"profile", "profile", "std::string",
101             "Validate if operations match for the given profile set">,
102      Option<"StrictOperationSpecAlignment", "strict-op-spec-alignment", "bool",
103             /*default=*/"false",
104             "Verify if the properties of certain operations align the spec requirement">,
105      Option<"level", "level", "mlir::tosa::TosaLevelEnum",
106             /*default=*/"mlir::tosa::TosaLevelEnum::EightK",
107             "Validate if operator parameters are within specfication for the given level",
108             [{::llvm::cl::values(
109               clEnumValN(mlir::tosa::TosaLevelEnum::EightK, "8k",
110                "Ranges are expected to be sufficient for applications with frame sizes up to 8K."),
111               clEnumValN(mlir::tosa::TosaLevelEnum::None, "none",
112                "Allows the full range of arguments specified by the operations according "
113                "to the operation data types.")
114              )}]>
115   ];
116}
117
118def TosaReduceTransposes : Pass<"tosa-reduce-transposes", "func::FuncOp"> {
119  let summary = "Reduce transposes through other operators";
120  let description = [{
121    Pass that identifies and reduces tosa.TRANSPOSE operations through chains
122    of operators.
123
124    The pass traverses dependencies of tosa.TRANSPOSE operations until they
125    terminate in either a tosa.RESHAPE that we can fold the hoisted
126    tosa.TRANSPOSE into, a tosa.TRANSPOSE that forms the identity with the
127    hoisted one, or a tosa.CONST with a dense elements attribute. It then
128    propagates the hoisted transform upward through the intervening operators
129    if the support is implemented. Finally, it observes that no duplication
130    will occur of both the chain that was hoisted through and the new chain
131    that results, and if so, it replaces the hoisted tosa.TRANSPOSE.
132
133    The pass has an important use-case in cleaning up the results of frameworks
134    that introduce a lot of data-layout transformations when legalizing to TOSA,
135    a common one being transformations between NHWC and NCHW layouts.
136  }];
137}
138
139#endif // MLIR_DIALECT_TOSA_TRANSFORMS_PASSES
140