Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions mlir/docs/Dialects/emitc.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ translating the following operations:
* `func.call`
* `func.func`
* `func.return`
* 'arith' Dialect
* `arith.constant`
16 changes: 16 additions & 0 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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>,
Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Conversion/ArithToEmitC/ArithToEmitCPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void ConvertArithToEmitC::runOnOperation() {

target.addLegalDialect<emitc::EmitCDialect>();
target.addIllegalDialect<arith::ArithDialect>();
target.addLegalOp<arith::ConstantOp>();

RewritePatternSet patterns(&getContext());

Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Target/Cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_mlir_translation_library(MLIRTargetCpp
${EMITC_MAIN_INCLUDE_DIR}/emitc/Target/Cpp

LINK_LIBS PUBLIC
MLIRArithDialect
MLIRControlFlowDialect
MLIREmitCDialect
MLIRFuncDialect
Expand Down
4 changes: 1 addition & 3 deletions mlir/lib/Target/Cpp/TranslateRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 0 additions & 12 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -323,14 +322,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());
Expand Down Expand Up @@ -1366,9 +1357,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");
Expand Down
24 changes: 23 additions & 1 deletion mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Target/Cpp/call.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/Target/Cpp/const.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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:[^ ]*]];
Expand All @@ -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};
10 changes: 5 additions & 5 deletions mlir/test/Target/Cpp/for.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Target/Cpp/if.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 3 additions & 38 deletions mlir/test/Target/Cpp/stdops.mlir
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,6 @@ cc_library(
]),
hdrs = glob(["include/mlir/Target/Cpp/*.h"]),
deps = [
":ArithDialect",
":ControlFlowDialect",
":EmitCDialect",
":FuncDialect",
Expand Down