def __init__(self, env, libs=None): self.destroyed = False libs = libs or ['prelude'] for lib in libs: if 'darwin' in sys.platform: prelude = join(dirname(realpath(__file__)), lib + '.dylib') elif 'linux' in sys.platform: prelude = join(dirname(realpath(__file__)), lib+ '.so') else: raise NotImplementedError # XXX: yeah, don't do this ctypes._dlopen(prelude, ctypes.RTLD_GLOBAL) cgen = env['cgen'] self.__namespace = cgen.globals self.__llmodule = cgen.module.clone() if not detect_avx_support(): tc = le.TargetMachine.new(features='-avx', cm=le.CM_JITDEFAULT) else: tc = le.TargetMachine.new(features='', cm=le.CM_JITDEFAULT) eb = le.EngineBuilder.new(self.__llmodule) self.__engine = eb.create(tc) #self.__engine.run_function(cgen.globals['__module'], []) mod = ModuleType('blir_wrapper') wrap_llvm_module(cgen.module, self.__engine, mod) self.__mod = mod
def run(llvm_ir): # Load the runtime ctypes._dlopen(os.path.join(_path, 'gonert.so'), ctypes.RTLD_GLOBAL) # Initialize LLVM llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() target = llvm.Target.from_default_triple() target_machine = target.create_target_machine() mod = llvm.parse_assembly(llvm_ir) mod.verify() engine = llvm.create_mcjit_compiler(mod, target_machine) # Execute the main() function # # !!! Note: Requires modification in Project 8 (see below) # main_ptr = engine.get_function_address('main') # main_func = ctypes.CFUNCTYPE(None)(main_ptr) # main_func() init_ptr = engine.get_function_address('__init') init_func = ctypes.CFUNCTYPE(None)(init_ptr) init_func() main_ptr = engine.get_function_address('_gone_main') main_func = ctypes.CFUNCTYPE(None)(main_ptr) main_func()
def run(llvm_ir): # Load the runtime if os.name != 'nt': ctypes._dlopen(os.path.join(_path, 'gonert.so'), ctypes.RTLD_GLOBAL) else: ctypes._dlopen(os.path.join(_path, 'gonert.dll'), ctypes.RTLD_GLOBAL) # Initialize LLVM llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() target = llvm.Target.from_default_triple() target_machine = target.create_target_machine() mod = llvm.parse_assembly(llvm_ir) mod.verify() engine = llvm.create_mcjit_compiler(mod, target_machine) # Execute the main() function # # !!! Note: Requires modification in Project 8 (see below) init_ptr = engine.get_function_address('__init') init_func = ctypes.CFUNCTYPE(None)(init_ptr) init_func() main_ptr = engine.get_function_address('_gone_main') main_func = ctypes.CFUNCTYPE(None)(main_ptr) main_func()
def main(): import gonelex import goneparse import gonecheck import gonecode import goneblock import sys import ctypes from errors import subscribe_errors, errors_reported from llvm.ee import ExecutionEngine # Load the Gone runtime library (see Makefile) ctypes._dlopen('./gonert.so', ctypes.RTLD_GLOBAL) lexer = gonelex.make_lexer() parser = goneparse.make_parser() with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")): program = parser.parse(open(sys.argv[1]).read()) # Check the program gonecheck.check_program(program) # If no errors occurred, generate code if not errors_reported(): functions = gonecode.generate_code(program) # Emit the code sequence bv = LLVMBlockVisitor() bv.generate_llvm(functions) print(bv.llvm.module) # Verify and run function that was created during code generation print(":::: RUNNING ::::") bv.llvm.function.verify() llvm_executor = ExecutionEngine.new(bv.llvm.module) llvm_executor.run_function(bv.llvm.vars['__start'], [])
def main(): import gonelex import goneparse import gonecheck import gonecode import sys import ctypes import time import argparse from errors import subscribe_errors, errors_reported from llvm.ee import ExecutionEngine global args parser = argparse.ArgumentParser("Compile and run a Gone program from a .g file") parser.add_argument('file', type=str, default='', nargs=1, help="the file containing Gone source") parser.add_argument('--verbose', '-v', action="store_true", help="print verbose output") parser.add_argument('--validate', '-c', action="store_true", help="perform llvm bitcode validation prior to program execution") args = parser.parse_args() # Load the Gone runtime library (see Makefile) ctypes._dlopen('./gonert.so', ctypes.RTLD_GLOBAL) lexer = gonelex.make_lexer() parser = goneparse.make_parser() with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")): program = parser.parse(open(args.file[0]).read()) gonecheck.check_program(program) if not errors_reported(): code = gonecode.generate_code(program) g = GenerateLLVMBlockVisitor() g.visit_functions(code.functions) if args.verbose: print("---- NATIVE ASSEMBLY ----") print(g.generator.module.to_native_assembly()) print("---- END NATIVE ASSEMBLY ----") if args.verbose: print(":::: RUNNING ::::") start = time.time() llvm_executor = ExecutionEngine.new(g.generator.module) llvm_executor.run_function(g.generator.main_func, []) if args.verbose: print(":::: FINISHED ::::") print("execution time: {0:.15f}s".format(time.time() - start))
from ctypes import pythonapi, util, py_object from numpy import ctypeslib, typeDict import platform.system as psystem from os.path import splitext, join, isfile, dirname from warnings import warn libfullpath = r'$libraryfullpath$' if not isfile(libfullpath) and osname=='nt': if isfile(join(dirname(__file__), libfullpath)): libfullpath = join(dirname(__file__), libfullpath) #libname, ext = splitext(libfullpath) libname = libfullpath.split('.')[0] ext ='.'+ '.'.join(libfullpath.split('.')[1:]) # must use ctypes.RTLD_GLOBAL for threading support ctypes._dlopen(libfullpath, ctypes.RTLD_GLOBAL) lib = ctypes.cdll.LoadLibrary(libfullpath) if psystem() == 'Windows': lib_threads = libname else: lib_threads = libname + '_threads'+ ext try: lib_threads = ctypes.cdll.LoadLibrary(lib_threads) except OSError, e: warn("Could not load threading library %s, threading support is disabled" %lib_threads) lib_threads = None _typelist = [('$libname$_plan_dft_1d', (typeDict['$complex$'], typeDict['$complex$'], 1)),
import struct import llvmlite.binding as llvm from artiq.language.core import int64 from artiq.py2llvm.infer_types import infer_function_types from artiq.py2llvm import base_types, lists from artiq.py2llvm.module import Module llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() if struct.calcsize("P") < 8: from ctypes import _dlopen, RTLD_GLOBAL _dlopen("libgcc_s.so", RTLD_GLOBAL) def _base_types(choice): a = 2 # promoted later to int64 b = a + 1 # initially int32, becomes int64 after a is promoted c = b//2 # initially int32, becomes int64 after b is promoted d = 4 and 5 # stays int32 x = int64(7) a += x # promotes a to int64 foo = True | True or False bar = None myf = 4.5 myf2 = myf + x if choice and foo and not bar:
import os import numpy as np import pyarrow as pa from pyarrow import csv import omniscidbe as dbe import ctypes ctypes._dlopen('libDBEngine.so', ctypes.RTLD_GLOBAL) d = dbe.PyDbEngine(enable_fsi=1, data='data', calcite_port=9091) assert not d.closed root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) csv_file = root + "/Tests/Import/datafiles/trips_with_headers_top1000.csv" print("DDL") r = d.executeDDL(""" CREATE TEMPORARY TABLE trips ( trip_id BIGINT, vendor_id TEXT ENCODING NONE, pickup_datetime TIMESTAMP, dropoff_datetime TIMESTAMP, store_and_fwd_flag TEXT ENCODING DICT, rate_code_id BIGINT, pickup_longitude DOUBLE, pickup_latitude DOUBLE, dropoff_longitude DOUBLE, dropoff_latitude DOUBLE, passenger_count BIGINT, trip_distance DOUBLE, fare_amount DOUBLE,
'int' : int_type, 'float' : float_type, 'string' : string_type } # The following class is going to generate the LLVM instruction stream. # The basic features of this class are going to mirror the experiments # you tried in Exercise 5. The execution module is very similar # to the interpreter written in Project 4. See specific comments # in the class. import exprast from collections import defaultdict import ctypes ctypes._dlopen("./ex5.so", ctypes.RTLD_GLOBAL) # STEP 1: Map map operator symbol names such as +, -, *, / # to actual opcode names 'add','sub','mul','div' to be emitted in # the SSA code. This is easy to do using dictionaries: binary_ops = { '+' : 'add', '-' : 'sub', '*' : 'mul', '/' : 'div', } unary_ops = { '+' : 'uadd', '-' : 'usub'
import os import llvmlite_artiq.binding as llvm from artiq.language.core import int64 from artiq.py2llvm.infer_types import infer_function_types from artiq.py2llvm import base_types, lists from artiq.py2llvm.module import Module llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() if struct.calcsize("P") < 8 and os.name != "nt": from ctypes import _dlopen, RTLD_GLOBAL _dlopen("libgcc_s.so", RTLD_GLOBAL) def _base_types(choice): a = 2 # promoted later to int64 b = a + 1 # initially int32, becomes int64 after a is promoted c = b//2 # initially int32, becomes int64 after b is promoted d = 4 and 5 # stays int32 x = int64(7) a += x # promotes a to int64 foo = True | True or False bar = None myf = 4.5 myf2 = myf + x if choice and foo and not bar:
# define a function that calls a function distance_func = Function.new(mod, Type.function(Type.double(), [Type.double(), Type.double()], False), "distance") block = distance_func.append_basic_block("entry") builder = Builder.new(block) x = distance_func.args[0] y = distance_func.args[1] d2 = builder.call(dsquared_func, [x,y]) d = builder.call(sqrt_func, [d2]) builder.ret(d) result = engine.run_function(distance_func, [a, b]) #print(result.as_real(Type.double())) # run a function from a dynamically loaded c library ctypes._dlopen("./rtlib.so", ctypes.RTLD_GLOBAL) print_float_func = Function.new(mod, Type.function(Type.void(), [Type.double()], False), "print_float") engine.run_function(print_float_func, [result]) main_func = Function.new(mod, Type.function(Type.void(), [], False), "main") block = main_func.append_basic_block("entry") builder = Builder.new(block) x = Constant.real(Type.double(), 3.0) y = Constant.real(Type.double(), 4.0) d = builder.call(distance_func, [x,y]) builder.call(print_float_func, [d]) builder.ret_void() engine.run_function(main_func, [])