llvm-ir | LLVM IR in natural Rust data structures | Compiler library

 by   cdisselkoen Rust Version: v0.8.1 License: MIT

kandi X-RAY | llvm-ir Summary

kandi X-RAY | llvm-ir Summary

llvm-ir is a Rust library typically used in Utilities, Compiler applications. llvm-ir has no bugs, it has no vulnerabilities, it has a Permissive License and it has low support. You can download it from GitHub.

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

            kandi-support Support

              llvm-ir has a low active ecosystem.
              It has 366 star(s) with 35 fork(s). There are 7 watchers for this library.
              OutlinedDot
              It had no major release in the last 6 months.
              There are 8 open issues and 14 have been closed. On average issues are closed in 20 days. There are 2 open pull requests and 0 closed requests.
              It has a neutral sentiment in the developer community.
              The latest version of llvm-ir is v0.8.1

            kandi-Quality Quality

              llvm-ir has 0 bugs and 0 code smells.

            kandi-Security Security

              llvm-ir has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              llvm-ir code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              llvm-ir is licensed under the MIT License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              llvm-ir releases are not available. You will need to build from source code and install.
              Installation instructions, examples and code snippets are available.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of llvm-ir
            Get all kandi verified functions for this library.

            llvm-ir Key Features

            No Key Features are available at this moment for llvm-ir.

            llvm-ir Examples and Code Snippets

            No Code Snippets are available at this moment for llvm-ir.

            Community Discussions

            QUESTION

            How to build llvm-hs from source
            Asked 2022-Apr-15 at 13:37

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

            Create 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.

            Source https://stackoverflow.com/questions/71880406

            QUESTION

            Why does gcc not compile emitted assembly by llvm?
            Asked 2022-Feb-21 at 17:13

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

            Never mind, i fixed it by installing clang/llvm 10, using WSL, sure, it's not the latest version, but it's better then nothing.

            Source https://stackoverflow.com/questions/71194638

            QUESTION

            Analyzing LLVM IR instruction
            Asked 2022-Jan-19 at 19:19

            I'm trying to analyze the following llvm-ir instruction:

            ...

            ANSWER

            Answered 2022-Jan-19 at 19:19

            Is 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.

            Source https://stackoverflow.com/questions/70652455

            QUESTION

            How to print floats from LLVM IR?
            Asked 2021-Oct-20 at 22:19

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

            On 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,

            Source https://stackoverflow.com/questions/69636349

            QUESTION

            How to manually provide core::panicking::panic* to lld?
            Asked 2021-Sep-05 at 02:55

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

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

            Source https://stackoverflow.com/questions/69055494

            QUESTION

            LLVM-IR Get Struct Element Pointer
            Asked 2021-Mar-24 at 10:05

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

            Okay. 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:

            Source https://stackoverflow.com/questions/66728399

            QUESTION

            How to use inline assembly using the llvm-c API
            Asked 2021-Feb-20 at 23:59

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

            The missing ingredient was:

            Source https://stackoverflow.com/questions/66285583

            QUESTION

            Creating a "class" definition using LLVM c++ API
            Asked 2020-Nov-01 at 12:13

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

            Looking 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.

            Source https://stackoverflow.com/questions/64631562

            QUESTION

            LLVM pass va_list to another function
            Asked 2020-Jul-09 at 14:27

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

            Your 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)).

            Source https://stackoverflow.com/questions/62803253

            QUESTION

            How to link a self written runtime when generating llvm-ir?
            Asked 2020-May-12 at 15:33

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

            You 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.

            Source https://stackoverflow.com/questions/61751290

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install llvm-ir

            This crate is on crates.io, so you can simply add it as a dependency in your Cargo.toml, selecting the feature corresponding to the LLVM version you want:. Currently, the supported LLVM versions are llvm-8, llvm-9, llvm-10, llvm-11, and llvm-12.

            Support

            Documentation for llvm-ir can be found on docs.rs, or of course you can generate local documentation with cargo doc --open. The documentation includes links to relevant parts of the LLVM documentation when appropriate. Note that some data structures differ slightly depending on your choice of LLVM version. The docs.rs documentation is generated with the llvm-12 feature; for other LLVM versions, you can get appropriate documentation with cargo doc --features=llvm-<x> --open where <x> is the LLVM version you're using.
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/cdisselkoen/llvm-ir.git

          • CLI

            gh repo clone cdisselkoen/llvm-ir

          • sshUrl

            git@github.com:cdisselkoen/llvm-ir.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link

            Explore Related Topics

            Consider Popular Compiler Libraries

            rust

            by rust-lang

            emscripten

            by emscripten-core

            zig

            by ziglang

            numba

            by numba

            kotlin-native

            by JetBrains

            Try Top Libraries by cdisselkoen

            pitchfork

            by cdisselkoenPython

            llvm-ir-analysis

            by cdisselkoenRust

            boolector-rs

            by cdisselkoenRust