Peeking into StgInfoTable in cmm

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Peeking into StgInfoTable in cmm

Andrew Martin
For an optimization I’m implementing, I’m trying to write an inline primop that gives me lets me inspect the closure type of an arbitrary heap-allocated value. I’m close to figuring this out, and the FCode machinery is starting to make sense, but one thing eludes me. Starting from a StgHeader, I get the info table pointer. This is easy since it’s the first field in the struct. But next I have to deference the info table pointer and get the closure type. There are two fields at the beginning of an info table that may or may not be present. I don’t know how to figure out if they are present or not. I suspect that there is something I can get out of DynFlags that tells me this, but I can’t find anything else in the stg-to-cmm pass that reads from an info table.

Sent from my iPhone
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Peeking into StgInfoTable in cmm

Matthew Pickering
Unless I am misunderstanding you want something which works like ghc-heap.

If so, perhaps these three files will be instructive?

https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTable.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTableProf.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/Closures.hs#L25

Cheers,

Matt

On Wed, Feb 6, 2019 at 7:29 PM Andrew Martin <[hidden email]> wrote:
>
> For an optimization I’m implementing, I’m trying to write an inline primop that gives me lets me inspect the closure type of an arbitrary heap-allocated value. I’m close to figuring this out, and the FCode machinery is starting to make sense, but one thing eludes me. Starting from a StgHeader, I get the info table pointer. This is easy since it’s the first field in the struct. But next I have to deference the info table pointer and get the closure type. There are two fields at the beginning of an info table that may or may not be present. I don’t know how to figure out if they are present or not. I suspect that there is something I can get out of DynFlags that tells me this, but I can’t find anything else in the stg-to-cmm pass that reads from an info table.
>
> Sent from my iPhone
> _______________________________________________
> Libraries mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Peeking into StgInfoTable in cmm

Andrew Martin
Thanks. The source code of ghc-heap has led me to what I was looking for. That whole library hinges upon unpackClosure# (stg_unpackClosurezh), whose implementation opens with the telling:

    info  = %GET_STD_INFO(UNTAG(closure));

In CmmParse.y, we find the implementation of GET_STD_INFO, and just a few lines below is the INFO_TYPE macro. The combination of these (along with a cmm implementation of UNTAG, which I am still unsure of exactly how to accomplish) should do the trick.

On Wed, Feb 6, 2019 at 2:53 PM Matthew Pickering <[hidden email]> wrote:
Unless I am misunderstanding you want something which works like ghc-heap.

If so, perhaps these three files will be instructive?

https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTable.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTableProf.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/Closures.hs#L25

Cheers,

Matt

On Wed, Feb 6, 2019 at 7:29 PM Andrew Martin <[hidden email]> wrote:
>
> For an optimization I’m implementing, I’m trying to write an inline primop that gives me lets me inspect the closure type of an arbitrary heap-allocated value. I’m close to figuring this out, and the FCode machinery is starting to make sense, but one thing eludes me. Starting from a StgHeader, I get the info table pointer. This is easy since it’s the first field in the struct. But next I have to deference the info table pointer and get the closure type. There are two fields at the beginning of an info table that may or may not be present. I don’t know how to figure out if they are present or not. I suspect that there is something I can get out of DynFlags that tells me this, but I can’t find anything else in the stg-to-cmm pass that reads from an info table.
>
> Sent from my iPhone
> _______________________________________________
> Libraries mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


--
-Andrew Thaddeus Martin

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Peeking into StgInfoTable in cmm

Andrew Martin
Or even better, it looks like cmmGetClosureType already combines the two for me.

On Wed, Feb 6, 2019 at 3:49 PM Andrew Martin <[hidden email]> wrote:
Thanks. The source code of ghc-heap has led me to what I was looking for. That whole library hinges upon unpackClosure# (stg_unpackClosurezh), whose implementation opens with the telling:

    info  = %GET_STD_INFO(UNTAG(closure));

In CmmParse.y, we find the implementation of GET_STD_INFO, and just a few lines below is the INFO_TYPE macro. The combination of these (along with a cmm implementation of UNTAG, which I am still unsure of exactly how to accomplish) should do the trick.

On Wed, Feb 6, 2019 at 2:53 PM Matthew Pickering <[hidden email]> wrote:
Unless I am misunderstanding you want something which works like ghc-heap.

If so, perhaps these three files will be instructive?

https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTable.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTableProf.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/Closures.hs#L25

Cheers,

Matt

On Wed, Feb 6, 2019 at 7:29 PM Andrew Martin <[hidden email]> wrote:
>
> For an optimization I’m implementing, I’m trying to write an inline primop that gives me lets me inspect the closure type of an arbitrary heap-allocated value. I’m close to figuring this out, and the FCode machinery is starting to make sense, but one thing eludes me. Starting from a StgHeader, I get the info table pointer. This is easy since it’s the first field in the struct. But next I have to deference the info table pointer and get the closure type. There are two fields at the beginning of an info table that may or may not be present. I don’t know how to figure out if they are present or not. I suspect that there is something I can get out of DynFlags that tells me this, but I can’t find anything else in the stg-to-cmm pass that reads from an info table.
>
> Sent from my iPhone
> _______________________________________________
> Libraries mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


--
-Andrew Thaddeus Martin


--
-Andrew Thaddeus Martin

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

RE: Peeking into StgInfoTable in cmm

Haskell - Libraries mailing list

You might also find compiler/ghci/RtClosureInspect.hs useful.

 

It’s the main module of the GHCi debugger, and allows Haskell code to inspect the Haskell heap.    So it has mechanisms for unpacking arbitrary closures, all in Haskell.

 

It’s tough going though.   It was written a long time ago by Pepe Iborra (in a GSoC project), and has not received much love since.  Maybe you could be a source of love?!

 

Simon

 

From: Libraries <[hidden email]> On Behalf Of Andrew Martin
Sent: 06 February 2019 20:57
To: Matthew Pickering <[hidden email]>
Cc: Haskell Libraries <[hidden email]>
Subject: Re: Peeking into StgInfoTable in cmm

 

Or even better, it looks like cmmGetClosureType already combines the two for me.

 

On Wed, Feb 6, 2019 at 3:49 PM Andrew Martin <[hidden email]> wrote:

Thanks. The source code of ghc-heap has led me to what I was looking for. That whole library hinges upon unpackClosure# (stg_unpackClosurezh), whose implementation opens with the telling:

 

    info  = %GET_STD_INFO(UNTAG(closure));

 

In CmmParse.y, we find the implementation of GET_STD_INFO, and just a few lines below is the INFO_TYPE macro. The combination of these (along with a cmm implementation of UNTAG, which I am still unsure of exactly how to accomplish) should do the trick.

 

On Wed, Feb 6, 2019 at 2:53 PM Matthew Pickering <[hidden email]> wrote:

Unless I am misunderstanding you want something which works like ghc-heap.

If so, perhaps these three files will be instructive?

https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTable.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTableProf.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/Closures.hs#L25

Cheers,

Matt

On Wed, Feb 6, 2019 at 7:29 PM Andrew Martin <[hidden email]> wrote:
>
> For an optimization I’m implementing, I’m trying to write an inline primop that gives me lets me inspect the closure type of an arbitrary heap-allocated value. I’m close to figuring this out, and the FCode machinery is starting to make sense, but one thing eludes me. Starting from a StgHeader, I get the info table pointer. This is easy since it’s the first field in the struct. But next I have to deference the info table pointer and get the closure type. There are two fields at the beginning of an info table that may or may not be present. I don’t know how to figure out if they are present or not. I suspect that there is something I can get out of DynFlags that tells me this, but I can’t find anything else in the stg-to-cmm pass that reads from an info table.
>
> Sent from my iPhone
> _______________________________________________
> Libraries mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


 

--

-Andrew Thaddeus Martin


 

--

-Andrew Thaddeus Martin


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Peeking into StgInfoTable in cmm

Andrew Martin
Woah, that's pretty neat. I didn't even realize that GHCi could do that. From a quick perusal of the GHCi docs, it looks like :print and :force are related commands. How is GHCi used to show the type of a closure though?

On Wed, Feb 6, 2019 at 5:29 PM Simon Peyton Jones <[hidden email]> wrote:

You might also find compiler/ghci/RtClosureInspect.hs useful.

 

It’s the main module of the GHCi debugger, and allows Haskell code to inspect the Haskell heap.    So it has mechanisms for unpacking arbitrary closures, all in Haskell.

 

It’s tough going though.   It was written a long time ago by Pepe Iborra (in a GSoC project), and has not received much love since.  Maybe you could be a source of love?!

 

Simon

 

From: Libraries <[hidden email]> On Behalf Of Andrew Martin
Sent: 06 February 2019 20:57
To: Matthew Pickering <[hidden email]>
Cc: Haskell Libraries <[hidden email]>
Subject: Re: Peeking into StgInfoTable in cmm

 

Or even better, it looks like cmmGetClosureType already combines the two for me.

 

On Wed, Feb 6, 2019 at 3:49 PM Andrew Martin <[hidden email]> wrote:

Thanks. The source code of ghc-heap has led me to what I was looking for. That whole library hinges upon unpackClosure# (stg_unpackClosurezh), whose implementation opens with the telling:

 

    info  = %GET_STD_INFO(UNTAG(closure));

 

In CmmParse.y, we find the implementation of GET_STD_INFO, and just a few lines below is the INFO_TYPE macro. The combination of these (along with a cmm implementation of UNTAG, which I am still unsure of exactly how to accomplish) should do the trick.

 

On Wed, Feb 6, 2019 at 2:53 PM Matthew Pickering <[hidden email]> wrote:

Unless I am misunderstanding you want something which works like ghc-heap.

If so, perhaps these three files will be instructive?

https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTable.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTableProf.hsc
https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/Closures.hs#L25

Cheers,

Matt

On Wed, Feb 6, 2019 at 7:29 PM Andrew Martin <[hidden email]> wrote:
>
> For an optimization I’m implementing, I’m trying to write an inline primop that gives me lets me inspect the closure type of an arbitrary heap-allocated value. I’m close to figuring this out, and the FCode machinery is starting to make sense, but one thing eludes me. Starting from a StgHeader, I get the info table pointer. This is easy since it’s the first field in the struct. But next I have to deference the info table pointer and get the closure type. There are two fields at the beginning of an info table that may or may not be present. I don’t know how to figure out if they are present or not. I suspect that there is something I can get out of DynFlags that tells me this, but I can’t find anything else in the stg-to-cmm pass that reads from an info table.
>
> Sent from my iPhone
> _______________________________________________
> Libraries mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


 

--

-Andrew Thaddeus Martin


 

--

-Andrew Thaddeus Martin



--
-Andrew Thaddeus Martin

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries