41
41
#include " mlir/Interfaces/InferTypeOpInterface.h"
42
42
#include " mlir/Support/LLVM.h"
43
43
#include " mlir/Support/LogicalResult.h"
44
+ #include " mlir/Transforms/InliningUtils.h"
44
45
45
46
using namespace mlir ;
46
47
@@ -117,6 +118,40 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
117
118
.Default ([](Attribute) { return AliasResult::NoAlias; });
118
119
}
119
120
};
121
+
122
+ // Minimal interface to inline region with only one block for now (not handling
123
+ // the terminator remapping), assuming everything is inlinable.
124
+ struct CIRInlinerInterface : DialectInlinerInterface {
125
+ using DialectInlinerInterface::DialectInlinerInterface;
126
+ // Always allows inlining.
127
+ bool isLegalToInline (Operation *call, Operation *callable,
128
+ bool wouldBeCloned) const final override {
129
+ return true ;
130
+ }
131
+
132
+ // Always allows inlining.
133
+ bool isLegalToInline (Region *dest, Region *src, bool wouldBeCloned,
134
+ IRMapping &valueMapping) const final override {
135
+ return true ;
136
+ }
137
+
138
+ // Always allows inlining.
139
+ bool isLegalToInline (Operation *op, Region *, bool wouldBeCloned,
140
+ IRMapping &) const final override {
141
+ return true ;
142
+ }
143
+
144
+ // Handle the terminator in the case of a single block
145
+ void handleTerminator (Operation *op,
146
+ ValueRange valuesToReplace) const final override {
147
+ // Only handle cir.return for now
148
+ if (auto returnOp = dyn_cast<cir::ReturnOp>(op))
149
+ for (auto &&[value, operand] :
150
+ llvm::zip (valuesToReplace, returnOp.getOperands ()))
151
+ value.replaceAllUsesWith (operand);
152
+ }
153
+ };
154
+
120
155
} // namespace
121
156
122
157
// / Dialect initialization, the instance will be owned by the context. This is
@@ -128,7 +163,7 @@ void cir::CIRDialect::initialize() {
128
163
#define GET_OP_LIST
129
164
#include " clang/CIR/Dialect/IR/CIROps.cpp.inc"
130
165
>();
131
- addInterfaces<CIROpAsmDialectInterface>();
166
+ addInterfaces<CIRInlinerInterface, CIROpAsmDialectInterface>();
132
167
}
133
168
134
169
Operation *cir::CIRDialect::materializeConstant (mlir::OpBuilder &builder,
0 commit comments