Skip to content

Added inline_closurecall as an import during registry loading - #9596

Merged
esc merged 2 commits into
numba:release0.60from
kc611:import-issue
Jun 10, 2024
Merged

esc merged 2 commits into
numba:release0.60from
kc611:import-issue

Conversation

@kc611

@kc611 kc611 commented May 29, 2024

Copy link
Copy Markdown
Contributor

Fixes: #9577

This PR intends to fix the import issue caused by a 0.60.0rc1 regression introduced in #9437.

@kc611 kc611 added 3 - Ready for Review skip_release_notes Skip towncrier requirement labels May 29, 2024
@gmarkall

Copy link
Copy Markdown
Member

Can you add a test?

@gmarkall gmarkall added 4 - Waiting on author Waiting for author to respond to review and removed 3 - Ready for Review labels May 29, 2024
@kc611

kc611 commented May 29, 2024 •

Copy link
Copy Markdown
Contributor Author

This is a bit hard to add as a test. The problem isn't as much as an import as it is of registration. The failure is actually because the code logic is unable to determine the type of class _Intrinsic during code lowering, but once the class has been registered the problem goes away.

i.e. to add a test for this we'd need a mechanism to basically run it in an isolated environment. (Not sure how that'd look like)

@gmarkall

gmarkall commented May 29, 2024 •

Copy link
Copy Markdown
Member

It could look like this (numba/tests/test_issue_9577):

from numba.tests.support import TestCase
import unittest

import numpy as np
import numba


@numba.njit
def issue_9577():
    range_start = 0
    for _ in range(1):
        np.array([
            1 for _ in range(range_start, 7)
        ])
        range_start = 0


class TestIssue9577(TestCase):

    @TestCase.run_test_in_subprocess
    def test_issue_9577(self):
        issue_9577()


if __name__ == '__main__':
    unittest.main()

@kc611

kc611 commented Jun 3, 2024

Copy link
Copy Markdown
Contributor Author

Ah okay, the @TestCase.run_test_in_subprocess was exactly what I was looking for. Thanks. 🙂

@gmarkall gmarkall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works as far as fixing the issue is concerned, so I'm going to approve it on that basis.

However, I do think there's something super-suspicious as to why it works. inline_closurecall is imported before _inline_arraycall creates a reference to a global called length_of_iterator (this should seem trivial), so my expectation is that this issue shouldn't have occurred - decorating with the @intrinsic decorator should already have made length_of_iterator available. My initial hypothesis is that somehow the typing context is not refreshed when required, but I don't want to go too much deeper into the issue as part of the review here.

@gmarkall gmarkall added 4 - Waiting on second reviewer Patch needs a second reviewer. and removed 4 - Waiting on author Waiting for author to respond to review labels Jun 3, 2024
@gmarkall

gmarkall commented Jun 3, 2024

Copy link
Copy Markdown
Member

(I've marked for a second review in case someone else thinks we ought to dig deeper into the underling issue now, or later)

@kc611

kc611 commented Jun 3, 2024 •

Copy link
Copy Markdown
Contributor Author

For additional context; a breakpoint at the point of failure:

typ = self.resolve_value_type(inst, gvar.value)

Throws the error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/kc611/Desktop/Workspaces/Numba/review/numba/numba/core/typeinfer.py", line 1502, in resolve_value_type
    raise TypingError(msg, loc=inst.loc)
numba.core.errors.TypingError: Cannot determine Numba type of <class 'numba.core.extending._Intrinsic'>

Which gets rewitten as the error:

Failed in nopython mode pipeline (step: nopython frontend)
NameError: name 'length_of_iterator' is not defined
  File "/home/kc611/Desktop/Workspaces/Numba/review/numba/numba/core/dispatcher.py", line 364, in error_rewrite
    raise e.with_traceback(None)
  File "/home/kc611/Desktop/Workspaces/Numba/review/numba/numba/core/dispatcher.py", line 423, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/kc611/Desktop/Workspaces/Numba/review/scratchpad/eg.py", line 13, in <module>
    _inner()
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
NameError: name 'length_of_iterator' is not defined

due to the following logic:

numba/numba/core/typeinfer.py

Lines 1594 to 1601 in 556545c

if (nm not in func_glbls.keys() and
nm not in special.__all__ and
nm not in __builtins__.keys() and
nm not in self.func_id.code.co_freevars):
errstr = "NameError: name '%s' is not defined"
msg = _termcolor.errmsg(errstr % nm)
e.patch_message(msg)
raise

@gmarkall

gmarkall commented Jun 3, 2024

Copy link
Copy Markdown
Member

Thanks for the context - additionally, the typing for that _Intrinsic must (I think) have been added to the builtin registry, by

def _register(self):
# _ctor_kwargs
from numba.core.typing.templates import (make_intrinsic_template,
infer_global)
template = make_intrinsic_template(self, self._defn, self._name,
prefer_literal=self._prefer_literal,
kwargs=self._ctor_kwargs)
infer(template)
infer_global(self, types.Function(template))

However, I think the typing context was never refreshed after this and prior to the InlineCosureCallPass running - it feels like there should have been a typingctx.refresh() happen at some point in between them, such that it isn't necessary to import inline_closurecall first so that the intrinsic for length_of_iterator is ready for consumption by a refresh that happens early on in the compilation process.

@sklam

sklam commented Jun 3, 2024

Copy link
Copy Markdown
Member

Indeed it works after adding a context refresh() in resolve_value_type().

diff --git a/numba/core/cpu.py b/numba/core/cpu.py
index ec55a2d5a..130e47c5b 100644
--- a/numba/core/cpu.py
+++ b/numba/core/cpu.py
@@ -71,7 +71,7 @@ class CPUContext(BaseContext):
                                    listobj, numbers, rangeobj, # noqa F401
                                    setobj, slicing, tupleobj, # noqa F401
                                    unicode,) # noqa F401
-        from numba.core import optional, inline_closurecall # noqa F401
+        from numba.core import optional # noqa F401
         from numba.misc import gdb_hook, literal # noqa F401
         from numba.np import linalg, arraymath, arrayobj # noqa F401
         from numba.np.random import generator_core, generator_methods # noqa F401
diff --git a/numba/core/typeinfer.py b/numba/core/typeinfer.py
index 0280dd9f5..934bb821e 100644
--- a/numba/core/typeinfer.py
+++ b/numba/core/typeinfer.py
@@ -1499,6 +1499,13 @@ https://numba.readthedocs.io/en/stable/user/troubleshoot.html#my-code-has-an-unt
             return self.context.resolve_value_type(val)
         except ValueError as e:
             msg = str(e)
+
+        self.context.refresh()
+        try:
+            return self.context.resolve_value_type(val)
+        except ValueError as e:
+            msg = str(e)
+
         raise TypingError(msg, loc=inst.loc)
 
     def typeof_arg(self, inst, target, arg):

@gmarkall

gmarkall commented Jun 3, 2024

Copy link
Copy Markdown
Member

Thanks for confirming @sklam. I think we should:

  • For 0.60, keep this PR's fix, and merge it, because it preserves the way things were working previously.
  • After 0.60, fix the bug that intrinsics aren't available until the typing context is somehow "explicitly" refreshed, because finding the right fix might take a little time, and could have some slight change in behaviour. Although any code implicitly relying on the wrong behaviour would also have been wrong, I suspect it might lead to the discovery of related issues that also end up needing fixing (as happened with Don't attempt to register overloads that aren't for this target in BaseContext and related fixes #9454).

@sklam sklam added 5 - Ready to merge Review and testing done, is ready to merge and removed 4 - Waiting on second reviewer Patch needs a second reviewer. labels Jun 3, 2024
@sklam sklam modified the milestones: 0.60.0-rc1, 0.60.0 Jun 3, 2024
@esc
esc merged commit b3dc3df into numba:release0.60 Jun 10, 2024
@esc

esc commented Jun 10, 2024

Copy link
Copy Markdown
Member

Unfortunately this PR had targeted the branch release0.60 rather than main so it will need to be backported to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge skip_release_notes Skip towncrier requirement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants