Is it possible to find the LLVM version from textual LLVM IR .ll file? - llvm

I have several .ll files containing LLVM IR code in textual form. I want to filter the files depending on their LLVM version for example I would like to find all the files that use LLVM version 3-7.
Currently, I have tried to convert .ll file to .bc file using llvm-as tool and tried using llvm-bcanalyzer to get some useful information such as the required version number but it seems that I was mistaken and llvm-bcanalyzer does not provide such information.
So is there any way to find out which version of LLVM was used to write a given .ll file?

Actually, the version of the entire LLVM toolchain will be same as version of the clang used to generate the .ll file, as clang is a part of LLVM toolchain only.
Just open the .ll file and search clang version and you will be able to find something like clang version 12.0.0

Related

Is it possible to compile LLIR to binary without clang?

I'm writing a compiler that embeds the LLVM API. By copying code from the llc tool, I can output assembly language or object files that I can turn into binaries using clang or an assembler.
But I want my compiler to be self contained. Is it possible to turn LLIR into binaries using LLVM? This seems like the sort of thing that should be in the LLVM toolkit.
Yes, it is possible and this is also done by llc with -filetype=obj argument.
You can consult the compileModule function to learn how to use the programmatic API.
Note that this will only generate an object file for a given translation unit. You will also need a linker to convert it into a proper executable or library. The LLVM linker, lld, can also be embedded into client applications as a library, so in the end you will be able to create a self-hosting compiler.

LLVM IR bitcode file versioning

I have LLVM IR files in textual form that were compiled targeting different LLVM versions. In my LLVM IR interpreter I want to identify which parser I should select to parse such a file, since the textual format is not backward compatible. However, I cannot see a version ID in these files.
If I instead change my parsers to parse the bit code files, I have longer compatibility guarantees. There is also a version field but the documentation mentions that it is currently always 0.
So is there any way to identify the LLVM version of a bit code file, or do I have no other choice than letting the user specify the version?

How to parse PNaCl as human readable LLVM IR?

I have a .pexe and want to get the LLVM IR code. How can this be done? This is the file I want to convert to LLVM IR: NaClAMBullet.pexe
I don't understand why it is not already readable LLVM IR code:
A PNaCl portable executable (pexe in short) is a single LLVM IR module.
Source: Google Native Client Reference
The PNaCl toolchain contains many tools to manipulate PNaCl and LLVM files, including pnacl-dis and pnacl-bcdis. A .pexe is indeed create from a single LLVM IR module, but it ins't the same as an LLVM IR module: the PNaCl bitcode is stable and different from LLVM IR.

How do I parse LLVM IR

I have LLVM IR code in text format. What I wanna do is to be able to parse it and modify that code. Is there an API which can help in parsing the LLVM IR code? What libraries should I have in my system? At this moment I have clang compiler installed as well LLVM, as I can use commands such as llc, opt and llvm-link.
LLVM is primarily a C++ library. It has all the tools you can imagine to parse, manipulate and produce IR in both textual and bitcode (binary) formats.
To get started, take a look at the llvm::ParseIRFile function, defined in header include/llvm/Support/IRReader.h.
The best way to proceed would be to download the LLVM source code and build it, following these instructions. It's then easy to write your own code that uses the LLVM libraries.

How to embed LLVM?

The LLVM Core project consists of:
Compiler - converts source code to LLVM IR
VM - executes compiled IR code
How can I embed the VM to a C++ application?
The LLVM is really a collection of libraries that you can link to, so it's pretty easy to embed. More often the LLVM takes IR that you generate and compiles it directly to machine code. There is also a library available to interpret and execute IR for platforms that do not support JIT compilation.
There's a pretty good tutorial available on the LLVM website here: http://llvm.org/docs/tutorial/. I suggest that you go through that and then ask more specific questions if you have them.
Take a look at the HowToUseJIT example in LLVM.