Option to online particular function in llvm - llvm

I want to make llvm inline particular function by option. I work not with clang frontend (with go frontend to be precise) and this option is needed to be accepted by llvm itself. So I want something like go -gccgoflags="-mllvm -finline-func=my_func". There is no other way for force inline (I do not want to change inliner code in llvm).

Related

C++ Source-to-Source Transformation with Clang

I am working on a project for which I need to "combine" code distributed over multiple C++ files into one file. Due to the nature of the project, I only need one entry function (the function that will be defined as the top function in the Xilinx High-Level-Synthesis software -> see context below). The signature of this function needs to be preserved in the transformation. Whether other functions from other files simply get copied into the file and get called as a subroutine or are inlined does not matter. I think due to variable and function scopes simply concatenating the files will not work.
Since I did not write the C++ code myself and it is quite comprehensive, I am looking for a way to do the transformation automatically. The possibilities I am aware of to do this are the following:
Compile the code to LLVM IR with inlining flags and use a C++/C backend to turn the LLVM code into the source language again. This will result in bad source code and require either an old release of Clang and LLVM or another backend like JuliaComputing. 1
The other option would be developing a tool that relies on using the AST and a library like LibTooling to restructure the code. This option would probably result in better code and put everything into one file without the unnecessary inlining. 2 However, this options seems too complicated to put the all the code into one file.
Hence my question: Are you aware of a better or simply alternative approach to solve this problem?
Context: The project aims to run some of the code on a Xilinx FPGA and the Vitis High-Level-Synthesis tool requires all code that is to be made into a single IP block to be contained in a single file. That is why I need to realise this transformation.

How to check if function is defined in system header files within ModulePass in LLVM Instrumentation pass?

I'm implementing a custom LLVM modulePass in (Transforms/Instrumentation), which performs some action on functions. I'm using Module::iterator to iterate through the functions in a Module. How can i check if any of these function are coming from system header files. Any ideas how to do it.
Clang has SourceLocations for all declarations, but that information is not converted to LLVM IR where the ModulePass's run -- except for debug information if debug info or coverage is enabled. Debug information is best effort, but if you haven't run any other transforms since clang emitted the IR, then it will usually work.
This is an intentional design decision, LLVM normally shouldn't be treating system and non-system functions differently. There's an exception localized inside lib/Transforms/Utils/SimplifyLibCalls.cpp that optimizes based on the names of well-known library functions. What makes this safe is that the user of LLVM would know whether or not these functions the ones defined by the language standard, and choose whether or not to apply this optimization.

Relationship between clang, opt, llc and llvm-linker

I looked at the source code of clang, llc, and opt a little while ago, to see how each one of them adds optimizations to the pipeline. My understanding was that clang adds the same optimizations that opt and llc have in their pipelines, by calling the same methods that opt and llc call. Also clang does not separately call opt and/or llc.
This is almost fine, except that there is a risk that at some point opt may end up with different optimizations in its pipeline (when compared with clang) due to source changes that is done in one but not the other. Same is true for the comparison of llc and clang. Is this perception correct?
Also I have seen charts that show the following workflow: clang, opt, llvm-linker, opt again (for IPA?) then llc. I cannot connect this workflow to what I have seen in the clang. Even my understanding of LTO is that the linker (Say gold) will call optimizations. I cannot understand the role of llvm-linker here.
Any insights is highly appreciated.
opt, llc and llvm-linker are developer-side tools that could be used to run some methods implemented in LLVM libraries. End-user normally should never use them.
The "charts" are probably just someones' custom-built quick'n'dirty LTO pipeline.

Converting GCC IR to LLVM IR

I have written an LLVM pass that modifies the Intermediate Representation (IR) code. To increase portability, I also want it to work with a gcc compiler. So I was wondering if there is any tool which can convert some Intermediate Representation (IR) of gcc to LLVM IR.
You probably want dragonegg (which is using the GCC front-end to build an LLVM IR).
And if you wanted to work on the GCC internal representations, MELT (a high level domain specific language to extend GCC) is probably the right tool.
It will probably be much easier to simply write another version of your code that works with gcc IR. What you want to do is likely not possible, and if it is possible, it's probably extremely difficult. (More so than writing the LLVM pass in the first place.)

How can I generate metadata output with clang similar to that of gcc-xml?

I found that GCCXML is not being maintained anymore (I think the last version is from 2009 from their CVS repository). People usually suggest to check out clang, but I couldn't find a comprehensive documentation that described how to generate a similar output. Not necessarily XML, but the same information in a parsable (documented, if binary or obscure) format. If there is a way to get the same information from a recent gcc version, that is also fine.
This is for a hobby project for dynamic invocation of C++ code. I know about similar projects (pygccxml, xrtti, openc++), but the point is to make it, for fun.
There used to be a way to print an xml dump with Clang but it was more or less supported and has been removed. There are developers options to get a dump at various stages, but the format is for human consumption, and unstable.
The recommendation for Clang users has always been code integration:
Either directly using Clang from C++, and for example use a RecursiveASTVisitor implementation
Or use libclang from C or C++.
Unlike gcc, clang is conceived as a set of a libraries to be reused, so it does not make much sense to try and write a parser for some clang output: it is just much more error-prone than just consuming the information right at the source.
You could use the Clang based CastXML to generate XML file.
https://github.com/CastXML/CastXML
You could code a GCC plugin, or better yet, a MELT extension, to do that. You'll need a GCC 4.6 or later version (4.7 is coming out soon).
However, extending GCC, either thru a plugin coded in C or better yet with an extension coded in MELT (a domain specific language suited to extend GCC) takes some time, because you need to understand and handle most of main GCC internal representations (Gimple and Tree-s).
If you want to use MELT, I'll be delighted to help you.