codegen.py 2.58 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
1
from llvmlite import binding as ll
2
3
# from llvmlite.llvmpy import core as lc
import llvmlite.ir as llvmir
dugupeiwen's avatar
dugupeiwen committed
4
from numba.core import utils
5
from numba.core.codegen import Codegen, CPUCodegen, CodeLibrary, CPUCodeLibrary
dugupeiwen's avatar
dugupeiwen committed
6
7
from .hlc import DATALAYOUT, TRIPLE, hlc

8
class HSACodeLibrary(CPUCodeLibrary):
dugupeiwen's avatar
dugupeiwen committed
9
10
11
12
13
14
15
16
17
18
    def _optimize_functions(self, ll_module):
        pass

    def _optimize_final_module(self):
        pass

    def _finalize_specific(self):
        pass

    def get_asm_str(self):
19
20
        # sugon: there has a bug. Don't print ASM code.
        return "ROC Not support get_asm_str\n"
dugupeiwen's avatar
dugupeiwen committed
21
22
23
        """
        Get the human-readable assembly.
        """
24
25
26
27
        # m = hlc.Module()
        # m.load_llvm(str(self._final_module))
        # out = m.finalize()
        # return str(out.hsail)
dugupeiwen's avatar
dugupeiwen committed
28
29


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# class JITHSACodegen(Codegen):
#     _library_class = HSACodeLibrary

#     def _init(self, llvm_module):
#         assert list(llvm_module.global_variables) == [], "Module isn't empty"
#         self._data_layout = DATALAYOUT[utils.MACHINE_BITS]
#         self._target_data = ll.create_target_data(self._data_layout)

#     def _create_empty_module(self, name):
#         ir_module = llvmir.Module(name)
#         ir_module.triple = TRIPLE
#         return ir_module

#     def _module_pass_manager(self):
#         raise NotImplementedError

#     def _function_pass_manager(self, llvm_module):
#         raise NotImplementedError

#     def _add_module(self, module):
#         pass

52
class JITHSACodegen(CPUCodegen):
dugupeiwen's avatar
dugupeiwen committed
53
54
    _library_class = HSACodeLibrary

55
56
57
58
59
60
61
62
63
64
65
66
67
68
    def __init__(self, module_name):
        # initialize_llvm()
        ll.initialize()
        ll.initialize_native_target()
        ll.initialize_native_asmprinter()

        self._data_layout = None
        self._llvm_module = ll.parse_assembly(
            str(self._create_empty_module(module_name)))
        self._llvm_module.name = "global_codegen_module"
        # self._rtlinker = RuntimeLinker()
    
        self._init(self._llvm_module)

dugupeiwen's avatar
dugupeiwen committed
69
70
    def _init(self, llvm_module):
        assert list(llvm_module.global_variables) == [], "Module isn't empty"
71
        self._data_layout = DATALAYOUT
dugupeiwen's avatar
dugupeiwen committed
72
73
74
        self._target_data = ll.create_target_data(self._data_layout)

    def _create_empty_module(self, name):
75
        ir_module = llvmir.Module(name)
dugupeiwen's avatar
dugupeiwen committed
76
        ir_module.triple = TRIPLE
77
78
        if self._data_layout:
            ir_module.data_layout = self._data_layout
dugupeiwen's avatar
dugupeiwen committed
79
80
81
82
83
84
85
86
87
88
        return ir_module

    def _module_pass_manager(self):
        raise NotImplementedError

    def _function_pass_manager(self, llvm_module):
        raise NotImplementedError

    def _add_module(self, module):
        pass