Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 9 additions & 9 deletions numba/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,15 @@ def get_external_function_type(self, fndesc):

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, fndesc.mangled_name)
self.call_conv.decorate_function(fn, fndesc.args, fndesc.argtypes, noalias=fndesc.noalias)
if fndesc.inline:
fn.attributes.add('alwaysinline')
return fn

def declare_external_function(self, module, fndesc):
fnty = self.get_external_function_type(fndesc)
fn = module.get_or_insert_function(fnty, name=fndesc.mangled_name)
fn = cgutils.get_or_insert_function(module, fnty, fndesc.mangled_name)
assert fn.is_declaration
for ak, av in zip(fndesc.args, fn.args):
av.name = "arg.%s" % ak
Expand Down Expand Up @@ -764,9 +764,9 @@ def get_c_value(self, builder, typ, name, dllimport=False):
"""
module = builder.function.module
try:
gv = module.get_global_variable_named(name)
except LLVMException:
gv = module.add_global_variable(typ, name)
gv = module.globals[name]
except KeyError:
gv = cgutils.add_global_variable(module, typ, name)
if dllimport and self.aot_mode and sys.platform == 'win32':
gv.storage_class = "dllimport"
return gv
Expand All @@ -787,7 +787,7 @@ def print_string(self, builder, text):
mod = builder.module
cstring = GENERIC_POINTER
fnty = Type.function(Type.int(), [cstring])
puts = mod.get_or_insert_function(fnty, "puts")
puts = cgutils.get_or_insert_function(mod, fnty, "puts")
return builder.call(puts, [text])

def debug_print(self, builder, text):
Expand All @@ -802,7 +802,7 @@ def printf(self, builder, format_string, *args):
else:
cstr = format_string
fnty = Type.function(Type.int(), (GENERIC_POINTER,), var_arg=True)
fn = mod.get_or_insert_function(fnty, "printf")
fn = cgutils.get_or_insert_function(mod, fnty, "printf")
return builder.call(fn, (cstr,) + tuple(args))

def get_struct_type(self, struct):
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def add_dynamic_addr(self, builder, intaddr, info):
addr = self.get_constant(types.uintp, intaddr).inttoptr(llvoidptr)
# Use a unique name by embedding the address value
symname = 'numba.dynamic.globals.{:x}'.format(intaddr)
gv = mod.add_global_variable(llvoidptr, name=symname)
gv = cgutils.add_global_variable(mod, llvoidptr, symname)
# Use linkonce linkage to allow merging with other GV of the same name.
# And, avoid optimization from assuming its value.
gv.linkage = 'linkonce'
Expand Down Expand Up @@ -1122,7 +1122,7 @@ def post_lowering(self, mod, library):
def create_module(self, name):
"""Create a LLVM module
"""
return lc.Module(name)
return ir.Module(name)

@property
def active_code_library(self):
Expand Down
3 changes: 2 additions & 1 deletion numba/core/callwrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from llvmlite.llvmpy.core import Type, Builder, Constant
import llvmlite.llvmpy.core as lc
from llvmlite import ir

from numba.core import types, config, cgutils

Expand Down Expand Up @@ -105,7 +106,7 @@ def build(self):
# (see CPython's methodobject.h)
pyobj = self.context.get_argument_type(types.pyobject)
wrapty = Type.function(pyobj, [pyobj, pyobj, pyobj])
wrapper = self.module.add_function(wrapty, name=wrapname)
wrapper = ir.Function(self.module, wrapty, name=wrapname)

builder = Builder(wrapper.append_basic_block('entry'))

Expand Down
27 changes: 25 additions & 2 deletions numba/core/cgutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,35 @@ def insert_pure_function(module, fnty, name):
Insert a pure function (in the functional programming sense) in the
given module.
"""
fn = module.get_or_insert_function(fnty, name=name)
fn = get_or_insert_function(module, fnty, name)
fn.attributes.add("readonly")
fn.attributes.add("nounwind")
return fn


def get_or_insert_function(module, fnty, name):
"""
Get the function named *name* with type *fnty* from *module*, or insert it
if it doesn't exist.
"""
fn = module.globals.get(name, None)
if fn is None:
fn = ir.Function(module, fnty, name)
return fn


def get_or_insert_named_metadata(module, name):
try:
return module.get_named_metadata(name)
except KeyError:
return module.add_named_metadata(name)


def add_global_variable(module, ty, name, addrspace=0):
unique_name = module.get_unique_name(name)
return ir.GlobalVariable(module, ty, unique_name, addrspace)


def terminate(builder, bbend):
bb = builder.basic_block
if bb.terminator is None:
Expand Down Expand Up @@ -935,7 +958,7 @@ def global_constant(builder_or_module, name, value, linkage='internal'):
module = builder_or_module
else:
module = builder_or_module.module
data = module.add_global_variable(value.type, name=name)
data = add_global_variable(module, value.type, name)
data.linkage = linkage
data.global_constant = True
data.initializer = value
Expand Down
2 changes: 1 addition & 1 deletion numba/core/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ def _init(self, llvm_module):
self._library_class._object_getbuffer_hook)

def _create_empty_module(self, name):
ir_module = lc.Module(cgutils.normalize_ir_text(name))
ir_module = llvmir.Module(cgutils.normalize_ir_text(name))
ir_module.triple = ll.get_process_triple()
if self._data_layout:
ir_module.data_layout = self._data_layout
Expand Down
6 changes: 3 additions & 3 deletions numba/core/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def create_cpython_wrapper(self, library, fndesc, env, call_helper,
release_gil=False):
wrapper_module = self.create_module("wrapper")
fnty = self.call_conv.get_function_type(fndesc.restype, fndesc.argtypes)
wrapper_callee = wrapper_module.add_function(fnty, fndesc.llvm_func_name)
wrapper_callee = ir.Function(wrapper_module, fnty, fndesc.llvm_func_name)
builder = PyCallWrapper(self, wrapper_module, wrapper_callee,
fndesc, env, call_helper=call_helper,
release_gil=release_gil)
Expand All @@ -167,13 +167,13 @@ def create_cfunc_wrapper(self, library, fndesc, env, call_helper):

wrapper_module = self.create_module("cfunc_wrapper")
fnty = self.call_conv.get_function_type(fndesc.restype, fndesc.argtypes)
wrapper_callee = wrapper_module.add_function(fnty, fndesc.llvm_func_name)
wrapper_callee = ir.Function(wrapper_module, fnty, fndesc.llvm_func_name)

ll_argtypes = [self.get_value_type(ty) for ty in fndesc.argtypes]
ll_return_type = self.get_value_type(fndesc.restype)

wrapty = ir.FunctionType(ll_return_type, ll_argtypes)
wrapfn = wrapper_module.add_function(wrapty, fndesc.llvm_cfunc_wrapper_name)
wrapfn = ir.Function(wrapper_module, wrapty, fndesc.llvm_cfunc_wrapper_name)
builder = ir.IRBuilder(wrapfn.append_basic_block('entry'))

status, out = self.call_conv.call_function(
Expand Down
7 changes: 4 additions & 3 deletions numba/core/debuginfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os.path

from llvmlite import ir
from numba.core import cgutils


class AbstractDIBuilder(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -119,7 +120,7 @@ def _var_type(self, lltype, size):
def mark_variable(self, builder, allocavalue, name, lltype, size, loc):
m = self.module
fnty = ir.FunctionType(ir.VoidType(), [ir.MetaDataType()] * 3)
decl = m.get_or_insert_function(fnty, name='llvm.dbg.declare')
decl = cgutils.get_or_insert_function(m, fnty, 'llvm.dbg.declare')

mdtype = self._var_type(lltype, size)
name = name.replace('.', '$') # for gdb to work correctly
Expand All @@ -146,7 +147,7 @@ def mark_subprogram(self, function, name, loc):
function.attributes.add('noinline')

def finalize(self):
dbgcu = self.module.get_or_insert_named_metadata(self.DBG_CU_NAME)
dbgcu = cgutils.get_or_insert_named_metadata(self.module, self.DBG_CU_NAME)
dbgcu.add(self.dicompileunit)
self._set_module_flags()

Expand All @@ -158,7 +159,7 @@ def _set_module_flags(self):
"""Set the module flags metadata
"""
module = self.module
mflags = module.get_or_insert_named_metadata('llvm.module.flags')
mflags = cgutils.get_or_insert_named_metadata(module, 'llvm.module.flags')
# Set *require* behavior to warning
# See http://llvm.org/docs/LangRef.html#module-flags-metadata
require_warning_behavior = self._const_int(2)
Expand Down
4 changes: 2 additions & 2 deletions numba/core/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ def lower_finalize_func(self, lower):
"""
fnty = Type.function(Type.void(),
[self.context.get_value_type(self.gentype)])
function = lower.module.get_or_insert_function(
fnty, name=self.gendesc.llvm_finalizer_name)
function = cgutils.get_or_insert_function(
lower.module, fnty, self.gendesc.llvm_finalizer_name)
entry_block = function.append_basic_block('entry')
builder = Builder(entry_block)

Expand Down
17 changes: 10 additions & 7 deletions numba/core/pythonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,9 +1217,10 @@ def nrt_meminfo_new_from_pyobject(self, data, pyobj):
cgutils.voidptr_t,
[cgutils.voidptr_t, cgutils.voidptr_t],
)
fn = mod.get_or_insert_function(
fn = cgutils.get_or_insert_function(
mod,
fnty,
name="NRT_meminfo_new_from_pyobject",
"NRT_meminfo_new_from_pyobject",
)
fn.args[0].add_attribute(lc.ATTR_NO_CAPTURE)
fn.args[1].add_attribute(lc.ATTR_NO_CAPTURE)
Expand All @@ -1232,9 +1233,10 @@ def nrt_meminfo_as_pyobject(self, miptr):
self.pyobj,
[cgutils.voidptr_t]
)
fn = mod.get_or_insert_function(
fn = cgutils.get_or_insert_function(
mod,
fnty,
name='NRT_meminfo_as_pyobject',
'NRT_meminfo_as_pyobject',
)
fn.return_value.add_attribute("noalias")
return self.builder.call(fn, [miptr])
Expand All @@ -1245,9 +1247,10 @@ def nrt_meminfo_from_pyobject(self, miobj):
cgutils.voidptr_t,
[self.pyobj]
)
fn = mod.get_or_insert_function(
fn = cgutils.get_or_insert_function(
mod,
fnty,
name='NRT_meminfo_from_pyobject',
'NRT_meminfo_from_pyobject',
)
fn.return_value.add_attribute("noalias")
return self.builder.call(fn, [miobj])
Expand All @@ -1272,7 +1275,7 @@ def nrt_adapt_buffer_from_python(self, buf, ptr):
# ------ utils -----

def _get_function(self, fnty, name):
return self.module.get_or_insert_function(fnty, name=name)
return cgutils.get_or_insert_function(self.module, fnty, name)

def alloca_obj(self):
return self.builder.alloca(self.pyobj)
Expand Down
35 changes: 19 additions & 16 deletions numba/core/runtime/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def allocate(self, builder, size):

mod = builder.module
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t])
fn = mod.get_or_insert_function(fnty, name="NRT_Allocate")
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_Allocate")
fn.return_value.add_attribute("noalias")
return builder.call(fn, [size])

Expand All @@ -36,7 +36,7 @@ def free(self, builder, ptr):

mod = builder.module
fnty = ir.FunctionType(ir.VoidType(), [cgutils.voidptr_t])
fn = mod.get_or_insert_function(fnty, name="NRT_Free")
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_Free")
return builder.call(fn, [ptr])

def meminfo_alloc(self, builder, size):
Expand All @@ -49,7 +49,7 @@ def meminfo_alloc(self, builder, size):

mod = builder.module
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t])
fn = mod.get_or_insert_function(fnty, name="NRT_MemInfo_alloc_safe")
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_MemInfo_alloc_safe")
fn.return_value.add_attribute("noalias")
return builder.call(fn, [size])

Expand All @@ -59,8 +59,8 @@ def meminfo_alloc_dtor(self, builder, size, dtor):
mod = builder.module
fnty = ir.FunctionType(cgutils.voidptr_t,
[cgutils.intp_t, cgutils.voidptr_t])
fn = mod.get_or_insert_function(fnty,
name="NRT_MemInfo_alloc_dtor_safe")
fn = cgutils.get_or_insert_function(mod, fnty,
"NRT_MemInfo_alloc_dtor_safe")
fn.return_value.add_attribute("noalias")
return builder.call(fn, [size,
builder.bitcast(dtor, cgutils.voidptr_t)])
Expand All @@ -78,8 +78,8 @@ def meminfo_alloc_aligned(self, builder, size, align):
mod = builder.module
u32 = ir.IntType(32)
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t, u32])
fn = mod.get_or_insert_function(fnty,
name="NRT_MemInfo_alloc_safe_aligned")
fn = cgutils.get_or_insert_function(mod, fnty,
"NRT_MemInfo_alloc_safe_aligned")
fn.return_value.add_attribute("noalias")
if isinstance(align, int):
align = self._context.get_constant(types.uint32, align)
Expand All @@ -99,7 +99,8 @@ def meminfo_new_varsize(self, builder, size):

mod = builder.module
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t])
fn = mod.get_or_insert_function(fnty, name="NRT_MemInfo_new_varsize")
fn = cgutils.get_or_insert_function(mod, fnty,
"NRT_MemInfo_new_varsize")
fn.return_value.add_attribute("noalias")
return builder.call(fn, [size])

Expand All @@ -113,8 +114,8 @@ def meminfo_new_varsize_dtor(self, builder, size, dtor):
mod = builder.module
fnty = ir.FunctionType(cgutils.voidptr_t,
[cgutils.intp_t, cgutils.voidptr_t])
fn = mod.get_or_insert_function(
fnty, name="NRT_MemInfo_new_varsize_dtor")
fn = cgutils.get_or_insert_function(
mod, fnty, "NRT_MemInfo_new_varsize_dtor")
return builder.call(fn, [size, dtor])

def meminfo_varsize_alloc(self, builder, meminfo, size):
Expand Down Expand Up @@ -149,7 +150,8 @@ def meminfo_varsize_free(self, builder, meminfo, ptr):
mod = builder.module
fnty = ir.FunctionType(ir.VoidType(),
[cgutils.voidptr_t, cgutils.voidptr_t])
fn = mod.get_or_insert_function(fnty, name="NRT_MemInfo_varsize_free")
fn = cgutils.get_or_insert_function(mod, fnty,
"NRT_MemInfo_varsize_free")
return builder.call(fn, (meminfo, ptr))

def _call_varsize_alloc(self, builder, meminfo, size, funcname):
Expand All @@ -158,7 +160,7 @@ def _call_varsize_alloc(self, builder, meminfo, size, funcname):
mod = builder.module
fnty = ir.FunctionType(cgutils.voidptr_t,
[cgutils.voidptr_t, cgutils.intp_t])
fn = mod.get_or_insert_function(fnty, name=funcname)
fn = cgutils.get_or_insert_function(mod, fnty, funcname)
fn.return_value.add_attribute("noalias")
return builder.call(fn, [meminfo, size])

Expand All @@ -173,8 +175,8 @@ def meminfo_data(self, builder, meminfo):
from numba.core.runtime.nrtdynmod import meminfo_data_ty

mod = builder.module
fn = mod.get_or_insert_function(meminfo_data_ty,
name="NRT_MemInfo_data_fast")
fn = cgutils.get_or_insert_function(mod, meminfo_data_ty,
"NRT_MemInfo_data_fast")
return builder.call(fn, [meminfo])

def get_meminfos(self, builder, ty, val):
Expand Down Expand Up @@ -204,7 +206,8 @@ def _call_incref_decref(self, builder, typ, value, funcname):
meminfos = self.get_meminfos(builder, typ, value)
for _, mi in meminfos:
mod = builder.module
fn = mod.get_or_insert_function(incref_decref_ty, name=funcname)
fn = cgutils.get_or_insert_function(mod, incref_decref_ty,
funcname)
# XXX "nonnull" causes a crash in test_dyn_array: can this
# function be called with a NULL pointer?
fn.args[0].add_attribute("noalias")
Expand All @@ -230,7 +233,7 @@ def get_nrt_api(self, builder):

fnty = ir.FunctionType(cgutils.voidptr_t, ())
mod = builder.module
fn = mod.get_or_insert_function(fnty, name="NRT_get_api")
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_get_api")
return builder.call(fn, ())

def eh_check(self, builder):
Expand Down
Loading