Skip to content

Commit

Permalink
[mlir] allow inlining complex ops (llvm#77514)
Browse files Browse the repository at this point in the history
Complex ops are pure ops just like the arithmetic ops so they can be
inlined.
  • Loading branch information
okkwon authored and justinfargnoli committed Jan 28, 2024
1 parent d7b32b0 commit 7c6cf0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
Expand Up @@ -11,13 +11,26 @@
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/Transforms/InliningUtils.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"

using namespace mlir;

#include "mlir/Dialect/Complex/IR/ComplexOpsDialect.cpp.inc"

namespace {
/// This class defines the interface for handling inlining for complex
/// dialect operations.
struct ComplexInlinerInterface : public DialectInlinerInterface {
using DialectInlinerInterface::DialectInlinerInterface;
/// All complex dialect ops can be inlined.
bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
return true;
}
};
} // namespace

void complex::ComplexDialect::initialize() {
addOperations<
#define GET_OP_LIST
Expand All @@ -28,6 +41,7 @@ void complex::ComplexDialect::initialize() {
#include "mlir/Dialect/Complex/IR/ComplexAttributes.cpp.inc"
>();
declarePromisedInterface<ComplexDialect, ConvertToLLVMPatternInterface>();
addInterfaces<ComplexInlinerInterface>();
}

Operation *complex::ComplexDialect::materializeConstant(OpBuilder &builder,
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/Transforms/inlining.mlir
Expand Up @@ -297,3 +297,23 @@ func.func @inline_convert_and_handle_attr_call(%arg0 : i16) -> (i16) {
%res = "test.conversion_call_op"(%arg0) { callee=@handle_attr_callee_fn } : (i16) -> (i16)
return %res : i16
}

// Check a function with complex ops is inlined.
func.func @double_square_complex(%cplx: complex<f32>) -> complex<f32> {
%double = complex.add %cplx, %cplx : complex<f32>
%square = complex.mul %double, %double : complex<f32>
return %square : complex<f32>
}

// CHECK-LABEL: func @inline_with_complex_ops
func.func @inline_with_complex_ops() -> complex<f32> {
%c1 = arith.constant 1.0 : f32
%c2 = arith.constant 2.0 : f32
%c = complex.create %c1, %c2 : complex<f32>

// CHECK: complex.add
// CHECK: complex.mul
// CHECK-NOT: call
%r = call @double_square_complex(%c) : (complex<f32>) -> (complex<f32>)
return %r : complex<f32>
}

0 comments on commit 7c6cf0a

Please sign in to comment.