LLVM Induction Variable Simplify Pass - llvm

I want to have only canonical induction variable in my IR before I pass it to one of my other passes to do some transformation.
However the pass -indvars doesn't seem to do it. How can I achieve this task?

After some digging I found out that -indvars pass now don't canonicalize induction variable because most other passes now are made to work without need for canonical loops.
However my pass required that loops be in canonical form, so I found the old induction variable simplify pass and explicitly included in my file.
You too can include that old pass to get your work done. You can find old pass here

Related

Calling a llvm pass outside of a pass

I am new to LLVM and C++ and was trying to write some code to perform static analysis. My static analysis needs access to memory dependence info, which in LLVM can be obtained using MemoryDependenceAnalysis. This analysis generates an object of type MemoryDependenceResults, which is precisely what I need. The only ways I've seen this object being obtained, though, is through an LLVM pass and that's not something I want. My impression is you have to write a pass to be able to use an existing pass. I was wondering is that true? Can I call a pass outside of a pass, i.e. regular code? Or alternatively can a llvm pass be invoked programmatically without needing to run the opt command?
What I need is a way to obtain this MemoryDependenceResults object in my program (which is not a pass) and then perform some more manipulations to it.
First, you can create a PassManager instance anywhere, add the pass into the manager and run it. Here are two PassManagers can be used - legacy or the new one. To make it simple, I recommend you try the legacy one first:
legacy::PassManager passManager;
passManager.add(new MemoryDependenceWrapperPass());
passManager.run(*module);
Second, if you wish to run a transform pass, you call it. But if you wish to run an analysis pass, you need at least a wrapper pass to get the analysis result since the API getAnalysis() is only available in a pass. (You can copy and rename MemoryDependenceWrapperPass to your version.)

How to run a module pass in LLVM

I'm trying to find a way to optimize away empty global constructors. Previous optimizations will turn constructors into functions that do nothing. I need to add a new pass to remove these functions from llvm.global_ctors.
First, I tried optimizeGlobalCtorsList but this function doesn't actually call the callback I give it even though llvm.global_ctors is populated.
Then I tried running GlobalOptPass. I tried this:
llvm::GlobalOptPass pass;
llvm::ModuleAnalysisManager MAM{true};
pass.run(module, MAM);
This ends up dereferencing a null pointer in AnalysisManager::lookupPass. I think I need to perform some sort of initialization or registration but I don't know how to do that. All the references on "llvm pass registration" talk about registering the pass with opt. I don't want to do that. I just want to run the pass.
Look in lib/Transforms/IPO/PassManagerBuilder.cpp (or lib/Passes/PassBuilder.cpp for the new pass manager) to see how opt sets up its pass pipeline. The code for opt is in tools/opt/opt.cpp and is very small, delegating almost all of its work to the core libraries.
You could use opt as a template for your own tool, or you could hack on the pass building pipline to insert your pass where you want it.

Django - Emulate a http-post request

I have this view function search(request). The url suffix is /search. It takes a few POST parameters and shows search results accordingly.
I want to make a second function show_popular(request). It takes no post or get parameters. But it should emulate a call to the search function with some hard coded post parameters.
I want to achieve this without changing anything in any existing function and without changing setup. Is that possible?
EDIT: I know this can be achieved by refactoring the search into a separate function and have several view functions call this. But in this particular case, I am not interested in that. In my case the show_popular function is only temporary, and for irrelevant reasons I do not wish to re-factor.
Yes, but you don't want to do that. Refactor search() into a function that handles the request and a function that performs the search, and call the latter from show_popular().

Python: How to check that...?

I'd like some advice on how to check for the correctness of the parameters I receive.
The checking is going to be done in C++, so if there's a good solution using Boost.Python (preferably) or the C API, please tell me about that. Otherwise, tell me what attributes the object should have to ensure that it meets the criteria.
So...
How do you check that an object is a function?
How do you check that an object is a bound method?
How do you check that an object is a class object?
How do you check that a class object is a child of another class?
When in doubt just work out how you would get the required effect by calling the usual Python builtins and translate it to C/C++. I'll just answer for Python, for C you would look up the global such as 'callable' and then call it like any other Python function.
Why would you care about it being a function rather than any other sort of callable? If you want you can find out if it is callable by using the builtin callable(f) but of course that won't tell you which arguments you need to pass when calling it. The best thing here is usually just to call it and see what happens.
isinstance(f, types.MethodType) but that won't help if it's a method of a builtin. Since there's no difference in how you call a function or a bound method you probably just want to check if it is callable as above.
isinstance(someclass, type) Note that this will include builtin types.
issubclass(someclass, baseclass)
I have two unconventional recommendations for you:
1) Don't check. The Python culture is to simply use objects as you need to, and if it doesn't work, then an exception will occur. Checking ahead of time adds overhead, and potentially limits how people can use your code because you're checking more strictly than you need to.
2) Don't check in C++. When combining Python and C (or C++), I recommend only doing things in C++ that need to be done there. Everything else should be done in Python. So check your parameters in a Python wrapper function, and then call an unchecked C++ entry point.

changing llvm::Function signature after code generation, before last CreateRet

I'm trying to implement the following functionality;
a function with no explicit return will by default return the last evaluation in the last executed block
So, currently the process i'm doing is
1) create a Function
llvm::Function* result = llvm::Function::Create(Compiler::Detail::getAnonymousFunctionSignature(llvmContext),
llvm::GlobalValue::ExternalLinkage,
name,
module());
result->setCallingConv( llvm::CallingConv::C );
2) add blocks and evaluations to the blocks
builder.createFoo.....
However, only in the second phase i have the llvm::Value* (and compile-time type) that i want to use by default as return value. The problem is that i need to use this type to determine the signature of the created function
Question:
how do i solve the problem?
is possible to change the signature after the function is created? is it legal?
do i need to create a new function with the updated signature and copy/assign the entry block of the first function to it and thats it? or do i need to reevaluate all the expressions?
is possible to not create the function before code generation? if it is so, at what point should i create the function?
a code example of how to achieve this would be awesome. thanks!
You cannot change function signature, because this will mean that it will have different Type (and thus you will need to update all the users, etc.; this procedure in most cases cannot be done automagically).
There are multiple possible solutions, for example, you can create the function with the updated signature, then use the functions from lib/Transforms/Utils/CloneFunction.cpp to copy the function body and then hack on the return type.
A better solution exists than CloneFunctionInto(), according to https://stackoverflow.com/a/18751365/2024042:
NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
Where NF is the new function you're cloning into and F is the old function that you have just cloned.