1 // RUN: mlir-opt %s -split-input-file -pass-pipeline="builtin.module(convert-math-to-funcs{convert-ctlz})" | FileCheck %s
2 // RUN: mlir-opt %s -split-input-file -pass-pipeline="builtin.module(convert-math-to-funcs{convert-ctlz=false})" | FileCheck --check-prefix=NOCVT %s
4 // Check a golden-path i32 conversion
6 // CHECK-LABEL: func.func @main(
7 // CHECK-SAME: %[[VAL_0:.*]]: i32
9 // CHECK: %[[VAL_1:.*]] = call @__mlir_math_ctlz_i32(%[[VAL_0]]) : (i32) -> i32
13 // CHECK-LABEL: func.func private @__mlir_math_ctlz_i32(
14 // CHECK-SAME: %[[ARG:.*]]: i32
15 // CHECK-SAME: ) -> i32 attributes {llvm.linkage = #llvm.linkage<linkonce_odr>} {
16 // CHECK: %[[C_32:.*]] = arith.constant 32 : i32
17 // CHECK: %[[C_0:.*]] = arith.constant 0 : i32
18 // CHECK: %[[ARGCMP:.*]] = arith.cmpi eq, %[[ARG]], %[[C_0]] : i32
19 // CHECK: %[[OUT:.*]] = scf.if %[[ARGCMP]] -> (i32) {
20 // CHECK: scf.yield %[[C_32]] : i32
22 // CHECK: %[[C_1INDEX:.*]] = arith.constant 1 : index
23 // CHECK: %[[C_1I32:.*]] = arith.constant 1 : i32
24 // CHECK: %[[C_32INDEX:.*]] = arith.constant 32 : index
25 // CHECK: %[[N:.*]] = arith.constant 0 : i32
26 // CHECK: %[[FOR_RET:.*]]:2 = scf.for %[[I:.*]] = %[[C_1INDEX]] to %[[C_32INDEX]] step %[[C_1INDEX]]
27 // CHECK: iter_args(%[[ARG_ITER:.*]] = %[[ARG]], %[[N_ITER:.*]] = %[[N]]) -> (i32, i32) {
28 // CHECK: %[[COND:.*]] = arith.cmpi slt, %[[ARG_ITER]], %[[C_0]] : i32
29 // CHECK: %[[IF_RET:.*]]:2 = scf.if %[[COND]] -> (i32, i32) {
30 // CHECK: scf.yield %[[ARG_ITER]], %[[N_ITER]] : i32, i32
32 // CHECK: %[[N_NEXT:.*]] = arith.addi %[[N_ITER]], %[[C_1I32]] : i32
33 // CHECK: %[[ARG_NEXT:.*]] = arith.shli %[[ARG_ITER]], %[[C_1I32]] : i32
34 // CHECK: scf.yield %[[ARG_NEXT]], %[[N_NEXT]] : i32, i32
36 // CHECK: scf.yield %[[IF_RET]]#0, %[[IF_RET]]#1 : i32, i32
38 // CHECK: scf.yield %[[FOR_RET]]#1 : i32
40 // CHECK: return %[[OUT]] : i32
42 // NOCVT-NOT: __mlir_math_ctlz_i32
43 func.func @main(%arg0: i32) {
44 %0 = math.ctlz %arg0 : i32
50 // Check that i8 input is preserved
52 // CHECK-LABEL: func.func @main(
53 // CHECK-SAME: %[[VAL_0:.*]]: i8
55 // CHECK: %[[VAL_1:.*]] = call @__mlir_math_ctlz_i8(%[[VAL_0]]) : (i8) -> i8
59 // CHECK-LABEL: func.func private @__mlir_math_ctlz_i8(
60 // CHECK-SAME: %[[ARG:.*]]: i8
61 // CHECK-SAME: ) -> i8 attributes {llvm.linkage = #llvm.linkage<linkonce_odr>} {
62 // CHECK: %[[C_8:.*]] = arith.constant 8 : i8
63 // CHECK: %[[C_0:.*]] = arith.constant 0 : i8
64 // CHECK: %[[ARGCMP:.*]] = arith.cmpi eq, %[[ARG]], %[[C_0]] : i8
65 // CHECK: %[[OUT:.*]] = scf.if %[[ARGCMP]] -> (i8) {
66 // CHECK: scf.yield %[[C_8]] : i8
68 // CHECK: %[[C_1INDEX:.*]] = arith.constant 1 : index
69 // CHECK: %[[C_1I32:.*]] = arith.constant 1 : i8
70 // CHECK: %[[C_8INDEX:.*]] = arith.constant 8 : index
71 // CHECK: %[[N:.*]] = arith.constant 0 : i8
72 // CHECK: %[[FOR_RET:.*]]:2 = scf.for %[[I:.*]] = %[[C_1INDEX]] to %[[C_8INDEX]] step %[[C_1INDEX]]
73 // CHECK: iter_args(%[[ARG_ITER:.*]] = %[[ARG]], %[[N_ITER:.*]] = %[[N]]) -> (i8, i8) {
74 // CHECK: %[[COND:.*]] = arith.cmpi slt, %[[ARG_ITER]], %[[C_0]] : i8
75 // CHECK: %[[IF_RET:.*]]:2 = scf.if %[[COND]] -> (i8, i8) {
76 // CHECK: scf.yield %[[ARG_ITER]], %[[N_ITER]] : i8, i8
78 // CHECK: %[[N_NEXT:.*]] = arith.addi %[[N_ITER]], %[[C_1I32]] : i8
79 // CHECK: %[[ARG_NEXT:.*]] = arith.shli %[[ARG_ITER]], %[[C_1I32]] : i8
80 // CHECK: scf.yield %[[ARG_NEXT]], %[[N_NEXT]] : i8, i8
82 // CHECK: scf.yield %[[IF_RET]]#0, %[[IF_RET]]#1 : i8, i8
84 // CHECK: scf.yield %[[FOR_RET]]#1 : i8
86 // CHECK: return %[[OUT]] : i8
88 // NOCVT-NOT: __mlir_math_ctlz_i32
89 func.func @main(%arg0: i8) {
90 %0 = math.ctlz %arg0 : i8
96 // Check that index is not converted
98 // CHECK-LABEL: func.func @ctlz_index
100 func.func @ctlz_index(%arg0: index) {
101 %0 = math.ctlz %arg0 : index