xref: /llvm-project/mlir/include/mlir/IR/OpAsmInterface.td (revision 3c64f86314fbf9a3cd578419f16e621a4de57eaa)
1//===- OpAsmInterface.td - Asm Interfaces for opse ---------*- 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 contains interfaces and other utilities for interacting with the
10// AsmParser and AsmPrinter.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_IR_OPASMINTERFACE_TD
15#define MLIR_IR_OPASMINTERFACE_TD
16
17include "mlir/IR/AttrTypeBase.td"
18include "mlir/IR/OpBase.td"
19
20//===----------------------------------------------------------------------===//
21// OpAsmOpInterface
22//===----------------------------------------------------------------------===//
23
24def OpAsmOpInterface : OpInterface<"OpAsmOpInterface"> {
25  let description = [{
26    This interface provides hooks to interact with the AsmPrinter and AsmParser
27    classes.
28  }];
29  let cppNamespace = "::mlir";
30
31  let methods = [
32    InterfaceMethod<[{
33        Get a special name to use when printing the results of this operation.
34        The given callback is invoked with a specific result value that starts a
35        result "pack", and the name to give this result pack. To signal that a
36        result pack should use the default naming scheme, a None can be passed
37        in instead of the name.
38
39        For example, if you have an operation that has four results and you want
40        to split these into three distinct groups you could do the following:
41
42        ```c++
43          setNameFn(getResult(0), "first_result");
44          setNameFn(getResult(1), "middle_results");
45          setNameFn(getResult(3), ""); // use the default numbering.
46        ```
47
48        This would print the operation as follows:
49
50        ```mlir
51          %first_result, %middle_results:2, %0 = "my.op" ...
52        ```
53      }],
54      "void", "getAsmResultNames",
55      (ins "::mlir::OpAsmSetValueNameFn":$setNameFn),
56      "", "return;"
57    >,
58    InterfaceMethod<[{
59        Get a special name to use when printing the block arguments for a region
60        immediately nested under this operation.
61      }],
62      "void", "getAsmBlockArgumentNames",
63      (ins
64        "::mlir::Region&":$region,
65        "::mlir::OpAsmSetValueNameFn":$setNameFn
66      ),
67      "", "return;"
68    >,
69    InterfaceMethod<[{
70        Get the name to use for a given block inside a region attached to this
71        operation.
72
73        For example if this operation has multiple blocks:
74
75        ```mlir
76          some.op() ({
77            ^bb0:
78              ...
79            ^bb1:
80              ...
81          })
82        ```
83
84        the method will be invoked on each of the blocks allowing the op to
85        print:
86
87        ```mlir
88          some.op() ({
89            ^custom_foo_name:
90              ...
91            ^custom_bar_name:
92              ...
93          })
94        ```
95      }],
96      "void", "getAsmBlockNames",
97      (ins "::mlir::OpAsmSetBlockNameFn":$setNameFn), "", ";"
98    >,
99    StaticInterfaceMethod<[{
100      Return the default dialect used when printing/parsing operations in
101      regions nested under this operation. This allows for eliding the dialect
102      prefix from the operation name, for example it would be possible to omit
103      the `spirv.` prefix from all operations within a SpirV module if this method
104      returned `spv`. The default implementation returns an empty string which
105      is ignored.
106      }],
107      "::llvm::StringRef", "getDefaultDialect", (ins), "", "return \"\";"
108    >,
109  ];
110}
111
112//===----------------------------------------------------------------------===//
113// OpAsmTypeInterface
114//===----------------------------------------------------------------------===//
115
116def OpAsmTypeInterface : TypeInterface<"OpAsmTypeInterface"> {
117  let description = [{
118    This interface provides hooks to interact with the AsmPrinter and AsmParser
119    classes.
120  }];
121  let cppNamespace = "::mlir";
122
123  let methods = [
124    InterfaceMethod<[{
125        Get a name to use when printing a value of this type.
126      }],
127      "void", "getAsmName",
128      (ins "::mlir::OpAsmSetNameFn":$setNameFn), "", ";"
129    >,
130  ];
131}
132
133//===----------------------------------------------------------------------===//
134// ResourceHandleParameter
135//===----------------------------------------------------------------------===//
136
137/// This parameter represents a handle to a resource that is encoded into the
138/// "dialect_resources" section of the assembly format. This parameter expects a
139/// C++ `handleType` that derives from `AsmDialectResourceHandleBase` and
140/// implements a derived handle to the desired resource type.
141class ResourceHandleParameter<string handleType, string desc = "">
142    : AttrOrTypeParameter<handleType, desc> {
143  let parser = "$_parser.parseResourceHandle<" # handleType # ">()";
144  let printer = "$_printer.printResourceHandle($_self)";
145}
146
147#endif // MLIR_IR_OPASMINTERFACE_TD
148