Disabling optimization when generating IR
I tested a small program and found that some statements are optimized out when I print the IR. I can see those statements in the CAst. So I guess the optimization happens during the IR generation phase. Does anyone know how to disable them? In my case currently I need to get all IR for debugging purpose.
Unfortunately WALA IR invariants require this optimization, so there is no way to disable it. Hopefully with source mapping information and ASTs you can recover what you need.
Then is it possible to prevent a variable to be optimized out? I am currently trying to converting python code to WALA IR (based on Ariadne). But I found global variable is optimized out and causes incorrect code:
g = 5
def test_global():
global g
g = 10
print(g)
test_global()
print(g)
WALA IR:
23 global:global script test1.py/test_global = v43 test1.py [53:0] -> [61:8] [43=[test_global]]
24 putfield v1.< PythonLoader, LRoot, test_global, <PythonLoader,LRoot> > = v43test1.py [53:0] -> [61:8] [43=[test_global]]
25 v45 = invokeFunction < PythonLoader, LCodeBody, do()LRoot; > v43 @25 exception:v46test1.py [60:0] -> [60:13] [43=[test_global]]
BB2
26 v48 = lexical:print@Lscript test1.py test1.py [61:0] -> [61:5]
27 v47 = invokeFunction < PythonLoader, LCodeBody, do()LRoot; > v48,v40:#5 @27 exception:v49test1.py [61:0] -> [61:8] [40=[g]]
BB3
<Code body of function Lscript test1.py/test_global>
CFG:
BB0[-1..-2]
-> BB1
BB1[0..2]
-> BB2
BB2[-1..-2]
Instructions:
BB0
BB1
1 v6 = lexical:print@Lscript test1.py test1.py [58:4] -> [58:9]
2 v4 = invokeFunction < PythonLoader, LCodeBody, do()LRoot; > v6,v2:#10 @2 exception:v7test1.py [58:4] -> [58:12] [2=[g]]
BB2
The variable g should not be optimized, otherwise the output is wrong. Or is there any other way to fix this issue?
I don't know the Python frontend at all, but this sounds like a bug. @juliandolby?
I'm not seeing this problem in https://github.com/wala/ML/pull/101. In that test file, we have global a:
https://github.com/wala/ML/pull/101/files#diff-2c8dda73fa64da2dbb9c44c62cd2d1de6a17f5b817ec4bf48a5dfb226a6abc14R1-R6
And, in the test code that passes, we are finding global a:
https://github.com/wala/ML/pull/101/files#diff-ae3dfefeb65bbe798173cc0e8da458306c9b620ef00aa183c592f5c9efd88bdaR58