Skip to content

Commit 8ff8f60

Browse files
yf225tianrengao
authored andcommitted
Raise user error if device-loop is empty after DCE (#1074)
1 parent 20643c7 commit 8ff8f60

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

helion/_compiler/generate_ast.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ def visit_For(self, node: ast.For) -> ast.AST | None:
325325
if persistent_body is not None:
326326
self.device_function.body = persistent_body # pyright: ignore[reportAttributeAccessIssue]
327327
self.device_function.dead_code_elimination()
328+
if not self.device_function.preamble and not self.device_function.body:
329+
raise exc.EmptyDeviceLoopAfterDCE
328330
return self.device_function.codegen_function_call()
329331
return None
330332
return self.generic_visit(node)

helion/exc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,10 @@ class NestedKernelCallsNotSupported(BaseError):
479479
"If you need to share code between kernels, consider extracting the shared logic "
480480
"into a regular Python function that can be called from within both kernels."
481481
)
482+
483+
484+
class EmptyDeviceLoopAfterDCE(BaseError):
485+
message = (
486+
"Device loop is empty after dead-code elimination. "
487+
"The kernel contains no operations that affect the output."
488+
)

test/test_errors.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,22 @@ def kernel_with_dot_mismatch(
545545
):
546546
code_and_output(kernel_with_dot_mismatch, (q, k))
547547

548+
def test_empty_device_loop_after_dce(self):
549+
@helion.kernel()
550+
def empty_kernel(x: torch.Tensor) -> torch.Tensor:
551+
# All computation is dead code
552+
output = torch.zeros_like(x)
553+
for _tile in hl.tile(x.size(0)):
554+
# Do nothing that affects the output
555+
_a = 1
556+
return output
557+
558+
with self.assertRaisesRegex(
559+
helion.exc.EmptyDeviceLoopAfterDCE,
560+
r"Device loop is empty after dead-code elimination",
561+
):
562+
code_and_output(empty_kernel, (torch.randn(4, 4, device=DEVICE),))
563+
548564

549565
if __name__ == "__main__":
550566
unittest.main()

0 commit comments

Comments
 (0)