使用内置传递的LLVM opt

zel*_*ell 2 llvm

我已成功运行llvm opt和我的玩具转换过程,但是看不到如何使用内置转换过程的' opt'http://llvm.org/docs/Passes.html#introduction

我有一个空的hi.c文件

int main(){
}
Run Code Online (Sandbox Code Playgroud)

例如,如果我想使用-instcount传递,

opt -instcount hi.c
Run Code Online (Sandbox Code Playgroud)

给了我奇怪的错误.

opt: hi.c:1:1: error: expected top-level entity
int main(){
^
Run Code Online (Sandbox Code Playgroud)

使用opt -instcount hi.bc也不起作用

WARNING: You're attempting to print out a bitcode file.
This is inadvisable as it may cause display problems. If
you REALLY want to taste LLVM bitcode first-hand, you
can force output with the `-f' option.
Run Code Online (Sandbox Code Playgroud)

如果我使用opt -inst-count -f hi.bc,输出是一个凌乱的bitcode.

问题:我们应该如何使用内置转换过程的'opt'(来自上面的链接)?谢谢你的想法.'opt -help'说

opt [options] <input bitcode file>

但我上面的例子'opt -instcount hi.bc'并没有按预期工作(见上文).

Mic*_*idl 9

起初:opt只适用于bitcode /可读的LLVM IR文件.因此传递.c文件永远不会有效.你必须先用clang编译.c文件:

clang -emit-llvm in.c -o input.bc
Run Code Online (Sandbox Code Playgroud)

你遇到的警告说基本上都是:

警告:您正在尝试打印出bitcode文件.这是不可取的,因为它可能会导致显示问题.如果您真的想亲自品尝LLVM bitcode,可以使用`-f'选项强制输出.

opt具有可能已修改的bitcode文件作为输出,因为您不支持输出文件,所以它会将其打印到stdout.这就是为什么你得到"凌乱"的bitcode.

要以它应该的方式使用opt,你可以使用/ dev/null来摆脱输出:

opt -inst-count input.bc -o /dev/null
Run Code Online (Sandbox Code Playgroud)

或支持输出文件

opt -inst-count input.bc -o output.bc
Run Code Online (Sandbox Code Playgroud)

或者将输出打印为可读的LLVM IR到stdout

opt -inst-count input.bc -S
Run Code Online (Sandbox Code Playgroud)

或者将ouptut作为可读的LLVM IR文件打印到磁盘

opt -inst-count input.bc -S -o output.ll
Run Code Online (Sandbox Code Playgroud)