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

Patch for codegen debugging #104

Merged
merged 2 commits into from
Nov 13, 2020
Merged
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
50 changes: 0 additions & 50 deletions numba/core/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,6 @@ def display(self, filename=None, view=False):
def __repr__(self):
return self.dot

def test_link():
ll.initialize()
ll.initialize_all_targets()
ll.initialize_native_asmprinter()

target = ll.Target.from_triple(ll.get_process_triple())
tm = target.create_target_machine()


llvm_module = ll.parse_assembly("""
declare i32 @PyArg_UnpackTuple(i8*, i8*, i64, i64, ...)
declare double @sin(double)
define i64 @foo() {
ret i64 ptrtoint (i32 (i8*, i8*, i64, i64, ...)* @PyArg_UnpackTuple to i64)
}
""")

engine = ll.create_mcjit_compiler(llvm_module, tm)
addr = engine.get_function_address('PyArg_UnpackTuple')
print('PyArg_UnpackTuple', addr) # printing 0x0


addr = engine.get_function_address('foo')
foo = ctypes.CFUNCTYPE(ctypes.c_int64)(addr)
print('foo', addr)
print("PyArg_UnpackTuple", foo()) # print non zero

class CodeLibrary(object):
"""
Expand Down Expand Up @@ -235,23 +209,17 @@ def add_ir_module(self, ir_module):
self.add_llvm_module(ll_module)

def add_llvm_module(self, ll_module):
if config.DEBUG_ARRAY_OPT >= 1:
print("CodeLibrary::add_llvm_module", self._name)
self._optimize_functions(ll_module)
# TODO: we shouldn't need to recreate the LLVM module object
ll_module = remove_redundant_nrt_refct(ll_module)
self._final_module.link_in(ll_module)
if config.DEBUG_ARRAY_OPT >= 1:
print("CodeLibrary::add_llvm_module end", self._name)

def finalize(self):
"""
Finalize the library. After this call, nothing can be added anymore.
Finalization involves various stages of code optimization and
linking.
"""
if config.DEBUG_ARRAY_OPT >= 1:
print("CodeLibrary::finalize", self._name)
#require_global_compiler_lock()
Copy link
Author

@PokhodenkoSA PokhodenkoSA Nov 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When require_global_compiler_lock() uncommented dppl tests fail:

======================================================================
FAIL: test_bool_arg (numba_dppy.tests.dppl.test_arg_types.TestDPPLArrayArgGPU)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/localdisk/work/spokhode/numba-dppy/numba_dppy/tests/dppl/test_arg_types.py", line 90, in test_bool_arg
    check_bool_kernel[global_size, dppl.DEFAULT_LOCAL_SIZE](A, True)
  File "/localdisk/work/spokhode/numba-dppy/numba_dppy/compiler.py", line 510, in __call__
    kernel = self.specialize(*args)
  File "/localdisk/work/spokhode/numba-dppy/numba_dppy/compiler.py", line 533, in specialize
    self.access_types)
  File "/localdisk/work/spokhode/numba-dppy/numba_dppy/compiler.py", line 123, in compile_kernel
    cres = compile_with_dppl(pyfunc, None, args, debug=debug)
  File "/localdisk/work/spokhode/numba-dppy/numba_dppy/compiler.py", line 111, in compile_with_dppl
    library.finalize()
  File "/localdisk/work/spokhode/numba/numba/core/codegen.py", line 255, in finalize
    require_global_compiler_lock()
  File "/localdisk/work/spokhode/numba/numba/core/compiler_lock.py", line 53, in require_global_compiler_lock
    assert global_compiler_lock.is_locked()
AssertionError

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DrTodd13 @reazulhoque
Do you know something about it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #120.


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

if config.DEBUG_ARRAY_OPT >= 1:
print("Before link_in")
# Link libraries for shared code
seen = set()
for library in self._linking_libraries:
Expand All @@ -280,8 +246,6 @@ def finalize(self):

self._final_module.verify()
self._finalize_final_module()
if config.DEBUG_ARRAY_OPT >= 1:
print("CodeLibrary::finalize end", self._name)

def _finalize_dynamic_globals(self):
# Scan for dynamic globals
Expand Down Expand Up @@ -321,7 +285,6 @@ def _finalize_final_module(self):
dump("OPTIMIZED DUMP %s" % self._name, self.get_llvm_str(), 'llvm')

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

for gv in module.global_variables:
if config.DEBUG_ARRAY_OPT >= 1:
print("scan_unresolved_symbols", gv)
if gv.name.startswith(prefix):
sym = gv.name[len(prefix):]
# Avoid remapping to existing GV
Expand All @@ -589,23 +550,17 @@ def scan_defined_symbols(self, module):
Scan and track all defined symbols.
"""
for fn in module.functions:
if config.DEBUG_ARRAY_OPT >= 1:
print("scan_defined_symbols", fn)
if not fn.is_declaration:
self._defined.add(fn.name)

def resolve(self, engine):
"""
Fix unresolved symbols if they are defined.
"""
if config.DEBUG_ARRAY_OPT >= 1:
print("RuntimeLinker resolve", self._unresolved, self._defined)
# An iterator to get all unresolved but available symbols
pending = [name for name in self._unresolved if name in self._defined]
# Resolve pending symbols
for name in pending:
if config.DEBUG_ARRAY_OPT >= 1:
print("name", name)
# Get runtime address
fnptr = engine.get_function_address(name)
# Fix all usage
Expand Down Expand Up @@ -650,17 +605,13 @@ def _load_defined_symbols(self, mod):
"""Extract symbols from the module
"""
for gsets in (mod.functions, mod.global_variables):
if config.DEBUG_ARRAY_OPT >= 1:
print("_load_defined_symbols", gsets, self._defined_symbols)
self._defined_symbols |= {gv.name for gv in gsets
if not gv.is_declaration}

def add_module(self, module):
"""Override ExecutionEngine.add_module
to keep info about defined symbols.
"""
if config.DEBUG_ARRAY_OPT >= 1:
print("add_module", module)
self._load_defined_symbols(module)
return self._ee.add_module(module)

Expand Down Expand Up @@ -923,7 +874,6 @@ def initialize_llvm():
ll.initialize()
ll.initialize_native_target()
ll.initialize_native_asmprinter()
ll.initialize_all_targets()


def get_host_cpu_features():
Expand Down