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