Skip to content

Instantly share code, notes, and snippets.

@luk-f-a
Last active April 18, 2020 16:29
Show Gist options
  • Save luk-f-a/085022163e5d32037542532da8924508 to your computer and use it in GitHub Desktop.
Save luk-f-a/085022163e5d32037542532da8924508 to your computer and use it in GitHub Desktop.
Type problem with function types subtyping

This line comes from the test code, right before the function is executed.

Type of tuple in test code Tuple(type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo1 at 0x7fdc32df7400>)), type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo2 at 0x7fdc32df7488>)))  code:  1566

This line comes from _intern. the tuple is created and assigned 1568 as typecode

Creating new tuple  
	 Type: Tuple(type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo1 at 0x7fdc32df7400>)), type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo2 at 0x7fdc32df7488>))) 
	 id: 140583722982144 
	 hash: -871723599256225085 
	 Found in typecache: False
	 Assigned typecode 1568

The function is called, and from inside the dispatcher code in C, we get : Inside _dispatcher.c - Dispatcher_call: type code 1568 (the type code matches)

The code is still resolving the call, it has not failed. Somehow, along the way the tuple with id 140583722982144 was garbage collected. At one point it's needed again and this triggers the creation of a new one, with a different typecode! 1570!

Creating new tuple  
	 Type: Tuple(type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo1 at 0x7fdc32df7400>)), type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo2 at 0x7fdc32df7488>))) 
	 id: 140583722980576 
	 hash: -871723599256225085 
	 Found in typecache: False
	 Assigned typecode 1570

This newly created tuple type is used in the setting of compatible types:

loading dispatcher->FunctionType in set_compatible. tuple type: Tuple(type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo1 at 0x7fdc32df7400>)), type(CPUDispatcher(<function TestBasicSubtyping.test_basic4.<locals>.foo2 at 0x7fdc32df7488>))) code: 1570

What I cannot answer is:

  • why is the 1568 Tuple getting garbage collected? I would have thought there's an live reference since it's the type of parameter in an active function call.
  • why is the 1570 Tuple (which gets loaded as compatible type) not the one used to match the types?

Without being able to address the underlying issues, I made a pragmatic fix in https://github.com/luk-f-a/numba/blob/222f0fd33082bd39b18a9adec97d4a18469a2599/numba/core/types/abstract.py#L48-L74

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