llvm-ir

LLVM tail call optimization

早过忘川 提交于 2019-12-05 10:58:22
Here is my understanding of things: A function "f" is tail recursive when calling itself is its last action. Tail-recursion can be significantly optimized by forming a loop instead of calling the function again; the function's parameters are updated in place, and the body is ran again. This is called recursive tail call optimization. LLVM implements recursive tail call optimization when using fastcc, GHC, or the HiPE calling convention. http://llvm.org/docs/CodeGenerator.html#tail-call-optimization I have some questions: Let's consider the silly example: int h(int x){ if (x <= 0) return x;

How to emit LLVM-IR from Cargo

我的未来我决定 提交于 2019-12-05 01:05:00
How can I get cargo to emit LLVM-IR instead of a binary for my project? I know that you can use the --emit=llvm-ir flag in rustc , but I've read some Github issues that show it's impossible to pass arbitrary compiler flags to cargo. Is there any way I can get cargo to emit LLVM-IR directly? Jacob There is cargo rustc to pass arbitrary compiler flags through Cargo to rustc . So I think: cargo rustc -- --emit=llvm-ir is what you want! EDIT: You should use Jacob's answer instead; a lot easier and less hacky. Build the project with cargo normally but add on the -v flag to show verbose output. The

Why is LLVM segfaulting when I try to emit object code?

故事扮演 提交于 2019-12-05 00:59:21
问题 I'm trying to follow along with the LLVM tutorial on compiler implementation, but my code segfaults when I try to emit object code. Here's a minimal example that attempts to compile a function func . To keep things simple, func is a function that does nothing. #include <iostream> #include <llvm/ADT/Optional.h> #include <llvm/IR/BasicBlock.h> #include <llvm/IR/DerivedTypes.h> #include <llvm/IR/Function.h> #include <llvm/IR/IRBuilder.h> #include <llvm/IR/LLVMContext.h> #include <llvm/IR

Possible to auto-generate llvm c++ api code from LLVM-IR?

心不动则不痛 提交于 2019-12-04 22:15:15
问题 The clang 3.0 online demo page http://llvm.org/demo/index.cgi provides an option to output LLVM C++ API code" representing the LLVM-IR for the input program. Is "produce LLVM C++ API code" output a clang option (and if so, what is it)? Or is it an llvm tool option (which one)? Is it possible to do the same thing but from LLVM-IR input? Basically I'd like to see the proper llvm c++ api calls needed to produce a particular given llvm-ir sequence. I'd like to learn backwards by example rather

OpenCL LLVM IR generation from Clang

邮差的信 提交于 2019-12-04 19:23:48
I am using the following command line for clang: clang -Dcl_clang_storage_class_specifiers -isystem $LIBCLC/generic/include -include clc/clc.h -target nvptx--nvidiacl -x cl some_kernel.cl -emit-llvm -S -o some_kernel.ll the result is: ; ModuleID = 'kernel.cl' target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64" target triple = "nvptx--nvidiacl" ; Function Attrs: noinline nounwind define void @vector_add(float addrspace(1)* nocapture %vec1, float addrspace(1)* nocapture %vec2, float addrspace(1)*

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:/

Get cpu cycles of LLVM IR using CostModel

跟風遠走 提交于 2019-12-04 12:31:55
问题 Since LLVM 3.0, there is CostModel.cpp under Analysis directory. Referring to its doc, it says This file defines the cost model analysis. It provides a very basic cost estimation for LLVM-IR. This analysis uses the services of the codegen to approximate the cost of any IR instruction when lowered to machine instructions. The cost results are unit-less and the cost number represents the throughput of the machine assuming that all loads hit the cache, all branches are predicted, etc. The cost

In LLVM IR, I want to copy a set of Instructions and paste those instructions to another place in IR through LLVM pass. How to do this?

柔情痞子 提交于 2019-12-04 12:20:45
I want to copy these set of instructions from one part and paste to that in another part in IR %0 = load i32, i32* @x, align 4 %1 = load i32, i32* @y, align 4 %add = add nsw i32 %0, %1 %2 = load i32, i32* @n, align 4 %cmp = icmp slt i32 %add, %2 %conv = zext i1 %cmp to i32 Assuming you are using C++ API, you will just have to clone each instruction separately while fixing references between them. Something like the following: llvm::ValueToValueMapTy vmap; for (auto *inst: instructions_to_clone) { auto *new_inst = inst->clone(); new_inst->insertBefore(insertion_pos); vmap[inst] = new_inst; llvm

Clang - Compiling a C header to LLVM IR/bitcode

天大地大妈咪最大 提交于 2019-12-04 08:57:14
问题 Say I have the following trivial C header file: // foo1.h typedef int foo; typedef struct { foo a; char const* b; } bar; bar baz(foo*, bar*, ...); My goal is to take this file, and produce an LLVM module that looks something like this : %struct.bar = type { i32, i8* } declare { i32, i8* } @baz(i32*, %struct.bar*, ...) In other words, convert a C .h file with declarations into the equivalent LLVM IR, including type resolution, macro expansion, and so on. Passing this through Clang to generate

generating CFG for whole source code with LLVM

三世轮回 提交于 2019-12-04 08:10:40
Does anyone from LLVM community know if there is a way to generate CFG for the whole input source code using opt -dot-cfg foo.ll(.bc) ? as this one generates the CFG per function thus the connections between functions will be ignored. It seems that the older analyze tool has depreciated. Nhome I wonder if you found any way to get interprocedural CFG. I found that inlining call functions by other inliner passes might be helpful but I couldn't be able to get it to work yet. I've posted this Finding all possible paths in a c/c++ program by LLVM 来源: https://stackoverflow.com/questions/26556356