|
| 1 | +//===- InlinerConfig.h - Config for the Inliner pass-------------*- 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 | +// This header file declares the config class used by the Inliner class. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef MLIR_TRANSFORMS_INLINER_CONFIG_H |
| 14 | +#define MLIR_TRANSFORMS_INLINER_CONFIG_H |
| 15 | + |
| 16 | +#include "mlir/Pass/PassManager.h" |
| 17 | +#include "mlir/Transforms/InliningUtils.h" |
| 18 | +#include "llvm/ADT/StringMap.h" |
| 19 | + |
| 20 | +namespace mlir { |
| 21 | +class OpPassManager; |
| 22 | +class Operation; |
| 23 | + |
| 24 | +class InlinerConfig { |
| 25 | +public: |
| 26 | + using DefaultPipelineTy = std::function<void(OpPassManager &)>; |
| 27 | + using OpPipelinesTy = llvm::StringMap<OpPassManager>; |
| 28 | + |
| 29 | + InlinerConfig() = default; |
| 30 | + InlinerConfig(DefaultPipelineTy defaultPipeline, |
| 31 | + unsigned maxInliningIterations) |
| 32 | + : defaultPipeline(std::move(defaultPipeline)), |
| 33 | + maxInliningIterations(maxInliningIterations) {} |
| 34 | + |
| 35 | + const DefaultPipelineTy &getDefaultPipeline() const { |
| 36 | + return defaultPipeline; |
| 37 | + } |
| 38 | + const OpPipelinesTy &getOpPipelines() const { return opPipelines; } |
| 39 | + unsigned getMaxInliningIterations() const { return maxInliningIterations; } |
| 40 | + const InlinerInterface::CloneCallbackTy &getCloneCallback() const { |
| 41 | + return cloneCallback; |
| 42 | + } |
| 43 | + bool getCanHandleMultipleBlocks() const { return canHandleMultipleBlocks; } |
| 44 | + |
| 45 | + void setDefaultPipeline(DefaultPipelineTy pipeline) { |
| 46 | + defaultPipeline = std::move(pipeline); |
| 47 | + } |
| 48 | + void setOpPipelines(OpPipelinesTy pipelines) { |
| 49 | + opPipelines = std::move(pipelines); |
| 50 | + } |
| 51 | + void setMaxInliningIterations(unsigned max) { maxInliningIterations = max; } |
| 52 | + void setCloneCallback(InlinerInterface::CloneCallbackTy callback) { |
| 53 | + cloneCallback = std::move(callback); |
| 54 | + } |
| 55 | + void setCanHandleMultipleBlocks(bool value = true) { |
| 56 | + canHandleMultipleBlocks = value; |
| 57 | + } |
| 58 | + |
| 59 | +private: |
| 60 | + /// An optional function that constructs an optimization pipeline for |
| 61 | + /// a given operation. This optimization pipeline is applied |
| 62 | + /// only to those callable operations that do not have dedicated |
| 63 | + /// optimization pipeline in opPipelines (based on the operation name). |
| 64 | + DefaultPipelineTy defaultPipeline; |
| 65 | + /// A map of operation names to pass pipelines to use when optimizing |
| 66 | + /// callable operations of these types. This provides a specialized pipeline |
| 67 | + /// instead of the one produced by defaultPipeline. |
| 68 | + OpPipelinesTy opPipelines; |
| 69 | + /// For SCC-based inlining algorithms, specifies maximum number of iterations |
| 70 | + /// when inlining within an SCC. |
| 71 | + unsigned maxInliningIterations{0}; |
| 72 | + /// Callback for cloning operations during inlining |
| 73 | + InlinerInterface::CloneCallbackTy cloneCallback = |
| 74 | + [](OpBuilder &builder, Region *src, Block *inlineBlock, |
| 75 | + Block *postInsertBlock, IRMapping &mapper, |
| 76 | + bool shouldCloneInlinedRegion) { |
| 77 | + // Check to see if the region is being cloned, or moved inline. In |
| 78 | + // either case, move the new blocks after the 'insertBlock' to improve |
| 79 | + // IR readability. |
| 80 | + Region *insertRegion = inlineBlock->getParent(); |
| 81 | + if (shouldCloneInlinedRegion) |
| 82 | + src->cloneInto(insertRegion, postInsertBlock->getIterator(), mapper); |
| 83 | + else |
| 84 | + insertRegion->getBlocks().splice(postInsertBlock->getIterator(), |
| 85 | + src->getBlocks(), src->begin(), |
| 86 | + src->end()); |
| 87 | + }; |
| 88 | + /// Determine if the inliner can inline a function containing multiple |
| 89 | + /// blocks into a region that requires a single block. By default, it is |
| 90 | + /// not allowed. If it is true, cloneCallback should perform the extra |
| 91 | + /// transformation. see the example in |
| 92 | + /// mlir/test/lib/Transforms/TestInliningCallback.cpp |
| 93 | + bool canHandleMultipleBlocks{false}; |
| 94 | +}; |
| 95 | + |
| 96 | +} // namespace mlir |
| 97 | + |
| 98 | +#endif // MLIR_TRANSFORMS_INLINER_CONFIG_H |
0 commit comments