xref: /llvm-project/mlir/include/mlir/Dialect/Transform/Transforms/TransformInterpreterUtils.h (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
1 //===- TransformInterpreterUtils.h - Transform Utils ------------*- C++ -*-===//
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 #ifndef MLIR_DIALECT_TRANSFORM_TRANSFORMS_TRANSFORMINTERPRETERUTILS_H
10 #define MLIR_DIALECT_TRANSFORM_TRANSFORMS_TRANSFORMINTERPRETERUTILS_H
11 
12 #include "mlir/Dialect/Transform/IR/TransformDialect.h"
13 #include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h"
14 #include "mlir/Pass/Pass.h"
15 #include "mlir/Support/LLVM.h"
16 #include <memory>
17 
18 namespace mlir {
19 class MLIRContext;
20 class ModuleOp;
21 class Operation;
22 template <typename>
23 class OwningOpRef;
24 class Region;
25 
26 namespace transform {
27 namespace detail {
28 
29 /// Expands the given list of `paths` to a list of `.mlir` files.
30 ///
31 /// Each entry in `paths` may either be a regular file, in which case it ends up
32 /// in the result list, or a directory, in which case all (regular) `.mlir`
33 /// files in that directory are added. Any other file types lead to a failure.
34 LogicalResult expandPathsToMLIRFiles(ArrayRef<std::string> paths,
35                                      MLIRContext *context,
36                                      SmallVectorImpl<std::string> &fileNames);
37 
38 /// Utility to parse and verify the content of a `transformFileName` MLIR file
39 /// containing a transform dialect specification.
40 LogicalResult
41 parseTransformModuleFromFile(MLIRContext *context,
42                              llvm::StringRef transformFileName,
43                              OwningOpRef<ModuleOp> &transformModule);
44 
45 /// Utility to parse, verify, aggregate and link the content of all mlir files
46 /// nested under `transformLibraryPaths` and containing transform dialect
47 /// specifications.
48 LogicalResult
49 assembleTransformLibraryFromPaths(MLIRContext *context,
50                                   ArrayRef<std::string> transformLibraryPaths,
51                                   OwningOpRef<ModuleOp> &transformModule);
52 
53 /// Utility to load a transform interpreter `module` from a module that has
54 /// already been preloaded in the context.
55 /// This mode is useful in cases where explicit parsing of a transform library
56 /// from file is expected to be prohibitively expensive.
57 /// In such cases, the transform module is expected to be found in the preloaded
58 /// library modules of the transform dialect.
59 /// Returns null if the module is not found.
60 ModuleOp getPreloadedTransformModule(MLIRContext *context);
61 
62 /// Finds the first TransformOpInterface named `kTransformEntryPointSymbolName`
63 /// that is either:
64 ///   1. nested under `root` (takes precedence).
65 ///   2. nested under `module`, if not found in `root`.
66 /// Reports errors and returns null if no such operation found.
67 TransformOpInterface findTransformEntryPoint(
68     Operation *root, ModuleOp module,
69     StringRef entryPoint = TransformDialect::kTransformEntryPointSymbolName);
70 
71 } // namespace detail
72 
73 /// Standalone util to apply the named sequence `transformRoot` to `payload` IR.
74 /// This is done in 2 steps:
75 ///   1. If `transformModule` is provided and is not nested under
76 ///      `transformRoot`, it will be "linked into" the IR containing
77 ///      `transformRoot` to resolve undefined named sequences.
78 ///   2. The transforms specified in `transformRoot` are applied to `payload`,
79 ///      assuming the named sequence has a single argument handle that will be
80 ///      associated with `payload` on run.
81 LogicalResult applyTransformNamedSequence(Operation *payload,
82                                           Operation *transformRoot,
83                                           ModuleOp transformModule,
84                                           const TransformOptions &options);
85 
86 LogicalResult applyTransformNamedSequence(RaggedArray<MappedValue> bindings,
87                                           TransformOpInterface transformRoot,
88                                           ModuleOp transformModule,
89                                           const TransformOptions &options);
90 
91 } // namespace transform
92 } // namespace mlir
93 
94 #endif // MLIR_DIALECT_TRANSFORM_TRANSFORMS_TRANSFORMINTERPRETERUTILS_H
95