[Mlir-commits] [mlir] [mlir][spirv] Allow unrealized casts in scf-to-spirv (PR #174111)
Jakub Kuderski
llvmlistbot at llvm.org
Wed Dec 31 12:55:11 PST 2025
https://github.com/kuhar created https://github.com/llvm/llvm-project/pull/174111
This is required for signature conversion to work.
Fixes: https://github.com/llvm/llvm-project/issues/173564
>From a95bc1bdb5fc7ede97a2755269c8e38fb2157f14 Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Wed, 31 Dec 2025 15:53:35 -0500
Subject: [PATCH] [mlir][spirv] Allow unrealized casts in scf-to-spirv
This is required for signature conversion to work.
Fixes: https://github.com/llvm/llvm-project/issues/173564
---
.../Conversion/SCFToSPIRV/SCFToSPIRVPass.cpp | 2 ++
.../SPIRV/Transforms/SPIRVConversion.cpp | 2 +-
.../Conversion/SCFToSPIRV/func-signature.mlir | 21 +++++++++++++++++++
3 files changed, 24 insertions(+), 1 deletion(-)
create mode 100644 mlir/test/Conversion/SCFToSPIRV/func-signature.mlir
diff --git a/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRVPass.cpp b/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRVPass.cpp
index d974f184c08fc..32a6a1299b1fe 100644
--- a/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRVPass.cpp
+++ b/mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRVPass.cpp
@@ -19,6 +19,7 @@
#include "mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
+#include "mlir/IR/BuiltinOps.h"
namespace mlir {
#define GEN_PASS_DEF_SCFTOSPIRV
@@ -40,6 +41,7 @@ void SCFToSPIRVPass::runOnOperation() {
auto targetAttr = spirv::lookupTargetEnvOrDefault(op);
std::unique_ptr<ConversionTarget> target =
SPIRVConversionTarget::get(targetAttr);
+ target->addLegalOp<UnrealizedConversionCastOp>();
SPIRVTypeConverter typeConverter(targetAttr);
ScfToSPIRVContext scfContext;
diff --git a/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp b/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
index f07307fcd2f9d..816226749463e 100644
--- a/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
+++ b/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
@@ -1018,7 +1018,7 @@ struct FuncOpConversion final : OpConversionPattern<func::FuncOp> {
: TypeRange()));
// Copy over all attributes other than the function name and type.
- for (const auto &namedAttr : funcOp->getAttrs()) {
+ for (NamedAttribute namedAttr : funcOp->getAttrs()) {
if (namedAttr.getName() != funcOp.getFunctionTypeAttrName() &&
namedAttr.getName() != SymbolTable::getSymbolAttrName())
newFuncOp->setAttr(namedAttr.getName(), namedAttr.getValue());
diff --git a/mlir/test/Conversion/SCFToSPIRV/func-signature.mlir b/mlir/test/Conversion/SCFToSPIRV/func-signature.mlir
new file mode 100644
index 0000000000000..55f652229fa14
--- /dev/null
+++ b/mlir/test/Conversion/SCFToSPIRV/func-signature.mlir
@@ -0,0 +1,21 @@
+// RUN: mlir-opt --convert-scf-to-spirv --reconcile-unrealized-casts %s | FileCheck %s
+
+// Make sure that this test pass handles function signature conversion properly.
+
+// CHECK-LABEL: spirv.func @add_scalar
+// CHECK-SAME: (%[[ARG0:.+]]: i32) -> i32
+func.func @add_scalar(%arg0: i32) -> i32 {
+ // CHECK-NEXT: %[[RES:.+]] = spirv.IAdd %[[ARG0]], %[[ARG0]] : i32
+ // CHECK-NEXT: spirv.ReturnValue %[[RES]] : i32
+ %0 = arith.addi %arg0, %arg0 : i32
+ return %0 : i32
+}
+
+// CHECK-LABEL: spirv.func @add_index
+// CHECK-SAME: (%[[ARG0:.+]]: i32) -> i32
+func.func @add_index(%arg0: index) -> index {
+ // CHECK-NEXT: %[[RES:.+]] = spirv.IAdd %[[ARG0]], %[[ARG0]] : i32
+ // CHECK-NEXT: spirv.ReturnValue %[[RES]] : i32
+ %0 = arith.addi %arg0, %arg0 : index
+ return %0 : index
+}
More information about the Mlir-commits
mailing list