Repository navigation
Replace assertion errors on IR assumption violation #7223
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
Changes from 4 commits
359b5b0
e307287
34dea22
ec01acd
30a4cd2
d67e05d
5463998
9ffdd3d
46a2f9f
edc82f7
d660e36
3d6fb07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,25 @@ | ||||||
| import unittest | ||||||
| from numba import objmode | ||||||
| from unittest.case import TestCase | ||||||
| import warnings | ||||||
| import numpy as np | ||||||
|
|
||||||
| from numba import objmode | ||||||
| from numba.core import ir, compiler | ||||||
| from numba.core import errors | ||||||
| from numba.core.compiler import ( | ||||||
| CompilerBase, | ||||||
| ReconstructSSA, | ||||||
| ) | ||||||
| from numba.core.compiler_machinery import ( | ||||||
| FunctionPass, | ||||||
| PassManager, | ||||||
| register_pass, | ||||||
| ) | ||||||
| from numba.core.untyped_passes import ( | ||||||
| TranslateByteCode, | ||||||
| IRProcessing, | ||||||
| ) | ||||||
| from numba import njit | ||||||
|
|
||||||
|
|
||||||
| class TestIR(unittest.TestCase): | ||||||
|
|
@@ -466,5 +484,76 @@ def baz(a, b): | |||||
| check_diffstr(tmp, ["c + b", "b + c"]) | ||||||
|
|
||||||
|
|
||||||
| class TestIRPedanticChecks(TestCase): | ||||||
| def test_var_in_scope_assumption(self): | ||||||
| # Create a pass that clear ir.Scope in ir.Block | ||||||
|
sklam marked this conversation as resolved.
Resolved
|
||||||
| @register_pass(mutates_CFG=False, analysis_only=False) | ||||||
| class RemoveVarInScope(FunctionPass): | ||||||
| _name = "_remove_var_in_scope" | ||||||
|
|
||||||
| def __init__(self): | ||||||
| FunctionPass.__init__(self) | ||||||
|
|
||||||
| # implement method to do the work, "state" is the internal compiler | ||||||
| # state from the CompilerBase instance. | ||||||
| def run_pass(self, state): | ||||||
| func_ir = state.func_ir | ||||||
| # walk the blocks | ||||||
| for blk in func_ir.blocks.values(): | ||||||
| oldscope = blk.scope | ||||||
| # put in a empty Scope | ||||||
|
sklam marked this conversation as resolved.
Resolved
|
||||||
| blk.scope = ir.Scope(parent=oldscope.parent, | ||||||
| loc=oldscope.loc) | ||||||
| return True | ||||||
|
|
||||||
| # Create a pass that always fail to stop the compiler | ||||||
|
sklam marked this conversation as resolved.
Resolved
|
||||||
| @register_pass(mutates_CFG=False, analysis_only=False) | ||||||
| class FailPass(FunctionPass): | ||||||
| _name = "_fail" | ||||||
|
|
||||||
| def __init__(self, *args, **kwargs): | ||||||
| FunctionPass.__init__(self) | ||||||
|
|
||||||
| def run_pass(self, state): | ||||||
| # This is unreachable. SSA pass should have raised before this | ||||||
| # pass | ||||||
|
sklam marked this conversation as resolved.
Resolved
|
||||||
| raise AssertionError("unreachable") | ||||||
|
|
||||||
| class MyCompiler(CompilerBase): | ||||||
| def define_pipelines(self): | ||||||
| pm = PassManager("testing pm") | ||||||
| pm.add_pass(TranslateByteCode, "analyzing bytecode") | ||||||
| pm.add_pass(IRProcessing, "processing IR") | ||||||
| pm.add_pass(RemoveVarInScope, "_remove_var_in_scope") | ||||||
| pm.add_pass(ReconstructSSA, "ssa") | ||||||
| pm.add_pass(FailPass, "_fail") | ||||||
| pm.finalize() | ||||||
| return [pm] | ||||||
|
|
||||||
| # import logging | ||||||
| # logging.basicConfig(level=logging.DEBUG) | ||||||
|
Contributor
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. Dead comments
Contributor
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.
Suggested change
|
||||||
| @njit(pipeline_class=MyCompiler) | ||||||
| def dummy(x): | ||||||
|
Member
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. Perhaps add a comment explaining why this
Member
Author
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. done in 3d6fb07 |
||||||
| a = 1 | ||||||
| b = 2 | ||||||
| if a < b: | ||||||
| a = 2 | ||||||
| else: | ||||||
| b = 3 | ||||||
| return a, b | ||||||
|
|
||||||
| with warnings.catch_warnings(): | ||||||
| # Make NumbaPedanticWarning an error | ||||||
| warnings.simplefilter("error", errors.NumbaPedanticWarning) | ||||||
| # Catch NumbaIRAssumptionWarning | ||||||
| with self.assertRaises(errors.NumbaIRAssumptionWarning) as raises: | ||||||
| dummy(1) | ||||||
| # Verify the error message | ||||||
| self.assertRegex( | ||||||
| str(raises.exception), | ||||||
| r"variable name '[a-z]' not in scope", | ||||||
|
Contributor
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.
Suggested change
|
||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| if __name__ == '__main__': | ||||||
| unittest.main() | ||||||
Uh oh!
There was an error while loading. Please reload this page.