Skip to content

Commit

Permalink
[CIR] Add inline interface to CIR dialect
Browse files Browse the repository at this point in the history
This allows the inliner to work with the CIR dialect.
  • Loading branch information
keryell committed Nov 25, 2024
1 parent 3fbb13e commit 983da6d
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/InliningUtils.h"

using namespace mlir;

Expand Down Expand Up @@ -114,6 +115,40 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
return AliasResult::NoAlias;
}
};

// Minimal interface to inline region with only one block for now (not handling
// the terminator remapping), assuming everything is inlinable.
struct CIRInlinerInterface : DialectInlinerInterface {
using DialectInlinerInterface::DialectInlinerInterface;
// Always allows inlining.
bool isLegalToInline(Operation *call, Operation *callable,
bool wouldBeCloned) const final override {
return true;
}

// Always allows inlining.
bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
IRMapping &valueMapping) const final override {
return true;
}

// Always allows inlining.
bool isLegalToInline(Operation *op, Region *, bool wouldBeCloned,
IRMapping &) const final override {
return true;
}

// Handle the terminator in the case of a single block
void handleTerminator(Operation *op,
ValueRange valuesToReplace) const final override {
// Only handle cir.return for now
if (auto returnOp = dyn_cast<cir::ReturnOp>(op))
for (auto &&[value, operand] :
llvm::zip(valuesToReplace, returnOp.getOperands()))
value.replaceAllUsesWith(operand);
}
};

} // namespace

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

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

0 comments on commit 983da6d

Please sign in to comment.