-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Reduce redundant module linking #3228
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1b4a94f
Try to cut redundant llvm module linking
sklam 79eff64
Internalize _compile_subroutine_no_cache
sklam 3e0c48a
Use contextmanager instead of try-finally
sklam f703cb7
Cleanup
sklam 7689131
Add docstring on caching arg
sklam 58ddff6
Merge branch 'master' into enh/reducelinkin
sklam File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import os | ||
import sys | ||
from itertools import permutations, takewhile | ||
from contextlib import contextmanager | ||
|
||
import numpy as np | ||
|
||
|
@@ -241,6 +242,7 @@ def __init__(self, typing_context): | |
self.special_ops = {} | ||
self.cached_internal_func = {} | ||
self._pid = None | ||
self._codelib_stack = [] | ||
|
||
self.data_model_manager = datamodel.default_manager | ||
|
||
|
@@ -776,7 +778,8 @@ def get_dummy_value(self): | |
def get_dummy_type(self): | ||
return GENERIC_POINTER | ||
|
||
def compile_subroutine_no_cache(self, builder, impl, sig, locals={}, flags=None): | ||
def _compile_subroutine_no_cache(self, builder, impl, sig, locals={}, | ||
flags=None): | ||
""" | ||
Invoke the compiler to compile a function to be used inside a | ||
nopython function, but without generating code to call that | ||
|
@@ -804,23 +807,36 @@ def compile_subroutine_no_cache(self, builder, impl, sig, locals={}, flags=None) | |
codegen.add_linking_library(cres.library) | ||
return cres | ||
|
||
def compile_subroutine(self, builder, impl, sig, locals={}): | ||
def compile_subroutine(self, builder, impl, sig, locals={}, flags=None, | ||
caching=True): | ||
""" | ||
Compile the function *impl* for the given *sig* (in nopython mode). | ||
Return a placeholder object that's callable from another Numba | ||
function. | ||
|
||
If *caching* evaluates True, the function keeps the compiled function | ||
for reuse in *.cached_internal_func*. | ||
""" | ||
cache_key = (impl.__code__, sig, type(self.error_model)) | ||
if impl.__closure__: | ||
# XXX This obviously won't work if a cell's value is | ||
# unhashable. | ||
cache_key += tuple(c.cell_contents for c in impl.__closure__) | ||
ty = self.cached_internal_func.get(cache_key) | ||
if ty is None: | ||
cres = self.compile_subroutine_no_cache(builder, impl, sig, | ||
locals=locals) | ||
if not caching: | ||
cached = None | ||
else: | ||
if impl.__closure__: | ||
# XXX This obviously won't work if a cell's value is | ||
# unhashable. | ||
cache_key += tuple(c.cell_contents for c in impl.__closure__) | ||
cached = self.cached_internal_func.get(cache_key) | ||
if cached is None: | ||
cres = self._compile_subroutine_no_cache(builder, impl, sig, | ||
locals=locals, | ||
flags=flags) | ||
lib = cres.library | ||
ty = types.NumbaFunction(cres.fndesc, sig) | ||
self.cached_internal_func[cache_key] = ty | ||
self.cached_internal_func[cache_key] = ty, lib | ||
|
||
ty, lib = self.cached_internal_func[cache_key] | ||
# Allow inlining the function inside callers. | ||
self.active_code_library.add_linking_library(lib) | ||
return ty | ||
|
||
def compile_internal(self, builder, impl, sig, args, locals={}): | ||
|
@@ -1064,6 +1080,22 @@ def create_module(self, name): | |
""" | ||
return lc.Module(name) | ||
|
||
@property | ||
def active_code_library(self): | ||
"""Get the active code library | ||
""" | ||
return self._codelib_stack[-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could someone end up calling this before the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If that happens, it is an internal error. |
||
|
||
@contextmanager | ||
def push_code_library(self, lib): | ||
"""Push the active code library for the context | ||
""" | ||
self._codelib_stack.append(lib) | ||
try: | ||
yield | ||
finally: | ||
self._codelib_stack.pop() | ||
|
||
|
||
class _wrap_impl(object): | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add what
caching
does to the docstring for this function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added docstring