Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit ca17991

Browse files
committed
Patch for codegen debugging
Changes by @DrTodd13: commit 2893849 - test_link() commit 9ba9540 - use config.DEBUG_ARRAY_OPT This changes could be moved to upstream.
1 parent de35af5 commit ca17991

File tree

1 file changed

+2
-52
lines changed

1 file changed

+2
-52
lines changed

numba/core/codegen.py

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,6 @@ def display(self, filename=None, view=False):
7777
def __repr__(self):
7878
return self.dot
7979

80-
def test_link():
81-
ll.initialize()
82-
ll.initialize_all_targets()
83-
ll.initialize_native_asmprinter()
84-
85-
target = ll.Target.from_triple(ll.get_process_triple())
86-
tm = target.create_target_machine()
87-
88-
89-
llvm_module = ll.parse_assembly("""
90-
declare i32 @PyArg_UnpackTuple(i8*, i8*, i64, i64, ...)
91-
declare double @sin(double)
92-
define i64 @foo() {
93-
ret i64 ptrtoint (i32 (i8*, i8*, i64, i64, ...)* @PyArg_UnpackTuple to i64)
94-
}
95-
""")
96-
97-
engine = ll.create_mcjit_compiler(llvm_module, tm)
98-
addr = engine.get_function_address('PyArg_UnpackTuple')
99-
print('PyArg_UnpackTuple', addr) # printing 0x0
100-
101-
102-
addr = engine.get_function_address('foo')
103-
foo = ctypes.CFUNCTYPE(ctypes.c_int64)(addr)
104-
print('foo', addr)
105-
print("PyArg_UnpackTuple", foo()) # print non zero
10680

10781
class CodeLibrary(object):
10882
"""
@@ -235,23 +209,17 @@ def add_ir_module(self, ir_module):
235209
self.add_llvm_module(ll_module)
236210

237211
def add_llvm_module(self, ll_module):
238-
if config.DEBUG_ARRAY_OPT >= 1:
239-
print("CodeLibrary::add_llvm_module", self._name)
240212
self._optimize_functions(ll_module)
241213
# TODO: we shouldn't need to recreate the LLVM module object
242214
ll_module = remove_redundant_nrt_refct(ll_module)
243215
self._final_module.link_in(ll_module)
244-
if config.DEBUG_ARRAY_OPT >= 1:
245-
print("CodeLibrary::add_llvm_module end", self._name)
246216

247217
def finalize(self):
248218
"""
249219
Finalize the library. After this call, nothing can be added anymore.
250220
Finalization involves various stages of code optimization and
251221
linking.
252222
"""
253-
if config.DEBUG_ARRAY_OPT >= 1:
254-
print("CodeLibrary::finalize", self._name)
255223
#require_global_compiler_lock()
256224

257225
# Report any LLVM-related problems to the user
@@ -263,8 +231,6 @@ def finalize(self):
263231
dump("FUNCTION OPTIMIZED DUMP %s" % self._name,
264232
self.get_llvm_str(), 'llvm')
265233

266-
if config.DEBUG_ARRAY_OPT >= 1:
267-
print("Before link_in")
268234
# Link libraries for shared code
269235
seen = set()
270236
for library in self._linking_libraries:
@@ -280,10 +246,8 @@ def finalize(self):
280246

281247
self._final_module.verify()
282248
self._finalize_final_module()
283-
if config.DEBUG_ARRAY_OPT >= 1:
284-
print("CodeLibrary::finalize end", self._name)
285249

286-
def _finalize_dynamic_globals(self):
250+
def _finalize_dyanmic_globals(self):
287251
# Scan for dynamic globals
288252
for gv in self._final_module.global_variables:
289253
if gv.name.startswith('numba.dynamic.globals'):
@@ -301,7 +265,7 @@ def _finalize_final_module(self):
301265
"""
302266
Make the underlying LLVM module ready to use.
303267
"""
304-
self._finalize_dynamic_globals()
268+
self._finalize_dyanmic_globals()
305269
self._verify_declare_only_symbols()
306270

307271
# Remember this on the module, for the object cache hooks
@@ -321,7 +285,6 @@ def _finalize_final_module(self):
321285
dump("OPTIMIZED DUMP %s" % self._name, self.get_llvm_str(), 'llvm')
322286

323287
if config.DUMP_ASSEMBLY:
324-
test_link()
325288
# CUDA backend cannot return assembly this early, so don't
326289
# attempt to dump assembly if nothing is produced.
327290
asm = self.get_asm_str()
@@ -571,8 +534,6 @@ def scan_unresolved_symbols(self, module, engine):
571534
prefix = self.PREFIX
572535

573536
for gv in module.global_variables:
574-
if config.DEBUG_ARRAY_OPT >= 1:
575-
print("scan_unresolved_symbols", gv)
576537
if gv.name.startswith(prefix):
577538
sym = gv.name[len(prefix):]
578539
# Avoid remapping to existing GV
@@ -589,23 +550,17 @@ def scan_defined_symbols(self, module):
589550
Scan and track all defined symbols.
590551
"""
591552
for fn in module.functions:
592-
if config.DEBUG_ARRAY_OPT >= 1:
593-
print("scan_defined_symbols", fn)
594553
if not fn.is_declaration:
595554
self._defined.add(fn.name)
596555

597556
def resolve(self, engine):
598557
"""
599558
Fix unresolved symbols if they are defined.
600559
"""
601-
if config.DEBUG_ARRAY_OPT >= 1:
602-
print("RuntimeLinker resolve", self._unresolved, self._defined)
603560
# An iterator to get all unresolved but available symbols
604561
pending = [name for name in self._unresolved if name in self._defined]
605562
# Resolve pending symbols
606563
for name in pending:
607-
if config.DEBUG_ARRAY_OPT >= 1:
608-
print("name", name)
609564
# Get runtime address
610565
fnptr = engine.get_function_address(name)
611566
# Fix all usage
@@ -650,17 +605,13 @@ def _load_defined_symbols(self, mod):
650605
"""Extract symbols from the module
651606
"""
652607
for gsets in (mod.functions, mod.global_variables):
653-
if config.DEBUG_ARRAY_OPT >= 1:
654-
print("_load_defined_symbols", gsets, self._defined_symbols)
655608
self._defined_symbols |= {gv.name for gv in gsets
656609
if not gv.is_declaration}
657610

658611
def add_module(self, module):
659612
"""Override ExecutionEngine.add_module
660613
to keep info about defined symbols.
661614
"""
662-
if config.DEBUG_ARRAY_OPT >= 1:
663-
print("add_module", module)
664615
self._load_defined_symbols(module)
665616
return self._ee.add_module(module)
666617

@@ -923,7 +874,6 @@ def initialize_llvm():
923874
ll.initialize()
924875
ll.initialize_native_target()
925876
ll.initialize_native_asmprinter()
926-
ll.initialize_all_targets()
927877

928878

929879
def get_host_cpu_features():

0 commit comments

Comments
 (0)