|
| 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/IR/TransformInterfaces.h" |
| 14 | +#include "mlir/Pass/Pass.h" |
| 15 | +#include "mlir/Support/LLVM.h" |
| 16 | +#include <memory> |
| 17 | + |
| 18 | +namespace mlir { |
| 19 | +struct LogicalResult; |
| 20 | +class MLIRContext; |
| 21 | +class ModuleOp; |
| 22 | +class Operation; |
| 23 | +template <typename> |
| 24 | +class OwningOpRef; |
| 25 | +class Region; |
| 26 | + |
| 27 | +namespace transform { |
| 28 | +namespace detail { |
| 29 | +/// Utility to parse and verify the content of a `transformFileName` MLIR file |
| 30 | +/// containing a transform dialect specification. |
| 31 | +LogicalResult |
| 32 | +parseTransformModuleFromFile(MLIRContext *context, |
| 33 | + llvm::StringRef transformFileName, |
| 34 | + OwningOpRef<ModuleOp> &transformModule); |
| 35 | + |
| 36 | +/// Utility to load a transform interpreter `module` from a module that has |
| 37 | +/// already been preloaded in the context. |
| 38 | +/// This mode is useful in cases where explicit parsing of a transform library |
| 39 | +/// from file is expected to be prohibitively expensive. |
| 40 | +/// In such cases, the transform module is expected to be found in the preloaded |
| 41 | +/// library modules of the transform dialect. |
| 42 | +/// Returns null if the module is not found. |
| 43 | +ModuleOp getPreloadedTransformModule(MLIRContext *context); |
| 44 | + |
| 45 | +/// Finds the first TransformOpInterface named `kTransformEntryPointSymbolName` |
| 46 | +/// that is either: |
| 47 | +/// 1. nested under `root` (takes precedence). |
| 48 | +/// 2. nested under `module`, if not found in `root`. |
| 49 | +/// Reports errors and returns null if no such operation found. |
| 50 | +TransformOpInterface findTransformEntryPoint( |
| 51 | + Operation *root, ModuleOp module, |
| 52 | + StringRef entryPoint = TransformDialect::kTransformEntryPointSymbolName); |
| 53 | + |
| 54 | +/// Merge all symbols from `other` into `target`. Both ops need to implement the |
| 55 | +/// `SymbolTable` trait. Operations are moved from `other`, i.e., `other` may be |
| 56 | +/// modified by this function and might not verify after the function returns. |
| 57 | +/// Upon merging, private symbols may be renamed in order to avoid collisions in |
| 58 | +/// the result. Public symbols may not collide, with the exception of |
| 59 | +/// instances of `SymbolOpInterface`, where collisions are allowed if at least |
| 60 | +/// one of the two is external, in which case the other op preserved (or any one |
| 61 | +/// of the two if both are external). |
| 62 | +// TODO: Reconsider cloning individual ops rather than forcing users of the |
| 63 | +// function to clone (or move) `other` in order to improve efficiency. |
| 64 | +// This might primarily make sense if we can also prune the symbols that |
| 65 | +// are merged to a subset (such as those that are actually used). |
| 66 | +LogicalResult mergeSymbolsInto(Operation *target, |
| 67 | + OwningOpRef<Operation *> other); |
| 68 | +} // namespace detail |
| 69 | + |
| 70 | +/// Standalone util to apply the named sequence `entryPoint` to the payload. |
| 71 | +/// This is done in 3 steps: |
| 72 | +/// 1. lookup the `entryPoint` symbol in `{payload, sharedTransformModule}` by |
| 73 | +/// calling detail::findTransformEntryPoint. |
| 74 | +/// 2. if the entry point is found and not nested under |
| 75 | +/// `sharedTransformModule`, call `detail::defineDeclaredSymbols` to "link" in |
| 76 | +/// the `sharedTransformModule`. Note: this may modify the transform IR |
| 77 | +/// embedded with the payload IR. |
| 78 | +/// 3. apply the transform IR to the payload IR, relaxing the requirement that |
| 79 | +/// the transform IR is a top-level transform op. We are applying a named |
| 80 | +/// sequence anyway. |
| 81 | +LogicalResult applyTransformNamedSequence( |
| 82 | + Operation *payload, ModuleOp transformModule, |
| 83 | + const TransformOptions &options, |
| 84 | + StringRef entryPoint = TransformDialect::kTransformEntryPointSymbolName); |
| 85 | + |
| 86 | +} // namespace transform |
| 87 | +} // namespace mlir |
| 88 | + |
| 89 | +#endif // MLIR_DIALECT_TRANSFORM_TRANSFORMS_TRANSFORMINTERPRETERUTILS_H |
0 commit comments