Skip to content

Commit

Permalink
Add vm.const.i{32,64}.zero to emitc conversions (#5066)
Browse files Browse the repository at this point in the history
  • Loading branch information
marbre authored Mar 11, 2021
1 parent 93a1ff1 commit 565c97c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
17 changes: 17 additions & 0 deletions iree/compiler/Dialect/VM/Conversion/VMToEmitC/ConvertVMToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ class ConstOpConversion : public OpRewritePattern<ConstOpTy> {
}
};

template <typename ConstZeroOpTy>
class ConstZeroOpConversion : public OpRewritePattern<ConstZeroOpTy> {
public:
using OpRewritePattern<ConstZeroOpTy>::OpRewritePattern;

LogicalResult matchAndRewrite(ConstZeroOpTy constZeroOp,
PatternRewriter &rewriter) const final {
auto type = constZeroOp.getType();
IntegerAttr value = rewriter.getIntegerAttr(type, 0);

rewriter.replaceOpWithNewOp<emitc::ConstOp>(constZeroOp, type, value);
return success();
}
};

template <typename LoadOpTy, typename GlobalOpTy>
class GlobalLoadOpConversion : public OpConversionPattern<LoadOpTy> {
using OpConversionPattern<LoadOpTy>::OpConversionPattern;
Expand Down Expand Up @@ -190,6 +205,7 @@ void populateVMToCPatterns(MLIRContext *context,

// Constants
patterns.insert<ConstOpConversion<IREE::VM::ConstI32Op>>(context);
patterns.insert<ConstZeroOpConversion<IREE::VM::ConstI32ZeroOp>>(context);

// Conditional assignment ops
patterns.insert<CallOpConversion<IREE::VM::SelectI32Op>>(context,
Expand Down Expand Up @@ -253,6 +269,7 @@ void populateVMToCPatterns(MLIRContext *context,

// ExtI64: Constants
patterns.insert<ConstOpConversion<IREE::VM::ConstI64Op>>(context);
patterns.insert<ConstZeroOpConversion<IREE::VM::ConstI64ZeroOp>>(context);

// ExtI64: Conditional assignment ops
patterns.insert<CallOpConversion<IREE::VM::SelectI64Op>>(context,
Expand Down
16 changes: 13 additions & 3 deletions iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops.mlir
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
// RUN: iree-opt -split-input-file -pass-pipeline='vm.module(iree-convert-vm-to-emitc)' %s | IreeFileCheck %s

// CHECK: vm.module @module {
vm.module @module {

vm.module @my_module {
// CHECK-LABEL: vm.func @const_i32_zero
vm.func @const_i32_zero() -> i32 {
// CHECK: %zero = "emitc.const"() {value = 0 : i32} : () -> i32
%zero = vm.const.i32.zero : i32
vm.return %zero : i32
}
}

// -----

vm.module @my_module {
// CHECK-LABEL: vm.func @const_i32
vm.func @const_i32() {
// CHECK-NEXT: %0 = "emitc.const"() {value = 0 : i32} : () -> i32
Expand All @@ -10,7 +21,6 @@ vm.module @module {
%1 = vm.const.i32 2 : i32
// CHECK-NEXT: %2 = "emitc.const"() {value = -2 : i32} : () -> i32
%2 = vm.const.i32 -2 : i32
// CHECK-NEXT: vm.return
vm.return
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
// RUN: iree-opt -split-input-file -pass-pipeline='vm.module(iree-convert-vm-to-emitc)' %s | IreeFileCheck %s

// CHECK: vm.module @module {
vm.module @module {

vm.module @my_module {
// CHECK-LABEL: vm.func @const_i64_zero
vm.func @const_i64_zero() -> i64 {
// CHECK: %zero = "emitc.const"() {value = 0 : i64} : () -> i64
%zero = vm.const.i64.zero : i64
vm.return %zero : i64
}
}

// -----

vm.module @my_module {
// CHECK-LABEL: vm.func @const_i64
vm.func @const_i64() {
// CHECK-NEXT: %0 = "emitc.const"() {value = 0 : i64} : () -> i64
Expand All @@ -10,7 +21,6 @@ vm.module @module {
%1 = vm.const.i64 2 : i64
// CHECK-NEXT: %2 = "emitc.const"() {value = -2 : i64} : () -> i64
%2 = vm.const.i64 -2 : i64
// CHECK-NEXT: vm.return
vm.return
}
}

0 comments on commit 565c97c

Please sign in to comment.