Hello LLVM devs,
I am working on LLVM backend for Chapel compiler. I’d like to use ‘llvm.invariant.start’ and constant type-based alias analysis metadata (tbaa). I have read documentation and looked how clang uses ‘llvm.invariant.start’ in code generation and it’s still not clear to me how to use both of these correctly. The problem I have is when should I use any of them and how are they really different.
Here is one possible example:
void f(int x, int z)
const int y = x+g(z);
One of the ways to compile above code is to:
%y = alloca i32
; … perform y computation here and store result into %y_tmp
store i32 %y_tmp, i32* %y
; … continue execution
From now on we have two choices on what to do with it.
I can either use
llvm.invariant.starton %y to mark that %y is never going to change. In this case, I’m unsure whether I should unmark %y with ‘llvm.invariant.end’ after I’m done executing.
I can mark store and subsequent loads using constant tbaa metadata.
Now here are few questions I have:
Should I go with 1, or with 2 in this case? If I have to go with 1, should I unmark memory with llvm.invariant.end after function is done executing?
In general, when should I use tbaa const and llvm.invariant? I can think of: global constants, local loop constants, local if constants, constant arguments in function.
Which optimizations does llvm.invariant.start and tbaa impact? How can I possibly check that I’ve added this metadata correctly and it indeed helps? Possibly by seeing that some optimization occured with new information.