1// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
2
3// CHECK: #include "myheader.h"
4emitc.include "myheader.h"
5// CHECK: #include <myheader.h>
6emitc.include <"myheader.h">
7
8// CHECK: void test_foo_print() {
9func.func @test_foo_print() {
10  // CHECK: [[V1:[^ ]*]] = foo::constant({0, 1});
11  %0 = emitc.call "foo::constant"() {args = [dense<[0, 1]> : tensor<2xi32>]} : () -> (i32)
12  // CHECK: [[V2:[^ ]*]] = foo::op_and_attr({0, 1}, [[V1]]);
13  %1 = emitc.call "foo::op_and_attr"(%0) {args = [dense<[0, 1]> : tensor<2xi32>, 0 : index]} : (i32) -> (i32)
14  // CHECK: [[V3:[^ ]*]] = foo::op_and_attr([[V2]], {0, 1});
15  %2 = emitc.call "foo::op_and_attr"(%1) {args = [0 : index, dense<[0, 1]> : tensor<2xi32>]} : (i32) -> (i32)
16  // CHECK: foo::print([[V3]]);
17  emitc.call "foo::print"(%2): (i32) -> ()
18  return
19}
20
21// CHECK: int32_t test_single_return(int32_t [[V2:.*]])
22func.func @test_single_return(%arg0 : i32) -> i32 {
23  // CHECK: return [[V2]]
24  return %arg0 : i32
25}
26
27// CHECK: std::tuple<int32_t, int32_t> test_multiple_return()
28func.func @test_multiple_return() -> (i32, i32) {
29  // CHECK: std::tie([[V3:.*]], [[V4:.*]]) = foo::blah();
30  %0:2 = emitc.call "foo::blah"() : () -> (i32, i32)
31  // CHECK: [[V5:[^ ]*]] = test_single_return([[V3]]);
32  %1 = call @test_single_return(%0#0) : (i32) -> i32
33  // CHECK: return std::make_tuple([[V5]], [[V4]]);
34  return %1, %0#1 : i32, i32
35}
36
37// CHECK: test_float
38func.func @test_float() {
39  // CHECK: foo::constant({(float)0.0e+00, (float)1.000000000e+00})
40  %0 = emitc.call "foo::constant"() {args = [dense<[0.000000e+00, 1.000000e+00]> : tensor<2xf32>]} : () -> f32
41  return
42}
43
44// CHECK: test_uint
45func.func @test_uint() {
46  // CHECK: uint32_t
47  %0 = emitc.call "foo::constant"() {args = [dense<[0, 1]> : tensor<2xui32>]} : () -> ui32
48  // CHECK: uint64_t
49  %1 = emitc.call "foo::constant"() {args = [dense<[0, 1]> : tensor<2xui64>]} : () -> ui64
50  return
51}
52
53// CHECK: int64_t test_plus_int(int64_t [[V1]])
54func.func @test_plus_int(%arg0 : i64) -> i64 {
55  // CHECK: mhlo::add([[V1]], [[V1]])
56  %0 = emitc.call "mhlo::add"(%arg0, %arg0) {args = [0 : index, 1 : index]} : (i64, i64) -> i64
57  return %0 : i64
58}
59
60// CHECK: Tensor<float, 2> mixed_types(Tensor<double, 2> [[V1]])
61func.func @mixed_types(%arg0: tensor<2xf64>) -> tensor<2xf32> {
62  // CHECK: foo::mixed_types([[V1]]);
63  %0 = emitc.call "foo::mixed_types"(%arg0) {args = [0 : index]} : (tensor<2xf64>) -> tensor<2xf32>
64  return %0 : tensor<2xf32>
65}
66
67// CHECK: Tensor<uint64_t> mhlo_convert(Tensor<uint32_t> [[V1]])
68func.func @mhlo_convert(%arg0: tensor<ui32>) -> tensor<ui64> {
69  // CHECK: mhlo::convert([[V1]]);
70  %0 = emitc.call "mhlo::convert"(%arg0) {args = [0 : index]} : (tensor<ui32>) -> tensor<ui64>
71  return %0 : tensor<ui64>
72}
73
74// CHECK: status_t opaque_types(bool [[V1:[^ ]*]], char [[V2:[^ ]*]]) {
75func.func @opaque_types(%arg0: !emitc.opaque<"bool">, %arg1: !emitc.opaque<"char">) -> !emitc.opaque<"status_t"> {
76  // CHECK: int [[V3:[^ ]*]] = a([[V1]], [[V2]]);
77  %0 = emitc.call "a"(%arg0, %arg1) : (!emitc.opaque<"bool">, !emitc.opaque<"char">) -> (!emitc.opaque<"int">)
78  // CHECK: char [[V4:[^ ]*]] = b([[V3]]);
79  %1 = emitc.call "b"(%0): (!emitc.opaque<"int">) -> (!emitc.opaque<"char">)
80  // CHECK: status_t [[V5:[^ ]*]] = c([[V3]], [[V4]]);
81  %2 = emitc.call "c"(%0, %1): (!emitc.opaque<"int">, !emitc.opaque<"char">) -> (!emitc.opaque<"status_t">)
82  return %2 : !emitc.opaque<"status_t">
83}
84
85func.func @apply(%arg0: i32) -> !emitc.ptr<i32> {
86  // CHECK: int32_t* [[V2]] = &[[V1]];
87  %0 = emitc.apply "&"(%arg0) : (i32) -> !emitc.ptr<i32>
88  // CHECK: int32_t [[V3]] = *[[V2]];
89  %1 = emitc.apply "*"(%0) : (!emitc.ptr<i32>) -> (i32)
90  return %0 : !emitc.ptr<i32>
91}
92