Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GHC not optimizing modules other than the main module

I'm currently writing a multi-module program in Haskell. I've found a strange problem where my files aren't being optimized properly, even though I'm passing in -O2 and so on. The files in question are shared 3D vector maths modules. When compiled individually to a .o, these modules are optimized correctly. When compiled as part of a larger program using --make, they are not optimized correctly. The core is quite different.

I've put in some simple stripped-down test code into a vector.hs file:

data TestVector = TestVector !Double !Double !Double !Double

addVec :: TestVector -> TestVector -> TestVector

addVec (TestVector x1 y1 z1 w1) (TestVector x2 y2 z2 w2) =
  TestVector (x1 + x2) (y1 + y2) (z1 + z2) (w1 + w2)

And imported it from main...

import Vector

This code gets compiled differently as a standalone .hs file as opposed to when I build main.hs using --make

My command line is:

ghc -tmpdir tmp -hidir hi -odir obj -fext-core -fexcess-precision -funbox-strict-fields -threaded -rtsopts -fwarn-missing-signatures -Wall -O2 Main.hs -o main

Cheers

like image 279
Tom Hammersley Avatar asked Sep 09 '11 20:09

Tom Hammersley


1 Answers

Add

{-# INLINE addVec #-}

in main module.

GHC needs indication of that possibility before doing that optimization, if the invokator/invokated are not in the same modules.

like image 160
robermorales Avatar answered Oct 26 '22 00:10

robermorales