From fe808e6b0e4960804e35d50be09bd1bf8add595e Mon Sep 17 00:00:00 2001
From: Frederik Hennig <frederik.hennig@fau.de>
Date: Thu, 11 Jan 2024 23:38:15 +0100
Subject: [PATCH] add additional constant folding test. Improved (de)constify.

---
 pystencils/nbackend/types/basic_types.py       | 18 ++++++++++++------
 .../nbackend/test_constant_folding.py          | 10 ++++++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/pystencils/nbackend/types/basic_types.py b/pystencils/nbackend/types/basic_types.py
index 0a56f2f57..42feb18b9 100644
--- a/pystencils/nbackend/types/basic_types.py
+++ b/pystencils/nbackend/types/basic_types.py
@@ -358,13 +358,19 @@ T = TypeVar("T", bound=PsAbstractType)
 
 def constify(t: T) -> T:
     """Adds the const qualifier to a given type."""
-    t_copy = copy(t)
-    t_copy._const = True
-    return t_copy
+    if not t.const:
+        t_copy = copy(t)
+        t_copy._const = True
+        return t_copy
+    else:
+        return t
 
 
 def deconstify(t: T) -> T:
     """Removes the const qualifier from a given type."""
-    t_copy = copy(t)
-    t_copy._const = False
-    return t_copy
+    if t.const:
+        t_copy = copy(t)
+        t_copy._const = False
+        return t_copy
+    else:
+        return t
diff --git a/pystencils_tests/nbackend/test_constant_folding.py b/pystencils_tests/nbackend/test_constant_folding.py
index 12149c3ce..7f395aaf7 100644
--- a/pystencils_tests/nbackend/test_constant_folding.py
+++ b/pystencils_tests/nbackend/test_constant_folding.py
@@ -27,6 +27,16 @@ def test_constant_folding_int(width):
 
     assert folder(expr) == PsTypedConstant(-53, SInt(width))
 
+    expr = pb.Product(
+        (
+            PsTypedConstant(2, SInt(width)),
+            PsTypedConstant(-3, SInt(width)),
+            PsTypedConstant(4, SInt(width))
+        )
+    )
+
+    assert folder(expr) == PsTypedConstant(-24, SInt(width))
+
 
 @pytest.mark.parametrize("width", (32, 64))
 def test_constant_folding_float(width):
-- 
GitLab