|
| 1 | +from llvmlite import binding as ll |
| 2 | +from llvmlite.llvmpy import core as lc |
| 3 | + |
| 4 | +from numba.core.codegen import BaseCPUCodegen, CodeLibrary |
| 5 | +from numba.core import utils |
| 6 | + |
| 7 | + |
| 8 | +SPIR_TRIPLE = {32: ' spir-unknown-unknown', |
| 9 | + 64: 'spir64-unknown-unknown'} |
| 10 | + |
| 11 | +SPIR_DATA_LAYOUT = { |
| 12 | + 32 : ('e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:' |
| 13 | + '256-v512:512-v1024:1024'), |
| 14 | + 64 : ('e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-' |
| 15 | + 'v512:512-v1024:1024') |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +class SPIRVCodeLibrary(CodeLibrary): |
| 20 | + def _optimize_functions(self, ll_module): |
| 21 | + pass |
| 22 | + |
| 23 | + def _optimize_final_module(self): |
| 24 | + # Run some lightweight optimization to simplify the module. |
| 25 | + pmb = ll.PassManagerBuilder() |
| 26 | + pmb.opt_level = 1 |
| 27 | + pmb.disable_unit_at_a_time = False |
| 28 | + pmb.disable_unroll_loops = True |
| 29 | + pmb.loop_vectorize = False |
| 30 | + pmb.slp_vectorize = False |
| 31 | + |
| 32 | + pm = ll.ModulePassManager() |
| 33 | + pmb.populate(pm) |
| 34 | + pm.run(self._final_module) |
| 35 | + |
| 36 | + def _finalize_specific(self): |
| 37 | + # Fix global naming |
| 38 | + for gv in self._final_module.global_variables: |
| 39 | + if '.' in gv.name: |
| 40 | + gv.name = gv.name.replace('.', '_') |
| 41 | + |
| 42 | + def get_asm_str(self): |
| 43 | + # Return nothing: we can only dump assembler code when it is later |
| 44 | + # generated (in numba.dppl.compiler). |
| 45 | + return None |
| 46 | + |
| 47 | + |
| 48 | +class JITSPIRVCodegen(BaseCPUCodegen): |
| 49 | + """ |
| 50 | + This codegen implementation generates optimized SPIR 2.0 |
| 51 | + """ |
| 52 | + |
| 53 | + _library_class = SPIRVCodeLibrary |
| 54 | + |
| 55 | + def _init(self, llvm_module): |
| 56 | + assert list(llvm_module.global_variables) == [], "Module isn't empty" |
| 57 | + self._data_layout = SPIR_DATA_LAYOUT[utils.MACHINE_BITS] |
| 58 | + self._target_data = ll.create_target_data(self._data_layout) |
| 59 | + |
| 60 | + def _create_empty_module(self, name): |
| 61 | + ir_module = lc.Module(name) |
| 62 | + ir_module.triple = SPIR_TRIPLE[utils.MACHINE_BITS] |
| 63 | + if self._data_layout: |
| 64 | + ir_module.data_layout = self._data_layout |
| 65 | + return ir_module |
| 66 | + |
| 67 | + def _module_pass_manager(self): |
| 68 | + raise NotImplementedError |
| 69 | + |
| 70 | + def _function_pass_manager(self, llvm_module): |
| 71 | + raise NotImplementedError |
| 72 | + |
| 73 | + def _add_module(self, module): |
| 74 | + pass |
0 commit comments