Privatize global variables

Dear LLVM,

To workaround the GPU modules visibility rules, we need to lower
global variables into scope-variables of main entry and arguments in
other functions. For example,

int foo = 0;

int func() { return foo + 1; }

int main() { return func(); }

should become:

int func(int* foo) { return *foo + 1; }

int main() { int foo = 0; return func(&foo); }

Is there a strong name/term for this in compiler theory?
Do you know are there any related techniques already implemented in LLVM passes?
I'd much appreciate all points that could help us to better understand
the context.

Thanks,
- Dima.

Dear LLVM,

To workaround the GPU modules visibility rules, we need to lower
global variables into scope-variables of main entry and arguments in
other functions. For example,

The -internalize pass changes globals to have internal linkage. Does this not suffice for your needs?

-- John T.

Hi John,

The -internalize pass changes globals to have internal linkage. Does this
not suffice for your needs?

To my understanding, it does not. The -internalize pass performs:

I->setLinkage(GlobalValue::InternalLinkage);

which means it only changes the linkage mode, while global variables
remain in place. And we need to *replace* globals with local variables
in main and arguments - in other functions.

- D.

Hi John,

The -internalize pass changes globals to have internal linkage. Does this
not suffice for your needs?

To my understanding, it does not. The -internalize pass performs:

I->setLinkage(GlobalValue::InternalLinkage);

which means it only changes the linkage mode, while global variables
remain in place.

Correct, but the globals are invisible to other code that is linked into the program (it's akin to giving the globals the "static" keyword in C).

  And we need to *replace* globals with local variables
in main and arguments - in other functions.

As far as I know, there is no LLVM transform that does this.

Can you describe your visibility problem in more detail? Apparently it's not just a problem of global symbols being visible at link-time.

-- John T.