SROA for global arrays

hi,

My requirement is if I have an global array, then can I break it into individual elements similar to what SROA does ? I know that allocation for globals is different from those of locals but still asking.

Regards
Sushant

Hi,

Doing such a transform in the general case would require whole-program analysis. If you have an array in your source code such as:

int x[42];

To do an SROA-like transform, you need to find everything that takes the address of the array and find which elements they access. SROA can do this for locals because the only things that can name the array are instructions within the function. If none of these allow a pointer to the array to escape then it can inspect each load and store and try to determine if they are independent. With a global, any other compilation unit (in the general case, including ones that you dynamically link) may take the address of the global and rely on its layout.

You can restrict this somewhat. If you have a global that's private or internal, then you can find all direct references to it in the current module and so the same techniques that SROA uses would work (though they'd require more work because they'd have to look at every function in the module). For DSO-local globals, you could do the same thing at link time with some form of LTO.

This seems like a lot of work though and I wouldn't expect to see much benefit in normal code. What is your use case?

David

Hi Sushant,

llvm already has an implementation for some simple cases of SROA for globals.
Check SRAGlobal() from /lib/Transforms/IPO/GlobalOpt.cpp.

Thank you, Alexey.

Hi Sushant,

llvm already has an implementation for some simple cases of SROA for globals.
Check SRAGlobal() from /lib/Transforms/IPO/GlobalOpt.cpp.

Thank you, Alexey.