- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经使用 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/
或者存在像 这样的指针和引用C ? 我正在尝试开始使用 vala,但很高兴知道 vala 是“按引用传递”还是“按值传递” 最佳答案 首先你应该明白默认的vala编译器valac编译为 C(作为一种中
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我确实对 crypt() PHP 函数感到困惑。 当第二个 crypt 显然使用不同的第二个参数时,以下两个 crypt 函数如何给出相同的输出?差异盐意味着差异哈希对吗? echo crypt("p
我正在尝试在方案中模拟堆栈。我正在使用 DrScheme 并选择语言 R5RS。我需要创建 pop、push 和 peek 的函数。但我无法弄清楚如何通过引用传递。我已经阅读了一些关于盒子的信息,但是
我已经查过维基百科并用 google 搜索过,但我仍然无法理解 ALGOL 60 中的按名称传递的工作原理。 最佳答案 我在 Pass-By-Name Parameter Passing 找到了很好的
问题:我想知道在 Excel 2003 VBA 中处理数组的最佳解决方案是什么 背景:我在 Excel 2003 中有一个超过 5000 行的宏。我在过去 2 年中构建了它,将新功能添加为新过程,这有
我正在尝试反转位图数组,而不修改源数组。但问题是源数组也被颠倒了。我做错了什么还是我应该以其他方式做?感谢您的帮助。 private GalleryAdapter galleryAdapter; pr
因此,关于按引用传递/按值传递,方法如何处理参数传递已有详细记录,但是变量赋值又如何呢? 例如,我刚刚编写了一些如下所示的代码: TreeNode parent = null; TreeNode cu
我正在编写一个脚本,它将从我们的星号系统中回填调用详细记录到我们的 MySQL 日志数据库中。在下面的代码中,我试图忽略重复键并继续到下一行,但是当这段代码执行时,我所看到的只是第一行欺骗警告,然后脚
我从一个 Action (executeProcess)重定向到另一个(executeIndex)。我希望能够不使用GET传递参数/变量(例如$this->redirect('index', arra
我经常看到方法接口(interface)的两种相互冲突的策略,大致概括如下: // Form 1: Pass in an object. double calculateTaxesOwed(TaxFo
目前正在学习回调在 JavaScript 中的含义,我正在努力理解回调是如何工作的,以及术语“passing this”或“passing that”的来源以及它如何与回调一起工作?我想展示一些示例代
我通过 URL 传递参数并且页面被正确重定向。如何访问下一个(重定向的)页面上(从上一页)传递并在 URL 中可用的参数?谁能告诉我该怎么做? 或者有没有其他的方式在页面之间传递参数? 是否有类似 A
我通过 URL 传递参数并且页面被正确重定向。如何访问下一个(重定向的)页面上(从上一页)传递并在 URL 中可用的参数?谁能告诉我该怎么做? 或者有没有其他的方式在页面之间传递参数? 是否有类似 A
按引用传递和按名称传递的参数传递模式有什么区别这里是 Python 中的一个示例,但假设我们不使用任何 Python 规则: def P(x,y) global i y=1 prin
我正在编写自己的 LLVM pass,它修改了 LLVM 位码。在生成位码时,我想禁用函数内联,但是当我完成对位码的修改后,我想调用执行函数内联的传递。这个可以吗。如果是,如何? 为了更好地理解我在说
我有一个模板函数,负责将模板值写入流。它看起来像这样: template void Write( T value, std::ostream& stream, endianness_t endian
我一直在使用两个程序 llvm 的 opt 和 clifford wolf 的 yosys两者都有类似的通行证接口(interface)。(他们使用共享库作为优化 passes ) 我想根据我的 ll
在我们使用引用传递的 C++ 中,我们引用了我们从参数传递给函数参数的地址,它本质上是一个指针,对吗?因此,虽然它们本质上是相同的东西,别名和所有,但指针不也需要内存空间吗?因此,无论我们在参数函数中
这是一个关于 64 位整数的效率问题。假设我不需要修改“int”参数的值,我应该通过值还是引用传递它。 假设是 32 位机: 1) 32 位 int:我猜答案是“按值传递”,因为“按引用传递”会产生额
我是一名优秀的程序员,十分优秀!