|
| 1 | +from llvmlite import ir |
| 2 | +from numba import types |
| 3 | +from numba.core import cgutils |
| 4 | +from numba.core.extending import intrinsic, overload |
| 5 | +from numba.core.errors import NumbaTypeError |
| 6 | +from numba.cuda.api_util import normalize_indices |
| 7 | + |
| 8 | + |
| 9 | +def ldca(array, i): |
| 10 | + """Generate a `ld.global.ca` instruction for element `i` of an array.""" |
| 11 | + |
| 12 | + |
| 13 | +def ldcg(array, i): |
| 14 | + """Generate a `ld.global.cg` instruction for element `i` of an array.""" |
| 15 | + |
| 16 | + |
| 17 | +def ldcs(array, i): |
| 18 | + """Generate a `ld.global.cs` instruction for element `i` of an array.""" |
| 19 | + |
| 20 | + |
| 21 | +def ldlu(array, i): |
| 22 | + """Generate a `ld.global.lu` instruction for element `i` of an array.""" |
| 23 | + |
| 24 | + |
| 25 | +def ldcv(array, i): |
| 26 | + """Generate a `ld.global.cv` instruction for element `i` of an array.""" |
| 27 | + |
| 28 | + |
| 29 | +def stcg(array, i, value): |
| 30 | + """Generate a `st.global.cg` instruction for element `i` of an array.""" |
| 31 | + |
| 32 | + |
| 33 | +def stcs(array, i, value): |
| 34 | + """Generate a `st.global.cs` instruction for element `i` of an array.""" |
| 35 | + |
| 36 | + |
| 37 | +def stwb(array, i, value): |
| 38 | + """Generate a `st.global.wb` instruction for element `i` of an array.""" |
| 39 | + |
| 40 | + |
| 41 | +def stwt(array, i, value): |
| 42 | + """Generate a `st.global.wt` instruction for element `i` of an array.""" |
| 43 | + |
| 44 | + |
| 45 | +def ld_cache_operator(operator): |
| 46 | + @intrinsic |
| 47 | + def impl(typingctx, array, index): |
| 48 | + if not isinstance(array, types.Array): |
| 49 | + msg = f"ldcs operates on arrays. Got type {array}" |
| 50 | + raise NumbaTypeError(msg) |
| 51 | + |
| 52 | + # Need to validate bitwidth |
| 53 | + |
| 54 | + # Need to validate indices |
| 55 | + |
| 56 | + signature = array.dtype(array, index) |
| 57 | + |
| 58 | + def codegen(context, builder, sig, args): |
| 59 | + array_type, index_type = sig.args |
| 60 | + loaded_type = context.get_value_type(array_type.dtype) |
| 61 | + ptr_type = loaded_type.as_pointer() |
| 62 | + ldcs_type = ir.FunctionType(loaded_type, [ptr_type]) |
| 63 | + |
| 64 | + array, indices = args |
| 65 | + |
| 66 | + index_type, indices = normalize_indices(context, builder, |
| 67 | + index_type, indices, |
| 68 | + array_type, |
| 69 | + array_type.dtype) |
| 70 | + array_struct = context.make_array(array_type)(context, builder, |
| 71 | + value=array) |
| 72 | + ptr = cgutils.get_item_pointer(context, builder, array_type, |
| 73 | + array_struct, indices, |
| 74 | + wraparound=True) |
| 75 | + |
| 76 | + bitwidth = array_type.dtype.bitwidth |
| 77 | + inst = f"ld.global.{operator}.b{bitwidth}" |
| 78 | + # See |
| 79 | + # https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#restricted-use-of-sub-word-sizes |
| 80 | + # for background on the choice of "r" for 8-bit operands - there is |
| 81 | + # no constraint for 8-bit operands, but the operand for loads and |
| 82 | + # stores is permitted to be greater than 8 bits. |
| 83 | + constraint_map = { |
| 84 | + 1: "b", |
| 85 | + 8: "r", |
| 86 | + 16: "h", |
| 87 | + 32: "r", |
| 88 | + 64: "l", |
| 89 | + 128: "q" |
| 90 | + } |
| 91 | + constraints = f"={constraint_map[bitwidth]},l" |
| 92 | + ldcs = ir.InlineAsm(ldcs_type, f"{inst} $0, [$1];", constraints) |
| 93 | + return builder.call(ldcs, [ptr]) |
| 94 | + |
| 95 | + return signature, codegen |
| 96 | + |
| 97 | + return impl |
| 98 | + |
| 99 | + |
| 100 | +ldca_intrinsic = ld_cache_operator("ca") |
| 101 | +ldcg_intrinsic = ld_cache_operator("cg") |
| 102 | +ldcs_intrinsic = ld_cache_operator("cs") |
| 103 | +ldlu_intrinsic = ld_cache_operator("lu") |
| 104 | +ldcv_intrinsic = ld_cache_operator("cv") |
| 105 | + |
| 106 | + |
| 107 | +def st_cache_operator(operator): |
| 108 | + @intrinsic |
| 109 | + def impl(typingctx, array, index, value): |
| 110 | + if not isinstance(array, types.Array): |
| 111 | + msg = f"ldcs operates on arrays. Got type {array}" |
| 112 | + raise NumbaTypeError(msg) |
| 113 | + |
| 114 | + # Need to validate bitwidth |
| 115 | + |
| 116 | + # Need to validate indices |
| 117 | + |
| 118 | + signature = types.void(array, index, value) |
| 119 | + |
| 120 | + def codegen(context, builder, sig, args): |
| 121 | + array_type, index_type, value_type = sig.args |
| 122 | + stored_type = context.get_value_type(array_type.dtype) |
| 123 | + ptr_type = stored_type.as_pointer() |
| 124 | + stcs_type = ir.FunctionType(ir.VoidType(), [ptr_type, stored_type]) |
| 125 | + |
| 126 | + array, indices, value = args |
| 127 | + |
| 128 | + index_type, indices = normalize_indices(context, builder, |
| 129 | + index_type, indices, |
| 130 | + array_type, |
| 131 | + array_type.dtype) |
| 132 | + array_struct = context.make_array(array_type)(context, builder, |
| 133 | + value=array) |
| 134 | + ptr = cgutils.get_item_pointer(context, builder, array_type, |
| 135 | + array_struct, indices, |
| 136 | + wraparound=True) |
| 137 | + |
| 138 | + casted_value = context.cast(builder, value, value_type, |
| 139 | + array_type.dtype) |
| 140 | + |
| 141 | + bitwidth = array_type.dtype.bitwidth |
| 142 | + inst = f"st.global.{operator}.b{bitwidth}" |
| 143 | + constraint_map = { |
| 144 | + 1: "b", |
| 145 | + 8: "r", |
| 146 | + 16: "h", |
| 147 | + 32: "r", |
| 148 | + 64: "l", |
| 149 | + 128: "q" |
| 150 | + } |
| 151 | + constraints = f"l,{constraint_map[bitwidth]},~{{memory}}" |
| 152 | + stcs = ir.InlineAsm(stcs_type, f"{inst} [$0], $1;", constraints) |
| 153 | + builder.call(stcs, [ptr, casted_value]) |
| 154 | + |
| 155 | + return signature, codegen |
| 156 | + |
| 157 | + return impl |
| 158 | + |
| 159 | + |
| 160 | +stcg_intrinsic = st_cache_operator("cg") |
| 161 | +stcs_intrinsic = st_cache_operator("cs") |
| 162 | +stwb_intrinsic = st_cache_operator("wb") |
| 163 | +stwt_intrinsic = st_cache_operator("wt") |
| 164 | + |
| 165 | + |
| 166 | +@overload(ldca, target='cuda') |
| 167 | +def ol_ldca(array, i): |
| 168 | + def impl(array, i): |
| 169 | + return ldca_intrinsic(array, i) |
| 170 | + return impl |
| 171 | + |
| 172 | + |
| 173 | +@overload(ldcg, target='cuda') |
| 174 | +def ol_ldcg(array, i): |
| 175 | + def impl(array, i): |
| 176 | + return ldcg_intrinsic(array, i) |
| 177 | + return impl |
| 178 | + |
| 179 | + |
| 180 | +@overload(ldcs, target='cuda') |
| 181 | +def ol_ldcs(array, i): |
| 182 | + def impl(array, i): |
| 183 | + return ldcs_intrinsic(array, i) |
| 184 | + return impl |
| 185 | + |
| 186 | + |
| 187 | +@overload(ldlu, target='cuda') |
| 188 | +def ol_ldlu(array, i): |
| 189 | + def impl(array, i): |
| 190 | + return ldlu_intrinsic(array, i) |
| 191 | + return impl |
| 192 | + |
| 193 | + |
| 194 | +@overload(ldcv, target='cuda') |
| 195 | +def ol_ldcv(array, i): |
| 196 | + def impl(array, i): |
| 197 | + return ldcv_intrinsic(array, i) |
| 198 | + return impl |
| 199 | + |
| 200 | + |
| 201 | +@overload(stcg, target='cuda') |
| 202 | +def ol_stcg(array, i, value): |
| 203 | + def impl(array, i, value): |
| 204 | + return stcg_intrinsic(array, i, value) |
| 205 | + return impl |
| 206 | + |
| 207 | + |
| 208 | +@overload(stcs, target='cuda') |
| 209 | +def ol_stcs(array, i, value): |
| 210 | + def impl(array, i, value): |
| 211 | + return stcs_intrinsic(array, i, value) |
| 212 | + return impl |
| 213 | + |
| 214 | + |
| 215 | +@overload(stwb, target='cuda') |
| 216 | +def ol_stwb(array, i, value): |
| 217 | + def impl(array, i, value): |
| 218 | + return stwb_intrinsic(array, i, value) |
| 219 | + return impl |
| 220 | + |
| 221 | + |
| 222 | +@overload(stwt, target='cuda') |
| 223 | +def ol_stwt(array, i, value): |
| 224 | + def impl(array, i, value): |
| 225 | + return stwt_intrinsic(array, i, value) |
| 226 | + return impl |
0 commit comments