slyubomirsky commented on code in PR #14394:
URL: https://github.com/apache/tvm/pull/14394#discussion_r1147086551


##########
tests/python/relax/test_analysis_contains_impure_call.py:
##########
@@ -0,0 +1,104 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import tvm
+import tvm.testing
+from tvm import relax as rx
+from tvm.relax.analysis import contains_impure_call
+from tvm.script import relax as R
+
+
+def test_simple_pure_case():
+    @tvm.script.ir_module
+    class PureTest:
+        @R.function
+        def pure_func(x: R.Tensor((), "int32")) -> R.Tensor((), "int32"):
+            y = R.add(x, x)
+            z = R.multiply(x, y)
+            return R.add(z, R.const(1, "int32"))
+
+    assert not contains_impure_call(PureTest["pure_func"])
+
+
+def test_simple_impure_case():
+    @tvm.script.ir_module
+    class ImpureTest:
+        @R.function
+        def impure_func() -> R.Object:
+            R.func_attr({"IsPure": False})
+            y = R.print(format="I am a message")
+            return y
+
+    assert contains_impure_call(ImpureTest["impure_func"])
+
+
+def test_nested_function():
+    @tvm.script.ir_module
+    class NestedTest:
+        @R.function
+        def pure_with_impure_nested() -> R.Tensor((), "int32"):
+            # unused
+            @R.function
+            def impure_inner() -> R.Object:
+                R.func_attr({"IsPure": False})
+                y = R.print(format="Another, worse, message")
+                return y
+
+            x = R.const(0, dtype="int32")
+            return R.add(x, x)
+
+    assert not contains_impure_call(NestedTest["pure_with_impure_nested"])
+    assert contains_impure_call(
+        NestedTest["pure_with_impure_nested"].body.blocks[0].bindings[0].value
+    )
+
+
+def test_ignoring_recursive_call():
+    # Ignoring a recursive call. This can be useful if some transformation
+    # removes an impure operation and the compiler needs to check if the impure
+    # function has become pure
+    @tvm.script.ir_module
+    class RecursiveTest:
+        @R.function
+        def recursive_impure() -> R.Object:
+            R.func_attr({"IsPure": False})
+            x = R.const(1, "int32")
+            y = R.add(x, x)
+            z = R.print(x, y, format="{} {}")
+            w = RecursiveTest.recursive_impure()
+            return w
+
+    assert contains_impure_call(RecursiveTest["recursive_impure"])
+    # but if we remove the impure call...
+    body = RecursiveTest["recursive_impure"].body
+    own_name = body.blocks[0].bindings[-1].value.op
+    # skipping the call to print...
+    new_bindings = [
+        body.blocks[0].bindings[0],
+        body.blocks[0].bindings[1],
+        body.blocks[0].bindings[-1],
+    ]
+    new_body = rx.SeqExpr([rx.BindingBlock(new_bindings)], body.body)
+
+    # if we didn't ignore the recursive call, the fact the var's StructInfo
+    # calls it impure would throw it off
+    assert not contains_impure_call(new_body, own_name=own_name)
+    assert contains_impure_call(new_body)

Review Comment:
   Somehow the second call fails. The `StructInfo` for the `GlobalVar` 
`recursive_impure` indicates that it is pure (which is wrong), whereas the 
`StructInfo` for `RecursiveTest["recursive_impure"]` (the `Function` node 
itself) correctly shows that it is impure. Why would this happen? Is there 
something in `block_builder.cc` that I would have to update?
   
   I would greatly appreciate any help.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to