[clang] [cir] Convert gpu.func and gpu.launch_func signatures with cir types (PR #245465)
Valentin Clement バレンタイン クレメン via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 17:50:14 PST 2026
https://github.com/clementval created https://github.com/llvm/llvm-project/pull/245465
In CIR + OpenACC, the OpenACC compute region will be converted and outlined to a `gpu.func`. The CIR to LLVM conversion will handle the body of the `gpu.func` fine but the signature will keep the CIR types. This patch adds two patterns to be able to convert the types in the signature as well.
>From b3f48cb84c3491cc398c4d6f419000af31163f9c Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Mon, 23 Nov 2026 17:48:03 -0800
Subject: [PATCH] [cir] Convert gpu.func and gpu.launch_func signatures with
cir types
---
clang/lib/CIR/CMakeLists.txt | 1 +
.../CIR/Lowering/DirectToLLVM/CMakeLists.txt | 1 +
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 62 +++++++++++++++++++
clang/lib/CIR/RegisterAllDialects.cpp | 3 +-
.../test/CIR/Lowering/gpu-func-signature.cir | 28 +++++++++
5 files changed, 94 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CIR/Lowering/gpu-func-signature.cir
diff --git a/clang/lib/CIR/CMakeLists.txt b/clang/lib/CIR/CMakeLists.txt
index 6436d554bb38ba..107573aabab109 100644
--- a/clang/lib/CIR/CMakeLists.txt
+++ b/clang/lib/CIR/CMakeLists.txt
@@ -19,5 +19,6 @@ add_clang_library(CIRRegisterAllDialects
CIROpenMPSupport
MLIRArithDialect
MLIRDLTIDialect
+ MLIRGPUDialect
MLIRLLVMDialect
)
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
index a67ccc7d1cea50..c86014737f7bbb 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
@@ -27,6 +27,7 @@ add_clang_library(clangCIRLoweringDirectToLLVM
MLIRBuiltinToLLVMIRTranslation
MLIRDLTIDialect
MLIRFuncDialect
+ MLIRGPUDialect
MLIRLLVMCommonConversion
MLIRLLVMDialect
MLIRLLVMIRTransforms
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 8ca12695812802..1fa4b9025b752c 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -22,6 +22,7 @@
#include "mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
+#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
@@ -5222,6 +5223,55 @@ void ConvertCIRToLLVMPass::processCIRAttrs(mlir::ModuleOp module) {
lowerOpenCLModuleMetadataAttrs(module);
}
+namespace {
+/// Convert the argument types of a gpu.func. gpu.func is not a CIR op, but
+/// kernels outlined from CIR carry CIR types in their signature; without this
+/// the body would be lowered while the signature keeps CIR types.
+struct GPUFuncSignatureLowering
+ : public mlir::OpConversionPattern<mlir::gpu::GPUFuncOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ mlir::LogicalResult
+ matchAndRewrite(mlir::gpu::GPUFuncOp func, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const override {
+ const mlir::TypeConverter *converter = getTypeConverter();
+ mlir::Block &entry = func.getBody().front();
+ mlir::TypeConverter::SignatureConversion signature(entry.getNumArguments());
+ if (failed(converter->convertSignatureArgs(func.getArgumentTypes(),
+ signature)))
+ return mlir::failure();
+ // Workgroup and private attributions follow the arguments; keep them.
+ for (unsigned i = func.getNumArguments(), e = entry.getNumArguments();
+ i < e; ++i)
+ signature.addInputs(i, entry.getArgument(i).getType());
+ rewriter.applySignatureConversion(&entry, signature, converter);
+ rewriter.modifyOpInPlace(func, [&] {
+ func.setType(mlir::FunctionType::get(
+ func.getContext(),
+ signature.getConvertedTypes().take_front(func.getNumArguments()),
+ func.getFunctionType().getResults()));
+ });
+ return mlir::success();
+ }
+};
+
+/// Convert the kernel operands of gpu.launch_func to the types expected by the
+/// converted kernel signature.
+struct GPULaunchFuncOperandLowering
+ : public mlir::OpConversionPattern<mlir::gpu::LaunchFuncOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ mlir::LogicalResult
+ matchAndRewrite(mlir::gpu::LaunchFuncOp launch, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const override {
+ rewriter.modifyOpInPlace(launch, [&] {
+ launch.getKernelOperandsMutable().assign(adaptor.getKernelOperands());
+ });
+ return mlir::success();
+ }
+};
+} // namespace
+
void ConvertCIRToLLVMPass::runOnOperation() {
llvm::TimeTraceScope scope("Convert CIR to LLVM Pass");
@@ -5291,6 +5341,18 @@ void ConvertCIRToLLVMPass::runOnOperation() {
mlir::populateOpenMPToLLVMConversionPatterns(converter, patterns);
target.addIllegalDialect<mlir::BuiltinDialect, cir::CIRDialect,
mlir::func::FuncDialect>();
+ // Kernels outlined from CIR keep CIR types in their signature and launch
+ // operands; convert them so later GPU-to-LLVM lowerings see LLVM types.
+ patterns.add<GPUFuncSignatureLowering, GPULaunchFuncOperandLowering>(
+ converter, &getContext());
+ target.addDynamicallyLegalOp<mlir::gpu::GPUFuncOp>(
+ [&](mlir::gpu::GPUFuncOp func) {
+ return converter.isSignatureLegal(func.getFunctionType());
+ });
+ target.addDynamicallyLegalOp<mlir::gpu::LaunchFuncOp>(
+ [&](mlir::gpu::LaunchFuncOp launch) {
+ return converter.isLegal(launch.getKernelOperands().getTypes());
+ });
llvm::SmallVector<mlir::Operation *> ops;
ops.push_back(module);
diff --git a/clang/lib/CIR/RegisterAllDialects.cpp b/clang/lib/CIR/RegisterAllDialects.cpp
index c584527234b574..dee48c34986c30 100644
--- a/clang/lib/CIR/RegisterAllDialects.cpp
+++ b/clang/lib/CIR/RegisterAllDialects.cpp
@@ -10,6 +10,7 @@
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/DLTI/DLTI.h"
+#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h"
#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
@@ -27,7 +28,7 @@ void registerAllDialects(mlir::DialectRegistry ®istry) {
// pointer types as keys.
registry
.insert<mlir::BuiltinDialect, cir::CIRDialect, mlir::arith::ArithDialect,
- mlir::DLTIDialect, mlir::LLVM::LLVMDialect,
+ mlir::DLTIDialect, mlir::gpu::GPUDialect, mlir::LLVM::LLVMDialect,
mlir::omp::OpenMPDialect, mlir::acc::OpenACCDialect>();
// Register extensions to integrate CIR types with OpenACC and OpenMP.
cir::omp::registerOpenMPExtensions(registry);
diff --git a/clang/test/CIR/Lowering/gpu-func-signature.cir b/clang/test/CIR/Lowering/gpu-func-signature.cir
new file mode 100644
index 00000000000000..4a58d948c8dfcf
--- /dev/null
+++ b/clang/test/CIR/Lowering/gpu-func-signature.cir
@@ -0,0 +1,28 @@
+// RUN: cir-opt %s --cir-to-llvm | FileCheck %s
+
+// gpu.func kernels outlined from CIR carry CIR types in their signature. The
+// lowering converts the signature and the gpu.launch_func operands too.
+
+!s32i = !cir.int<s, 32>
+
+module attributes {cir.triple = "x86_64-unknown-linux-gnu", gpu.container_module} {
+ gpu.module @kernels {
+ // CHECK: gpu.func @k(%[[ARG:.*]]: !llvm.ptr) kernel {
+ // CHECK-NOT: cir.
+ // CHECK: llvm.store %{{.*}}, %[[ARG]] {{.*}} : i32, !llvm.ptr
+ gpu.func @k(%arg0: !cir.ptr<!s32i>) kernel {
+ %0 = cir.const #cir.int<42> : !s32i
+ cir.store align(4) %0, %arg0 : !s32i, !cir.ptr<!s32i>
+ gpu.return
+ }
+ }
+
+ // CHECK-LABEL: llvm.func @host
+ cir.func @host() {
+ %c1 = arith.constant 1 : index
+ %0 = cir.alloca "x" align(4) init : !cir.ptr<!s32i>
+ // CHECK: gpu.launch_func @kernels::@k blocks in ({{.*}}) threads in ({{.*}}) args(%{{.*}} : !llvm.ptr)
+ gpu.launch_func @kernels::@k blocks in (%c1, %c1, %c1) threads in (%c1, %c1, %c1) args(%0 : !cir.ptr<!s32i>)
+ cir.return
+ }
+}
More information about the cfe-commits
mailing list