xref: /llvm-project/mlir/test/Target/Cpp/control_flow.mlir (revision c4fd1fd6d4e0d21b2315fadecbcdc0892f3c6925)
1// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
2
3// simple(10, true)  -> 20
4// simple(10, false) -> 30
5func.func @simple(i64, i1) -> i64 {
6^bb0(%a: i64, %cond: i1):
7  cf.cond_br %cond, ^bb1, ^bb2
8^bb1:
9  cf.br ^bb3(%a: i64)
10^bb2:
11  %b = emitc.call_opaque "add"(%a, %a) : (i64, i64) -> i64
12  cf.br ^bb3(%b: i64)
13^bb3(%c: i64):
14  cf.br ^bb4(%c, %a : i64, i64)
15^bb4(%d : i64, %e : i64):
16  %0 = emitc.call_opaque "add"(%d, %e) : (i64, i64) -> i64
17  return %0 : i64
18}
19  // CPP-DECLTOP: int64_t simple(int64_t [[A:[^ ]*]], bool [[COND:[^ ]*]]) {
20    // CPP-DECLTOP-NEXT: int64_t [[B:[^ ]*]];
21    // CPP-DECLTOP-NEXT: int64_t [[V0:[^ ]*]];
22    // CPP-DECLTOP-NEXT: int64_t [[C:[^ ]*]];
23    // CPP-DECLTOP-NEXT: int64_t [[D:[^ ]*]];
24    // CPP-DECLTOP-NEXT: int64_t [[E:[^ ]*]];
25    // CPP-DECLTOP-NEXT: if ([[COND]]) {
26    // CPP-DECLTOP-NEXT: goto [[BB1:[^ ]*]];
27    // CPP-DECLTOP-NEXT: } else {
28    // CPP-DECLTOP-NEXT: goto [[BB2:[^ ]*]];
29    // CPP-DECLTOP-NEXT: }
30    // CPP-DECLTOP-NEXT: [[BB1]]:
31    // CPP-DECLTOP-NEXT: [[C]] = [[A]];
32    // CPP-DECLTOP-NEXT: goto [[BB3:[^ ]*]];
33    // CPP-DECLTOP-NEXT: [[BB2]]:
34    // CPP-DECLTOP-NEXT: [[B]] = add([[A]], [[A]]);
35    // CPP-DECLTOP-NEXT: [[C]] = [[B]];
36    // CPP-DECLTOP-NEXT: goto [[BB3]];
37    // CPP-DECLTOP-NEXT: [[BB3]]:
38    // CPP-DECLTOP-NEXT: [[D]] = [[C]];
39    // CPP-DECLTOP-NEXT: [[E]] = [[A]];
40    // CPP-DECLTOP-NEXT: goto [[BB4:[^ ]*]];
41    // CPP-DECLTOP-NEXT: [[BB4]]:
42    // CPP-DECLTOP-NEXT: [[V0]] = add([[D]], [[E]]);
43    // CPP-DECLTOP-NEXT: return [[V0]];
44
45
46func.func @block_labels0() {
47^bb1:
48    cf.br ^bb2
49^bb2:
50    return
51}
52// CPP-DECLTOP: void block_labels0() {
53  // CPP-DECLTOP-NEXT: goto label2;
54  // CPP-DECLTOP-NEXT: label2:
55  // CPP-DECLTOP-NEXT: return;
56  // CPP-DECLTOP-NEXT: }
57
58
59// Repeat the same function to make sure the names of the block labels get reset.
60func.func @block_labels1() {
61^bb1:
62    cf.br ^bb2
63^bb2:
64    return
65}
66// CPP-DECLTOP: void block_labels1() {
67  // CPP-DECLTOP-NEXT: goto label2;
68  // CPP-DECLTOP-NEXT: label2:
69  // CPP-DECLTOP-NEXT: return;
70  // CPP-DECLTOP-NEXT: }
71