llvm-ir | LLVM IR in natural Rust data structures | Compiler library
kandi X-RAY | llvm-ir Summary
kandi X-RAY | llvm-ir Summary
llvm-ir seeks to provide a Rust-y representation of LLVM IR. It's based on the idea that an LLVM Instruction shouldn't be an opaque datatype, but rather an enum with variants like Add, Call, and Store. Likewise, types like BasicBlock, Function, and Module should be Rust structs containing as much information as possible. Unlike other safe LLVM bindings such as inkwell, llvm-ir does not rely on continuous FFI to the LLVM API. It uses the LLVM API only for its initial parsing step, to pull in all the data it needs to construct its rich representation of LLVM IR. Once llvm-ir creates a Module data structure by parsing an LLVM file (using the excellent llvm-sys low-level LLVM bindings), it drops the LLVM FFI objects and makes no further FFI calls. This allows you to work with the resulting LLVM IR in pure safe Rust. llvm-ir is intended for consumption of LLVM IR, and not necessarily production of LLVM IR (yet). That is, it is aimed at program analysis and related applications which want to read and analyze LLVM IR. In the future, perhaps llvm-ir could be able to output its Modules back into LLVM files, or even send them directly to the LLVM library for compiling. If this interests you, contributions are welcome! (Or in the meantime, check out inkwell for a different safe interface for producing LLVM IR.) But if you're looking for a nice read-oriented representation of LLVM IR for working in pure Rust, that's exactly what llvm-ir can provide today.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of llvm-ir
llvm-ir Key Features
llvm-ir Examples and Code Snippets
Community Discussions
Trending Discussions on llvm-ir
QUESTION
I'm working on parsing large llvm-ir code using haskell & llvm-hs for some research work.
I used gllvm (comment on this post - Getting LLVM IR for a clang/clang++ project) to convert openssl (libssl) to .bc files and then to .ll using llvm-dis and parsing the .ll file using haskell & llvm-hs.
During this I'm facing this issue - https://github.com/llvm-hs/llvm-hs/issues/282#issue-511780377 -- Missing Function Attribute "NoFree" #282 & unhandled function attribute enum value: FunctionAttributeKind 26 which are basically edge cases not handled by llvm-hs in it's release but fixed in this commit - https://github.com/llvm-hs/llvm-hs/pull/284
initially I had installed llvm-hs using cabal install --lib llvm-hs
what I need help with is I'm not sure how to build it from source.
I cloned the llvm-hs repo and checked out to the commit with fix (or latest commit) of branch llvm-9 and added the path of this repo to the $PATH, how do i use cabal to build this from source ? I am working on a small project so it doesn't have a project file or a .cabal file
Please help!
...ANSWER
Answered 2022-Apr-15 at 13:37Create a cabal project that depends one llvm-hs and use https://cabal.readthedocs.io/en/3.6/cabal-project.html#specifying-packages-from-remote-version-control-locations to specify the commit.
Note that since that PR is pulling from a fork, you will use the URL of the fork repository, not the main repository.
QUESTION
I have a hello world program in C, then i compiled it to llvm-ir using clang, then compiled that llvm-ir to assembly, however, GCC doesn't compile the assembly code to an exe file, i have been trying to fix this problem for a week, but i don't know why it doesn't work, here is all the code: hello.c:
...ANSWER
Answered 2022-Feb-21 at 17:13Never mind, i fixed it by installing clang/llvm 10, using WSL, sure, it's not the latest version, but it's better then nothing.
QUESTION
I'm trying to analyze the following llvm-ir instruction:
...ANSWER
Answered 2022-Jan-19 at 19:19Is it a pointer to an empty struct ?
Yup. Somewhat like a void pointer. void*
is not a valid type in LLVM IR, most frontends use i8*
for this purpose.
What does this instruction do ?
It's an indirect call. That first {}*
is the return type of the callee (and guaranteed to be a nonnull
pointer at that!), the callee is the value i64 140019712366044
casted to pointer to function that returns {}*
and takes two parameters, {}*
and i64
. Then for the first parameter, it passes this function i64 140019153768096
casted to {}*
and 0 as the second parameter.
Those constant numbers suggest that this is a JIT and those are pointers to things outside this LLVM IR module.
QUESTION
I have a problem using the printf()
function from within LLVM IR. I just want to print the value of a float but I just get 0.000000
. It works fine for integers and strings but not floats.
Simple example:
...ANSWER
Answered 2021-Oct-20 at 22:19On running your example using lli-10
I get a
@printf' defined with type 'i32 (i8*, ...)*' but expected 'i32 (i8*, double)*'
error rather than a successful call that prints 0.000000.
This error is expected, and the reason why is explained here. The relevant bit from the post is
... if the type in front of the call is a function pointer type, then that is used as the type of the thing being called, while if it's not, then it is used as the return type, and the type of the call is inferred from the arguments present on the instruction.
That is why LLVM infers printf
's type in the first call as i32 (i8*, double)*
but later finds out it's declared as i32 (i8*, ...)*
. To fix this simply annotate the calls properly,
QUESTION
I am compiling the Rust code of an rlib to LLVM IR, and then using Clang to compile & link it with a C program. This works until my code contains panics, at which point I get linker errors:
...ANSWER
Answered 2021-Sep-05 at 02:55Based on this answer, I worked around this by passing -Z build-std
to Cargo, thereby getting the LLVM IR from Rust's core
library, and linking it in.
But then I realized I can do one better, and avoid the long compilation time imposed by -Z std
, by just taking the definitions of
core::panicking::panic_bounds_check
and core::panicking::panic
from these IR files, simplifying their body, and adding them to a hand-written panic.ll
file:
QUESTION
I'm building LLVM-IR with the C API and have a pointer to the following struct type:
...ANSWER
Answered 2021-Mar-24 at 10:05Okay. So, answering here. As you see, you need to first get zero index, after than indexes 7 and 0. The reason is simple to explain. Let's see an example why extra zero is needed:
QUESTION
I can not figure out how to piece everything together.
I want to generate llvm-ir for the following "C" instruction:
...ANSWER
Answered 2021-Feb-20 at 23:59The missing ingredient was:
QUESTION
I am working on a custom front-end language using the LLVM c++ api and am adding a "class" definition construct to the language. The lexer and parser are written in C++ and LLVM c++ api is being used to generate IR code and compile to different machine backends. I can already define functions and variables, call functions and evaluate basic arithmetic with the custom front-end language.
In order to do add a "class" definition to the language, I am following the suggestion on https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/object-oriented-constructs/classes.html
This suggests that we create a "structtype" to contain all the data members of the class and separately define a bunch of functions for the methods of the class (if I understand the link correctly).
Thus in my compiler code, I defined an AST node called "ClassAST" which contains the names and types for data members of the class and a collection of function definitions for the methods for the "class" being defined in the front-end language. The c++ code snippet for the AST node is as follows:
...ANSWER
Answered 2020-Nov-01 at 12:13Looking at the source code of Module::getTypeByName
, it looks up the name in the LLVMContext
instance. This means you don't have to add your type to the module and name lookup should work just fine, even through a Module
instance.
QUESTION
I've been using LLVM for quite some time now and I solved problems myself by searching and using clang to output the generated code, but I could never solve this problem. I'm currently developing a compiler with my custom frontend and LLVM as backend. Since I wanted a custom println function that works exactly like the printf function (varadic argument formatting), I tried to implement it using the instrisic @llvm.va_start and @llvm.va_end.
The problem now is that everything compiles well, but when I run the program, it shows me strange numbers instead of the real arguments that are used, for example:
Input:
...ANSWER
Answered 2020-Jul-09 at 09:08Your main problem looks to be that you're returning a pointer to alloca
-allocated memory (i.e. local memory) from @va_start
. You should either make it take a pointer as an argument like @llvm.va_start
does or get rid of the function altogether and call @llvm.va_start
directly from @println
.
PS: I don't understand what the point of your %0
type is. If it's supposed to represent an argument list, why not use i8*
(which is what the LLVM vararg functions expect) directly instead of bitcasting it.
PPS: The format string and number of arguments in your source code doesn't match those in your generated LLVM. I'm guessing the LLVM was actually generated from a different source code (specifically println("Hello World %i?", 1)
).
QUESTION
I'm currently working on a scheme compiler. At the moment im writing the runtime with the help of Boehm GC, with the goal to be able to call the functions from llvm-ir later on. What is the best way to link everything together in the end?
Currently I am building a static libary out of my C++ runtime which uses extern "C" in order to prevent name mangling.
Are there good examples which demonstrate how to do it?
Thanks in advance
...ANSWER
Answered 2020-May-12 at 15:33You can generate llvm-ir for your static library using clang++. Then link that with your compiler's output (i.e the llvm-ir from which you intend to call the library functions) using llvm-link
. And you've got your final ir.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install llvm-ir
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page