public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
* Interfacing with external code using libgccjit
@ 2023-06-15 19:59 Anselm Schüler
  2023-06-16 17:27 ` David Malcolm
  0 siblings, 1 reply; 4+ messages in thread
From: Anselm Schüler @ 2023-06-15 19:59 UTC (permalink / raw)
  To: jit

[-- Attachment #1: Type: text/plain, Size: 815 bytes --]

Hi, I’m very new to libgccjit and was hoping to use it to implement a 
DSL that abstracts over some high-precision mathematics.
However, to do that, I need to interface with a high-precision library 
such as GMP or MPFR.
I’m wondering if that’s possible. Can I use 
gcc_jit_context_new_function with GCC_JIT_FUNCTION_IMPORTED to define 
such functions from e.g. MPFR? If so, is the function retrieved from 
the binary itself or does it need to be installed on the target 
machine? Do I need to pass -lmpfr to libgccjit or does it suffice to 
use it during the compilation of the host program?
Additionally, it seems to be necessary to redefine all the types 
defined by the library in order to be able to pass and receive them. Is 
there some facility for automating this? It seems rather laborious.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Interfacing with external code using libgccjit
  2023-06-15 19:59 Interfacing with external code using libgccjit Anselm Schüler
@ 2023-06-16 17:27 ` David Malcolm
  2023-06-16 19:07   ` Basile Starynkevitch
  0 siblings, 1 reply; 4+ messages in thread
From: David Malcolm @ 2023-06-16 17:27 UTC (permalink / raw)
  To: Anselm Schüler, jit

On Thu, 2023-06-15 at 21:59 +0200, Anselm Schüler wrote:
> Hi, I’m very new to libgccjit and was hoping to use it to implement a
> DSL that abstracts over some high-precision mathematics.

Hi!

> However, to do that, I need to interface with a high-precision
> library 
> such as GMP or MPFR.
> I’m wondering if that’s possible. 

It ought to be possible, but you need some way to express the
declarations from the library's headers via the ligccjit API; we don't
yet have a convenient way to take a header and express it in libgccjit
form.  As you note, doing it "by hand" is laborious; sorry that we 
don't yet have an automated way of doing this.  Possible approaches
would be to use a library that can parse C headers, such as:
  https://github.com/vnmakarov/mir
or perhaps using libabigail to work with the ABI representation:
  https://sourceware.org/libabigail/
(which would be parsing the debug data)


> Can I use 
> gcc_jit_context_new_function with GCC_JIT_FUNCTION_IMPORTED to define
> such functions from e.g. MPFR? 

Yes.


> If so, is the function retrieved from 
> the binary itself or does it need to be installed on the target 
> machine? 

It's equivalent to parsing a declaration from a header file; it doesn't
bring in the definition of the function.

> Do I need to pass -lmpfr to libgccjit or does it suffice to 
> use it during the compilation of the host program?

I believe you'd need to pass "-lmpfr" to
gcc_jit_context_add_driver_option.


> Additionally, it seems to be necessary to redefine all the types 
> defined by the library in order to be able to pass and receive them.
> Is 
> there some facility for automating this? It seems rather laborious.
> 

Yes, as noted above, this is currently a major drawback, and an
automated way of doing this would be very helpful.

Hope this is helpful
Dave


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Interfacing with external code using libgccjit
  2023-06-16 17:27 ` David Malcolm
@ 2023-06-16 19:07   ` Basile Starynkevitch
  2023-06-16 19:15     ` Marc Nieper-Wißkirchen
  0 siblings, 1 reply; 4+ messages in thread
From: Basile Starynkevitch @ 2023-06-16 19:07 UTC (permalink / raw)
  To: David Malcolm, Anselm Schüler, jit


On 6/16/23 19:27, David Malcolm via Jit wrote:
> On Thu, 2023-06-15 at 21:59 +0200, Anselm Schüler wrote:
>> Hi, I’m very new to libgccjit and was hoping to use it to implement a
>> DSL that abstracts over some high-precision mathematics.
> Hi!
>
>> However, to do that, I need to interface with a high-precision
>> library
>> such as GMP or MPFR.
>> I’m wondering if that’s possible.
> It ought to be possible, but you need some way to express the
> declarations from the library's headers via the ligccjit API; we don't
> yet have a convenient way to take a header and express it in libgccjit
> form.  As you note, doing it "by hand" is laborious; sorry that we
> don't yet have an automated way of doing this.  Possible approaches
> would be to use a library that can parse C headers, such as:
>    https://github.com/vnmakarov/mir
> or perhaps using libabigail to work with the ABI representation:
>    https://sourceware.org/libabigail/
> (which would be parsing the debug data)
>

Another possible approach might be to use some GCC plugin (perhaps 
inspired by some code from https://github.com/bstarynk/bismon ...) to 
parse the GMP or MPFR header files, and later inject some constants 
(describing the external code, such as GMP or MPFR) into libgccjit




Maybe future GCC support for C++ 2x modules could be also relevant

Cheers

-- 
Basile Starynkevitch                  <basile@starynkevitch.net>
(only mine opinions / les opinions sont miennes uniquement)
92340 Bourg-la-Reine, France
web page: starynkevitch.net/Basile/


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Interfacing with external code using libgccjit
  2023-06-16 19:07   ` Basile Starynkevitch
@ 2023-06-16 19:15     ` Marc Nieper-Wißkirchen
  0 siblings, 0 replies; 4+ messages in thread
From: Marc Nieper-Wißkirchen @ 2023-06-16 19:15 UTC (permalink / raw)
  To: Basile Starynkevitch; +Cc: David Malcolm, Anselm Schüler, jit

The simplest way IMO is to write a stub in C with ordinary functions
that calls into GMP and MPFR, and then call the C stub from libgccjit
code. When you enable LTO, the stub should be optimized away.

Am Fr., 16. Juni 2023 um 21:08 Uhr schrieb Basile Starynkevitch
<basile@starynkevitch.net>:
>
>
> On 6/16/23 19:27, David Malcolm via Jit wrote:
> > On Thu, 2023-06-15 at 21:59 +0200, Anselm Schüler wrote:
> >> Hi, I’m very new to libgccjit and was hoping to use it to implement a
> >> DSL that abstracts over some high-precision mathematics.
> > Hi!
> >
> >> However, to do that, I need to interface with a high-precision
> >> library
> >> such as GMP or MPFR.
> >> I’m wondering if that’s possible.
> > It ought to be possible, but you need some way to express the
> > declarations from the library's headers via the ligccjit API; we don't
> > yet have a convenient way to take a header and express it in libgccjit
> > form.  As you note, doing it "by hand" is laborious; sorry that we
> > don't yet have an automated way of doing this.  Possible approaches
> > would be to use a library that can parse C headers, such as:
> >    https://github.com/vnmakarov/mir
> > or perhaps using libabigail to work with the ABI representation:
> >    https://sourceware.org/libabigail/
> > (which would be parsing the debug data)
> >
>
> Another possible approach might be to use some GCC plugin (perhaps
> inspired by some code from https://github.com/bstarynk/bismon ...) to
> parse the GMP or MPFR header files, and later inject some constants
> (describing the external code, such as GMP or MPFR) into libgccjit
>
>
>
>
> Maybe future GCC support for C++ 2x modules could be also relevant
>
> Cheers
>
> --
> Basile Starynkevitch                  <basile@starynkevitch.net>
> (only mine opinions / les opinions sont miennes uniquement)
> 92340 Bourg-la-Reine, France
> web page: starynkevitch.net/Basile/
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-16 19:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-15 19:59 Interfacing with external code using libgccjit Anselm Schüler
2023-06-16 17:27 ` David Malcolm
2023-06-16 19:07   ` Basile Starynkevitch
2023-06-16 19:15     ` Marc Nieper-Wißkirchen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).