xref: /llvm-project/mlir/test/Conversion/ControlFlowToLLVM/switch.mlir (revision eb6c4197d5263ed2e086925b2b2f032a19442d2b)
1// RUN: mlir-opt %s -convert-cf-to-llvm -split-input-file | FileCheck %s
2
3// Unstructured control flow is converted, but the enclosing op is not
4// converted.
5
6// CHECK-LABEL: func.func @single_case(
7//  CHECK-SAME:     %[[val:.*]]: i32, %[[idx:.*]]: index) -> index {
8//       CHECK:   %[[cast0:.*]] = builtin.unrealized_conversion_cast %[[idx]] : index to i64
9//       CHECK:   llvm.switch %[[val]] : i32, ^[[bb1:.*]](%[[cast0]] : i64) [
10//       CHECK:   ]
11//       CHECK: ^[[bb1]](%[[arg0:.*]]: i64):
12//       CHECK:   %[[cast1:.*]] = builtin.unrealized_conversion_cast %[[arg0]] : i64 to index
13//       CHECK:   return %[[cast1]] : index
14//       CHECK: }
15func.func @single_case(%val: i32, %idx: index) -> index {
16  cf.switch %val : i32, [
17    default: ^bb1(%idx : index)
18  ]
19^bb1(%arg0: index):
20  return %arg0 : index
21}
22
23// -----
24
25// func.func and func.return types match. No unrealized_conversion_cast is
26// needed.
27
28// CHECK-LABEL: func.func @single_case_type_match(
29//  CHECK-SAME:     %[[val:.*]]: i32, %[[i:.*]]: i64) -> i64 {
30//       CHECK:   llvm.switch %[[val]] : i32, ^[[bb1:.*]](%[[i]] : i64) [
31//       CHECK:   ]
32//       CHECK: ^[[bb1]](%[[arg0:.*]]: i64):
33//       CHECK:   return %[[arg0]] : i64
34//       CHECK: }
35func.func @single_case_type_match(%val: i32, %i: i64) -> i64 {
36  cf.switch %val : i32, [
37    default: ^bb1(%i : i64)
38  ]
39^bb1(%arg0: i64):
40  return %arg0 : i64
41}
42
43// -----
44
45//   CHECK-LABEL: func.func @multi_case
46// CHECK-COUNT-2:   unrealized_conversion_cast {{.*}} : index to i64
47//         CHECK:   llvm.switch %{{.*}} : i32, ^{{.*}}(%{{.*}} : i64) [
48//         CHECK:     12: ^{{.*}}(%{{.*}} : i64),
49//         CHECK:     13: ^{{.*}}(%{{.*}} : i64),
50//         CHECK:     14: ^{{.*}}(%{{.*}} : i64)
51//         CHECK:   ]
52func.func @multi_case(%val: i32, %idx1: index, %idx2: index, %i: i64) -> index {
53  cf.switch %val : i32, [
54    default: ^bb1(%idx1 : index),
55    12: ^bb2(%idx2 : index),
56    13: ^bb1(%idx1 : index),
57    14: ^bb3(%i : i64)
58  ]
59^bb1(%arg0: index):
60  return %arg0 : index
61^bb2(%arg1: index):
62  return %arg1 : index
63^bb3(%arg2: i64):
64  %cast = arith.index_cast %arg2 : i64 to index
65  return %cast : index
66}
67