Skip to content

Commit 87b424b

Browse files
committed
[CIR] Add inline interface to CIR dialect
This allows the inliner to work with the CIR dialect.
1 parent 9577e42 commit 87b424b

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "mlir/Interfaces/InferTypeOpInterface.h"
4141
#include "mlir/Support/LLVM.h"
4242
#include "mlir/Support/LogicalResult.h"
43+
#include "mlir/Transforms/InliningUtils.h"
4344

4445
using namespace mlir;
4546

@@ -114,6 +115,40 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
114115
return AliasResult::NoAlias;
115116
}
116117
};
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+
117152
} // namespace
118153

119154
/// Dialect initialization, the instance will be owned by the context. This is
@@ -125,7 +160,7 @@ void cir::CIRDialect::initialize() {
125160
#define GET_OP_LIST
126161
#include "clang/CIR/Dialect/IR/CIROps.cpp.inc"
127162
>();
128-
addInterfaces<CIROpAsmDialectInterface>();
163+
addInterfaces<CIRInlinerInterface, CIROpAsmDialectInterface>();
129164
}
130165

131166
Operation *cir::CIRDialect::materializeConstant(mlir::OpBuilder &builder,

0 commit comments

Comments
 (0)