ML icon indicating copy to clipboard operation
ML copied to clipboard

Potentially redundant global read in IR stemming from `__init__.py`

Open khatchad opened this issue 1 year ago • 1 comments

If there is an import statement in an __init__.py file that imports a module in a subpackage, then we'll have two global reads of the same module. The first one is from the one that is imported automatically and the other from the explicit import call. We could reuse the automatic one. Right now, there are two identical ones.

The potentially redundant read occurs here:

https://github.com/wala/ML/blob/b813bde5f9f9a5df87426192aecb8f0b64b1dcc5/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java#L983-L985

This issue can be exercised by running the following test case:

https://github.com/ponder-lab/ML/blob/d2946f69704a20bb575ea3bdcade2f43bed7f27a/com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflow2Model.java#L2611-L2621

That results in the following IR for https://github.com/ponder-lab/ML/blob/d2946f69704a20bb575ea3bdcade2f43bed7f27a/com.ibm.wala.cast.python.test/data/proj51/src/init.py:

callees of node __init__.py : []

IR of node 2, context CallStringContext: [ com.ibm.wala.FakeRootClass.fakeRootMethod()V@2 ]
<Code body of function Lscript src/__init__.py>
CFG:
BB0[-1..-2]
    -> BB1
BB1[0..96]
    -> BB2
BB2[-1..-2]
Instructions:
BB0
BB1
0   global:global script src/__init__.py = v1<no information>
1   putfield v1.< PythonLoader, LRoot, src, <PythonLoader,LRoot> > = v1__init__.py [1:0] -> [1:21]
2   v3 = global:global script src/module.py  __init__.py [1:0] -> [1:21]
3   putfield v1.< PythonLoader, LRoot, module, <PythonLoader,LRoot> > = v3__init__.py [1:0] -> [1:21]
...
95   v240 = global:global script src/module.py__init__.py [1:0] -> [1:21] [240=[f]]
96   v242 = fieldref v240.v239:#f            __init__.py [1:0] -> [1:21] [242=[f]240=[f]]
BB2

Note that v3 and v240 have the same value, though the results seem to differ in the printing (I wonder if that's from the PA?).

khatchad avatar Jul 03 '24 22:07 khatchad

This looks like it was part of the original code:

https://github.com/wala/ML/blame/261455fb03e274947967022d822caec089c4f5e4/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java#L615-L617

khatchad avatar Jul 22 '24 19:07 khatchad