我如何在GHCI中加载优化的代码? [英] How can I load optimized code in GHCI?

查看:130
本文介绍了我如何在GHCI中加载优化的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个依赖于优化的模块。我想在ghci中测试这个模块。但是在 - interactive 模式下启动ghc会自动禁用优化;如果我使用 -O 编译模块,然后尝试在交互式会话中加载它, ghc 坚持加载它对于一个简单的测试用例来区分优化模块和未优化模块,下面的 isOptimized 计算结果为 True 优化,但 False 关闭优化:

  isOptimized :: Bool 
isOptimized = g

g :: Bool
g = False

{ - #NOINLINE g # - }
{ - #RULESg / Trueg = True# - }


解决方案

使用 ghci -fobject-code -O Test.hs cabal repl --ghc-options = - fobject-code -O。更详细地说:


  1. 必须使用 -fobject-code flag 。必须在 -fobject-code 之后给出优化标志

  2. 。命令行或模块顶部的OPTIONS_GHC编译指示中。尝试 ghc --interactive -O -fobject-code 会产生一个警告,-O与--interactive冲突; -O忽略。这可能是一个bug。 如果您正在使用cabalized项目并使用 cabal repl ,您需要在命令行(即 cabal repl --ghc-options = - fobject-code -O)或编译指示中传递标志。当调用ghci时,Cabal(当前)放弃使用 ghc-options 设置.cabal文件中的优化标志;实际上,它明确地设置 -O0 。这可能是一个bug。

请注意,在任何情况下,您有时需要手动强制重新编译,未优化模式。出于某种原因,只要 -fobject-code 保持打开状态,优化标志更改时,构建工件就不会失效。如果从干净的石板开始,在.cabal文件中设置 -fobject-code ,运行 cabal repl 编译模块,然后记住你需要在命令行中设置-O并运行 cabal repl --ghc-options = -O ,ghc会很高兴地加载之前编译,未优化的模块。这也可能是一个bug。



测试单个模块的最可靠方案似乎是将 { - #OPTIONS_GHC -fobject-code -O# - } 在模块的顶部。无论您如何调用ghci,您都将获得优化的代码。我还没有调查在多模块情况下会发生什么情况,其中一些但不是所有模块都有编译指示。

顺便提一句,请注意,只有模块中的代码 / em>进行了优化。即使进行了优化,在repl中评估 g 总是会产生 False ,因为repl输入不会被重写规则。

I am writing a module that relies on optimization. I want to test this module in ghci. But starting ghc in --interactive mode automatically disables optimization; if I compile the module with -O and then try to load it in an interactive session, ghc insists on loading it in interpreted mode.

For a simple test case to distinguish optimized and unoptimized modules, isOptimized below evaluates to True with optimization on, but False with optimization off:

isOptimized :: Bool
isOptimized = g

g :: Bool
g = False

{-# NOINLINE g #-}
{-# RULES "g/True"  g = True #-}

解决方案

Either use ghci -fobject-code -O Test.hs or cabal repl --ghc-options="-fobject-code -O". In more detail:

  1. ghci must be invoked with the -fobject-code flag.

  2. Optimization flag(s) must be given after -fobject-code on the command line, or in an OPTIONS_GHC pragma at the top of the module. Trying ghc --interactive -O -fobject-code produces a warning that "-O conflicts with --interactive; -O ignored." This is perhaps a bug.

  3. If you're working on a cabalized project and using cabal repl, you need to pass the flags either on the command line (i.e. cabal repl --ghc-options="-fobject-code -O") or in a pragma. Cabal (currently) discards optimization flags set in the .cabal file with ghc-options when invoking ghci; in fact, it explicitly sets -O0 instead. This is perhaps a bug.

Note in any case that you sometimes need to force recompilation manually when switching between optimized and unoptimized mode. Build artifacts are, for some reason, not invalidated when the optimization flags change so long as -fobject-code remains on. If, starting from a clean slate, you have -fobject-code set in your .cabal file, run cabal repl which compiles the module, and then remember you need to set -O on the command line and run cabal repl --ghc-options=-O, ghc will happily load the previously-compiled, unoptimized module. This is also perhaps a bug.

The most reliable scenario for testing a single module seems to be to put {-# OPTIONS_GHC -fobject-code -O #-} at the top of the module. You will get optimized code no matter how you invoke ghci. I haven't investigated what happens in multi-module situations where some but not all modules have the pragma.

Incidentally, note that only code in the module is optimized. Even with optimization on, evaluating g in the repl will always produce False, because the repl input is not subject to rewrite rules.

这篇关于我如何在GHCI中加载优化的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆