Skip to content

Commit 7cc9ae9

Browse files
authored
[mlir] allow inlining complex ops (#77514)
Complex ops are pure ops just like the arithmetic ops so they can be inlined.
1 parent 6bc7e37 commit 7cc9ae9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,26 @@
1111
#include "mlir/Dialect/Complex/IR/Complex.h"
1212
#include "mlir/IR/Builders.h"
1313
#include "mlir/IR/DialectImplementation.h"
14+
#include "mlir/Transforms/InliningUtils.h"
1415
#include "llvm/ADT/StringExtras.h"
1516
#include "llvm/ADT/TypeSwitch.h"
1617

1718
using namespace mlir;
1819

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

22+
namespace {
23+
/// This class defines the interface for handling inlining for complex
24+
/// dialect operations.
25+
struct ComplexInlinerInterface : public DialectInlinerInterface {
26+
using DialectInlinerInterface::DialectInlinerInterface;
27+
/// All complex dialect ops can be inlined.
28+
bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
29+
return true;
30+
}
31+
};
32+
} // namespace
33+
2134
void complex::ComplexDialect::initialize() {
2235
addOperations<
2336
#define GET_OP_LIST
@@ -28,6 +41,7 @@ void complex::ComplexDialect::initialize() {
2841
#include "mlir/Dialect/Complex/IR/ComplexAttributes.cpp.inc"
2942
>();
3043
declarePromisedInterface<ComplexDialect, ConvertToLLVMPatternInterface>();
44+
addInterfaces<ComplexInlinerInterface>();
3145
}
3246

3347
Operation *complex::ComplexDialect::materializeConstant(OpBuilder &builder,

mlir/test/Transforms/inlining.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,23 @@ func.func @inline_convert_and_handle_attr_call(%arg0 : i16) -> (i16) {
297297
%res = "test.conversion_call_op"(%arg0) { callee=@handle_attr_callee_fn } : (i16) -> (i16)
298298
return %res : i16
299299
}
300+
301+
// Check a function with complex ops is inlined.
302+
func.func @double_square_complex(%cplx: complex<f32>) -> complex<f32> {
303+
%double = complex.add %cplx, %cplx : complex<f32>
304+
%square = complex.mul %double, %double : complex<f32>
305+
return %square : complex<f32>
306+
}
307+
308+
// CHECK-LABEL: func @inline_with_complex_ops
309+
func.func @inline_with_complex_ops() -> complex<f32> {
310+
%c1 = arith.constant 1.0 : f32
311+
%c2 = arith.constant 2.0 : f32
312+
%c = complex.create %c1, %c2 : complex<f32>
313+
314+
// CHECK: complex.add
315+
// CHECK: complex.mul
316+
// CHECK-NOT: call
317+
%r = call @double_square_complex(%c) : (complex<f32>) -> (complex<f32>)
318+
return %r : complex<f32>
319+
}

0 commit comments

Comments
 (0)