You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取链接C++标准库后的完整LLVM IR以统计指令数?

问题:获取链接C++标准库后的完整LLVM IR

我正在用LLVM Pass分析C++程序的指令数量,遇到一个问题:调用cmath里的sqrt()这类标准库函数时,LLVM IR里只有函数声明:

declare double @sqrt(double noundef) #1

没有函数内部的指令,导致没法统计整个程序的总指令数。

我的测试C++程序:

#include <cmath>
#include <iostream>

using namespace std;

int main(){
    int num = 10;
    float c = (float) std::sqrt((double) num);
    return 0;
}

对应的LLVM IR(仅包含声明,无sqrt实现):

; ModuleID = 'hello.cpp'
source_filename = "hello.cpp"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

module asm ".globl _ZSt21ios_base_library_initv"

; Function Attrs: mustprogress noinline norecurse nounwind uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca float, align 4
  store i32 0, ptr %1, align 4
  store i32 10, ptr %2, align 4
  %4 = load i32, ptr %2, align 4
  %5 = sitofp i32 %4 to double
  %6 = call double @sqrt(double noundef %5) #2
  %7 = fptrunc double %6 to float
  store float %7, ptr %3, align 4
  ret i32 0
}

; Function Attrs: nounwind
declare double @sqrt(double noundef) #1

attributes #0 = { mustprogress noinline norecurse nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{i32 7, !"frame-pointer", i32 2}
!5 = !{!"Ubuntu clang version 18.1.3 (1)"}

请问如何获取链接C++标准库后的完整LLVM IR?


解决方案

要获取包含标准库函数实现的完整LLVM IR,有以下几种可行方法:

方法1:用Clang直接生成链接后的完整IR

使用clang的-static(静态链接)和-emit-llvm(生成IR)选项,同时加上-S输出汇编形式的IR文件:

clang -static -emit-llvm -S hello.cpp -o hello_full.ll

这个命令会把程序和静态版本的C++标准库一起编译成包含所有函数实现的LLVM IR文件hello_full.ll,里面会包含sqrt等标准库函数的完整IR指令。

注意:静态链接会把整个标准库的代码都打包进来,IR文件会比较大,但能确保你拿到所有依赖函数的实现。

方法2:提取标准库的IR并手动合并

  1. 找到系统中静态版本的数学库(sqrt属于libm而非C++标准库,路径通常为/usr/lib/x86_64-linux-gnu/libm.a)。
  2. 使用llvm-ar工具提取库中sqrt对应的目标文件:
llvm-ar x /usr/lib/x86_64-linux-gnu/libm.a sqrt.o
  1. 将提取出的目标文件转换成LLVM IR:
llvm-dis sqrt.o -o sqrt.ll
  1. 用llvm-link把你的程序IR和标准库函数的IR合并:
llvm-link hello.ll sqrt.ll -o hello_full.ll

这种方法更灵活,只合并你需要的标准库函数IR,但需要手动定位对应的库和目标文件。

方法3:从可执行文件反编译生成IR

如果你已经有了链接好的可执行文件,可以用llvm-objcopy把它转换成LLVM IR:

# 先编译成静态链接的可执行文件
clang -static hello.cpp -o hello
# 提取.text段并转换为LLVM IR
llvm-objcopy --dump-section .text=hello_text.ll hello /dev/null

不过这种方法得到的IR是从机器码反编译回来的,结构可能和原始LLVM IR有差异,适合仅统计指令数的场景。


内容的提问来源于stack exchange,提问作者tapas1994

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 04:07:07