[Template-haskell] Profiling with Template Haskell

Sean Seefried sseefried@cse.unsw.EDU.AU
Thu, 7 Aug 2003 11:54:27 +1000 (EST)


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

--1585569010-717039729-1060221267=:23223
Content-Type: TEXT/PLAIN; charset=US-ASCII

Hi Simon,

> Hmm, yes good point.  GHC compiles different binaries for profiled and
> non-profiled execution.  Splice code that runs at compile time should
> presumably be non-profiled (though I'm sure someone will want to
> profile it too).  So TH should link non-profiled libraries at compile
> time, but it's not clever enough to realise this.
>
> I'll put this on my list of things to fix.  I'm in the middle of a
> heart transplant on GHC at the moment, aimed at making a better
> substrate for TH, so I may not fix this very quickly unless it's
> important to you.
>
> It will mean that you'll have to compile your own program modules both
> profiled and non-profiled -- the former for run-time and the latter
> for compile time -- unless you keep compile-time code and run-time
> code in separate modules.  That's annoying but I don't see an easy
> alternative.

It was quite easy to trick GHC into linking in the non-profiled code
while splicing. I'll use the same example as last time

---------
{- Splices.hs -}
module Splices
where

import Language.Haskell.THSyntax

d_fun =
    [d|
     fun = putStrLn "I am a function"
     |]
----------
{- Main.hs -}

import Splices

$(d_fun)

main = putStrLn "hello"
--------

#Makefile

GHC=ghc -fglasgow-exts -package haskell-src

all: Main

Main: Main.p_o Splices.p_o
	$(GHC) -prof  Main.p_o Splices.p_o -o Main

%.hi: %.o
	@\:

%.o: %.hs
	$(GHC) -c $< -o $@

%.p_hi: %.p_o
	@\:

%.p_o: %.hs
	$(GHC) -prof -hisuf p_hi -c $< -o $@

Main.o: Splices.hi
Main.p_o: Splices.hi Splices.p_hi

--------

Now this actually compiles with no problems. But when run gives a Bus
error.  On another more complicated program in which I tried this
trick I received a "Illegal instruction" error, which makes me think
that control was passed to an area of memory that did not contain
code.

My supervisor Manuel suggested that the infotables are different in
the code for profiling.  It seems that the process of linking in
non-profiled code during splicing, yet linking profiled code at the
end mixes the two types of code and this results in jumps to incorrect
locations in memory at run-time.

I have attached all the files necessary to test this out yourself.
(I'm using GHC HEAD but I think it should work on older versions of GHC).

Sean
--1585569010-717039729-1060221267=:23223
Content-Type: APPLICATION/octet-stream; name="th_prof.tar.gz"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.53.0308071154270.23223@pill0.orchestra.cse.unsw.EDU.AU>
Content-Description: 
Content-Disposition: attachment; filename="th_prof.tar.gz"

H4sIAAAAAAAAA+2WX2vbMBDA89r7FEdIoS3YyLFjQ1ihsIdl0Dylb1sJwpEt
Edsylk076IefZCdpNzq6hyWBcT+w9cd3pzvsO9+Sq8qXZnRMWMBYHEUjZkni
WT+y6bDuiYMRS5IgCqdRGNp5MA3CaITsqF7t6EzLG8SRMUJkjRKbP8hZsSw7
hUOnRZW1blpc1YVKhQGYXG3WWVddA5T2y8BbrLt21Tb3FY6lKAo9PrfDxD9l
ybciU4U45hkf5f8sPOR/nAz5H8aU/yfhy+LzbS5T9LK84CbXT554bg16NU+3
PBcoudnavPdMkwIAL4o5Lm1hAHD3Ye7Xa70vIG4OF5Mra/baGml0hu/KoKd3
duDSl2qOl77Vu/s+d2vtltK82klx8slpTO7c43o9KPRH7VTsfFDC3073pDJd
hk7nVztpIbgNAC6a0gaPN762lxW66e33g0bu664dYnVe7QOQCvZBvd18E6BU
536xf8nB+yO2AB/lf5Awl/9slkRTWwRc/scxo/w/BaXedIU4/P+fpGgEwK4r
uOdV3tkq4C+GKuA/LFY/qpY/A/Rdgm0PAC3fNi/9iMPea8vwFXmJ3G2nrdLV
eJB6eQQ4d9wEQRAEQRAEQRAEQRAEQRAEQRAEQRD/Gz8BpkWFQQAoAAA=

--1585569010-717039729-1060221267=:23223--