def __init__(self, tree: Program, symbol_table: SymbolTable, source_file: str) -> None: llvm.initialize() llvm.initialize_all_targets() llvm.initialize_all_asmprinters() self.tree = tree self.codegen = CodeGenerator(symbol_table) self.source_file = source_file self.target = llvm.Target.from_triple(llvm.get_default_triple())
def _binding_initialize(): global __initialized if not __initialized: binding.initialize() if not ptx_enabled: # native == currently running CPU. ASM printer includes opcode emission binding.initialize_native_target() binding.initialize_native_asmprinter() else: binding.initialize_all_targets() binding.initialize_all_asmprinters() __initialized = True
def llvm_get_target(triple_or_target=None): global __llvm_initialized if not __llvm_initialized: # Lazy initialisation llvm.initialize() llvm.initialize_all_targets() llvm.initialize_all_asmprinters() __llvm_initialized = True if isinstance(triple_or_target, llvm.Target): return triple_or_target if triple_or_target is None: return llvm.Target.from_default_triple() return llvm.Target.from_triple(triple_or_target)
def compile_IR(ir): """Return execution engine with IR compiled in. Parameters ---------- ir : str Specify LLVM IR code as a string. Returns ------- engine : Execution engine. Examples -------- To get the address of the compiled functions, use:: addr = engine.get_function_address("<function name>") """ triple = re.search(r'target\s+triple\s*=\s*"(?P<triple>[-\d\w\W_]+)"\s*$', ir, re.M).group('triple') # Create execution engine llvm.initialize() llvm.initialize_all_targets() llvm.initialize_all_asmprinters() target = llvm.Target.from_triple(triple) target_machine = target.create_target_machine() backing_mod = llvm.parse_assembly("") engine = llvm.create_mcjit_compiler(backing_mod, target_machine) # Create LLVM module and compile mod = llvm.parse_assembly(ir) mod.verify() engine.add_module(mod) engine.finalize_object() engine.run_static_constructors() return engine
# # The Autobentifier # # Copyright (c) 2020, Arm Limited. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # from llvmlite import binding binding.initialize() binding.initialize_all_targets() binding.initialize_all_asmprinters() with open("demo.elf.ll") as fp: x = fp.read() xl = binding.parse_assembly(x)
def test_llvm_lite_ptx_pycuda(): # Create some useful types double = ir.DoubleType() fnty = ir.FunctionType(ir.VoidType(), (double, double, double.as_pointer())) # Create an empty module... module = ir.Module(name=__file__) # and declare a function named "fpadd" inside it func = ir.Function(module, fnty, name="fpadd") # Now implement basic addition # basic blocks are sequences of instructions that have exactly one # entry point and one exit point (no control flow) # We only need one in this case # See available operations at: # http://llvmlite.readthedocs.io/en/latest/ir/builder.html#instruction-building block = func.append_basic_block(name="entry") builder = ir.IRBuilder(block) a, b, res = func.args result = builder.fadd(a, b, name="res") builder.store(result, res) builder.ret_void() # Add kernel mark metadata module.add_named_metadata( "nvvm.annotations", [func, "kernel", ir.IntType(32)(1)]) # Uncomment to print the module IR. This prints LLVM IR assembly. # print("LLVM IR:\n", module) binding.initialize() binding.initialize_all_targets() binding.initialize_all_asmprinters() capability = pycuda_default.device.compute_capability() # Create compilation target, use default triple target = binding.Target.from_triple("nvptx64-nvidia-cuda") target_machine = target.create_target_machine(cpu="sm_{}{}".format( capability[0], capability[1]), codemodel='small') mod = binding.parse_assembly(str(module)) mod.verify() ptx = target_machine.emit_assembly(mod) # Uncomment to print generated x86 assembly # print("PTX assembly:\n", ptx) ptx_mod = pycuda.driver.module_from_buffer(ptx.encode()) cuda_func = ptx_mod.get_function('fpadd') # Run the function via ctypes a = np.float64(10.0) b = np.float64(3.5) res = np.empty(1, dtype=np.float64) dev_res = pycuda.driver.Out(res) cuda_func(a, b, dev_res, block=(1, 1, 1)) assert res[0] == (a + b)
def initialize_llvm(): llvm.initialize() llvm.initialize_all_targets() llvm.initialize_all_asmprinters()
import os, sys, tempfile, subprocess, io from artiq.compiler import types, ir from llvmlite import ir as ll, binding as llvm llvm.initialize() llvm.initialize_all_targets() llvm.initialize_all_asmprinters() class RunTool: def __init__(self, pattern, **tempdata): self._pattern = pattern self._tempdata = tempdata self._tempnames = {} self._tempfiles = {} def __enter__(self): for key, data in self._tempdata.items(): if data is None: fd, filename = tempfile.mkstemp() os.close(fd) self._tempnames[key] = filename else: with tempfile.NamedTemporaryFile(delete=False) as f: f.write(data) self._tempnames[key] = f.name cmdline = [] for argument in self._pattern: cmdline.append(argument.format(**self._tempnames)) # https://bugs.python.org/issue17023