[Haskell-cafe] Whole program analysis as a GHC plugin

Christiaan Baaij christiaan.baaij at gmail.com
Tue Mar 17 10:47:08 UTC 2015

forgot to cc the list in an earlier email...

Hi David,

Not all identifier have an unfolding in the .hi files… you’d have to recompile all shipped libraries with: -fexpose-all-unfoldings.
Even then, identifiers that evaluate to bottom will, annoyingly, still not get an unfolding in the .hi files.
Many identifiers do, however, have an unfolding!

With that out of the way, I’ve created a whole-program transformation compiler that converts “structural” Haskell to Hardware (VHDL/Verilog).
You can take a look at: https://github.com/clash-lang/clash-compiler/tree/master/clash-ghc/src-ghc/CLaSH/GHC <https://github.com/clash-lang/clash-compiler/tree/master/clash-ghc/src-ghc/CLaSH/GHC>
for all the modules that interact with the GHC API.
Especially look at: https://github.com/clash-lang/clash-compiler/blob/master/clash-ghc/src-ghc/CLaSH/GHC/LoadModules.hs#L56 <https://github.com/clash-lang/clash-compiler/blob/master/clash-ghc/src-ghc/CLaSH/GHC/LoadModules.hs#L56>
Which, given a module name “A", gives you all binders from the module “A", and all the binders from other modules that are needed by the binders in “A”
It uses: https://github.com/clash-lang/clash-compiler/blob/master/clash-ghc/src-ghc/CLaSH/GHC/LoadInterfaceFiles.hs#L59 <https://github.com/clash-lang/clash-compiler/blob/master/clash-ghc/src-ghc/CLaSH/GHC/LoadInterfaceFiles.hs#L59>
to load the unfoldings from “.hi” files.

The code is pretty much hacked together as I was exploring the GHC API to get the things I wanted.
So my apologies for the poor readability.



> On 16 Mar 2015, at 18:34, David Darais <darais at cs.umd.edu> wrote:
> Hello cafe,
> I'm currently implementing a whole-program analysis for Haskell programs using the GHC api. I have a GHC plugin written and I'm successfully slurping in programs and processing them with my analysis tools.
> I'm getting stuck when my tool encounters an identifier that lives in another module. I'd like to unfold the identifier and retrieve its source code, allowing me to do whole-program analysis.
> Assuming I have the source code of the whole program, and that I get to (re)compile everything with my plugin enabled, I'm wondering:
> 1) Does the GHC api already support a nice way of doing this?
> 2) I see that there is support for unfolding identifiers if they've been specially marked in .hi files. Is there a way to mark everything to support unfolding, and would this give me the information I'm looking for?
> 3) Does anybody have experience with implementing a whole-program analysis for GHC as a plugin, and if so, how did you go about it?
> Many Thanks,
> David
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150317/46fb3284/attachment.html>

More information about the Haskell-Cafe mailing list