Skip to content

Commit 83b246f

Browse files
authored
Merge pull request #6813 from gmarkall/grm-cuda-remove-llvmpy
Mostly CUDA: Replace llvmpy API usage with llvmlite APIs
2 parents c8acdf8 + 25255f0 commit 83b246f

45 files changed

Lines changed: 467 additions & 423 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎numba/core/base.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,15 @@ def get_external_function_type(self, fndesc):
428428

429429
def declare_function(self, module, fndesc):
430430
fnty = self.call_conv.get_function_type(fndesc.restype, fndesc.argtypes)
431-
fn = module.get_or_insert_function(fnty, name=fndesc.mangled_name)
431+
fn = cgutils.get_or_insert_function(module, fnty, fndesc.mangled_name)
432432
self.call_conv.decorate_function(fn, fndesc.args, fndesc.argtypes, noalias=fndesc.noalias)
433433
if fndesc.inline:
434434
fn.attributes.add('alwaysinline')
435435
return fn
436436

437437
def declare_external_function(self, module, fndesc):
438438
fnty = self.get_external_function_type(fndesc)
439-
fn = module.get_or_insert_function(fnty, name=fndesc.mangled_name)
439+
fn = cgutils.get_or_insert_function(module, fnty, fndesc.mangled_name)
440440
assert fn.is_declaration
441441
for ak, av in zip(fndesc.args, fn.args):
442442
av.name = "arg.%s" % ak
@@ -764,9 +764,9 @@ def get_c_value(self, builder, typ, name, dllimport=False):
764764
"""
765765
module = builder.function.module
766766
try:
767-
gv = module.get_global_variable_named(name)
768-
except LLVMException:
769-
gv = module.add_global_variable(typ, name)
767+
gv = module.globals[name]
768+
except KeyError:
769+
gv = cgutils.add_global_variable(module, typ, name)
770770
if dllimport and self.aot_mode and sys.platform == 'win32':
771771
gv.storage_class = "dllimport"
772772
return gv
@@ -787,7 +787,7 @@ def print_string(self, builder, text):
787787
mod = builder.module
788788
cstring = GENERIC_POINTER
789789
fnty = Type.function(Type.int(), [cstring])
790-
puts = mod.get_or_insert_function(fnty, "puts")
790+
puts = cgutils.get_or_insert_function(mod, fnty, "puts")
791791
return builder.call(puts, [text])
792792

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

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

11271127
@property
11281128
def active_code_library(self):

‎numba/core/callwrapper.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from llvmlite.llvmpy.core import Type, Builder, Constant
22
import llvmlite.llvmpy.core as lc
3+
from llvmlite import ir
34

45
from numba.core import types, config, cgutils
56

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

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

‎numba/core/cgutils.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,35 @@ def insert_pure_function(module, fnty, name):
404404
Insert a pure function (in the functional programming sense) in the
405405
given module.
406406
"""
407-
fn = module.get_or_insert_function(fnty, name=name)
407+
fn = get_or_insert_function(module, fnty, name)
408408
fn.attributes.add("readonly")
409409
fn.attributes.add("nounwind")
410410
return fn
411411

412412

413+
def get_or_insert_function(module, fnty, name):
414+
"""
415+
Get the function named *name* with type *fnty* from *module*, or insert it
416+
if it doesn't exist.
417+
"""
418+
fn = module.globals.get(name, None)
419+
if fn is None:
420+
fn = ir.Function(module, fnty, name)
421+
return fn
422+
423+
424+
def get_or_insert_named_metadata(module, name):
425+
try:
426+
return module.get_named_metadata(name)
427+
except KeyError:
428+
return module.add_named_metadata(name)
429+
430+
431+
def add_global_variable(module, ty, name, addrspace=0):
432+
unique_name = module.get_unique_name(name)
433+
return ir.GlobalVariable(module, ty, unique_name, addrspace)
434+
435+
413436
def terminate(builder, bbend):
414437
bb = builder.basic_block
415438
if bb.terminator is None:
@@ -935,7 +958,7 @@ def global_constant(builder_or_module, name, value, linkage='internal'):
935958
module = builder_or_module
936959
else:
937960
module = builder_or_module.module
938-
data = module.add_global_variable(value.type, name=name)
961+
data = add_global_variable(module, value.type, name)
939962
data.linkage = linkage
940963
data.global_constant = True
941964
data.initializer = value

‎numba/core/codegen.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ def _init(self, llvm_module):
11931193
self._library_class._object_getbuffer_hook)
11941194

11951195
def _create_empty_module(self, name):
1196-
ir_module = lc.Module(cgutils.normalize_ir_text(name))
1196+
ir_module = llvmir.Module(cgutils.normalize_ir_text(name))
11971197
ir_module.triple = ll.get_process_triple()
11981198
if self._data_layout:
11991199
ir_module.data_layout = self._data_layout

‎numba/core/cpu.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def create_cpython_wrapper(self, library, fndesc, env, call_helper,
156156
release_gil=False):
157157
wrapper_module = self.create_module("wrapper")
158158
fnty = self.call_conv.get_function_type(fndesc.restype, fndesc.argtypes)
159-
wrapper_callee = wrapper_module.add_function(fnty, fndesc.llvm_func_name)
159+
wrapper_callee = ir.Function(wrapper_module, fnty, fndesc.llvm_func_name)
160160
builder = PyCallWrapper(self, wrapper_module, wrapper_callee,
161161
fndesc, env, call_helper=call_helper,
162162
release_gil=release_gil)
@@ -167,13 +167,13 @@ def create_cfunc_wrapper(self, library, fndesc, env, call_helper):
167167

168168
wrapper_module = self.create_module("cfunc_wrapper")
169169
fnty = self.call_conv.get_function_type(fndesc.restype, fndesc.argtypes)
170-
wrapper_callee = wrapper_module.add_function(fnty, fndesc.llvm_func_name)
170+
wrapper_callee = ir.Function(wrapper_module, fnty, fndesc.llvm_func_name)
171171

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

175175
wrapty = ir.FunctionType(ll_return_type, ll_argtypes)
176-
wrapfn = wrapper_module.add_function(wrapty, fndesc.llvm_cfunc_wrapper_name)
176+
wrapfn = ir.Function(wrapper_module, wrapty, fndesc.llvm_cfunc_wrapper_name)
177177
builder = ir.IRBuilder(wrapfn.append_basic_block('entry'))
178178

179179
status, out = self.call_conv.call_function(

‎numba/core/debuginfo.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os.path
88

99
from llvmlite import ir
10+
from numba.core import cgutils
1011

1112

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

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

148149
def finalize(self):
149-
dbgcu = self.module.get_or_insert_named_metadata(self.DBG_CU_NAME)
150+
dbgcu = cgutils.get_or_insert_named_metadata(self.module, self.DBG_CU_NAME)
150151
dbgcu.add(self.dicompileunit)
151152
self._set_module_flags()
152153

@@ -158,7 +159,7 @@ def _set_module_flags(self):
158159
"""Set the module flags metadata
159160
"""
160161
module = self.module
161-
mflags = module.get_or_insert_named_metadata('llvm.module.flags')
162+
mflags = cgutils.get_or_insert_named_metadata(module, 'llvm.module.flags')
162163
# Set *require* behavior to warning
163164
# See http://llvm.org/docs/LangRef.html#module-flags-metadata
164165
require_warning_behavior = self._const_int(2)

‎numba/core/generators.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def lower_finalize_func(self, lower):
177177
"""
178178
fnty = Type.function(Type.void(),
179179
[self.context.get_value_type(self.gentype)])
180-
function = lower.module.get_or_insert_function(
181-
fnty, name=self.gendesc.llvm_finalizer_name)
180+
function = cgutils.get_or_insert_function(
181+
lower.module, fnty, self.gendesc.llvm_finalizer_name)
182182
entry_block = function.append_basic_block('entry')
183183
builder = Builder(entry_block)
184184

‎numba/core/pythonapi.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,10 @@ def nrt_meminfo_new_from_pyobject(self, data, pyobj):
12171217
cgutils.voidptr_t,
12181218
[cgutils.voidptr_t, cgutils.voidptr_t],
12191219
)
1220-
fn = mod.get_or_insert_function(
1220+
fn = cgutils.get_or_insert_function(
1221+
mod,
12211222
fnty,
1222-
name="NRT_meminfo_new_from_pyobject",
1223+
"NRT_meminfo_new_from_pyobject",
12231224
)
12241225
fn.args[0].add_attribute(lc.ATTR_NO_CAPTURE)
12251226
fn.args[1].add_attribute(lc.ATTR_NO_CAPTURE)
@@ -1232,9 +1233,10 @@ def nrt_meminfo_as_pyobject(self, miptr):
12321233
self.pyobj,
12331234
[cgutils.voidptr_t]
12341235
)
1235-
fn = mod.get_or_insert_function(
1236+
fn = cgutils.get_or_insert_function(
1237+
mod,
12361238
fnty,
1237-
name='NRT_meminfo_as_pyobject',
1239+
'NRT_meminfo_as_pyobject',
12381240
)
12391241
fn.return_value.add_attribute("noalias")
12401242
return self.builder.call(fn, [miptr])
@@ -1245,9 +1247,10 @@ def nrt_meminfo_from_pyobject(self, miobj):
12451247
cgutils.voidptr_t,
12461248
[self.pyobj]
12471249
)
1248-
fn = mod.get_or_insert_function(
1250+
fn = cgutils.get_or_insert_function(
1251+
mod,
12491252
fnty,
1250-
name='NRT_meminfo_from_pyobject',
1253+
'NRT_meminfo_from_pyobject',
12511254
)
12521255
fn.return_value.add_attribute("noalias")
12531256
return self.builder.call(fn, [miobj])
@@ -1272,7 +1275,7 @@ def nrt_adapt_buffer_from_python(self, buf, ptr):
12721275
# ------ utils -----
12731276

12741277
def _get_function(self, fnty, name):
1275-
return self.module.get_or_insert_function(fnty, name=name)
1278+
return cgutils.get_or_insert_function(self.module, fnty, name)
12761279

12771280
def alloca_obj(self):
12781281
return self.builder.alloca(self.pyobj)

‎numba/core/runtime/context.py‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def allocate(self, builder, size):
2424

2525
mod = builder.module
2626
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t])
27-
fn = mod.get_or_insert_function(fnty, name="NRT_Allocate")
27+
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_Allocate")
2828
fn.return_value.add_attribute("noalias")
2929
return builder.call(fn, [size])
3030

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

3737
mod = builder.module
3838
fnty = ir.FunctionType(ir.VoidType(), [cgutils.voidptr_t])
39-
fn = mod.get_or_insert_function(fnty, name="NRT_Free")
39+
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_Free")
4040
return builder.call(fn, [ptr])
4141

4242
def meminfo_alloc(self, builder, size):
@@ -49,7 +49,7 @@ def meminfo_alloc(self, builder, size):
4949

5050
mod = builder.module
5151
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t])
52-
fn = mod.get_or_insert_function(fnty, name="NRT_MemInfo_alloc_safe")
52+
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_MemInfo_alloc_safe")
5353
fn.return_value.add_attribute("noalias")
5454
return builder.call(fn, [size])
5555

@@ -59,8 +59,8 @@ def meminfo_alloc_dtor(self, builder, size, dtor):
5959
mod = builder.module
6060
fnty = ir.FunctionType(cgutils.voidptr_t,
6161
[cgutils.intp_t, cgutils.voidptr_t])
62-
fn = mod.get_or_insert_function(fnty,
63-
name="NRT_MemInfo_alloc_dtor_safe")
62+
fn = cgutils.get_or_insert_function(mod, fnty,
63+
"NRT_MemInfo_alloc_dtor_safe")
6464
fn.return_value.add_attribute("noalias")
6565
return builder.call(fn, [size,
6666
builder.bitcast(dtor, cgutils.voidptr_t)])
@@ -78,8 +78,8 @@ def meminfo_alloc_aligned(self, builder, size, align):
7878
mod = builder.module
7979
u32 = ir.IntType(32)
8080
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t, u32])
81-
fn = mod.get_or_insert_function(fnty,
82-
name="NRT_MemInfo_alloc_safe_aligned")
81+
fn = cgutils.get_or_insert_function(mod, fnty,
82+
"NRT_MemInfo_alloc_safe_aligned")
8383
fn.return_value.add_attribute("noalias")
8484
if isinstance(align, int):
8585
align = self._context.get_constant(types.uint32, align)
@@ -99,7 +99,8 @@ def meminfo_new_varsize(self, builder, size):
9999

100100
mod = builder.module
101101
fnty = ir.FunctionType(cgutils.voidptr_t, [cgutils.intp_t])
102-
fn = mod.get_or_insert_function(fnty, name="NRT_MemInfo_new_varsize")
102+
fn = cgutils.get_or_insert_function(mod, fnty,
103+
"NRT_MemInfo_new_varsize")
103104
fn.return_value.add_attribute("noalias")
104105
return builder.call(fn, [size])
105106

@@ -113,8 +114,8 @@ def meminfo_new_varsize_dtor(self, builder, size, dtor):
113114
mod = builder.module
114115
fnty = ir.FunctionType(cgutils.voidptr_t,
115116
[cgutils.intp_t, cgutils.voidptr_t])
116-
fn = mod.get_or_insert_function(
117-
fnty, name="NRT_MemInfo_new_varsize_dtor")
117+
fn = cgutils.get_or_insert_function(
118+
mod, fnty, "NRT_MemInfo_new_varsize_dtor")
118119
return builder.call(fn, [size, dtor])
119120

120121
def meminfo_varsize_alloc(self, builder, meminfo, size):
@@ -149,7 +150,8 @@ def meminfo_varsize_free(self, builder, meminfo, ptr):
149150
mod = builder.module
150151
fnty = ir.FunctionType(ir.VoidType(),
151152
[cgutils.voidptr_t, cgutils.voidptr_t])
152-
fn = mod.get_or_insert_function(fnty, name="NRT_MemInfo_varsize_free")
153+
fn = cgutils.get_or_insert_function(mod, fnty,
154+
"NRT_MemInfo_varsize_free")
153155
return builder.call(fn, (meminfo, ptr))
154156

155157
def _call_varsize_alloc(self, builder, meminfo, size, funcname):
@@ -158,7 +160,7 @@ def _call_varsize_alloc(self, builder, meminfo, size, funcname):
158160
mod = builder.module
159161
fnty = ir.FunctionType(cgutils.voidptr_t,
160162
[cgutils.voidptr_t, cgutils.intp_t])
161-
fn = mod.get_or_insert_function(fnty, name=funcname)
163+
fn = cgutils.get_or_insert_function(mod, fnty, funcname)
162164
fn.return_value.add_attribute("noalias")
163165
return builder.call(fn, [meminfo, size])
164166

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

175177
mod = builder.module
176-
fn = mod.get_or_insert_function(meminfo_data_ty,
177-
name="NRT_MemInfo_data_fast")
178+
fn = cgutils.get_or_insert_function(mod, meminfo_data_ty,
179+
"NRT_MemInfo_data_fast")
178180
return builder.call(fn, [meminfo])
179181

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

231234
fnty = ir.FunctionType(cgutils.voidptr_t, ())
232235
mod = builder.module
233-
fn = mod.get_or_insert_function(fnty, name="NRT_get_api")
236+
fn = cgutils.get_or_insert_function(mod, fnty, "NRT_get_api")
234237
return builder.call(fn, ())
235238

236239
def eh_check(self, builder):

0 commit comments

Comments
 (0)