recipes icon indicating copy to clipboard operation
recipes copied to clipboard

add llvm 11.0, llvmlite and numba

Open wolfv opened this issue 3 years ago • 9 comments

wolfv avatar Aug 08 '22 15:08 wolfv

Test script:

from ctypes import CFUNCTYPE, c_double

import llvmlite.binding as llvm


# All these initializations are required for code generation!
llvm.initialize()
llvm.initialize_all_targets()
llvm.initialize_all_asmprinters()  # yes, even this one

llvm_ir = """
   ; ModuleID = "examples/ir_fpadd.py"
   target triple = "wasm32"
   target datalayout = ""

   define double @"fpadd"(double %".1", double %".2")
   {
   entry:
     %"res" = fadd double %".1", %".2"
     ret double %"res"
   }
   """

def create_execution_engine():
    """
    Create an ExecutionEngine suitable for JIT code generation on
    the host CPU.  The engine is reusable for an arbitrary number of
    modules.
    """
    # Create a target machine representing the host
    target = llvm.Target.from_triple("wasm32")
    target_machine = target.create_target_machine()
    # And an execution engine with an empty backing module
    backing_mod = llvm.parse_assembly("")
    engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
    return engine


def compile_ir(engine, llvm_ir):
    """
    Compile the LLVM IR string with the given engine.
    The compiled module object is returned.
    """
    # Create a LLVM module object from the IR
    mod = llvm.parse_assembly(llvm_ir)
    mod.verify()
    # Now add the module and make sure it is ready for execution
#     engine.add_module(mod)
#     engine.finalize_object()
#     engine.run_static_constructors()
    return mod


engine = create_execution_engine()
mod = compile_ir(engine, llvm_ir)

# Look up the function pointer (a Python int)
# func_ptr = engine.get_function_address("fpadd")

# # Run the function via ctypes
# cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
# res = cfunc(1.0, 3.5)
# print("fpadd(...) =", res)

target = llvm.Target.from_triple("wasm32")
target_machine = target.create_target_machine()
asm = str(target_machine.emit_assembly(mod))
print(asm)

wolfv avatar Aug 08 '22 17:08 wolfv

Test script:

from ctypes import CFUNCTYPE, c_double

import llvmlite.binding as llvm


# All these initializations are required for code generation!
llvm.initialize()
llvm.initialize_all_targets()
llvm.initialize_all_asmprinters()  # yes, even this one

llvm_ir = """
   ; ModuleID = "examples/ir_fpadd.py"
   target triple = "wasm32"
   target datalayout = ""

   define double @"fpadd"(double %".1", double %".2")
   {
   entry:
     %"res" = fadd double %".1", %".2"
     ret double %"res"
   }
   """

def create_execution_engine():
    """
    Create an ExecutionEngine suitable for JIT code generation on
    the host CPU.  The engine is reusable for an arbitrary number of
    modules.
    """
    # Create a target machine representing the host
    target = llvm.Target.from_triple("wasm32")
    target_machine = target.create_target_machine()
    # And an execution engine with an empty backing module
    backing_mod = llvm.parse_assembly("")
    engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
    return engine


def compile_ir(engine, llvm_ir):
    """
    Compile the LLVM IR string with the given engine.
    The compiled module object is returned.
    """
    # Create a LLVM module object from the IR
    mod = llvm.parse_assembly(llvm_ir)
    mod.verify()
    # Now add the module and make sure it is ready for execution
#     engine.add_module(mod)
#     engine.finalize_object()
#     engine.run_static_constructors()
    return mod


engine = create_execution_engine()
mod = compile_ir(engine, llvm_ir)

# Look up the function pointer (a Python int)
# func_ptr = engine.get_function_address("fpadd")

# # Run the function via ctypes
# cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
# res = cfunc(1.0, 3.5)
# print("fpadd(...) =", res)

target = llvm.Target.from_triple("wasm32")
target_machine = target.create_target_machine()
asm = str(target_machine.emit_assembly(mod))
print(asm)

@wolfv can we make this an actual pytest test?

DerThorsten avatar Aug 08 '22 17:08 DerThorsten

@wolfv good pkg to check if the numba stuff works: https://pypi.org/project/umap-learn/ =)

DerThorsten avatar Aug 08 '22 17:08 DerThorsten

of course, but I just wanted to keep it there to not loose this valuable content :)

wolfv avatar Aug 08 '22 17:08 wolfv

Datashader is also a great test case for these packages when they are available. We are eagerly awaiting trying out Datashader standalone in the browser!

jbednar avatar Sep 23 '22 15:09 jbednar

Is this blocked by anything? Would be awesome to get numba support into pyodide.

twiecki avatar May 06 '23 13:05 twiecki

@DerThorsten , can you provide some context here? Is this being closed for lack of progress, or for some other reason?

jbednar avatar Nov 30 '23 16:11 jbednar

@DerThorsten , can you provide some context here? Is this being closed for lack of progress, or for some other reason?

Lack of progress, but I'll reopen it in the hope someone wants to continue

DerThorsten avatar Dec 01 '23 13:12 DerThorsten

this needs to be converted to the new rattler-recipe format

DerThorsten avatar May 10 '24 07:05 DerThorsten