Skip to content

Commit fa2c8f7

Browse files
committed
[CIR] Add inline interface to CIR dialect
This allows the inliner to work with the CIR dialect.
1 parent b0f04c4 commit fa2c8f7

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

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

121156
/// Dialect initialization, the instance will be owned by the context. This is
@@ -127,7 +162,7 @@ void cir::CIRDialect::initialize() {
127162
#define GET_OP_LIST
128163
#include "clang/CIR/Dialect/IR/CIROps.cpp.inc"
129164
>();
130-
addInterfaces<CIROpAsmDialectInterface>();
165+
addInterfaces<CIRInlinerInterface, CIROpAsmDialectInterface>();
131166
}
132167

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

0 commit comments

Comments
 (0)