Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes the usage of llvmlite.llvmpy #932

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions numba_dpex/core/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

from llvmlite import binding as ll
from llvmlite.llvmpy import core as lc
from llvmlite import ir as llvmir
from numba.core import utils
from numba.core.codegen import CPUCodegen, CPUCodeLibrary

Expand Down Expand Up @@ -71,7 +71,7 @@ def _init(self, llvm_module):
)

def _create_empty_module(self, name):
ir_module = lc.Module(name)
ir_module = llvmir.Module(name)
ir_module.triple = SPIR_TRIPLE[utils.MACHINE_BITS]
if self._data_layout:
ir_module.data_layout = self._data_layout
Expand Down
30 changes: 15 additions & 15 deletions numba_dpex/core/runtime/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import functools

import llvmlite.llvmpy.core as lc
from llvmlite import ir
from llvmlite.llvmpy.core import ATTR_NO_CAPTURE, Type
from llvmlite import ir as llvmir
from numba.core import cgutils, types


Expand Down Expand Up @@ -61,8 +59,8 @@ def meminfo_alloc_unchecked(self, builder, size, usm_type, device):
Returns: A pointer to the MemInfo is returned.
"""
mod = builder.module
u64 = ir.IntType(64)
fnty = ir.FunctionType(
u64 = llvmir.IntType(64)
fnty = llvmir.FunctionType(
cgutils.voidptr_t, [cgutils.intp_t, u64, cgutils.voidptr_t]
)
fn = cgutils.get_or_insert_function(mod, fnty, "DPEXRT_MemInfo_alloc")
Expand Down Expand Up @@ -94,9 +92,9 @@ def meminfo_fill_unchecked(
Returns: A pointer to the `MemInfo` is returned.
"""
mod = builder.module
u64 = ir.IntType(64)
b = ir.IntType(1)
fnty = ir.FunctionType(
u64 = llvmir.IntType(64)
b = llvmir.IntType(1)
fnty = llvmir.FunctionType(
cgutils.voidptr_t,
[cgutils.voidptr_t, u64, b, cgutils.int8_t, cgutils.voidptr_t],
)
Expand All @@ -119,10 +117,12 @@ def arraystruct_from_python(self, pyapi, obj, ptr):
Returns:
_type_: _description_
"""
fnty = Type.function(Type.int(), [pyapi.pyobj, pyapi.voidptr])
fnty = llvmir.FunctionType(
llvmir.IntType(32), [pyapi.pyobj, pyapi.voidptr]
)
fn = pyapi._get_function(fnty, "DPEXRT_sycl_usm_ndarray_from_python")
fn.args[0].add_attribute(lc.ATTR_NO_CAPTURE)
fn.args[1].add_attribute(lc.ATTR_NO_CAPTURE)
fn.args[0].add_attribute("nocapture")
fn.args[1].add_attribute("nocapture")

self.error = pyapi.builder.call(fn, (obj, ptr))

Expand All @@ -143,15 +143,15 @@ def usm_ndarray_to_python_acqref(self, pyapi, aryty, ary, dtypeptr):
args = [
pyapi.voidptr,
pyapi.pyobj,
ir.IntType(32),
ir.IntType(32),
llvmir.IntType(32),
llvmir.IntType(32),
pyapi.pyobj,
]
fnty = Type.function(pyapi.pyobj, args)
fnty = llvmir.FunctionType(pyapi.pyobj, args)
fn = pyapi._get_function(
fnty, "DPEXRT_sycl_usm_ndarray_to_python_acqref"
)
fn.args[0].add_attribute(ATTR_NO_CAPTURE)
fn.args[0].add_attribute("nocapture")

aryptr = cgutils.alloca_once_value(pyapi.builder, ary)
ptr = pyapi.builder.bitcast(aryptr, pyapi.voidptr)
Expand Down
63 changes: 33 additions & 30 deletions numba_dpex/core/targets/kernel_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import numpy as np
from llvmlite import binding as ll
from llvmlite import ir as llvmir
from llvmlite.llvmpy import core as lc
from numba import typeof
from numba.core import cgutils, types, typing, utils
from numba.core.base import BaseContext
Expand Down Expand Up @@ -153,40 +152,40 @@ def _gen_arg_addrspace_md(self, fn):
else:
codes.append(address_space.PRIVATE)

consts = [lc.Constant.int(lc.Type.int(), x) for x in codes]
name = lc.MetaDataString.get(mod, "kernel_arg_addr_space")
return lc.MetaData.get(mod, [name] + consts)
consts = [llvmir.Constant(llvmir.IntType(32), x) for x in codes]
name = llvmir.MetaDataString(mod, "kernel_arg_addr_space")
return mod.add_metadata([name] + consts)

def _gen_arg_access_qual_md(self, fn):
"""Generate kernel_arg_access_qual metadata."""
mod = fn.module
consts = [lc.MetaDataString.get(mod, "none")] * len(fn.args)
name = lc.MetaDataString.get(mod, "kernel_arg_access_qual")
return lc.MetaData.get(mod, [name] + consts)
consts = [llvmir.MetaDataString(mod, "none")] * len(fn.args)
name = llvmir.MetaDataString(mod, "kernel_arg_access_qual")
return mod.add_metadata([name] + consts)

def _gen_arg_type(self, fn):
"""Generate kernel_arg_type metadata."""
mod = fn.module
fnty = fn.type.pointee
consts = [lc.MetaDataString.get(mod, str(a)) for a in fnty.args]
name = lc.MetaDataString.get(mod, "kernel_arg_type")
return lc.MetaData.get(mod, [name] + consts)
consts = [llvmir.MetaDataString(mod, str(a)) for a in fnty.args]
name = llvmir.MetaDataString(mod, "kernel_arg_type")
return mod.add_metadata([name] + consts)

def _gen_arg_type_qual(self, fn):
"""Generate kernel_arg_type_qual metadata."""
mod = fn.module
fnty = fn.type.pointee
consts = [lc.MetaDataString.get(mod, "") for _ in fnty.args]
name = lc.MetaDataString.get(mod, "kernel_arg_type_qual")
return lc.MetaData.get(mod, [name] + consts)
consts = [llvmir.MetaDataString(mod, "") for _ in fnty.args]
name = llvmir.MetaDataString(mod, "kernel_arg_type_qual")
return mod.add_metadata([name] + consts)

def _gen_arg_base_type(self, fn):
"""Generate kernel_arg_base_type metadata."""
mod = fn.module
fnty = fn.type.pointee
consts = [lc.MetaDataString.get(mod, str(a)) for a in fnty.args]
name = lc.MetaDataString.get(mod, "kernel_arg_base_type")
return lc.MetaData.get(mod, [name] + consts)
consts = [llvmir.MetaDataString(mod, str(a)) for a in fnty.args]
name = llvmir.MetaDataString(mod, "kernel_arg_base_type")
return mod.add_metadata([name] + consts)

def _finalize_wrapper_module(self, fn):
"""Add metadata and calling convention to the wrapper function.
Expand All @@ -207,10 +206,11 @@ def _finalize_wrapper_module(self, fn):
fn.calling_convention = CC_SPIR_KERNEL

# Mark kernels
ocl_kernels = mod.get_or_insert_named_metadata("opencl.kernels")
ocl_kernels = cgutils.get_or_insert_named_metadata(
mod, "opencl.kernels"
)
ocl_kernels.add(
lc.MetaData.get(
mod,
mod.add_metadata(
[
fn,
self._gen_arg_addrspace_md(fn),
Expand All @@ -223,33 +223,34 @@ def _finalize_wrapper_module(self, fn):
)

# Other metadata
empty_md = lc.MetaData.get(mod, ())
others = [
"opencl.used.extensions",
"opencl.used.optional.core.features",
"opencl.compiler.options",
]

for name in others:
nmd = mod.get_or_insert_named_metadata(name)
nmd = cgutils.get_or_insert_named_metadata(mod, name)
if not nmd.operands:
nmd.add(empty_md)
mod.add_metadata([])

def _generate_kernel_wrapper(self, func, argtypes):
module = func.module
arginfo = self.get_arg_packer(argtypes)
wrapperfnty = lc.Type.function(lc.Type.void(), arginfo.argument_types)
wrapperfnty = llvmir.FunctionType(
llvmir.VoidType(), arginfo.argument_types
)
wrapper_module = self.create_module("dpex.kernel.wrapper")
wrappername = "dpexPy_{name}".format(name=func.name)
argtys = list(arginfo.argument_types)
fnty = lc.Type.function(
lc.Type.int(),
fnty = llvmir.FunctionType(
llvmir.IntType(32),
[self.call_conv.get_return_type(types.pyobject)] + argtys,
)
func = wrapper_module.add_function(fnty, name=func.name)
func = llvmir.Function(wrapper_module, fnty, name=func.name)
func.calling_convention = CC_SPIR_FUNC
wrapper = wrapper_module.add_function(wrapperfnty, name=wrappername)
builder = lc.Builder(wrapper.append_basic_block(""))
wrapper = llvmir.Function(wrapper_module, wrapperfnty, name=wrappername)
builder = llvmir.IRBuilder(wrapper.append_basic_block(""))

callargs = arginfo.from_arguments(builder, wrapper.args)

Expand Down Expand Up @@ -399,7 +400,7 @@ def declare_function(self, module, fndesc):
function.

Args:
module (llvmlite.llvmpy.core.Module) : The LLVM module into which
module (llvmlite.ir.Module) : The LLVM module into which
the kernel function will be inserted.
fndesc (numba.core.funcdesc.PythonFunctionDescriptor) : The
signature of the function.
Expand All @@ -410,7 +411,9 @@ def declare_function(self, module, fndesc):

"""
fnty = self.call_conv.get_function_type(fndesc.restype, fndesc.argtypes)
fn = module.get_or_insert_function(fnty, name=fndesc.mangled_name)
fn = cgutils.get_or_insert_function(
module, fnty, name=fndesc.mangled_name
)
if not self.enable_debuginfo:
fn.attributes.add("alwaysinline")
ret = super(DpexKernelTargetContext, self).declare_function(
Expand Down
4 changes: 2 additions & 2 deletions numba_dpex/dpctl_iface/dpctl_capi_fn_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
declarations into an LLVM module.
"""

import llvmlite.llvmpy.core as lc
from llvmlite import ir as llvmir
from numba.core import cgutils, types

import numba_dpex.utils as utils
Expand Down Expand Up @@ -36,7 +36,7 @@ def _build_dpctl_function(builder, return_ty, arg_list, func_name):
Return: A Python object wrapping an LLVM Function.

"""
func_ty = lc.Type.function(return_ty, arg_list)
func_ty = llvmir.FunctionType(return_ty, arg_list)
fn = cgutils.get_or_insert_function(builder.module, func_ty, func_name)
return fn

Expand Down
35 changes: 18 additions & 17 deletions numba_dpex/ocl/oclimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
from functools import reduce

import dpctl
import llvmlite.binding as ll
import llvmlite.llvmpy.core as lc
from llvmlite import ir
from llvmlite.llvmpy.core import Type
from llvmlite import binding as ll
from llvmlite import ir as llvmir
from numba.core import cgutils, types
from numba.core.imputils import Registry
from numba.core.typing.npydecl import parse_dtype
Expand All @@ -26,7 +24,8 @@
registry = Registry()
lower = registry.lower

_void_value = lc.Constant.null(lc.Type.pointer(lc.Type.int(8)))
_void_value = llvmir.Constant(llvmir.IntType(8).as_pointer(), None)


# -----------------------------------------------------------------------------

Expand Down Expand Up @@ -56,11 +55,11 @@ def _declare_function(context, builder, name, sig, cargs, mangler=mangle_c):
"""
mod = builder.module
if sig.return_type == types.void:
llretty = lc.Type.void()
llretty = llvmir.VoidType()
else:
llretty = context.get_value_type(sig.return_type)
llargs = [context.get_value_type(t) for t in sig.args]
fnty = Type.function(llretty, llargs)
fnty = llvmir.FunctionType(llretty, llargs)
mangled = mangler(name, cargs)
fn = cgutils.get_or_insert_function(mod, fnty, mangled)
fn.calling_convention = kernel_target.CC_SPIR_FUNC
Expand Down Expand Up @@ -186,7 +185,7 @@ def insert_and_call_atomic_fn(
ll_p = None
name = ""
if dtype.name == "float32":
ll_val = ir.FloatType()
ll_val = llvmir.FloatType()
ll_p = ll_val.as_pointer()
if fn_type == "add":
name = "numba_dpex_atomic_add_f32"
Expand All @@ -196,7 +195,7 @@ def insert_and_call_atomic_fn(
raise TypeError("Operation type is not supported %s" % (fn_type))
elif dtype.name == "float64":
if True:
ll_val = ir.DoubleType()
ll_val = llvmir.DoubleType()
ll_p = ll_val.as_pointer()
if fn_type == "add":
name = "numba_dpex_atomic_add_f64"
Expand All @@ -222,12 +221,12 @@ def insert_and_call_atomic_fn(

mod = builder.module
if sig.return_type == types.void:
llretty = lc.Type.void()
llretty = llvmir.VoidType()
else:
llretty = context.get_value_type(sig.return_type)

llargs = [ll_p, context.get_value_type(sig.args[2])]
fnty = ir.FunctionType(llretty, llargs)
fnty = llvmir.FunctionType(llretty, llargs)

fn = cgutils.get_or_insert_function(mod, fnty, name)
fn.calling_convention = kernel_target.CC_SPIR_FUNC
Expand Down Expand Up @@ -281,8 +280,8 @@ def native_atomic_add(context, builder, sig, args):
retty = context.get_value_type(sig.return_type)
spirv_fn_arg_types = [
ptr_type,
ir.IntType(32),
ir.IntType(32),
llvmir.IntType(32),
llvmir.IntType(32),
context.get_value_type(sig.args[2]),
]

Expand All @@ -299,7 +298,7 @@ def native_atomic_add(context, builder, sig, args):
],
)

fnty = ir.FunctionType(retty, spirv_fn_arg_types)
fnty = llvmir.FunctionType(retty, spirv_fn_arg_types)
fn = cgutils.get_or_insert_function(builder.module, fnty, mangled_fn_name)
fn.calling_convention = kernel_target.CC_SPIR_FUNC

Expand Down Expand Up @@ -516,18 +515,20 @@ def _generic_array(context, builder, shape, dtype, symbol_name, addrspace):
"""
elemcount = reduce(operator.mul, shape)
lldtype = context.get_data_type(dtype)
laryty = Type.array(lldtype, elemcount)
laryty = llvmir.ArrayType(lldtype, elemcount)

if addrspace == address_space.LOCAL:
lmod = builder.module

# Create global variable in the requested address-space
gvmem = lmod.add_global_variable(laryty, symbol_name, addrspace)
gvmem = cgutils.add_global_variable(
lmod, laryty, symbol_name, addrspace
)

if elemcount <= 0:
raise ValueError("array length <= 0")
else:
gvmem.linkage = lc.LINKAGE_INTERNAL
gvmem.linkage = "internal"

if dtype not in types.number_domain:
raise TypeError("unsupported type: %s" % dtype)
Expand Down
12 changes: 8 additions & 4 deletions numba_dpex/printimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from functools import singledispatch

import llvmlite.llvmpy.core as lc
from numba.core import cgutils, types, typing
import llvmlite.ir as llvmir
from numba.core import cgutils, types
from numba.core.imputils import Registry

from numba_dpex.utils import address_space
Expand All @@ -15,8 +15,12 @@


def declare_print(lmod):
voidptrty = lc.Type.pointer(lc.Type.int(8), addrspace=address_space.GENERIC)
printfty = lc.Type.function(lc.Type.int(), [voidptrty], var_arg=True)
voidptrty = llvmir.PointerType(
llvmir.IntType(8), addrspace=address_space.GENERIC
)
printfty = llvmir.FunctionType(
llvmir.IntType(32), [voidptrty], var_arg=True
)
printf = cgutils.get_or_insert_function(lmod, printfty, "printf")
return printf

Expand Down
Loading