Skip to content

Commit ab50b7c

Browse files
committed
[MLIR] Split InlinerConfig into seperate header and add pass overload with it
1 parent c59cc54 commit ab50b7c

File tree

5 files changed

+123
-96
lines changed

5 files changed

+123
-96
lines changed

mlir/include/mlir/Transforms/Inliner.h

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,85 +17,14 @@
1717
#include "mlir/Interfaces/CallInterfaces.h"
1818
#include "mlir/Pass/AnalysisManager.h"
1919
#include "mlir/Pass/PassManager.h"
20+
#include "mlir/Transforms/InlinerConfig.h"
2021
#include "mlir/Transforms/InliningUtils.h"
2122
#include "llvm/ADT/StringMap.h"
2223

2324
namespace mlir {
2425
class OpPassManager;
2526
class Operation;
2627

27-
class InlinerConfig {
28-
public:
29-
using DefaultPipelineTy = std::function<void(OpPassManager &)>;
30-
using OpPipelinesTy = llvm::StringMap<OpPassManager>;
31-
32-
InlinerConfig() = default;
33-
InlinerConfig(DefaultPipelineTy defaultPipeline,
34-
unsigned maxInliningIterations)
35-
: defaultPipeline(std::move(defaultPipeline)),
36-
maxInliningIterations(maxInliningIterations) {}
37-
38-
const DefaultPipelineTy &getDefaultPipeline() const {
39-
return defaultPipeline;
40-
}
41-
const OpPipelinesTy &getOpPipelines() const { return opPipelines; }
42-
unsigned getMaxInliningIterations() const { return maxInliningIterations; }
43-
const InlinerInterface::CloneCallbackTy &getCloneCallback() const {
44-
return cloneCallback;
45-
}
46-
bool getCanHandleMultipleBlocks() const { return canHandleMultipleBlocks; }
47-
48-
void setDefaultPipeline(DefaultPipelineTy pipeline) {
49-
defaultPipeline = std::move(pipeline);
50-
}
51-
void setOpPipelines(OpPipelinesTy pipelines) {
52-
opPipelines = std::move(pipelines);
53-
}
54-
void setMaxInliningIterations(unsigned max) { maxInliningIterations = max; }
55-
void setCloneCallback(InlinerInterface::CloneCallbackTy callback) {
56-
cloneCallback = std::move(callback);
57-
}
58-
void setCanHandleMultipleBlocks(bool value = true) {
59-
canHandleMultipleBlocks = value;
60-
}
61-
62-
private:
63-
/// An optional function that constructs an optimization pipeline for
64-
/// a given operation. This optimization pipeline is applied
65-
/// only to those callable operations that do not have dedicated
66-
/// optimization pipeline in opPipelines (based on the operation name).
67-
DefaultPipelineTy defaultPipeline;
68-
/// A map of operation names to pass pipelines to use when optimizing
69-
/// callable operations of these types. This provides a specialized pipeline
70-
/// instead of the one produced by defaultPipeline.
71-
OpPipelinesTy opPipelines;
72-
/// For SCC-based inlining algorithms, specifies maximum number of iterations
73-
/// when inlining within an SCC.
74-
unsigned maxInliningIterations{0};
75-
/// Callback for cloning operations during inlining
76-
InlinerInterface::CloneCallbackTy cloneCallback =
77-
[](OpBuilder &builder, Region *src, Block *inlineBlock,
78-
Block *postInsertBlock, IRMapping &mapper,
79-
bool shouldCloneInlinedRegion) {
80-
// Check to see if the region is being cloned, or moved inline. In
81-
// either case, move the new blocks after the 'insertBlock' to improve
82-
// IR readability.
83-
Region *insertRegion = inlineBlock->getParent();
84-
if (shouldCloneInlinedRegion)
85-
src->cloneInto(insertRegion, postInsertBlock->getIterator(), mapper);
86-
else
87-
insertRegion->getBlocks().splice(postInsertBlock->getIterator(),
88-
src->getBlocks(), src->begin(),
89-
src->end());
90-
};
91-
/// Determine if the inliner can inline a function containing multiple
92-
/// blocks into a region that requires a single block. By default, it is
93-
/// not allowed. If it is true, cloneCallback should perform the extra
94-
/// transformation. see the example in
95-
/// mlir/test/lib/Transforms/TestInliningCallback.cpp
96-
bool canHandleMultipleBlocks{false};
97-
};
98-
9928
/// This is an implementation of the inliner
10029
/// that operates bottom up over the Strongly Connected Components(SCCs)
10130
/// of the CallGraph. This enables a more incremental propagation
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

mlir/include/mlir/Transforms/Passes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "mlir/Pass/Pass.h"
1818
#include "mlir/Pass/PassManager.h"
1919
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
20+
#include "mlir/Transforms/InlinerConfig.h"
2021
#include "mlir/Transforms/LocationSnapshot.h"
2122
#include "mlir/Transforms/ViewOpGraph.h"
2223
#include "llvm/Support/Debug.h"
@@ -111,6 +112,10 @@ createInlinerPass(llvm::StringMap<OpPassManager> opPipelines);
111112
std::unique_ptr<Pass>
112113
createInlinerPass(llvm::StringMap<OpPassManager> opPipelines,
113114
std::function<void(OpPassManager &)> defaultPipelineBuilder);
115+
/// Creates an instance of the inliner pass, using the provided config and
116+
/// threshold.
117+
std::unique_ptr<Pass> createInlinerPass(const InlinerConfig &inlinerConfig,
118+
unsigned inliningThreshold = -1);
114119

115120
/// Creates an optimization pass to remove dead values.
116121
std::unique_ptr<Pass> createRemoveDeadValuesPass();

mlir/lib/Transforms/InlinerPass.cpp

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ static void defaultInlinerOptPipeline(OpPassManager &pm) {
4040
namespace {
4141
class InlinerPass : public impl::InlinerBase<InlinerPass> {
4242
public:
43-
InlinerPass();
44-
InlinerPass(const InlinerPass &) = default;
45-
InlinerPass(std::function<void(OpPassManager &)> defaultPipeline);
46-
InlinerPass(std::function<void(OpPassManager &)> defaultPipeline,
47-
llvm::StringMap<OpPassManager> opPipelines);
43+
InlinerPass(const InlinerConfig &inlinerConfig, unsigned inliningThreshold_);
4844
void runOnOperation() override;
4945

5046
/// A callback provided to the inliner driver to execute
@@ -73,23 +69,14 @@ class InlinerPass : public impl::InlinerBase<InlinerPass> {
7369
};
7470
} // namespace
7571

76-
InlinerPass::InlinerPass() : InlinerPass(defaultInlinerOptPipeline) {}
77-
78-
InlinerPass::InlinerPass(
79-
std::function<void(OpPassManager &)> defaultPipelineArg)
80-
: InlinerPass(std::move(defaultPipelineArg),
81-
llvm::StringMap<OpPassManager>{}) {}
82-
83-
InlinerPass::InlinerPass(std::function<void(OpPassManager &)> defaultPipeline,
84-
llvm::StringMap<OpPassManager> opPipelines)
85-
: config(std::move(defaultPipeline), maxInliningIterations) {
86-
if (opPipelines.empty())
87-
return;
72+
InlinerPass::InlinerPass(const InlinerConfig &inlinerConfig,
73+
unsigned inliningThreshold_)
74+
: config(inlinerConfig) {
75+
inliningThreshold = inliningThreshold_;
8876

8977
// Update the option for the op specific optimization pipelines.
90-
for (auto &it : opPipelines)
78+
for (auto &it : config.getOpPipelines())
9179
opPipelineList.addValue(it.second);
92-
config.setOpPipelines(std::move(opPipelines));
9380
}
9481

9582
// Return true if the inlining ratio does not exceed the threshold.
@@ -183,16 +170,23 @@ LogicalResult InlinerPass::initializeOptions(
183170
}
184171

185172
std::unique_ptr<Pass> mlir::createInlinerPass() {
186-
return std::make_unique<InlinerPass>();
173+
return createInlinerPass(llvm::StringMap<OpPassManager>{});
187174
}
188175
std::unique_ptr<Pass>
189176
mlir::createInlinerPass(llvm::StringMap<OpPassManager> opPipelines) {
190-
return std::make_unique<InlinerPass>(defaultInlinerOptPipeline,
191-
std::move(opPipelines));
177+
return createInlinerPass(std::move(opPipelines), defaultInlinerOptPipeline);
192178
}
193179
std::unique_ptr<Pass> mlir::createInlinerPass(
194180
llvm::StringMap<OpPassManager> opPipelines,
195181
std::function<void(OpPassManager &)> defaultPipelineBuilder) {
196-
return std::make_unique<InlinerPass>(std::move(defaultPipelineBuilder),
197-
std::move(opPipelines));
182+
InlinerConfig config;
183+
184+
config.setDefaultPipeline(std::move(defaultPipelineBuilder));
185+
config.setOpPipelines(std::move(opPipelines));
186+
187+
return createInlinerPass(config);
188+
}
189+
std::unique_ptr<Pass> createInlinerPass(const InlinerConfig &inlinerConfig,
190+
unsigned inliningThreshold) {
191+
return std::make_unique<InlinerPass>(inlinerConfig, inliningThreshold);
198192
}

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7342,6 +7342,7 @@ cc_library(
73427342
"include/mlir/Transforms/FoldUtils.h",
73437343
"include/mlir/Transforms/GreedyPatternRewriteDriver.h",
73447344
"include/mlir/Transforms/Inliner.h",
7345+
"include/mlir/Transforms/InlinerConfig.h",
73457346
"include/mlir/Transforms/LoopInvariantCodeMotionUtils.h",
73467347
"include/mlir/Transforms/RegionUtils.h",
73477348
"include/mlir/Transforms/WalkPatternRewriteDriver.h",

0 commit comments

Comments
 (0)