gpt4 book ai didi

c++ - 使用 LLVM pass 添加内在函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:57 26 4
gpt4 key购买 nike

我已经使用 LLVM 传递向输入代码添加了一个内在函数。我能够看到内部调用,但我无法弄清楚如何将代码编译到我的目标架构 (x86_64)。我正在运行以下命令:

clang++ $(llvm-config --ldflags --libs all) ff.s -o foo

但是链接器提示 undefined reference :

/tmp/ff-2ada42.o: In function `fact(unsigned int)':
/home/rubens/Desktop/ff.cpp:9: undefined reference to `llvm.x86.sse3.mwait.i32.i32'
/tmp/ff-2ada42.o: In function `fib(unsigned int)':
/home/rubens/Desktop/ff.cpp:16: undefined reference to `llvm.x86.sse3.mwait.i32.i32'
/home/rubens/Desktop/ff.cpp:16: undefined reference to `llvm.x86.sse3.mwait.i32.i32'
/home/rubens/Desktop/ff.cpp:16: undefined reference to `llvm.x86.sse3.mwait.i32.i32'

尽管使用了来自 llvm-config 的 ldflags,但编译并未继续。关于应该如何正确编译代码的任何想法?

为了生成汇编代码,我做了以下工作:

# Generating optimized code
clang++ $(llvm-config --cxxflags) -emit-llvm -c ff.cpp -o ff.bc
opt ff.bc -load path/to/mypass.so -mypass > opt_ff.bc

# Generating assembly
llc opt_ff.bc -o ff.s

我目前使用的是 llvm 版本 3.4.2; clang 版本 3.4.2(标签/RELEASE_34/dot2-final); gcc 版本 4.9.2 (GCC);和 Linux 3.17.2-1-ARCH x86_64。


编辑:使用内在函数添加 IR:

文件 ~/llvm/include/llvm/IR/IntrinsicsX86.td:

...
589 // Thread synchronization ops.
590 let TargetPrefix = "x86" in { // All intrinsics start with "llvm.x86.".
591 def int_x86_sse3_monitor : GCCBuiltin<"__builtin_ia32_monitor">,
592 Intrinsic<[], [llvm_ptr_ty,
593 llvm_i32_ty, llvm_i32_ty], []>;
594 def int_x86_sse3_mwait : GCCBuiltin<"__builtin_ia32_mwait">,
595 Intrinsic<[], [llvm_i32_ty,
596 llvm_i32_ty], []>;
597 }
...

并调用(来自文件 ff.s):

...
.Ltmp2:
callq llvm.x86.sse3.mwait.i32.i32
movl $_ZStL8__ioinit, %edi
callq _ZNSt8ios_base4InitC1Ev
movl $_ZNSt8ios_base4InitD1Ev, %edi
movl $_ZStL8__ioinit, %esi
movl $__dso_handle, %edx
callq __cxa_atexit
popq %rax
ret
...

编辑 2:这是我在 opt pass 期间添加内在函数的方式:

Function *f(bb->getParent());
Module *m(f->getParent());

std::vector<Type *> types(2, Type::getInt32Ty(getGlobalContext()));
Function *mwait = Intrinsic::getDeclaration(m, Intrinsic::x86_sse3_mwait, types);

std::vector<Value *> args;
IRBuilder<> builder(&bb->front());
for (uint32_t i : {1, 2}) args.push_back(builder.getInt32(i));

ArrayRef<Value *> args_ref(args);
builder.CreateCall(mwait, args_ref);

最佳答案

编辑:我目前正在编写一个 LLVM pass,它基本上正在做你在这个问题中试图做的事情。您的代码存在以下问题:

std::vector<Type *> types(2, Type::getInt32Ty(getGlobalContext()));
Function *mwait = Intrinsic::getDeclaration(m, Intrinsic::x86_sse3_mwait, types);

您正在尝试获取名称为 llvm.x86.sse3.mwait.i32.i32 的内部函数的减速,但此内部函数不存在。但是,llvm.x86.sse3.mwait 存在,因此您必须这样写:

Function *mwait = Intrinsic::getDeclaration(m, Intrinsic::x86_sse3_mwait);

注意调用中缺少类型参数。这是因为 llvm.x86.sse3.mwait 没有重载。

我希望你能同时解决这个问题。


好的,因为我想暂时回答您的问题,这里是一个大胆的猜测答案。

问题在于您通过优化器传递添加内在函数的方式。看起来您只是在创建一个与内在函数同名的函数,而不是内在函数本身。

这里有一小段 C++ 代码,它只使用内置的 clang 来获取 IR 中的内在函数(我使用 clang 3.5,但这应该不会有任何影响)。

int main ()
{
__builtin_ia32_mwait(4,2);
}

clang -emit-llvm -S 编译它我得到:

; ModuleID = 'intrin.cpp'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
call void @llvm.x86.sse3.mwait(i32 4, i32 2)
ret i32 0
}

; Function Attrs: nounwind
declare void @llvm.x86.sse3.mwait(i32, i32) #1

attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind }

!llvm.ident = !{!0}

!0 = metadata !{metadata !"clang version 3.5.0 "}

请注意,SSE3 内在函数没有像您的版本中那样的类型重载。

在生成的文件上使用 llc 为我提供:

.Ltmp2:
.cfi_def_cfa_register %rbp
movl $4, %ecx
movl $2, %eax
mwait
xorl %eax, %eax
popq %rbp
retq

已创建正确的程序集。

所以我假设您在 opt pass 中将内在函数引入函数的方式是错误的。

获取内部函数并调用它:

vector<Type*> types;
types.push_back(IntegerType::get(/*LLVM context*/, 32));
types.push_back(IntegerType::get(/*LLVM context*/, 32));

Function* func = Intrinsic::getDeclaration(/* module */, Intrinsic::x86_sse3_mwait, types);
CallInst* call = CallInst::Create(func, /* arguments */);

关于c++ - 使用 LLVM pass 添加内在函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27569967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com