[Mlir-commits] [mlir] [mlir][SPIRVToLLVM] Erase empty `spirv.mlir.loop` in `LoopPattern` (PR #113527)

Longsheng Mou llvmlistbot at llvm.org
Wed Oct 23 23:29:24 PDT 2024


https://github.com/CoTinker created https://github.com/llvm/llvm-project/pull/113527

This PR erases `spirv.mlir.loop` with an empty region in `LoopPattern`, resolving a crash. Fixes #113404.

>From 17b720e2bf6ca0ab2a861a62b140005e51d208a2 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 24 Oct 2024 11:15:23 +0800
Subject: [PATCH 1/2] [mlir][spirv] Erase empty `spirv.mlir.loop` in
 `LoopPattern`

This PR erases `spirv.mlir.loop` with an empty region in `LoopPattern`, resolving a crash.
---
 mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp b/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp
index 74c169c9a7e76a..044a3c8c65e3b2 100644
--- a/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp
+++ b/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp
@@ -1083,6 +1083,12 @@ class LoopPattern : public SPIRVToLLVMConversion<spirv::LoopOp> {
     if (loopOp.getLoopControl() != spirv::LoopControl::None)
       return failure();
 
+    // `spirv.mlir.loop` with empty region is redundant and should be erased.
+    if (loopOp.getBody().empty()) {
+      rewriter.eraseOp(loopOp);
+      return success();
+    }
+
     Location loc = loopOp.getLoc();
 
     // Split the current block after `spirv.mlir.loop`. The remaining ops will

>From fc69e5b61f13e9f781a7229e0f138b7da65582e5 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 24 Oct 2024 14:27:46 +0800
Subject: [PATCH 2/2] Add tests

---
 .../Conversion/SPIRVToLLVM/control-flow-ops-to-llvm.mlir  | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mlir/test/Conversion/SPIRVToLLVM/control-flow-ops-to-llvm.mlir b/mlir/test/Conversion/SPIRVToLLVM/control-flow-ops-to-llvm.mlir
index 3557830e779e24..756fc5415e20f7 100644
--- a/mlir/test/Conversion/SPIRVToLLVM/control-flow-ops-to-llvm.mlir
+++ b/mlir/test/Conversion/SPIRVToLLVM/control-flow-ops-to-llvm.mlir
@@ -86,6 +86,14 @@ spirv.module Logical GLSL450 {
 //===----------------------------------------------------------------------===//
 
 spirv.module Logical GLSL450 {
+  // CHECK-LABEL: @empty_loop
+  spirv.func @empty_loop() "None" {
+    // CHECK: llvm.return
+    spirv.mlir.loop {
+    }
+    spirv.Return
+  }
+
   // CHECK-LABEL: @infinite_loop
   spirv.func @infinite_loop(%count : i32) -> () "None" {
     // CHECK:   llvm.br ^[[BB1:.*]]



More information about the Mlir-commits mailing list