def __init__(self, cfg): self.cfg = cfg self.module = ir.Module() self.params = [ ] # list to hold parameters to be passed to a function call self.builder = None self.cur_func = None self.temp_ptr_dict = {} self.label_block_dict = {} self.label_builder_dict = {} self.fname_fn_dict = {} self.global_name_type_dict = {} self.binding = binding self.binding.initialize() self.binding.initialize_native_target() self.binding.initialize_native_asmprinter() self.module = ir.Module(name=__file__) self.module.triple = self.binding.get_default_triple() self._create_execution_engine() # declare external functions self._declare_printf_function() self._declare_scanf_function()
def _generate_globals_module(self): module_ir = ir.Module("Globals") for node, socket in iter_all_unlinked_inputs(self.tree): name = get_global_input_name(node, socket) variable = ir.GlobalVariable(module_ir, socket.ir_type, name) variable.linkage = "internal" return module_ir
def run_llvm(ops): llvm.load_library_permanently("./libio.so") mem_size = 10 ** 6 import numpy as np arr = np.zeros(mem_size, dtype=np.int8) mem = arr.ctypes.data_as(POINTER(c_int8)) fnty = ir.FunctionType(int8, (int8.as_pointer(),)) module = ir.Module(name="module_name") func = ir.Function(module, fnty, name="foo") write_fnty = ir.FunctionType(void, (int8, int32)) write_func = ir.Function(module, write_fnty, name="sys_write") read_fnty = ir.FunctionType(int8, (int32,)) read_func = ir.Function(module, read_fnty, name="sys_read") ptr = ir.GlobalVariable(module, int8_ptr, "ptr") ptr.linkage = 'internal' to_llvm(ops, func, ptr, write_func, read_func) target_machine, engine = create_execution_engine() print(module) mod = compile_ir(engine, str(module)) print(target_machine.emit_assembly(mod)) func_ptr = engine.get_function_address("foo") cfunc = CFUNCTYPE(c_int8, POINTER(c_int8))(func_ptr) cfunc(mem)
def test_context_use_pos(): from sleepy.types import CodegenContext, make_di_location from sleepy.syntactical_analysis.grammar import TreePosition from llvmlite import ir module = ir.Module(name='module_name') file_path = DummyPath("test") context = CodegenContext(builder=ir.IRBuilder(), module=module, emits_debug=True, file_path=file_path) program = '123456789' outer_pos = TreePosition(word=program, from_pos=0, to_pos=9, file_path=file_path) with context.use_pos(outer_pos): assert context.current_pos == outer_pos assert context.builder.debug_metadata == make_di_location( outer_pos, context=context) inner_pos = TreePosition(word=program, from_pos=2, to_pos=4, file_path=file_path) with context.use_pos(inner_pos): assert context.current_pos == inner_pos assert context.builder.debug_metadata == make_di_location( inner_pos, context=context) assert context.current_pos == outer_pos assert context.builder.debug_metadata == make_di_location( outer_pos, context=context)
def codegen_compile(func, datatype: str): """ :param func: :param datatype: either 'float', 'double' or 'int' :return: """ func_name = func.__name__ sig = signature(func) if datatype.startswith('int'): llvm_type = ll.IntType(64) type_dummy_instance = LLVMInt64 c_type = c_int64 elif datatype.startswith('uint'): llvm_type = ll.IntType(64) type_dummy_instance = LLVMUInt64 c_type = c_uint64 elif datatype in ['float', 'double']: llvm_type = ll.DoubleType() type_dummy_instance = LLVMDouble c_type = c_double else: return None llvm_param_types = [llvm_type for c in param_names[:len(sig.parameters)]] fntype = ll.FunctionType(llvm_type, llvm_param_types) module = ll.Module() llvm_func = ll.Function(module, fntype, name=func_name) bb_entry = llvm_func.append_basic_block() builder = ll.IRBuilder() builder.position_at_end(bb_entry) context = Context(builder) params = [type_dummy_instance(context, arg) for arg in llvm_func.args] ret = func(*params) context.builder.ret(ret.instruction) code = str(module) llmod = llvm.parse_assembly(code) pmb = llvm.create_pass_manager_builder() pmb.opt_level = 2 pm = llvm.create_module_pass_manager() pmb.populate(pm) pm.run(llmod) ee = llvm.create_mcjit_compiler(llmod, target_machine) ee.finalize_object() cfptr = ee.get_function_address(func_name) cfunc = CFUNCTYPE(c_type, *[c_type for c in llvm_param_types])(cfptr) # keep the reference alive # (this is probably an ugly hack? but whatever) cfunc.execution_engine = ee cfunc.target_asm = target_machine.emit_assembly(llmod) cfunc.llvm_code = code return cfunc
def __init__(self, env=None): self.llvm_module = ll.Module("main") self.env = env if self.env is not None: self.env.init_module(self) fractions.init_module(self)
def __init__(self) -> None: super().__init__() self.module = ir.Module() self.module.triple = "x86_64-pc-linux-gnu" self.module.data_layout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" self.scope = 0 self.symbol_table: Dict[str, List[int]] = dict() self.functions: Dict[str, Function] = dict() self.blocks: List[Block] = [] self.builders: List[IRBuilder] = [] self.local_vars = [] self.global_vars = {} self.is_load_var = True self.structures = dict() self.end_condition_block = None self.vstring_num = 0 self.init_internal_functions()
def main(): llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() i64 = ir.IntType(64) # int64_t main() ftype_main = ir.FunctionType(i64, []) module = ir.Module(name='sokoide_module') fn_main = ir.Function(module, ftype_main, name="main") block = fn_main.append_basic_block(name='entrypoint') # function prototype (external linkage implemented in builtin.c) for # void write(int64_t) ftype_write = ir.FunctionType(ir.VoidType(), [i64]) fn_write = ir.Function(module, ftype_write, name="write") # make a block for main (entrypoint) builder = ir.IRBuilder(block) # call write(42) builder.call(fn_write, (ir.Constant(i64, 42), ), name="write") # return 42 builder.ret(ir.Constant(i64, 42)) llvm_ir = str(module) llvm_ir_parsed = llvm.parse_assembly(llvm_ir) with open("out.ll", "w") as f: f.write(str(llvm_ir_parsed))
def main(): global abstract_syntax_tree global builder args = args_.manage_arguments() input_location = args.file_location[0] output_location = input_location.partition('.')[0] output_filename = output_location.split('/')[-1] errors_ula.main() abstract_syntax_tree = errors_ula.result flttyp = ir.FloatType() # create float type fnctyp = ir.FunctionType(flttyp, ()) # create function type to return a float module = ir.Module(name="ula") # create module named "ula" func = ir.Function(module, fnctyp, name="main") # create "main" function block = func.append_basic_block(name="entry") # create block "entry" label builder = ir.IRBuilder(block) generate_code(abstract_syntax_tree ) # call code_gen() to traverse tree & generate code builder.ret(builder.load(var_dict[last_var])) # specify return value if (__name__ == "__main__"): output_location = output_filename + '.ir' with open(output_location, 'w') as file_writer: file_writer.write(str(module) + '\n') print(str(module)) return module
def to_llvm_function(exprs, vars_, name="__arybo"): if not llvmlite_available: raise RuntimeError( "llvmlite module unavailable! can't assemble to LLVM IR...") if not isinstance(exprs, collections.Iterable): exprs = (exprs, ) M = ll.Module() args_types = [IntType(v.nbits) for v in vars_] fntype = ll.FunctionType(IntType(exprs[-1].nbits), args_types) func = ll.Function(M, fntype, name=name) func.attributes.add("nounwind") BB = func.append_basic_block() IRB = ll.IRBuilder() IRB.position_at_end(BB) sym_to_value = {} for i, v in enumerate(vars_): arg = func.args[i] arg.name = v.name sym_to_value[v.name] = arg ret = to_llvm_ir(exprs, sym_to_value, IRB) IRB.ret(ret) return M
def main(): if (len(sys.argv) < 2): print("ERRO - Por Favor Informe Um Arquivo .tpp") else: file = open(sys.argv[1], 'r', encoding='utf-8') sin = Sintatica(file.read(), Lexica().tokens) dot = Digraph(comment='TREE') Tree().printTree(sin.ast, dot) #dot.render('out/sintatica/normal.gv', view=True) Tree().poda(sin.ast) dot = Digraph(comment='TREE') Tree().printTreeCut(sin.ast, dot) #dot.render('out/semantica/poda.gv', view=True) sema = Semantica() sema.percorrerTree(sin.ast) sema.verificacoes() #sema.printTabSymbols() llvm.initialize() llvm.initialize_all_targets() llvm.initialize_native_target() llvm.initialize_native_asmparser() modulo = ir.Module(sys.argv[1]) modulo.triple = llvm.get_process_triple() target = llvm.Target.from_triple(modulo.triple) targetMachine = target.create_target_machine() modulo.data_layout = targetMachine.target_data Geracao().percorrer(sin.ast, modulo) arquivo = open('teste.ll', 'w') arquivo.write(str(modulo)) arquivo.close() print(modulo)
def __init__(self, scanner, parser): # filename should be proc name self.module = ir.Module(name=scanner.getFileName()) void = self.getType("bool") fnty = ir.FunctionType(void, tuple()) func = ir.Function(self.module, fnty, name="main") # do this when main pgm found block = func.append_basic_block() self.builderRoot = ir.IRBuilder(block) self.builder = self.builderRoot # root function self.builderStack = [] # make stack of functions interpreting # if stmts come in pairs of 2's # when starting else clause pop off last stmt # or if no else/done with if, pop off both and phi join # they are added backwards # so we can always be working on last entry self.condStack = [] # stack of current if stmt block self.ifBlock = None # the last if block we will use to join on else terminate self.ifHandle = None # handle for phi node self.loopStack = [] self.symTable = parser.symTable self.loadDefaults()
def codegen(self): ir_mod = ir.Module() for k, defn in self._definitions.items(): defn.codegen_declare(ir_mod) for k, defn in self._definitions.items(): defn.codegen_definition(ir_mod) return ir_mod
def __init__(self, code): self.semantica = Semantica(code) self.arvore = self.semantica.ast llvm.initialize() llvm.initialize_all_targets() llvm.initialize_native_target() llvm.initialize_native_asmprinter() self.module = ir.Module('meu_modulo.bc') self.module.triple = llvm.get_process_triple() target = llvm.Target.from_triple(self.module.triple) target_machine = target.create_target_machine() self.module.data_layout = target_machine.target_data #llvm.shutdown() self.variaveis = [] self.variaveisGlobais = [] self.escopo = "global" self.funcs = [] self.andaArvore(self.arvore) self.funcllvm = '' self.endBlock = '' arquivo = open('meu_modulo.ll', 'w') arquivo.write(str(self.module)) arquivo.close() print(self.module)
def test_llvm_callback_stub(): from ctypes import c_int, c_void_p, CFUNCTYPE, cast from lfortran.codegen.gen import create_callback_stub from llvmlite import ir data = [0, 0] ftype = CFUNCTYPE(c_int, c_int, c_int) @ftype def f(a, b): data[0] = a data[1] = b return a + b faddr = cast(f, c_void_p).value mod = ir.Module() create_callback_stub( mod, "f", faddr, ir.FunctionType(ir.IntType(64), [ir.IntType(64), ir.IntType(64)])) e = LLVMEvaluator() e.add_module(str(mod)) stub = ftype(e.ee.get_function_address('f')) assert data == [0, 0] assert stub(2, 3) == 5 assert data == [2, 3]
def __init__(self): self.module = ir.Module('Main') self.symbolTable = SymbolTable() self.builder = ir.IRBuilder() self.globalNameGen = self.globalNameGenerator() self.flags = {'bind': False}
def generate_code(ast, undefined_args): module = ir.Module(name="prog") declare_printf(module) declare_exit(module) declare_isoverflow(module) generate_prog(ast, module, undefined_args) return module
def JIT(file_name, function_name, args, opt_on=False, verbose=False): src = "" with open(file_name, "r") as src_file: src = src_file.read() if (verbose): print("Reading source:") print(src) llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() transformer = pAST.PythonVisitor() ast = transformer(src) module = ir.Module(name="JITPy") #for only 1 arg args_in = [] if (len(args_in) == 1): args.append(args) else: args_in = args infer_ty, ret_ty, arg_tys = type_infer(src, ast, function_name, args_in) #print(infer_ty) ir_module = specialize(ast, infer_ty) llvm_ir = str(ir_module) if (verbose): print(llvm_ir) engine = create_execution_engine() mod = compile_ir(engine, llvm_ir) cfunc = wrap_function(ret_ty, args_in, function_name, engine) result = cfunc(*args_in) result_str = function_name + "(" + str(args_in) + ")" + " = " + str(result) print(result_str)
def __init__(self, **kwargs): self.info = {"global_variables": []} self.local_vars = [] self.aux = [] self.functions = [] self.module = ir.module.Module() llvm.initialize() llvm.initialize_all_targets() llvm.initialize_native_target() llvm.initialize_native_asmprinter() self.module = ir.Module('module.bc') self.module.triple = llvm.get_default_triple() target = llvm.Target.from_triple(self.module.triple) target_machine = target.create_target_machine() self.module.data_layout = target_machine.target_data self.escrevaInteiro = ir.Function(self.module, ir.FunctionType( ir.VoidType(), [ir.IntType(32)]), name="escrevaInteiro") self.escrevaFlutuante = ir.Function(self.module, ir.FunctionType( ir.VoidType(), [ir.FloatType()]), name="escrevaFlutuante") self.leiaInteiro = ir.Function(self.module, ir.FunctionType(ir.IntType(32), []), name="leiaInteiro") self.leiaFlutuante = ir.Function(self.module, ir.FunctionType(ir.FloatType(), []), name="leiaFlutuante")
def init(self): if not isinstance(self.root, Descriptor): raise TypeError('cannot initialize, root descriptor not set') self._module = m = ll.Module() self.root.setup(m) fty = ll.FunctionType(ll.VoidType(), tuple()) f_step = ll.Function(m, fty, name='step') f_burst = ll.Function(m, fty, name='burst') builder = ll.IRBuilder() # f_step block = f_step.append_basic_block() builder.position_at_end(block) self.root.compile(m, builder) builder.ret_void() # f_burst block = f_burst.append_basic_block() loop = f_burst.append_basic_block() exit = f_burst.append_basic_block() builder.position_at_end(block) cnt = builder.alloca(INT_TYPE) builder.store(ll.Constant(INT_TYPE, BURST_SIZE), cnt) builder.branch(loop) builder.position_at_end(loop) # Actual simulation code builder.call(f_step, tuple()) # Loop code v = builder.load(cnt) v = builder.sub(v, ll.Constant(INT_TYPE, 1)) builder.store(v, cnt) cond = builder.icmp_unsigned('==', v, ll.Constant(INT_TYPE, 0)) builder.cbranch(cond, loop, exit) builder.position_at_end(exit) builder.ret_void() llmod = llvm.parse_assembly(str(m)) target = llvm.Target.from_default_triple().create_target_machine() self._exec_engine = ee = llvm.create_mcjit_compiler(llmod, target) ee.finalize_object() self._step_fn = CFUNCTYPE(None)(ee.get_function_address('step')) self._burst_fn = CFUNCTYPE(None)(ee.get_function_address('burst')) for v in m.global_values: if isinstance(v, ll.GlobalVariable): self._pins.append(v.name) self.ready = True
def test_nvvm_accepts_encoding(self): # Test that NVVM will accept a constant containing all possible 8-bit # characters. Taken from the test case added in llvmlite PR #53: # # https://github.com/numba/llvmlite/pull/53 # # This test case is included in Numba to ensure that the encoding used # by llvmlite (e.g. utf-8, latin1, etc.) does not result in an input to # NVVM that it cannot parse correctly # Create a module with a constant containing all 8-bit characters c = ir.Constant(ir.ArrayType(ir.IntType(8), 256), bytearray(range(256))) m = ir.Module() gv = ir.GlobalVariable(m, c.type, "myconstant") gv.global_constant = True gv.initializer = c nvvm.fix_data_layout(m) # Parse with LLVM then dump the parsed module into NVVM parsed = llvm.parse_assembly(str(m)) ptx = nvvm.llvm_to_ptx(str(parsed)) # Ensure all characters appear in the generated constant array. elements = ", ".join([str(i) for i in range(256)]) myconstant = f"myconstant[256] = {{{elements}}}".encode('utf-8') self.assertIn(myconstant, ptx)
def __init__(self): self.type_map = { 'bool': ir.IntType(1), 'int': ir.IntType(32), 'float': ir.FloatType(), 'double': ir.DoubleType(), 'void': ir.VoidType(), 'str': ir.ArrayType(ir.IntType(8), 1), # Note i8 in most languages are characters } self.module = ir.Module('main') # Defining builtin function (printf) fnty = ir.FunctionType(self.type_map['int'], [ir.IntType(8).as_pointer()], var_arg=True) func = ir.Function(self.module, fnty, 'printf') # This helps to keep track of Defined Variabled self.variables = {'printf': (func, ir.IntType(32))} # Current Builder self.builder = None self.i = 0
def generate(self): self.binding.initialize() self.binding.initialize_native_target() self.binding.initialize_native_asmprinter() self.binding.load_library_permanently('./io.so') self.module = ir.Module('module') self.module.triple = self.binding.get_default_triple() target = self.binding.Target.from_default_triple() target_machine = target.create_target_machine() backing_mod = binding.parse_assembly("") engine = binding.create_mcjit_compiler(backing_mod, target_machine) self.engine = engine self.builder = None self.declare_runtime_functions() self.declare_functions(self.tree) self._traverse(self.tree) main = self.module.get_global("principal") main.name = "main" # pm_builder = binding.PassManagerBuilder() # pm_builder.populate() llvm_ir = str(self.module) print('Sem otimização') print(llvm_ir) mod = self.binding.parse_assembly(llvm_ir) mod.verify() m_pass = binding.ModulePassManager() m_pass.add_basic_alias_analysis_pass() m_pass.add_cfg_simplification_pass() m_pass.add_constant_merge_pass() m_pass.add_dead_arg_elimination_pass() m_pass.add_dead_code_elimination_pass() m_pass.add_function_attrs_pass() m_pass.add_global_dce_pass() m_pass.add_gvn_pass() m_pass.add_instruction_combining_pass() m_pass.add_global_optimizer_pass() m_pass.add_type_based_alias_analysis_pass() m_pass.run(mod) print('\n\nCom otimização') print(mod) self.engine.add_module(mod) self.engine.finalize_object() self.engine.run_static_constructors() with open('out.ll', 'w') as out: out.write(str(mod))
def _create_empty_module(self, name): ir_module = ir.Module(name) ir_module.triple = CUDA_TRIPLE if self._data_layout: ir_module.data_layout = self._data_layout nvvm.add_ir_version(ir_module) return ir_module
def make_test_context(emits_ir: bool = True) -> CodegenContext: module = ir.Module(name="test_module") builder = ir.IRBuilder() if emits_ir else None return CodegenContext(builder=builder, module=module, emits_debug=True, file_path=DummyPath("test"))
def __init__(self): super(simpleCVisitor, self).__init__() #控制llvm生成 self.Module = ir.Module() self.Module.triple = "x86_64-pc-linux-gnu" # llvm.Target.from_default_triple() self.Module.data_layout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" # llvm.create_mcjit_compiler(backing_mod, target_machine) #语句块 self.Blocks = [] #待生成的llvm语句块 self.Builders = [] #函数列表 self.Functions = dict() #结构体列表 self.Structure = Structure() #当前所在函数 self.CurrentFunction = '' self.Constants = 0 #这个变量是否需要加载 self.WhetherNeedLoad = True #endif块 self.EndifBlock = None #符号表 self.SymbolTable = SymbolTable()
def _config_llvm(self): self.module = ir.Module(name=__file__) self.module.triple = self.binding.get_default_triple() func_type = ir.FunctionType(ir.VoidType(), [], False) base_func = ir.Function(self.module, func_type, name="main") block = base_func.append_basic_block(name="entry") self.builder = ir.IRBuilder(block)
def __init__(self, file=None, name=None): S.Scope.__init__(self) if name: self.qname = self.mname = name else: self.file = file self.qname, self.mname = find_name(self.file) self.llvm = ir.Module(name=self.qname) self.llvm.triple = binding.get_default_triple() self.imports = set() self.links = set() self.libs = set() self.runtime = runtime.Runtime(self) self.static = static.Static(self) self.runtime.declare() typ = T.arr(T.i8, len(self.qname) + 1) ptr = self.add_global(typ, name=self.mangle('_name')) ptr.initializer = typ(bytearray(self.qname + '\0', 'utf-8')) self.name_ptr = ptr.gep([T.i32(0), T.i32(0)]) self.builder = None self.arg_ptrs = None self.landingpad = None self.before = None self.loop = None self.after = None self.ret_ptr = None self.name_counter = 0
def __init__(self) -> None: self.module: ll.Module = ll.Module("zet") self.module.triple = llvm.get_default_triple() self.block : ll.Block = None self.builder: ll.IRBuilder = None self.variables = {} self.functions = {} self.methods = {} self.structs = {} self.flags = { "string" : 0 } self.modifiers = { "attribute" : Attribute(), "static": Static(), } self.sizes = { "i32" : 4, "i8" : 1, "str" : 1, "void": 0, } self.types = { "i32" : ll.IntType(32), "i8" : ll.IntType(8), "str" : ll.IntType(8).as_pointer(), "void": ll.VoidType() }
def __init__(self): self.types = { 'Void': ir.VoidType(), 'Int': ir.IntType(32), } self.module = ir.Module() self.symbols = SymbolTable()