From c87b153b1ad3fe63da087e36904418472233f4b2 Mon Sep 17 00:00:00 2001
From: Isuru Fernando <isuruf@gmail.com>
Date: Fri, 2 Jun 2023 02:50:17 -0500
Subject: [PATCH] Fix NoOpInstrunction with predicate

and no other instructions with the same predicate
---
 loopy/codegen/instruction.py |  2 +-
 loopy/target/__init__.py     |  3 +++
 loopy/target/c/__init__.py   |  4 ++++
 loopy/target/python.py       |  4 ++++
 test/test_target.py          | 11 +++++++++++
 5 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/loopy/codegen/instruction.py b/loopy/codegen/instruction.py
index 8e8eb86a1..371979d97 100644
--- a/loopy/codegen/instruction.py
+++ b/loopy/codegen/instruction.py
@@ -297,7 +297,7 @@ def generate_c_instruction_code(codegen_state, insn):
 def generate_nop_instruction_code(codegen_state, insn):
     if codegen_state.vectorization_info is not None:
         raise UnvectorizableError("C instructions cannot be vectorized")
-    return codegen_state.ast_builder.emit_comment(
+    return codegen_state.ast_builder.emit_noop_with_comment(
         "no-op (insn=%s)" % (insn.id))
 
 # vim: foldmethod=marker
diff --git a/loopy/target/__init__.py b/loopy/target/__init__.py
index 5bb1043b9..54a0729da 100644
--- a/loopy/target/__init__.py
+++ b/loopy/target/__init__.py
@@ -271,6 +271,9 @@ class ASTBuilderBase(Generic[ASTType]):
     def emit_comment(self, s):
         raise NotImplementedError()
 
+    def emit_noop_with_comment(self, s):
+        raise NotImplementedError()
+
     # }}}
 
     def process_ast(self, node):
diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py
index 426d87fe5..d8475cdb1 100644
--- a/loopy/target/c/__init__.py
+++ b/loopy/target/c/__init__.py
@@ -1263,6 +1263,10 @@ class CFamilyASTBuilder(ASTBuilderBase[Generable]):
         from cgen import Comment
         return Comment(s)
 
+    def emit_noop_with_comment(self, s):
+        from cgen import Line
+        return Line(f"; /*{s}*/")
+
     @property
     def can_implement_conditionals(self):
         return True
diff --git a/loopy/target/python.py b/loopy/target/python.py
index b50ddaf8e..f9cc06147 100644
--- a/loopy/target/python.py
+++ b/loopy/target/python.py
@@ -253,6 +253,10 @@ class PythonASTBuilderBase(ASTBuilderBase[Generable]):
         from genpy import Comment
         return Comment(s)
 
+    def emit_noop_with_comment(self, s):
+        from cgen import Line
+        return Line(f"pass #{s}")
+
     @property
     def can_implement_conditionals(self):
         return True
diff --git a/test/test_target.py b/test/test_target.py
index 2115d60cc..389c865b6 100644
--- a/test/test_target.py
+++ b/test/test_target.py
@@ -29,6 +29,7 @@ import pyopencl.clmath
 import pyopencl.clrandom
 import pyopencl.tools
 import pytest
+import pymbolic.primitives as prim
 
 from loopy.target.c import CTarget
 from loopy.target.opencl import OpenCLTarget
@@ -704,6 +705,16 @@ def test_empty_array_stride_check(ctx_factory):
         einsum(cq, a=np.random.randn(3, 2, 5).copy(order="F"), x=np.random.randn(5))
 
 
+def test_no_op_with_predicate(ctx_factory):
+    ctx = ctx_factory()
+
+    predicate = prim.Comparison(prim.Variable("a"), ">", 0)
+    knl = lp.make_kernel([],
+        ["<> a = 1", lp.NoOpInstruction(predicates=[predicate])])
+    code = lp.generate_code_v2(knl).device_code()
+    cl.Program(ctx, code).build()
+
+
 def test_empty_array_stride_check_fortran(ctx_factory):
     # https://github.com/inducer/loopy/issues/583
     ctx = ctx_factory()
-- 
GitLab