llvm

Convert std::string to llvm::MemoryBuffer

巧了我就是萌 提交于 2019-12-04 18:02:58
I am looking to create an LLVM Module from existing LLVM IR code. The two methods I have found are the following: ParseIRFile - This accepts a file name and generates a module ParseIR - This accepts MemoryBuffer and generates a module I want to create a Module when the LLVM IR is already read to a string as an std::string or const char * . Is there a way to convert an IR string to llvm::MemoryBuffer ? I figured this out with the help of a colleague. This is how you would do it: std::string IRString = readfile("add.ll"); MemoryBuffer *mem = MemoryBuffer::getMemBuffer(IRString); 来源: https:/

七天LLVM零基础入门(Linux版本)------第二天

混江龙づ霸主 提交于 2019-12-04 17:04:09
作者: snsn1984 在第一天的时候,我们简单了解了llvm和clang。今天我们继续对LLVM进行学习。 第一步:学会如何查找LLVM的文档 LLVM的文档很多,也很全面,是开源软件中文档比较全面并且比较细致的,这是很难得的一个事情。所以只要和LLVM打交道,就一定要学会并且善于去使用这些LLVM的文档。 因为LLVM文档比较多,初学者可能不太容易搞清楚,我之前写过一个关于LLVM的文档的博文,可以参考: http://blog.csdn.net/snsn1984/article/details/8165529 这个博文把文档的地址和文档的分类进行了简单介绍。 第二步:熟悉LLVM的IR IR是 intermediate representation 的缩写,顾名思义是中间表示的的缩写。中间表示已经被越来越多的编译器所采用,传统的编译器多采用汇编语言作为自己的中间语言,而现在大一些的编译器都有了自己专属的中间表示。LLVM IR的官方文档地址: http://llvm.org/docs/LangRef.html 简要介绍可参加我之前的博文: http://blog.csdn.net/snsn1984/article/details/8037414 LLVM IR是整个LLVM框架中极其重要的一部分,一般与LLVM相关的项目都无法避开LLVM IR这个部分

七天LLVM零基础入门(Linux版本)------第五天

我是研究僧i 提交于 2019-12-04 17:03:55
作者: snsn1984 第一步:复习文档 Write an LLVM pass http://llvm.org/docs/WritingAnLLVMPass.html 第二步:阅读LLVM编程规范 http://llvm.org/docs/CodingStandards.html 编程规范是编程中需要注意的基础点,代码风格符合不符合规范,将会直接影响整个代码的可读性和代码质量。 所以一定要认真阅读,并且在自己编写代码的过程中要遵守规范。最容易犯错误的两点就是缩进和空格。不同 的编程规范的缩进是不同的,但是对TAB的限制是大多数规范都有的。任何不是必须的空格,都是不应该出现 的。 第三步:参照LLVM编程规范写Pass例子 编写一个Pass,删除掉程序中所有的没有使用的指令。比如: %2 = add i32 %1, 1; %3 = add i32 %2, 1; ret; 那么因为%3并没有被使用,所以指令%3 = add i32 %2, 1; 就可以被删除了,删除之后,如果%2没有别的地方使用, 那么这条语句也可以删除掉了。依次循环下去,最后得到的是一个没有无用的代码的程序。 延伸阅读: http://en.wikipedia.org/wiki/Dead_code_elimination ------------------------------------------

七天LLVM零基础入门(Linux版本)------第六天

天涯浪子 提交于 2019-12-04 17:03:42
作者: snsn1984 第一步:对优化代码的pass继续进行深入的分析 首先检查一下第五天中的Pass练习,什么情况下可以进行优化,什么情况下不可以进行优化。 可以参照系统自带的Pass: http://llvm.org/docs/doxygen/html/DCE_8cpp_source.html 需要将判断是否需要优化的主要函数 isInstructionTriviallyDead (I, TLI) 进行深入的分析。函数的具体内容位置: http://llvm.org/docs/doxygen/html/Local_8cpp_source.html#l00269 将第五天要求的pass进一步进行完善。 第二步:学习LLVM Test 参照LLVM文档: http://llvm.org/docs/TestingGuide.html 此处需要注意的是,在llvm的build目录下,在已经运行make成功的情况下,直接进行make check,可以运行llvm 自带的所有测试。 此处可参考我的一个博文: http://blog.csdn.net/snsn1984/article/details/8617068 第三步:写一个测试用例 写一个测试程序输出的测试用例。 可以测试最简单的hello world程序,测试该程序的输出的是不是程序本身期望输出的“hello world”。

七天LLVM零基础入门(Linux版本)------第七天

断了今生、忘了曾经 提交于 2019-12-04 17:03:32
作者: snsn1984 这是这一次零基础入门的最后一天,这次的主要任务是实战一个Pass。 任务描述: 实现一个Pass,该Pass可以读取一个程序中所有的for循环的循环头的名字并输出。 同时,实现一个测试用例去测试输出的结果是否正确。该Pass的输入应该包含嵌套 循环。 提示: An llvm::FunctionPass using llvm::LoopInfo LoopInfo &LI=getAnalysis<LoopInfo>() //In runOnFunction getAnalysisUsage(AnalysisUsage &AU) //In structure std::vector<Loop*> workList(LI.begin(), LI.end()); 在这个任务完成之后,应该已经达到可以达到可以分析和实现LLVM的Pass,查找和 分析LLVM的源码,查找需要使用的文档,并且可以知道下一步该如何自己学习LLVM。 -------------------------------------转载请注明出处---------------------------------------------------- 来源: oschina 链接: https://my.oschina.net/u/860439/blog/110810

七天LLVM零基础入门(Linux版本)------总结

岁酱吖の 提交于 2019-12-04 17:03:18
七天LLVM零基础入门这系列文章,为了让刚接触LLVM的人在最短的时间内快速的熟悉LLVM。这系列文章,现在经过将近一年的时间的使用,有了不少反馈的意见,现在将这些情况总结一下。 1. 在按照这个七天的学习计划进行的学习过程中,不少人出现了延误现象,主要体现在读文档的时候,读的过于仔细,然后感觉时间不够用。虽然在文档上花了很长时 间,但是到最后该用文档中的东西的时候,反而无从下手。出现这个问题,主要还是不会把握文档的重点,这个系列中列出来的文档,在日后接触LLVM的过程 中,需要不断的去读,反复的去读。不是读一遍就可以完全掌握的,在最开始读的时候首先要熟悉文档的主要结构,了解文档里都覆盖了哪些内容,然后在用的时候 可以知道在哪里能找到,这就达到了目的。 特别是针对刚毕业的学生,特别容易对文档抠的太仔细,全是新东西,花了很长时间看完了,回头发现什么也没记住。这就导致既花费了时间,又没有效果,不是在 公司实际工作中该出现的,在工作中学习东西就是为了更快的使用,不是为了考试,所以如果读完了没有收获,那么等于就是白读了。 2. 这个系列中第五天布置了一个练习,在第六天的时候给出了一个官方的例子。这个最初在刚开始的时候,是我每天一篇写出来,所以不存在什么问题。现在把整个系 列都发布了,有些让学习这个系列的人就延续了在学校时候的做法,如果有答案是完全不动脑子的

Linker error while building clang using Makefile with a checker

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 16:56:21
I'm facing a problem during the linking phase. What am I trying to do? I'm trying to add a checker to clang. I built LLVM and libc++ from the source code (Yes, some test failed during libcxx testing after build). To identify the libc++ library, I added the following code to the Clang's root directory Makefile CXX.Flags += -stdlib=libc++ CXX.Flags += -std=c++11 CXX.Flags += -nostdinc++ CXX.Flags += -I/path/to/my/own/c++library/libcxx/include LD.Flags += -L/path/to/my/own/c++library/libcxx/lib The linker problem surfaced as following. Undefined symbols for architecture x86_64: "std::__1::_

How can I code generate unused declarations with Clang?

流过昼夜 提交于 2019-12-04 16:44:59
I'm looking to code generate an llvm::Module from some C++ code. Specifically, I will be mixing code generated by Clang and code from another source. Unfortunately, Clang's CodeGenModule class appears to insist on only generating a declaration into the Module if there is a definition which uses it. How can I convince the CodeGenModule to generate these declarations anyway? Puppy Apparently, -femit-all-decls does not actually emit all the decls, so I had to modify Clang's source code to actually emit all the decls. 来源: https://stackoverflow.com/questions/14032496/how-can-i-code-generate-unused

CMake build of LLVM clang fails with “Unexpected failure executing llvm-build: Traceback (…) import llvmbuild”

假如想象 提交于 2019-12-04 16:34:25
问题 I want to build LLVM clang compiler, but CMake ends up with the following error message: CMake Error at CMakeLists.txt:256 (message): Unexpected failure executing llvm-build: Traceback (most recent call last): File "C:/.../llvm/utils/llvm-build/llvm-build", line 3, in <module> import llvmbuild File "C:\...\llvm\utils\llvm-build\llvmbuild\__init__.py", line 1, in <module> from main import main ImportError: No module named main -- Configuring incomplete, errors occurred! I've got all needed

Using the LLVM linker to produce C code

帅比萌擦擦* 提交于 2019-12-04 16:33:13
I have tried to generate C code from C++ code compiled by llvm-g++ , using the following commands: llvm-g++ -emit-llvm -c ./example.cpp -o example.o llc -march=c example.o I tried these commands on a machine running Ubuntu (Linux 3.16.0-45-generic) . However instead of writing C code to standard output, the LLVM static linker reports that the compiled file is invalid: error: expected top-level entity . How can I generate C code using the LLVM linker? mjturner Unfortunately emitting LLVM bitcode on Ubuntu systems can be a bit painful because they ship DragonEgg as the default frontend - see