Skip to content

Commit 754364d

Browse files
committed
[CIR] Add inline interface to CIR dialect
This allows the inliner to work with the CIR dialect.
1 parent 0cdda56 commit 754364d

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
@@ -41,6 +41,7 @@
4141
#include "mlir/Interfaces/InferTypeOpInterface.h"
4242
#include "mlir/Support/LLVM.h"
4343
#include "mlir/Support/LogicalResult.h"
44+
#include "mlir/Transforms/InliningUtils.h"
4445

4546
using namespace mlir;
4647

@@ -117,6 +118,40 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
117118
.Default([](Attribute) { return AliasResult::NoAlias; });
118119
}
119120
};
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+
120155
} // namespace
121156

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

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

0 commit comments

Comments
 (0)