This is an automated email from the ASF dual-hosted git repository.

sanirudh pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/unity by this push:
     new e7c7314046 [Unity][Transform] Keep R.ExternFunc in dead-code 
elimination (#16118)
e7c7314046 is described below

commit e7c73140469ba59a6ac3c1d585a49626f064566a
Author: Eric Lunderberg <[email protected]>
AuthorDate: Tue Nov 14 08:21:05 2023 -0600

    [Unity][Transform] Keep R.ExternFunc in dead-code elimination (#16118)
    
    Prior to this commit, the `DeadCodeElimination` and `FuseTIR` passes
    treated `R.ExternFunc` differently with respect to removal of internal
    functions: `DeadCodeElimination` treated these functions as internal,
    to be removed if there are no internal callers, while `FuseTIR`
    treated these functions as external, to be retained in all cases.
    
    Because the visibility of a function is determined by the
    `tvm::attr::kGlobalSymbol` attribute; which is present for `PrimFunc`
    and `relax::Function`, but missing for `relax::ExternFunc`; there is
    no information in the `IRModule` that can determine whether a
    `relax::ExternFunc` should be visible externally.
    
    For consistency, and to allow the use of `DeadCodeElimination` in
    future refactors of `FuseTIR`, this commit updates
    `DeadCodeElimination` to retain instances of `relax::ExternFunc`.
---
 src/relax/transform/dead_code_elimination.cc               |  2 +-
 tests/python/relax/test_transform_dead_code_elimination.py | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/relax/transform/dead_code_elimination.cc 
b/src/relax/transform/dead_code_elimination.cc
index 6d9f25296a..248e4c1c00 100644
--- a/src/relax/transform/dead_code_elimination.cc
+++ b/src/relax/transform/dead_code_elimination.cc
@@ -124,7 +124,7 @@ IRModule DeadCodeElimination(const IRModule& arg_mod, 
Array<runtime::String> ent
     entry_functions.insert(mod->GetGlobalVar(name));
   }
   for (const auto& [gv, func] : mod->functions) {
-    if (func->GetLinkageType() == LinkageType::kExternal) {
+    if (func.as<ExternFuncNode>() || func->GetLinkageType() == 
LinkageType::kExternal) {
       entry_functions.insert(gv);
     }
   }
diff --git a/tests/python/relax/test_transform_dead_code_elimination.py 
b/tests/python/relax/test_transform_dead_code_elimination.py
index 7b749d6778..c0a2d47b19 100644
--- a/tests/python/relax/test_transform_dead_code_elimination.py
+++ b/tests/python/relax/test_transform_dead_code_elimination.py
@@ -497,5 +497,15 @@ def test_unused_dfb2():
     verify(Input, Expected)
 
 
+def test_extern_func():
+    """DeadCodeElimination should retain the ExternFunc in the IRModule."""
+
+    builder = tvm.relax.BlockBuilder()
+    builder.add_func(tvm.relax.extern("extern_func"), "extern_func")
+    before = builder.get()
+
+    verify(before, before)
+
+
 if __name__ == "__main__":
     tvm.testing.main()

Reply via email to