[mlir][emitc] Arith to EmitC conversion: constants (#83798)

* Add a conversion from `arith.constant` to `emitc.constant`.
* Drop the translation for `arith.constant`s.
This commit is contained in:
Tina Jung 2024-03-08 09:16:10 +01:00 committed by GitHub
parent 9f5be5f009
commit 0ddb122147
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 70 additions and 66 deletions

View File

@ -30,5 +30,3 @@ translating the following operations:
* `func.call`
* `func.func`
* `func.return`
* 'arith' Dialect
* `arith.constant`

View File

@ -24,6 +24,21 @@ using namespace mlir;
//===----------------------------------------------------------------------===//
namespace {
class ArithConstantOpConversionPattern
: public OpConversionPattern<arith::ConstantOp> {
public:
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(arith::ConstantOp arithConst,
arith::ConstantOp::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<emitc::ConstantOp>(
arithConst, arithConst.getType(), adaptor.getValue());
return success();
}
};
template <typename ArithOp, typename EmitCOp>
class ArithOpConversion final : public OpConversionPattern<ArithOp> {
public:
@ -51,6 +66,7 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
// clang-format off
patterns.add<
ArithConstantOpConversionPattern,
ArithOpConversion<arith::AddFOp, emitc::AddOp>,
ArithOpConversion<arith::DivFOp, emitc::DivOp>,
ArithOpConversion<arith::MulFOp, emitc::MulOp>,

View File

@ -38,7 +38,6 @@ void ConvertArithToEmitC::runOnOperation() {
target.addLegalDialect<emitc::EmitCDialect>();
target.addIllegalDialect<arith::ArithDialect>();
target.addLegalOp<arith::ConstantOp>();
RewritePatternSet patterns(&getContext());

View File

@ -6,7 +6,6 @@ add_mlir_translation_library(MLIRTargetCpp
${EMITC_MAIN_INCLUDE_DIR}/emitc/Target/Cpp
LINK_LIBS PUBLIC
MLIRArithDialect
MLIRControlFlowDialect
MLIREmitCDialect
MLIRFuncDialect

View File

@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlow.h"
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
@ -41,8 +40,7 @@ void registerToCppTranslation() {
},
[](DialectRegistry &registry) {
// clang-format off
registry.insert<arith::ArithDialect,
cf::ControlFlowDialect,
registry.insert<cf::ControlFlowDialect,
emitc::EmitCDialect,
func::FuncDialect,
math::MathDialect,

View File

@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
@ -335,14 +334,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
return printConstantOp(emitter, operation, value);
}
static LogicalResult printOperation(CppEmitter &emitter,
arith::ConstantOp constantOp) {
Operation *operation = constantOp.getOperation();
Attribute value = constantOp.getValue();
return printConstantOp(emitter, operation, value);
}
static LogicalResult printOperation(CppEmitter &emitter,
emitc::AssignOp assignOp) {
auto variableOp = cast<emitc::VariableOp>(assignOp.getVar().getDefiningOp());
@ -1391,9 +1382,6 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
// Func ops.
.Case<func::CallOp, func::FuncOp, func::ReturnOp>(
[&](auto op) { return printOperation(*this, op); })
// Arithmetic ops.
.Case<arith::ConstantOp>(
[&](auto op) { return printOperation(*this, op); })
.Case<emitc::LiteralOp>([&](auto op) { return success(); })
.Default([&](Operation *) {
return op.emitOpError("unable to find printer for op");

View File

@ -1,4 +1,26 @@
// RUN: mlir-opt -convert-arith-to-emitc %s | FileCheck %s
// RUN: mlir-opt -split-input-file -convert-arith-to-emitc %s | FileCheck %s
// CHECK-LABEL: arith_constants
func.func @arith_constants() {
// CHECK: emitc.constant
// CHECK-SAME: value = 0 : index
%c_index = arith.constant 0 : index
// CHECK: emitc.constant
// CHECK-SAME: value = 0 : i32
%c_signless_int_32 = arith.constant 0 : i32
// CHECK: emitc.constant
// CHECK-SAME: value = 0.{{0+}}e+00 : f32
%c_float_32 = arith.constant 0.0 : f32
// CHECK: emitc.constant
// CHECK-SAME: value = dense<0> : tensor<i32>
%c_tensor_single_value = arith.constant dense<0> : tensor<i32>
// CHECK: emitc.constant
// CHECK-SAME: value{{.*}}[1, 2], [-3, 9], [0, 0], [2, -1]{{.*}}tensor<4x2xi64>
%c_tensor_value = arith.constant dense<[[1, 2], [-3, 9], [0, 0], [2, -1]]> : tensor<4x2xi64>
return
}
// -----
func.func @arith_ops(%arg0: f32, %arg1: f32) {
// CHECK: [[V0:[^ ]*]] = emitc.add %arg0, %arg1 : (f32, f32) -> f32

View File

@ -18,7 +18,7 @@ func.func @emitc_call_opaque() {
func.func @emitc_call_opaque_two_results() {
%0 = arith.constant 0 : index
%0 = "emitc.constant"() <{value = 0 : index}> : () -> index
%1:2 = emitc.call_opaque "two_results" () : () -> (i32, i32)
return
}

View File

@ -8,6 +8,11 @@ func.func @emitc_constant() {
%c3 = "emitc.constant"(){value = -1 : si8} : () -> si8
%c4 = "emitc.constant"(){value = 255 : ui8} : () -> ui8
%c5 = "emitc.constant"(){value = #emitc.opaque<"CHAR_MIN">} : () -> !emitc.opaque<"char">
%c6 = "emitc.constant"(){value = 2 : index} : () -> index
%c7 = "emitc.constant"(){value = 2.0 : f32} : () -> f32
%c8 = "emitc.constant"(){value = dense<0> : tensor<i32>} : () -> tensor<i32>
%c9 = "emitc.constant"(){value = dense<[0, 1]> : tensor<2xindex>} : () -> tensor<2xindex>
%c10 = "emitc.constant"(){value = dense<[[0.0, 1.0], [2.0, 3.0]]> : tensor<2x2xf32>} : () -> tensor<2x2xf32>
return
}
// CPP-DEFAULT: void emitc_constant() {
@ -17,6 +22,11 @@ func.func @emitc_constant() {
// CPP-DEFAULT-NEXT: int8_t [[V3:[^ ]*]] = -1;
// CPP-DEFAULT-NEXT: uint8_t [[V4:[^ ]*]] = 255;
// CPP-DEFAULT-NEXT: char [[V5:[^ ]*]] = CHAR_MIN;
// CPP-DEFAULT-NEXT: size_t [[V6:[^ ]*]] = 2;
// CPP-DEFAULT-NEXT: float [[V7:[^ ]*]] = (float)2.000000000e+00;
// CPP-DEFAULT-NEXT: Tensor<int32_t> [[V8:[^ ]*]] = {0};
// CPP-DEFAULT-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]] = {0, 1};
// CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};
// CPP-DECLTOP: void emitc_constant() {
// CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
@ -25,9 +35,19 @@ func.func @emitc_constant() {
// CPP-DECLTOP-NEXT: int8_t [[V3:[^ ]*]];
// CPP-DECLTOP-NEXT: uint8_t [[V4:[^ ]*]];
// CPP-DECLTOP-NEXT: char [[V5:[^ ]*]];
// CPP-DECLTOP-NEXT: size_t [[V6:[^ ]*]];
// CPP-DECLTOP-NEXT: float [[V7:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<int32_t> [[V8:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]];
// CPP-DECLTOP-NEXT: [[V0]] = INT_MAX;
// CPP-DECLTOP-NEXT: [[V1]] = 42;
// CPP-DECLTOP-NEXT: [[V2]] = -1;
// CPP-DECLTOP-NEXT: [[V3]] = -1;
// CPP-DECLTOP-NEXT: [[V4]] = 255;
// CPP-DECLTOP-NEXT: [[V5]] = CHAR_MIN;
// CPP-DECLTOP-NEXT: [[V6]] = 2;
// CPP-DECLTOP-NEXT: [[V7]] = (float)2.000000000e+00;
// CPP-DECLTOP-NEXT: [[V8]] = {0};
// CPP-DECLTOP-NEXT: [[V9]] = {0, 1};
// CPP-DECLTOP-NEXT: [[V10]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};

View File

@ -33,12 +33,12 @@ func.func @test_for(%arg0 : index, %arg1 : index, %arg2 : index) {
// CPP-DECLTOP-NEXT: return;
func.func @test_for_yield() {
%start = arith.constant 0 : index
%stop = arith.constant 10 : index
%step = arith.constant 1 : index
%start = "emitc.constant"() <{value = 0 : index}> : () -> index
%stop = "emitc.constant"() <{value = 10 : index}> : () -> index
%step = "emitc.constant"() <{value = 1 : index}> : () -> index
%s0 = arith.constant 0 : i32
%p0 = arith.constant 1.0 : f32
%s0 = "emitc.constant"() <{value = 0 : i32}> : () -> i32
%p0 = "emitc.constant"() <{value = 1.0 : f32}> : () -> f32
%0 = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> i32
%1 = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> f32

View File

@ -49,7 +49,7 @@ func.func @test_if_else(%arg0: i1, %arg1: f32) {
func.func @test_if_yield(%arg0: i1, %arg1: f32) {
%0 = arith.constant 0 : i8
%0 = "emitc.constant"() <{value = 0 : i8}> : () -> i8
%x = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> i32
%y = "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> f64
emitc.if %arg0 {

View File

@ -1,37 +1,6 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
func.func @std_constant() {
%c0 = arith.constant 0 : i32
%c1 = arith.constant 2 : index
%c2 = arith.constant 2.0 : f32
%c3 = arith.constant dense<0> : tensor<i32>
%c4 = arith.constant dense<[0, 1]> : tensor<2xindex>
%c5 = arith.constant dense<[[0.0, 1.0], [2.0, 3.0]]> : tensor<2x2xf32>
return
}
// CPP-DEFAULT: void std_constant() {
// CPP-DEFAULT-NEXT: int32_t [[V0:[^ ]*]] = 0;
// CPP-DEFAULT-NEXT: size_t [[V1:[^ ]*]] = 2;
// CPP-DEFAULT-NEXT: float [[V2:[^ ]*]] = (float)2.000000000e+00;
// CPP-DEFAULT-NEXT: Tensor<int32_t> [[V3:[^ ]*]] = {0};
// CPP-DEFAULT-NEXT: Tensor<size_t, 2> [[V4:[^ ]*]] = {0, 1};
// CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V5:[^ ]*]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};
// CPP-DECLTOP: void std_constant() {
// CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
// CPP-DECLTOP-NEXT: size_t [[V1:[^ ]*]];
// CPP-DECLTOP-NEXT: float [[V2:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<int32_t> [[V3:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<size_t, 2> [[V4:[^ ]*]];
// CPP-DECLTOP-NEXT: Tensor<float, 2, 2> [[V5:[^ ]*]];
// CPP-DECLTOP-NEXT: [[V0]] = 0;
// CPP-DECLTOP-NEXT: [[V1]] = 2;
// CPP-DECLTOP-NEXT: [[V2]] = (float)2.000000000e+00;
// CPP-DECLTOP-NEXT: [[V3]] = {0};
// CPP-DECLTOP-NEXT: [[V4]] = {0, 1};
// CPP-DECLTOP-NEXT: [[V5]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};
func.func @std_call() {
%0 = call @one_result () : () -> i32
%1 = call @one_result () : () -> i32
@ -49,13 +18,11 @@ func.func @std_call() {
func.func @std_call_two_results() {
%c = arith.constant 0 : i8
%0:2 = call @two_results () : () -> (i32, f32)
%1:2 = call @two_results () : () -> (i32, f32)
return
}
// CPP-DEFAULT: void std_call_two_results() {
// CPP-DEFAULT-NEXT: int8_t [[V0:[^ ]*]] = 0;
// CPP-DEFAULT-NEXT: int32_t [[V1:[^ ]*]];
// CPP-DEFAULT-NEXT: float [[V2:[^ ]*]];
// CPP-DEFAULT-NEXT: std::tie([[V1]], [[V2]]) = two_results();
@ -64,18 +31,16 @@ func.func @std_call_two_results() {
// CPP-DEFAULT-NEXT: std::tie([[V3]], [[V4]]) = two_results();
// CPP-DECLTOP: void std_call_two_results() {
// CPP-DECLTOP-NEXT: int8_t [[V0:[^ ]*]];
// CPP-DECLTOP-NEXT: int32_t [[V1:[^ ]*]];
// CPP-DECLTOP-NEXT: float [[V2:[^ ]*]];
// CPP-DECLTOP-NEXT: int32_t [[V3:[^ ]*]];
// CPP-DECLTOP-NEXT: float [[V4:[^ ]*]];
// CPP-DECLTOP-NEXT: [[V0]] = 0;
// CPP-DECLTOP-NEXT: std::tie([[V1]], [[V2]]) = two_results();
// CPP-DECLTOP-NEXT: std::tie([[V3]], [[V4]]) = two_results();
func.func @one_result() -> i32 {
%0 = arith.constant 0 : i32
%0 = "emitc.constant"() <{value = 0 : i32}> : () -> i32
return %0 : i32
}
// CPP-DEFAULT: int32_t one_result() {
@ -89,8 +54,8 @@ func.func @one_result() -> i32 {
func.func @two_results() -> (i32, f32) {
%0 = arith.constant 0 : i32
%1 = arith.constant 1.0 : f32
%0 = "emitc.constant"() <{value = 0 : i32}> : () -> i32
%1 = "emitc.constant"() <{value = 1.0 : f32}> : () -> f32
return %0, %1 : i32, f32
}
// CPP-DEFAULT: std::tuple<int32_t, float> two_results() {

View File

@ -1769,7 +1769,6 @@ cc_library(
]),
hdrs = glob(["include/mlir/Target/Cpp/*.h"]),
deps = [
":ArithDialect",
":ControlFlowDialect",
":EmitCDialect",
":FuncDialect",