1 //===-- mlir-c/Pass.h - C API to Pass Management ------------------*- C -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM
4 // Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header declares the C interface to MLIR pass manager.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef MLIR_C_PASS_H
15 #define MLIR_C_PASS_H
16
17 #include "mlir-c/IR.h"
18 #include "mlir-c/Support.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 //===----------------------------------------------------------------------===//
25 /** Opaque type declarations.
26 *
27 * Types are exposed to C bindings as structs containing opaque pointers. They
28 * are not supposed to be inspected from C. This allows the underlying
29 * representation to change without affecting the API users. The use of structs
30 * instead of typedefs enables some type safety as structs are not implicitly
31 * convertible to each other.
32 *
33 * Instances of these types may or may not own the underlying object. The
34 * ownership semantics is defined by how an instance of the type was obtained.
35 */
36 //===----------------------------------------------------------------------===//
37
38 #define DEFINE_C_API_STRUCT(name, storage) \
39 struct name { \
40 storage *ptr; \
41 }; \
42 typedef struct name name
43
44 DEFINE_C_API_STRUCT(MlirPass, void);
45 DEFINE_C_API_STRUCT(MlirPassManager, void);
46 DEFINE_C_API_STRUCT(MlirOpPassManager, void);
47
48 #undef DEFINE_C_API_STRUCT
49
50 /// Create a new top-level PassManager.
51 MLIR_CAPI_EXPORTED MlirPassManager mlirPassManagerCreate(MlirContext ctx);
52
53 /// Destroy the provided PassManager.
54 MLIR_CAPI_EXPORTED void mlirPassManagerDestroy(MlirPassManager passManager);
55
56 /// Checks if a PassManager is null.
mlirPassManagerIsNull(MlirPassManager passManager)57 static inline bool mlirPassManagerIsNull(MlirPassManager passManager) {
58 return !passManager.ptr;
59 }
60
61 /// Cast a top-level PassManager to a generic OpPassManager.
62 MLIR_CAPI_EXPORTED MlirOpPassManager
63 mlirPassManagerGetAsOpPassManager(MlirPassManager passManager);
64
65 /// Run the provided `passManager` on the given `module`.
66 MLIR_CAPI_EXPORTED MlirLogicalResult
67 mlirPassManagerRun(MlirPassManager passManager, MlirModule module);
68
69 /** Nest an OpPassManager under the top-level PassManager, the nested
70 * passmanager will only run on operations matching the provided name.
71 * The returned OpPassManager will be destroyed when the parent is destroyed.
72 * To further nest more OpPassManager under the newly returned one, see
73 * `mlirOpPassManagerNest` below. */
74 MLIR_CAPI_EXPORTED MlirOpPassManager mlirPassManagerGetNestedUnder(
75 MlirPassManager passManager, MlirStringRef operationName);
76
77 /** Nest an OpPassManager under the provided OpPassManager, the nested
78 * passmanager will only run on operations matching the provided name.
79 * The returned OpPassManager will be destroyed when the parent is destroyed. */
80 MLIR_CAPI_EXPORTED MlirOpPassManager mlirOpPassManagerGetNestedUnder(
81 MlirOpPassManager passManager, MlirStringRef operationName);
82
83 /** Add a pass and transfer ownership to the provided top-level mlirPassManager.
84 * If the pass is not a generic operation pass or a ModulePass, a new
85 * OpPassManager is implicitly nested under the provided PassManager. */
86 MLIR_CAPI_EXPORTED void mlirPassManagerAddOwnedPass(MlirPassManager passManager,
87 MlirPass pass);
88
89 /** Add a pass and transfer ownership to the provided mlirOpPassManager. If the
90 * pass is not a generic operation pass or matching the type of the provided
91 * PassManager, a new OpPassManager is implicitly nested under the provided
92 * PassManager. */
93 MLIR_CAPI_EXPORTED void
94 mlirOpPassManagerAddOwnedPass(MlirOpPassManager passManager, MlirPass pass);
95
96 /** Print a textual MLIR pass pipeline by sending chunks of the string
97 * representation and forwarding `userData to `callback`. Note that the callback
98 * may be called several times with consecutive chunks of the string. */
99 MLIR_CAPI_EXPORTED void mlirPrintPassPipeline(MlirOpPassManager passManager,
100 MlirStringCallback callback,
101 void *userData);
102
103 /** Parse a textual MLIR pass pipeline and add it to the provided OpPassManager.
104 */
105 MLIR_CAPI_EXPORTED MlirLogicalResult
106 mlirParsePassPipeline(MlirOpPassManager passManager, MlirStringRef pipeline);
107
108 #ifdef __cplusplus
109 }
110 #endif
111
112 #endif // MLIR_C_PASS_H
113