From 3f1f979875295f7a322ab1f5023b53a53d221d08 Mon Sep 17 00:00:00 2001
From: Christos Kotsalos <kotsaloscv@gmail.com>
Date: Wed, 31 Jul 2024 14:19:35 +0200
Subject: [PATCH] DaCe fix: https://github.com/spcl/dace/issues/1625

---
 .../runners/dace_iterator/__init__.py                  | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/gt4py/next/program_processors/runners/dace_iterator/__init__.py b/src/gt4py/next/program_processors/runners/dace_iterator/__init__.py
index eeca53ea0d..70b680b85e 100644
--- a/src/gt4py/next/program_processors/runners/dace_iterator/__init__.py
+++ b/src/gt4py/next/program_processors/runners/dace_iterator/__init__.py
@@ -24,6 +24,8 @@
 import numpy as np
 from dace.sdfg import utils as sdutils
 from dace.transformation.auto import auto_optimize as autoopt
+from dace.transformation.pass_pipeline import Pipeline
+from dace.transformation.passes import DeadStateElimination
 
 import gt4py.next.iterator.ir as itir
 from gt4py.next import common
@@ -298,6 +300,14 @@ def build_sdfg_from_itir(
     # TODO(edopao): remove `inline_loop_blocks` when DaCe transformations support LoopRegion construct
     sdutils.inline_loop_blocks(sdfg)
 
+    # TODO(kotsaloscv): remove the DeadStateElimination transformation once this issue is fixed: https://github.com/spcl/dace/issues/1625
+    # From DaCe v0.16 to v0.16.1, there were changes in the CFG analysis that appear to be incompatible
+    # with SDFGs that have structurally inaccessible states. The workaround involves calling DeadStateElimination first
+    # to remove those states so that they don't cause issues in subsequent passes.
+    dace_pipeline = Pipeline([DeadStateElimination()])
+    for sd in sdfg.all_sdfgs_recursive():
+        dace_pipeline.apply_pass(sd, {})
+
     # run DaCe transformations to simplify the SDFG
     sdfg.simplify()