# AOT compilation of jitclass

**URL:** <https://numba.discourse.group/t/aot-compilation-of-jitclass/1229>\
**Category:** Numba\
**Created:** [March 6, 2022, 8:31am UTC](https://numba.discourse.group/t/aot-compilation-of-jitclass/1229 "2022-03-06T08:31:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbduKT](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/abdukt/32/554_2.png) [@AbduKT](https://numba.discourse.group/u/AbduKT)\
**Post date:** [March 6, 2022, 8:31am UTC](https://numba.discourse.group/t/aot-compilation-of-jitclass/1229/1 "2022-03-06T08:31:14Z")

</div>

Does jitclass support ahead of time compilation?

---

<div class="post-metadata">

**Author:** ![guilherme](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/guilherme/32/111_2.png) [@guilherme](https://numba.discourse.group/u/guilherme)\
**Post date:** [March 8, 2022, 3:34am UTC](https://numba.discourse.group/t/aot-compilation-of-jitclass/1229/2 "2022-03-08T03:34:25Z")

</div>

Hi AbduKT,

One can create and use a jitclass inside an ahead of time function. See:

```python
# example.py
from numba.pycc import CC
import numpy as np
from numba.core import types
from numba.experimental import jitclass

cc = CC('my_module')

spec = [
    ('array', types.int32[:])
]

@jitclass(spec)
class Foo(object):
    def __init__ (self, value):
        self.array = np.arange(value, dtype=np.int32)

@cc.export('get_sum', 'int32(int32)')
def get_sum(n):
    myFoo = Foo(n)
    return myFoo.array.sum()

if __name__ == ' __main__':
    cc.compile()

```

```bash
$ python example.py # will produce my_module.so
$ python -c 'import my_module; print(my_module.get_sum(4)) # print "6"`

```

But it doesn’t seem to be possible to pass a jitclass as an argument:

```python
# example2
from numba.pycc import CC
import numpy as np
from numba.core import types
from numba.experimental import jitclass

cc = CC('my_module')

spec = [
    ('array', types.int32[:])
]

@jitclass(spec)
class Foo(object):
    def __init__ (self, value):
        self.array = np.arange(value, dtype=np.int32)

@cc.export('get_sum', 'int32(Foo)')
def get_sum(x):
    return x.array.sum()

if __name__ == ' __main__':
    cc.compile()

```

```bash
$ python example2.py
Traceback (most recent call last):
  File "example2.py", line 18, in <module>
    @cc.export('get_first', 'int32(Foo)')
  File "/home/guilhermeleobas/git/numba/numba/pycc/cc.py", line 136, in export
    fn_args, fn_retty = sigutils.normalize_signature(sig)
  File "/home/guilhermeleobas/git/numba/numba/core/sigutils.py", line 29, in normalize_signature
    parsed = _parse_signature_string(sig)
  File "/home/guilhermeleobas/git/numba/numba/core/sigutils.py", line 19, in _parse_signature_string
    return eval(signature_str, {}, types. __dict__ )
  File "<string>", line 1, in <module>
NameError: name 'Foo' is not defined
(numba) 

```

Numba crashes when it tries to parse the signature.

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [March 8, 2022, 1:34pm UTC](https://numba.discourse.group/t/aot-compilation-of-jitclass/1229/3 "2022-03-08T13:34:36Z")

</div>

As I understand it, functions with jitclass arguments cannot be cached as each jitclass instance ends up as a different type.

Prefer structuref, which has a bit more boilerplate and a steeper learning curve but works well with caching and AOT compilation.

---

<div class="post-metadata">

**Author:** ![wsjeon](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/wsjeon/32/608_2.png) [@wsjeon](https://numba.discourse.group/u/wsjeon)\
**Post date:** [June 3, 2022, 3:24pm UTC](https://numba.discourse.group/t/aot-compilation-of-jitclass/1229/4 "2022-06-03T15:24:51Z")

</div>

@nelson2005 I’ve been facing the similar issue and would like to find out structuref but failed. Would you share any relevant link for that? I’d like to try.

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [June 3, 2022, 3:56pm UTC](https://numba.discourse.group/t/aot-compilation-of-jitclass/1229/5 "2022-06-03T15:56:50Z")

</div>

Hi @wsjeon! I’m not sure what you’re asking.

The structref docs are [here](https://numba.readthedocs.io/en/stable/extending/high-level.html?highlight=structref#defining-a-structref).

An in-depth discussion with the OG of structref @DannyWeitekamp is [here](https://numba.discourse.group/t/any-numba-equivalent-for-casting-a-raw-pointer-to-a-structref-dict-list-etc/351)

If those links don’t help, perhaps you could paste some minimal code of what you’re trying to do?

---

<div class="post-metadata">

**Author:** ![wsjeon](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/wsjeon/32/608_2.png) [@wsjeon](https://numba.discourse.group/u/wsjeon)\
**Post date:** [June 8, 2022, 1:43am UTC](https://numba.discourse.group/t/aot-compilation-of-jitclass/1229/6 "2022-06-08T01:43:34Z")

</div>

Hi, @nelson2005. I meant I couldn’t find out the documentation for StructRef (somehow, with two keywords “numba” and “structref”, the documentation couldn’t be found at that time), but I found it after asking this question and understood how to use it 🙂 Thanks for your help!

Now, I found that my code works well with StructRef with JIT Compilation and try to AOT compile my code with StructRef. Is there any example code I can use for this case?
