https://github.com/Siya-05 updated https://github.com/llvm/llvm-project/pull/223013
>From 272088a1d31bab8c0d50d93587093bd26e69ae80 Mon Sep 17 00:00:00 2001 From: Sivapriya <[email protected]> Date: Fri, 11 Sep 2026 18:28:54 +0000 Subject: [PATCH 1/2] [CIR][CUDA] Add semantic surface reference type --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 17 ++++++++++++++++- clang/lib/CIR/CodeGen/Targets/NVPTX.cpp | 5 +---- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 3 +++ clang/test/CIR/CodeGenCUDA/surface.cu | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index c969bdbe322395..c8333e884e8c38 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -401,6 +401,20 @@ def CIR_VPtrType : CIR_Type<"VPtr", "vptr", [ }]; } +//===----------------------------------------------------------------------===// +// CUDADeviceSurfaceType +//===----------------------------------------------------------------------===// + +def CIR_CUDADeviceSurfaceType : + CIR_Type<"CUDADeviceSurface", "cuda_surface"> { + let summary = "CUDA device surface reference type"; + let description = [{ + `!cir.cuda_surface` represents a CUDA built-in surface reference in CIR. + The type preserves surface semantics at the CIR level and is lowered to + the target device representation during lowering to LLVM IR. + }]; +} + //===----------------------------------------------------------------------===// // CUDADeviceTextureType //===----------------------------------------------------------------------===// @@ -1165,7 +1179,8 @@ def CIR_AnyType : AnyTypeOf<[ CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, CIR_IntType, CIR_AnyFloatType, CIR_PointerType, CIR_FuncType, CIR_StructType, CIR_UnionType, CIR_BitFieldType, - CIR_ComplexType, CIR_VPtrType, CIR_CUDADeviceTextureType, + CIR_ComplexType, CIR_VPtrType, CIR_CUDADeviceSurfaceType, + CIR_CUDADeviceTextureType, CIR_DataMemberType, CIR_MethodType, CIR_EhTokenType, CIR_CleanupTokenType, CIR_CatchTokenType ]>; diff --git a/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp b/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp index 9d9b5b196ac5b2..8fecef08316156 100644 --- a/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp +++ b/clang/lib/CIR/CodeGen/Targets/NVPTX.cpp @@ -110,10 +110,7 @@ class NVPTXTargetCIRGenInfo : public TargetCIRGenInfo { } mlir::Type getCUDADeviceBuiltinSurfaceDeviceType() const override { - // On the device side, surface reference is represented as an object handle - // in 64-bit integer. - return cir::IntType::get(&getABIInfo().cgt.getMLIRContext(), 64, - /*isSigned=*/true); + return cir::CUDADeviceSurfaceType::get(&getABIInfo().cgt.getMLIRContext()); } mlir::Type getCUDADeviceBuiltinTextureDeviceType() const override { diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 31e01c770c55b4..b08f7e8f05b9af 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -3855,6 +3855,9 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter, assert(!cir::MissingFeatures::addressSpace()); return mlir::LLVM::LLVMPointerType::get(type.getContext()); }); + converter.addConversion([&](cir::CUDADeviceSurfaceType type) -> mlir::Type { + return mlir::IntegerType::get(type.getContext(), 64); + }); converter.addConversion([&](cir::CUDADeviceTextureType type) -> mlir::Type { return mlir::IntegerType::get(type.getContext(), 64); }); diff --git a/clang/test/CIR/CodeGenCUDA/surface.cu b/clang/test/CIR/CodeGenCUDA/surface.cu index 6ec9b53ad64731..67d7257c07c6e5 100644 --- a/clang/test/CIR/CodeGenCUDA/surface.cu +++ b/clang/test/CIR/CodeGenCUDA/surface.cu @@ -18,7 +18,7 @@ struct __attribute__((device_builtin_surface_type)) surface<void, dim> surface<void, 2> surf; -// CIR-DEVICE: cir.global external target_address_space(1) @surf = #cir.undef : !s64i +// CIR-DEVICE: cir.global external target_address_space(1) @surf = #cir.undef : !cir.cuda_surface // CIR now matches OG CodeGen and emits undef for CUDA shadow variables. // LLVM-DEVICE: @surf ={{.*}} addrspace(1) externally_initialized global i64 undef >From 223aa1c1c326bc6d94bd70ffee6a4ca0c60ce0c5 Mon Sep 17 00:00:00 2001 From: Sivapriya <[email protected]> Date: Mon, 14 Sep 2026 02:12:46 +0000 Subject: [PATCH 2/2] [CIR][CUDA] Address review comments --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 4 ++-- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index c8333e884e8c38..674f6cffb32f41 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -410,8 +410,8 @@ def CIR_CUDADeviceSurfaceType : let summary = "CUDA device surface reference type"; let description = [{ `!cir.cuda_surface` represents a CUDA built-in surface reference in CIR. - The type preserves surface semantics at the CIR level and is lowered to - the target device representation during lowering to LLVM IR. + The type preserves surface semantics at the CIR level and lowers to the NVPTX + device-side handle representation (`i64`) during lowering to LLVM IR. }]; } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index b08f7e8f05b9af..8ccaba5d4ab202 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -3855,6 +3855,8 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter, assert(!cir::MissingFeatures::addressSpace()); return mlir::LLVM::LLVMPointerType::get(type.getContext()); }); + // On the device side, surface reference is represented as an object handle + // in 64-bit integer. converter.addConversion([&](cir::CUDADeviceSurfaceType type) -> mlir::Type { return mlir::IntegerType::get(type.getContext(), 64); }); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
