GHC 不优化主模块以外的模块

发布于 2024-12-04 00:30:19 字数 815 浏览 15 评论 0原文

我目前正在用 Haskell 编写一个多模块程序。我发现了一个奇怪的问题,即使我传递了 -O2 等,我的文件也没有得到正确优化。相关文件是共享 3D 矢量数学模块。当单独编译为 .o 时,这些模块会被正确优化。当使用 --make 编译为较大程序的一部分时,它们未正确优化。核心是完全不同的。

我已将一些简单的精简测试代码放入了 vector.hs 文件中:

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)

并从 main 导入它...

import Vector

此代码作为独立的 .hs 文件的编译方式与我使用 --make

我的命令行是:

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

干杯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2024-12-11 00:30:19

添加到

{-# INLINE addVec #-}

主模块中。

如果调用者/被调用者不在同一模块中,GHC 需要在进行优化之前指示这种可能性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文