为什么LLVM执行引擎比编译的代码快? [英] Why is the LLVM execution engine faster than compiled code?

查看:62
本文介绍了为什么LLVM执行引擎比编译的代码快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对LLVM的编译器,我提供了两种运行代码的方法:

I have a compiler which targets LLVM, and I provide two ways to run the code:

  1. 自动运行.此模式将代码编译为LLVM,并使用ExecutionEngine JIT将其即时编译为机器代码并运行,而无需生成输出文件.
  2. 编译并单独运行.此模式输出LLVM .bc文件,我手动对其进行了优化(使用 opt ),编译为本机程序集(使用 llc ),并编译为机器代码并链接了(使用 gcc ),然后运行.
  1. Run it automatically. This mode compiles the code to LLVM and uses the ExecutionEngine JIT to compile it into machine code on-the-fly and run it without ever generating an output file.
  2. Compile it and run separately. This mode outputs an LLVM .bc file, which I manually optimise (with opt), compile to native assembly (with llc) compile to machine code and link (with gcc), and run.

我期望方法2比方法1更快,或者至少是相同的速度,但是运行一些速度测试后,我惊讶地发现方法2始终运行大约慢一倍.那是巨大的速度差.

I was expecting approach #2 to be faster than approach #1, or at least the same speed, but running a few speed tests, I am surprised to find that #2 consistently runs about twice as slow. That is a huge speed difference.

两种情况都运行相同的LLVM源代码.使用方法1,我还没有费心去运行任何LLVM优化遍历(这就是为什么我期望它慢一些的原因).使用方法2,我在 -std-compile-opts llc -O3 上运行 opt ,最大限度地提高优化效果,但仍无法排名第一.这是同一程序的示例运行:

Both cases are running the same LLVM source code. With approach #1, I haven't yet bothered to run any LLVM optimisation passes (which is why I was expecting it to be slower). With approach #2, I am running opt with -std-compile-opts and llc with -O3, to maximise optimisation, yet it isn't getting anywhere near #1. Here is an example run of the same program:

  • 未优化的第一名:11.833s
  • 未优化的第二名:22.262秒
  • #2进行了优化( -std-compile-opts -O3 ):18.823s
  • #1 without optimisation: 11.833s
  • #2 without optimisation: 22.262s
  • #2 with optimisation (-std-compile-opts and -O3): 18.823s

ExecutionEngine是否执行我不知道的特殊操作?我有什么办法可以优化编译后的代码,以达到与ExecutionEngine JIT相同的性能?

Is the ExecutionEngine doing something special that I don't know about? Is there any way for me to optimise the compiled code to achieve the same performance as the ExecutionEngine JIT?

推荐答案

具有JIT的VM运行某些应用程序的速度比编译应用程序快是正常的.这是因为具有JIT的VM就像一个模拟器,可以模拟虚拟计算机,并实时运行编译器.由于这两个任务都是使用JIT内置到VM中的,因此机器模拟器可以将信息提供给编译器,以便可以重新编译代码以更有效地运行.它提供的信息不适用于静态编译的代码.

It is normal for a VM with JIT to run some applications faster than than a compiled application. That's because a VM with JIT is like a simulator that simulates a virtual computer, and also runs a compiler in realtime. Because both tasks are built into the VM with JIT, the machine simulator can feed information to the compiler so that the code can be recompiled to run more efficiently. The information that it provides is not available to statically compiled code.

在Java VM和Python的PyPy VM等中也注意到了这种效果.

This effect has also been noted with Java VMs and with Python's PyPy VM, among others.

这篇关于为什么LLVM执行引擎比编译的代码快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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