223 lines · plain
1// RUN: mlir-opt %s -convert-index-to-spirv | FileCheck %s2// RUN: mlir-opt %s -convert-index-to-spirv=use-64bit-index=false | FileCheck %s --check-prefix=INDEX323// RUN: mlir-opt %s -convert-index-to-spirv=use-64bit-index=true | FileCheck %s --check-prefix=INDEX644 5module attributes {6 spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [Int64], []>, #spirv.resource_limits<>>7} {8// CHECK-LABEL: @trivial_ops9func.func @trivial_ops(%a: index, %b: index) {10 // CHECK: spirv.IAdd11 %0 = index.add %a, %b12 // CHECK: spirv.ISub13 %1 = index.sub %a, %b14 // CHECK: spirv.IMul15 %2 = index.mul %a, %b16 // CHECK: spirv.SDiv17 %3 = index.divs %a, %b18 // CHECK: spirv.UDiv19 %4 = index.divu %a, %b20 // CHECK: spirv.SRem21 %5 = index.rems %a, %b22 // CHECK: spirv.UMod23 %6 = index.remu %a, %b24 // CHECK: spirv.GL.SMax25 %7 = index.maxs %a, %b26 // CHECK: spirv.GL.UMax27 %8 = index.maxu %a, %b28 // CHECK: spirv.GL.SMin29 %9 = index.mins %a, %b30 // CHECK: spirv.GL.UMin31 %10 = index.minu %a, %b32 // CHECK: spirv.ShiftLeftLogical33 %11 = index.shl %a, %b34 // CHECK: spirv.ShiftRightArithmetic35 %12 = index.shrs %a, %b36 // CHECK: spirv.ShiftRightLogical37 %13 = index.shru %a, %b38 return39}40 41// CHECK-LABEL: @bitwise_ops42func.func @bitwise_ops(%a: index, %b: index) {43 // CHECK: spirv.BitwiseAnd44 %0 = index.and %a, %b45 // CHECK: spirv.BitwiseOr46 %1 = index.or %a, %b47 // CHECK: spirv.BitwiseXor48 %2 = index.xor %a, %b49 return50}51 52// INDEX32-LABEL: @constant_ops53// INDEX64-LABEL: @constant_ops54func.func @constant_ops() {55 // INDEX32: spirv.Constant 42 : i3256 // INDEX64: spirv.Constant 42 : i6457 %0 = index.constant 4258 // INDEX32: spirv.Constant true59 // INDEX64: spirv.Constant true60 %1 = index.bool.constant true61 // INDEX32: spirv.Constant false62 // INDEX64: spirv.Constant false63 %2 = index.bool.constant false64 return65}66 67// CHECK-LABEL: @ceildivs68// CHECK-SAME: %[[NI:.*]]: index, %[[MI:.*]]: index69func.func @ceildivs(%n: index, %m: index) -> index {70 // CHECK-DAG: %[[N:.*]] = builtin.unrealized_conversion_cast %[[NI]]71 // CHECK-DAG: %[[M:.*]] = builtin.unrealized_conversion_cast %[[MI]]72 // CHECK: %[[ZERO:.*]] = spirv.Constant 073 // CHECK: %[[POS_ONE:.*]] = spirv.Constant 174 // CHECK: %[[NEG_ONE:.*]] = spirv.Constant -175 76 // CHECK: %[[M_POS:.*]] = spirv.SGreaterThan %[[M]], %[[ZERO]]77 // CHECK: %[[X:.*]] = spirv.Select %[[M_POS]], %[[NEG_ONE]], %[[POS_ONE]]78 79 // CHECK: %[[N_PLUS_X:.*]] = spirv.IAdd %[[N]], %[[X]]80 // CHECK: %[[N_PLUS_X_DIV_M:.*]] = spirv.SDiv %[[N_PLUS_X]], %[[M]]81 // CHECK: %[[POS_RES:.*]] = spirv.IAdd %[[N_PLUS_X_DIV_M]], %[[POS_ONE]]82 83 // CHECK: %[[NEG_N:.*]] = spirv.ISub %[[ZERO]], %[[N]]84 // CHECK: %[[NEG_N_DIV_M:.*]] = spirv.SDiv %[[NEG_N]], %[[M]]85 // CHECK: %[[NEG_RES:.*]] = spirv.ISub %[[ZERO]], %[[NEG_N_DIV_M]]86 87 // CHECK: %[[N_POS:.*]] = spirv.SGreaterThan %[[N]], %[[ZERO]]88 // CHECK: %[[SAME_SIGN:.*]] = spirv.LogicalEqual %[[N_POS]], %[[M_POS]]89 // CHECK: %[[N_NON_ZERO:.*]] = spirv.INotEqual %[[N]], %[[ZERO]]90 // CHECK: %[[CMP:.*]] = spirv.LogicalAnd %[[SAME_SIGN]], %[[N_NON_ZERO]]91 // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[POS_RES]], %[[NEG_RES]]92 %result = index.ceildivs %n, %m93 94 // %[[RESULTI:.*] = builtin.unrealized_conversion_cast %[[RESULT]]95 // return %[[RESULTI]]96 return %result : index97}98 99// CHECK-LABEL: @ceildivu100// CHECK-SAME: %[[NI:.*]]: index, %[[MI:.*]]: index101func.func @ceildivu(%n: index, %m: index) -> index {102 // CHECK-DAG: %[[N:.*]] = builtin.unrealized_conversion_cast %[[NI]]103 // CHECK-DAG: %[[M:.*]] = builtin.unrealized_conversion_cast %[[MI]]104 // CHECK: %[[ZERO:.*]] = spirv.Constant 0105 // CHECK: %[[ONE:.*]] = spirv.Constant 1106 107 // CHECK: %[[N_MINUS_ONE:.*]] = spirv.ISub %[[N]], %[[ONE]]108 // CHECK: %[[N_MINUS_ONE_DIV_M:.*]] = spirv.UDiv %[[N_MINUS_ONE]], %[[M]]109 // CHECK: %[[N_MINUS_ONE_DIV_M_PLUS_ONE:.*]] = spirv.IAdd %[[N_MINUS_ONE_DIV_M]], %[[ONE]]110 111 // CHECK: %[[CMP:.*]] = spirv.IEqual %[[N]], %[[ZERO]]112 // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[ZERO]], %[[N_MINUS_ONE_DIV_M_PLUS_ONE]]113 %result = index.ceildivu %n, %m114 115 // %[[RESULTI:.*] = builtin.unrealized_conversion_cast %[[RESULT]]116 // return %[[RESULTI]]117 return %result : index118}119 120// CHECK-LABEL: @floordivs121// CHECK-SAME: %[[NI:.*]]: index, %[[MI:.*]]: index122func.func @floordivs(%n: index, %m: index) -> index {123 // CHECK-DAG: %[[N:.*]] = builtin.unrealized_conversion_cast %[[NI]]124 // CHECK-DAG: %[[M:.*]] = builtin.unrealized_conversion_cast %[[MI]]125 // CHECK: %[[ZERO:.*]] = spirv.Constant 0126 // CHECK: %[[POS_ONE:.*]] = spirv.Constant 1127 // CHECK: %[[NEG_ONE:.*]] = spirv.Constant -1128 129 // CHECK: %[[M_NEG:.*]] = spirv.SLessThan %[[M]], %[[ZERO]]130 // CHECK: %[[X:.*]] = spirv.Select %[[M_NEG]], %[[POS_ONE]], %[[NEG_ONE]]131 132 // CHECK: %[[X_MINUS_N:.*]] = spirv.ISub %[[X]], %[[N]]133 // CHECK: %[[X_MINUS_N_DIV_M:.*]] = spirv.SDiv %[[X_MINUS_N]], %[[M]]134 // CHECK: %[[NEG_RES:.*]] = spirv.ISub %[[NEG_ONE]], %[[X_MINUS_N_DIV_M]]135 136 // CHECK: %[[POS_RES:.*]] = spirv.SDiv %[[N]], %[[M]]137 138 // CHECK: %[[N_NEG:.*]] = spirv.SLessThan %[[N]], %[[ZERO]]139 // CHECK: %[[DIFF_SIGN:.*]] = spirv.LogicalNotEqual %[[N_NEG]], %[[M_NEG]]140 // CHECK: %[[N_NON_ZERO:.*]] = spirv.INotEqual %[[N]], %[[ZERO]]141 142 // CHECK: %[[CMP:.*]] = spirv.LogicalAnd %[[DIFF_SIGN]], %[[N_NON_ZERO]]143 // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[POS_RES]], %[[NEG_RES]]144 %result = index.floordivs %n, %m145 146 // %[[RESULTI:.*] = builtin.unrealized_conversion_cast %[[RESULT]]147 // return %[[RESULTI]]148 return %result : index149}150 151// CHECK-LABEL: @index_cmp152func.func @index_cmp(%a : index, %b : index) {153 // CHECK: spirv.IEqual154 %0 = index.cmp eq(%a, %b)155 // CHECK: spirv.INotEqual156 %1 = index.cmp ne(%a, %b)157 158 // CHECK: spirv.SLessThan159 %2 = index.cmp slt(%a, %b)160 // CHECK: spirv.SLessThanEqual161 %3 = index.cmp sle(%a, %b)162 // CHECK: spirv.SGreaterThan163 %4 = index.cmp sgt(%a, %b)164 // CHECK: spirv.SGreaterThanEqual165 %5 = index.cmp sge(%a, %b)166 167 // CHECK: spirv.ULessThan168 %6 = index.cmp ult(%a, %b)169 // CHECK: spirv.ULessThanEqual170 %7 = index.cmp ule(%a, %b)171 // CHECK: spirv.UGreaterThan172 %8 = index.cmp ugt(%a, %b)173 // CHECK: spirv.UGreaterThanEqual174 %9 = index.cmp uge(%a, %b)175 return176}177 178// CHECK-LABEL: @index_sizeof179func.func @index_sizeof() {180 // CHECK: spirv.Constant 32 : i32181 %0 = index.sizeof182 return183}184 185// INDEX32-LABEL: @index_cast_from186// INDEX64-LABEL: @index_cast_from187// INDEX32-SAME: %[[AI:.*]]: index188// INDEX64-SAME: %[[AI:.*]]: index189func.func @index_cast_from(%a: index) -> (i64, i32, i64, i32) {190 // INDEX32: %[[A:.*]] = builtin.unrealized_conversion_cast %[[AI]] : index to i32191 // INDEX64: %[[A:.*]] = builtin.unrealized_conversion_cast %[[AI]] : index to i64192 193 // INDEX32: %[[V0:.*]] = spirv.SConvert %[[A]] : i32 to i64194 %0 = index.casts %a : index to i64195 // INDEX64: %[[V1:.*]] = spirv.SConvert %[[A]] : i64 to i32196 %1 = index.casts %a : index to i32197 // INDEX32: %[[V2:.*]] = spirv.UConvert %[[A]] : i32 to i64198 %2 = index.castu %a : index to i64199 // INDEX64: %[[V3:.*]] = spirv.UConvert %[[A]] : i64 to i32200 %3 = index.castu %a : index to i32201 202 // INDEX32: return %[[V0]], %[[A]], %[[V2]], %[[A]]203 // INDEX64: return %[[A]], %[[V1]], %[[A]], %[[V3]]204 return %0, %1, %2, %3 : i64, i32, i64, i32205}206 207// INDEX32-LABEL: @index_cast_to208// INDEX64-LABEL: @index_cast_to209// INDEX32-SAME: %[[A:.*]]: i32, %[[B:.*]]: i64210// INDEX64-SAME: %[[A:.*]]: i32, %[[B:.*]]: i64211func.func @index_cast_to(%a: i32, %b: i64) -> (index, index, index, index) {212 // INDEX64: %[[V0:.*]] = spirv.SConvert %[[A]] : i32 to i64213 %0 = index.casts %a : i32 to index214 // INDEX32: %[[V1:.*]] = spirv.SConvert %[[B]] : i64 to i32215 %1 = index.casts %b : i64 to index216 // INDEX64: %[[V2:.*]] = spirv.UConvert %[[A]] : i32 to i64217 %2 = index.castu %a : i32 to index218 // INDEX32: %[[V3:.*]] = spirv.UConvert %[[B]] : i64 to i32219 %3 = index.castu %b : i64 to index220 return %0, %1, %2, %3 : index, index, index, index221}222}223