[Mlir-commits] [mlir] [mlir][gpu] Fix crash in gpu-to-llvm with unranked memref and bare-ptr calling convention (PR #185062)
Mehdi Amini
llvmlistbot at llvm.org
Fri Mar 6 09:42:44 PST 2026
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/185062
When using `--gpu-to-llvm` with `use-bare-pointers-for-kernels=true` and a `gpu.launch_func` whose kernel has an `UnrankedMemRefType` argument, `LLVMTypeConverter::promoteOperands` would hit an `llvm_unreachable` because unranked memrefs are not supported with the bare-pointer calling convention.
Fix this by checking for unranked memref kernel arguments before calling `promoteOperands` and returning a proper conversion failure with a diagnostic instead of crashing.
Fixes #184939
Assisted-by: Claude Code
>From 2e11d6ed5159646d4ea457ed7840c88475b56b33 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Fri, 6 Mar 2026 04:52:02 -0800
Subject: [PATCH] [mlir][gpu] Fix crash in gpu-to-llvm with unranked memref and
bare-ptr calling convention
When using `--gpu-to-llvm` with `use-bare-pointers-for-kernels=true` and a
`gpu.launch_func` whose kernel has an `UnrankedMemRefType` argument,
`LLVMTypeConverter::promoteOperands` would hit an `llvm_unreachable` because
unranked memrefs are not supported with the bare-pointer calling convention.
Fix this by checking for unranked memref kernel arguments before calling
`promoteOperands` and returning a proper conversion failure with a diagnostic
instead of crashing.
Fixes #184939
Assisted-by: Claude Code
---
.../GPUCommon/GPUToLLVMConversion.cpp | 10 ++++++++
.../lower-launch-func-bare-ptr-invalid.mlir | 23 +++++++++++++++++++
2 files changed, 33 insertions(+)
create mode 100644 mlir/test/Conversion/GPUCommon/lower-launch-func-bare-ptr-invalid.mlir
diff --git a/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp b/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
index 0f72bf0c0d59e..d48a0db4d9de0 100644
--- a/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
+++ b/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
@@ -976,6 +976,16 @@ LogicalResult LegalizeLaunchFuncOpPattern::matchAndRewrite(
// Note: If `useBarePtrCallConv` is set in the type converter's options,
// the value of `kernelBarePtrCallConv` will be ignored.
OperandRange origArguments = launchOp.getKernelOperands();
+ bool effectiveBarePtr = kernelBarePtrCallConv ||
+ getTypeConverter()->getOptions().useBarePtrCallConv;
+ if (effectiveBarePtr) {
+ for (Value arg : origArguments) {
+ if (isa<UnrankedMemRefType>(arg.getType()))
+ return rewriter.notifyMatchFailure(
+ loc, "unranked memref kernel argument is not supported with "
+ "the bare-pointer calling convention");
+ }
+ }
SmallVector<Value, 8> llvmArguments = getTypeConverter()->promoteOperands(
loc, origArguments, adaptor.getKernelOperands(), rewriter,
/*useBarePtrCallConv=*/kernelBarePtrCallConv);
diff --git a/mlir/test/Conversion/GPUCommon/lower-launch-func-bare-ptr-invalid.mlir b/mlir/test/Conversion/GPUCommon/lower-launch-func-bare-ptr-invalid.mlir
new file mode 100644
index 0000000000000..19dbdc94b6d92
--- /dev/null
+++ b/mlir/test/Conversion/GPUCommon/lower-launch-func-bare-ptr-invalid.mlir
@@ -0,0 +1,23 @@
+// RUN: mlir-opt %s --gpu-to-llvm="use-bare-pointers-for-kernels=1" -split-input-file -verify-diagnostics
+
+// Test that gpu.launch_func with an unranked memref kernel argument fails
+// gracefully (no crash) when using the bare-pointer calling convention.
+// See issue #184939.
+
+module attributes {gpu.container_module} {
+ gpu.module @kernels {
+ gpu.func @kernel(%arg0: memref<*xi32>) kernel {
+ gpu.return
+ }
+ }
+ func.func @main(%arg: memref<*xi32>) {
+ %c1 = arith.constant 1 : index
+ %c0 = arith.constant 0 : i32
+ // expected-error at +1 {{failed to legalize operation 'gpu.launch_func'}}
+ gpu.launch_func @kernels::@kernel
+ blocks in (%c1, %c1, %c1) threads in (%c1, %c1, %c1)
+ dynamic_shared_memory_size %c0
+ args(%arg : memref<*xi32>)
+ return
+ }
+}
More information about the Mlir-commits
mailing list