Skip to content

[mlir][builtin] Make unrealized_conversion_cast inlineable #139722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mlir/include/mlir/InitAllDialects.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
#include "mlir/Target/LLVM/NVVM/Target.h"
#include "mlir/Target/LLVM/ROCDL/Target.h"
#include "mlir/Target/SPIRV/Target.h"
#include "mlir/Transforms/InliningUtils.h"

namespace mlir {

Expand Down Expand Up @@ -166,6 +167,7 @@ inline void registerAllDialects(DialectRegistry &registry) {
bufferization::func_ext::registerBufferizableOpInterfaceExternalModels(
registry);
builtin::registerCastOpInterfaceExternalModels(registry);
builtin::registerBuiltinDialectInlinerInterfaceExternalModel(registry);
cf::registerBufferizableOpInterfaceExternalModels(registry);
cf::registerBufferDeallocationOpInterfaceExternalModels(registry);
gpu::registerBufferDeallocationOpInterfaceExternalModels(registry);
Expand Down
6 changes: 6 additions & 0 deletions mlir/include/mlir/Transforms/InliningUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ inlineCall(InlinerInterface &interface,
CallOpInterface call, CallableOpInterface callable, Region *src,
bool shouldCloneInlinedRegion = true);

namespace builtin {
/// Register the builtin dialect inliner interface external model.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add .. ' e.g. unrealized_conversion_cast'

void registerBuiltinDialectInlinerInterfaceExternalModel(
DialectRegistry &registry);
} // namespace builtin

} // namespace mlir

#endif // MLIR_TRANSFORMS_INLININGUTILS_H
21 changes: 21 additions & 0 deletions mlir/lib/Transforms/Utils/InliningUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "mlir/Transforms/InliningUtils.h"

#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/Operation.h"
#include "mlir/Interfaces/CallInterfaces.h"
Expand Down Expand Up @@ -557,3 +558,23 @@ LogicalResult mlir::inlineCall(
return cleanupState();
return success();
}

namespace {
/// This class defines the interface for handling inlining with builtin
/// operations.
struct BuiltinInlinerInterface : public DialectInlinerInterface {
using DialectInlinerInterface::DialectInlinerInterface;

/// All builtin ops can be inlined.
bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
return true;
}
};
} // namespace

void mlir::builtin::registerBuiltinDialectInlinerInterfaceExternalModel(
DialectRegistry &registry) {
registry.addExtension(+[](MLIRContext *ctx, BuiltinDialect *dialect) {
dialect->addInterfaces<BuiltinInlinerInterface>();
});
}
10 changes: 7 additions & 3 deletions mlir/test/Transforms/inlining.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
// RUN: mlir-opt %s -inline='op-pipelines=func.func(canonicalize,cse)' | FileCheck %s --check-prefix INLINE_SIMPLIFY

// Inline a function that takes an argument.
func.func @func_with_arg(%c : i32) -> i32 {
%b = arith.addi %c, %c : i32
return %b : i32
func.func @func_with_arg(%arg0 : i32) -> i32 {
%b = arith.addi %arg0, %arg0 : i32
%c = builtin.unrealized_conversion_cast %b : i32 to i64
%d = builtin.unrealized_conversion_cast %c : i64 to i32
return %d : i32
}

// CHECK-LABEL: func @inline_with_arg
func.func @inline_with_arg(%arg0 : i32) -> i32 {
// CHECK-NEXT: arith.addi
// CHECK-NEXT: unrealized_conversion_cast
// CHECK-NEXT: unrealized_conversion_cast
// CHECK-NEXT: return

%0 = call @func_with_arg(%arg0) : (i32) -> i32
Expand Down