From 565c97c381e46bd8eaf8618783de1a462cc9f962 Mon Sep 17 00:00:00 2001 From: Marius Brehler Date: Thu, 11 Mar 2021 19:45:28 +0100 Subject: [PATCH] Add vm.const.i{32,64}.zero to emitc conversions (#5066) --- .../Conversion/VMToEmitC/ConvertVMToEmitC.cpp | 17 +++++++++++++++++ .../VM/Conversion/VMToEmitC/test/const_ops.mlir | 16 +++++++++++++--- .../VMToEmitC/test/const_ops_i64.mlir | 16 +++++++++++++--- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/iree/compiler/Dialect/VM/Conversion/VMToEmitC/ConvertVMToEmitC.cpp b/iree/compiler/Dialect/VM/Conversion/VMToEmitC/ConvertVMToEmitC.cpp index a788be853c31..9664c6789c06 100644 --- a/iree/compiler/Dialect/VM/Conversion/VMToEmitC/ConvertVMToEmitC.cpp +++ b/iree/compiler/Dialect/VM/Conversion/VMToEmitC/ConvertVMToEmitC.cpp @@ -103,6 +103,21 @@ class ConstOpConversion : public OpRewritePattern { } }; +template +class ConstZeroOpConversion : public OpRewritePattern { + public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(ConstZeroOpTy constZeroOp, + PatternRewriter &rewriter) const final { + auto type = constZeroOp.getType(); + IntegerAttr value = rewriter.getIntegerAttr(type, 0); + + rewriter.replaceOpWithNewOp(constZeroOp, type, value); + return success(); + } +}; + template class GlobalLoadOpConversion : public OpConversionPattern { using OpConversionPattern::OpConversionPattern; @@ -190,6 +205,7 @@ void populateVMToCPatterns(MLIRContext *context, // Constants patterns.insert>(context); + patterns.insert>(context); // Conditional assignment ops patterns.insert>(context, @@ -253,6 +269,7 @@ void populateVMToCPatterns(MLIRContext *context, // ExtI64: Constants patterns.insert>(context); + patterns.insert>(context); // ExtI64: Conditional assignment ops patterns.insert>(context, diff --git a/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops.mlir b/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops.mlir index 451bf7d81904..b17486cc2839 100644 --- a/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops.mlir +++ b/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops.mlir @@ -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 @@ -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 } } diff --git a/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops_i64.mlir b/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops_i64.mlir index 89b205ad8a1a..0161dc4bc29e 100644 --- a/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops_i64.mlir +++ b/iree/compiler/Dialect/VM/Conversion/VMToEmitC/test/const_ops_i64.mlir @@ -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 @@ -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 } }