- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在玩具编译器(第一次)上取得进展,并试图了解如何分配/构造 LLVM 结构类型。Kaleidoscope 教程没有包括甚至没有提到这一点,我不知道我在 LLVM 源/测试中寻找什么来找到可能的例子。
所以我写了一个简单的 C++ 示例,用 clang 转储了 IR,试图理解它产生了什么,但老实说,我并没有完全遵循它。对我来说显而易见的是函数定义/声明和一些函数调用以及 memset
调用,所以我得到了它的一部分,但它还没有全部组合在一起。 (P.S 我对 alloca 指令文档的解释是它创建的任何东西在返回时都会被释放所以我不能使用那个权利,它基本上只用于局部变量?)
我所做的是:
alloc.cpp
struct Alloc {
int age;
};
//Alloc allocCpy() {
// return *new Alloc();
//}
Alloc *allocPtr() {
return new Alloc();
}
int main() {
Alloc *ptr = allocPtr();
// ptr->name = "Courtney";
// Alloc cpy = allocCpy();
// cpy.name = "Robinson";
// std::cout << ptr->name << std::endl;
// std::cout << cpy.name << std::endl;
return 0;
}
然后运行clang -S -emit-llvm alloc.cpp
生成alloc.ll
; ModuleID = 'alloc.cpp'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.11.0"
%struct.Alloc = type { i32 }
; Function Attrs: ssp uwtable
define %struct.Alloc* @_Z8allocPtrv() #0 {
entry:
%call = call noalias i8* @_Znwm(i64 4) #3
%0 = bitcast i8* %call to %struct.Alloc*
%1 = bitcast %struct.Alloc* %0 to i8*
call void @llvm.memset.p0i8.i64(i8* %1, i8 0, i64 4, i32 4, i1 false)
ret %struct.Alloc* %0
}
; Function Attrs: nobuiltin
declare noalias i8* @_Znwm(i64) #1
; Function Attrs: nounwind
declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) #2
; Function Attrs: ssp uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%ptr = alloca %struct.Alloc*, align 8
store i32 0, i32* %retval
%call = call %struct.Alloc* @_Z8allocPtrv()
store %struct.Alloc* %call, %struct.Alloc** %ptr, align 8
ret i32 0
}
attributes #0 = { ssp uwtable "disable-tail-calls"="false" "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" "target-cpu"="core2" "target-features"="+cx16,+sse,+sse2,+sse3,+ssse3" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nobuiltin "disable-tail-calls"="false" "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" "target-cpu"="core2" "target-features"="+cx16,+sse,+sse2,+sse3,+ssse3" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nounwind }
attributes #3 = { builtin }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"PIC Level", i32 2}
!1 = !{!"clang version 3.7.0 (tags/RELEASE_370/final)"}
有人能解释一下这个 IR 中发生了什么以及它如何映射回 C++ 吗?或者忽略这个具体的例子,人们会/应该如何为一个 LLVM StructType 分配堆内存,这个 LLVM StructType 存在于创建它的函数中(如果你觉得慷慨,以后如何释放内存)。
我注释掉的部分来 self 原来的例子,但作为一个完全的新手,IR 的洞察力甚至更差......
最佳答案
my interpretation of the alloca instruction docs is that it anything created from that gets freed on return so I can't use that right, it's essentially only for local variables?
是的。此外,目前关于 LLVM IR 的建议是,尽管 alloca
可以按您预期的那样工作,但优化是另一种情况。他们建议您立即alloca
入口 block 中的所有本地人,即使您不允许用户访问它们或它们并不总是包含有意义的数据。
堆分配是一个库特性。它不是 LLVM 或编译器的特性。当您使用 new T()
时,编译器只需调用 operator new
来获取内存,然后在那里构造 T
。没有魔法参与。你看到的大多数垃圾都是特定于 C++-ABI 的,而不是 LLVM 的任何要求。它最终会降低为类似 void* p = malloc(size);新(p)T();
。对于几乎所有类型 T
,这几乎可以归结为将一系列存储到 p
或调用用户定义的函数。
您可以使用您选择的运行时库中的内存分配函数。
trying to understand how to allocate/construct an LLVM struct type
LLVM 类型系统不包含构造的概念。这是源语言的概念。
就 LLVM 而言,结构只是一堆位,所有内存位置或多或少都相同。如果您希望这些位是特定的东西,请将您想要的位存储到该位置。如果要将这些位放在堆上,则调用运行时库堆分配函数并将这些位存储到该位置。
请注意,垃圾回收是一个稍微不同的故事,因为 w.r.t 发生了一些尴尬的事情。在堆栈中查找局部变量以进行标记。
郑重声明,您不会试图理解 Clang 的 LLVM IR。我已经这样做了好几年了,这太疯狂了,你会花那么长时间才能开始掌握,更不用说你不想知道的所有 C++ 特定的 ABI 细节了。与尝试对其进行逆向工程相比,您会在他们的 IRC channel 的 #llvm 中进一步询问或在这里询问具体问题。
关于c++ - 了解内存分配的工作原理 (LLVM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533608/
我指的是 https://llvm.org/docs/GettingStarted.html从其源代码构建 LLVM。我正在使用 Ubuntu 18.04。 $ cmake -G Ninja -DCM
使用 ModulePass,我的目标是向上遍历 SSA 图:从一个具有 0..2 个操作数的语句(大多数操作码属于该语句)开始,我想找出两件事: 操作数是元数据/常量(简单:只需尝试转换为常量类型)还
注意:我注意到 this question与这个问题有很大关系,所以如果您对我的问题感兴趣,那么您一定要阅读另一个问题及其答案。 我可以想到一些 OOP 语言前端可以做的优化,例如创建临时变量来保存来
我正在尝试使用 clang-3.2 创建自动矢量化代码 从这里的幻灯片 - http://llvm.org/devmtg/2012-04-12/Slides/Hal_Finkel.pdf 我应该能够使
我想问的是,我们可以将现有pass生成的信息转化为新pass吗? 如果是,那么如何? 例如 -loops 给出了自然循环信息,所以我们可以通过调用它来将这些信息用于新的传递。 最佳答案 您可以通过覆盖
在 LLVM IR 中,当我想从一个数组中获取一个值时,似乎有三种方法可以做到这一点:使用 extractvalue、使用 extractelement 和使用 getelementptr 然后加载。
我想逐步介绍一下我生成的LLVM IR代码。就llc和lli而言,该代码在语法上是有效的并且类型有效,但是结果不是我所期望的。 这些块足够大,以至于我无法简单地读取该错误就无法成功完成,并且我很难制作
我想弄清楚如何使用 trampoline intrinsics在 LLVM 中。该文档提到了存储蹦床所需的一些存储量,这取决于平台。我的问题是,我如何确定需要多少? 我找到了 this example
我需要使用抽象解释来使用 LLVM 进行一些分析。这可能吗?或者我需要更轻松地使用分析工具。如果我可以通过 LLVM 做到这一点,哪些类将帮助我从原始源代码中制定语句以获取变量之间的关系(以及每个变量
我正在创建一种静态编译的编程语言,并使用 LLVM 作为其后端。我希望我的语言在发生整数溢出时陷入/崩溃。 我知道类似 llvm.sadd.with.overflow 的事情,但我认为这不是最佳/有效
我正在尝试学习 LLVM tablegen。 http://llvm.org/docs/TableGen/LangRef.html表明 field关键字存在但不解释其含义。有人知道这是什么意思吗? 我
Fibonacci LLVM 示例使用 errs() getIR() ) 我一直在搜索 llvm::Module Class Reference并没有得到任何帮助。 Fibonacci.cpp 的一部
我想在 llvm IR 中找到对 llvm.pow.f64 函数的所有函数调用。请建议我一个方法来做到这一点。 最佳答案 嗯,这是一个基本的 FunctionPass找到所有对函数的调用: class
我正在尝试从 llvm 中间代码中删除未使用的 block 。 首先,我通过从入口 basicblock 开始并从它们的终止符指令添加所有 basicblock 后继者来构建可访问的 basicblo
我想获取 llvm 中每条指令的后继列表。如果我没理解错的话,对于除了branch(br)之外的所有指令,后继指令就是下一条。但是对于分支指令来说有点棘手。 例如,如果我有以下 C 代码: int m
我有文本格式的 LLVM IR 代码。我想做的是能够解析它并修改该代码。是否有可以帮助解析 LLVM IR 代码的 API?我的系统中应该有哪些库?此刻我有clang编译器也安装了 LLVM,因为我可
(1) @str = private constant [13 x i8] c"Hello World\0A\00" (2) define i32 @main(){ (3) %r2 = getelem
如果我有一组基本块和边,我需要为它们创建一个具有新入口和端点的新函数。 我可以直接在 LLVM 中创建它吗,就像 createFunction(F) 一样然后 F.insert(bb, edges)其
我通过教程使用 LLVM http://llvm.org/releases/3.1/docs/tutorial/ 编写玩具编译器 但是关于符号表处理的内容并不多。 有一个命令 llvm-nm 显示符号
我希望编写一个 LLVM 传递两个 i32 类型的参数来选择函数。我的第一次尝试(如下所示)失败了: bool MyFunctionPass::runOnFunction(Function &f) {
我是一名优秀的程序员,十分优秀!