You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think we need to make the names unique to a translation unit, similarly to what we did in LFortran recently. We need to port the solution from here: lfortran/lfortran#1898 and use it for these global variables. Update: implemented in #2138.
I think in the C backend we need to use a global map (using a unique hash) where we save the actual (unique) name, and look it up. Similarly how we handle similar things in the LLVM backend.
@Shaikh-Ubaid yes, exactly. I was going to post the same thing. We use this to compile a larger project, file by file, to C, then we link everything at the end.
The solution is to make these global variables have a unique name, using lcompilers_unique_ID from #2138.
LFortran does this for some of the generated functions, but only when --generate-object-code is used. We could also hide this behind an option, such as --separate-compilation or something like that, if needed.
The reason that global-mangling doesn't work is that I think we wrap all the global symbols in a module named _global_symbols(#2149 (comment)). I checked the ASR passed to unique_symbols pass and it has the following modules:
Activity
certik commentedon Jul 9, 2023
This was caused by the (correct) fix not to use C macros but global variables. But they now clash.
certik commentedon Jul 9, 2023
I think we need to make the names unique to a translation unit, similarly to what we did in LFortran recently. We need to port the solution from here: lfortran/lfortran#1898 and use it for these global variables. Update: implemented in #2138.
certik commentedon Jul 10, 2023
I think in the C backend we need to use a global map (using a unique hash) where we save the actual (unique) name, and look it up. Similarly how we handle similar things in the LLVM backend.
Smit-create commentedon Jul 10, 2023
Can you please add a small reproducible example? Is this something related to #2076?
ubaidsk commentedon Jul 10, 2023
I am probably able to reproduce it:
certik commentedon Jul 10, 2023
@Shaikh-Ubaid yes, exactly. I was going to post the same thing. We use this to compile a larger project, file by file, to C, then we link everything at the end.
The solution is to make these global variables have a unique name, using
lcompilers_unique_IDfrom #2138.LFortran does this for some of the generated functions, but only when
--generate-object-codeis used. We could also hide this behind an option, such as--separate-compilationor something like that, if needed.Smit-create commentedon Jul 10, 2023
This would also fail for functions and subroutines:
certik commentedon Jul 10, 2023
I think we need a
--separate-compilationflag, which will make all these symbols unique for each translation unit. Otherwise they are not unique.certik commentedon Jul 10, 2023
A quick workaround:
This works.
Remove global variables
certik commentedon Jul 10, 2023
So the workaround in #2143 makes this issue not a blocker anymore.
However, it can become again at any time, until we have a proper fix.
To fix this, let's implement a nice ASR pass, as discussed in #2142.
certik commentedon Jul 17, 2023
@Smit-create how do I use the PR #2149 here?
Here is an example:
And I tried:
But it doesn't seem to mangle the symbols, so things still fail at link time when compiling separately two files like the above.
Smit-create commentedon Jul 18, 2023
This works for me:
The reason that global-mangling doesn't work is that I think we wrap all the global symbols in a module named
_global_symbols(#2149 (comment)). I checked the ASR passed tounique_symbolspass and it has the following modules:certik commentedon Jul 18, 2023
@Smit-create awesome, I think it works!! Here is what I tried:
Thank you for this. I think this is good enough and we can improve it later. Great job!