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