lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.51k stars 164 forks source link

How to write and maintain runtime library code callable from ASR passes in libASR #1150

Open certik opened 2 years ago

certik commented 2 years ago

The easiest is to write the runtime library in the frontend language, compile to ASR and insert by the frontend. This does not work for ASR passes, that have to be part of libasr and usable across all LCompilers.

There are multiple approaches possible. See:

It seems that a conceptually clean approach is to simply ship the needed runtime code as ASR as part of libasr, and to construct this ASR code directly in C++. We will always need this, so we might as well make it as easy as possible, using all kinds of helper functionality. The big advantage of this is that we can generate it specifically for the combination of types that are needed.

For larger runtime codes that maybe do not need to change types, we can also consider automatically generating the C++ code that constructs a given ASR, then implement and maintain a given library in any of our frontends (LPython, LFortran, ...), convert to ASR and call a (to be written!) ASR->"C++ code to construct it" backend. It would be like reading in the --show-asr output, but without a parser, so it would be faster. One big advantage of this approach is that if we modify ASR, we can simply rerun the frontend to generate new update C++ code that constructs it.

czgdp1807 commented 2 years ago

Makes sense. We can start doing this for optimisation, gain more experience and add all kinds of utility functions to generate the ASR.