[Mlir-commits] [mlir] [mlir][gpu] Fix crash in `gpu-module-to-binary` (PR #75477)

Fabian Mora llvmlistbot at llvm.org
Thu Dec 14 06:29:22 PST 2023


https://github.com/fabianmcg created https://github.com/llvm/llvm-project/pull/75477

This patch fixes the error in issue #75434. The crash was being caused by not checking for a lack of target attributes in a GPU module. It's now considered an error to invoke the pass with a GPU module with no target attributes.

>From dba20467578220dab1967930a31ca8878889c6fa Mon Sep 17 00:00:00 2001
From: Fabian Mora <fmora.dev at gmail.com>
Date: Thu, 14 Dec 2023 14:23:34 +0000
Subject: [PATCH] [mlir][gpu] Fix crash in `gpu-module-to-binary`

This patch fixes the error in issue #75434. The crash was being caused by not
checking for a lack of target attributes in a GPU module. It's now considered an
error to invoke the pass with a GPU module with no target attributes.
---
 mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp  |  3 +++
 mlir/test/Dialect/GPU/module-to-binary-invalid.mlir | 12 ++++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 mlir/test/Dialect/GPU/module-to-binary-invalid.mlir

diff --git a/mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp b/mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp
index e81992ca8e9a33..70d36297e103f3 100644
--- a/mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp
+++ b/mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp
@@ -101,6 +101,9 @@ LogicalResult moduleSerializer(GPUModuleOp op,
                                const TargetOptions &targetOptions) {
   OpBuilder builder(op->getContext());
   SmallVector<Attribute> objects;
+  // Fail if there are no target attributes
+  if (!op.getTargetsAttr())
+    return op.emitError("the module has no target attributes");
   // Serialize all targets.
   for (auto targetAttr : op.getTargetsAttr()) {
     assert(targetAttr && "Target attribute cannot be null.");
diff --git a/mlir/test/Dialect/GPU/module-to-binary-invalid.mlir b/mlir/test/Dialect/GPU/module-to-binary-invalid.mlir
new file mode 100644
index 00000000000000..7043c4fe49e1a6
--- /dev/null
+++ b/mlir/test/Dialect/GPU/module-to-binary-invalid.mlir
@@ -0,0 +1,12 @@
+// RUN: mlir-opt %s --gpu-module-to-binary --verify-diagnostics
+
+module attributes {gpu.container_module} {
+  // expected-error @below {{the module has no target attributes}}
+  gpu.module @kernel_module1 {
+    llvm.func @kernel(%arg0: i32, %arg1: !llvm.ptr,
+        %arg2: !llvm.ptr, %arg3: i64, %arg4: i64,
+        %arg5: i64) attributes {gpu.kernel} {
+      llvm.return
+    }
+  }
+}



More information about the Mlir-commits mailing list