[clang] [CIR][CUDA] Support built-in CUDA texture type (PR #214781)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 16 21:58:15 PDT 2026
https://github.com/Siya-05 updated https://github.com/llvm/llvm-project/pull/214781
>From 5f801e233214bcb10b95a9957b7b7594fd27a5df Mon Sep 17 00:00:00 2001
From: Sivapriya Venkateswarar <sivapriyaofficial2005 at gmail.com>
Date: Fri, 7 Aug 2026 19:53:10 +0400
Subject: [PATCH] [CIR][CUDA] Support built-in CUDA texture type
---
.../include/clang/CIR/Dialect/IR/CIRTypes.td | 17 +++++++++++-
clang/lib/CIR/CodeGen/CIRGenCUDANV.cpp | 24 +++++++++++++++--
clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 2 +-
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 3 +++
clang/test/CIR/CodeGenCUDA/texture.cu | 26 +++++++++++++++++++
5 files changed, 68 insertions(+), 4 deletions(-)
create mode 100644 clang/test/CIR/CodeGenCUDA/texture.cu
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index 29afaa6d41f4b..929d451cbbe8f 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -390,6 +390,20 @@ def CIR_VPtrType : CIR_Type<"VPtr", "vptr", [
}];
}
+//===----------------------------------------------------------------------===//
+// CUDADeviceTextureType
+//===----------------------------------------------------------------------===//
+
+def CIR_CUDADeviceTextureType :
+ CIR_Type<"CUDADeviceTexture", "cuda_texture"> {
+ let summary = "CUDA device texture reference type";
+ let description = [{
+ `!cir.cuda_texture` represents a CUDA built-in texture reference in CIR.
+ The type preserves texture semantics at the CIR level and is lowered to
+ the target device representation during lowering to LLVM IR.
+ }];
+}
+
//===----------------------------------------------------------------------===//
// BoolType
//===----------------------------------------------------------------------===//
@@ -968,7 +982,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_ComplexType, CIR_VPtrType, CIR_DataMemberType, CIR_MethodType,
+ CIR_ComplexType, CIR_VPtrType, CIR_CUDADeviceTextureType,
+ CIR_DataMemberType, CIR_MethodType,
CIR_EhTokenType, CIR_CleanupTokenType, CIR_CatchTokenType
]>;
diff --git a/clang/lib/CIR/CodeGen/CIRGenCUDANV.cpp b/clang/lib/CIR/CodeGen/CIRGenCUDANV.cpp
index 2288c8bf5a818..ab4baf336d379 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCUDANV.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenCUDANV.cpp
@@ -119,6 +119,24 @@ class CIRGenNVCUDARuntime : public CIRGenCUDARuntime {
cir::CUDADeviceVarKind::Surface,
});
}
+
+ void registerDeviceTex(const VarDecl *vd, cir::GlobalOp &var, bool isExtern) {
+ auto &builder = cgm.getBuilder();
+
+ var->setAttr(cir::CUDAVarRegistrationInfoAttr::getMnemonic(),
+ cir::CUDAVarRegistrationInfoAttr::get(
+ builder.getContext(),
+ getDeviceSideName(cast<NamedDecl>(vd)),
+ cir::CUDADeviceVarKind::Texture, isExtern,
+ /*isConstant=*/false,
+ /*isManaged=*/false));
+
+ deviceVars.push_back({
+ var,
+ vd,
+ cir::CUDADeviceVarKind::Texture,
+ });
+ }
};
} // namespace
@@ -484,8 +502,10 @@ void CIRGenNVCUDARuntime::handleVarRegistration(const VarDecl *vd,
registerDeviceSurf(vd, var, !vd->hasDefinition());
} else if (vd->getType()->isCUDADeviceBuiltinTextureType()) {
- cgm.errorNYI(vd->getSourceRange(),
- "handleVarRegistration: Texture registration");
+ // Builtin textures and their template arguments are also registered
+ // with CUDA runtime.
+ if (!vd->hasExternalStorage())
+ registerDeviceTex(vd, var, !vd->hasDefinition());
}
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index e5af4eec7720f..4ebc8d43b181a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -311,7 +311,7 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
cgm.getTargetCIRGenInfo().getCUDADeviceBuiltinSurfaceDeviceType())
return ty;
} else if (type->isCUDADeviceBuiltinTextureType()) {
- assert(!cir::MissingFeatures::cudaTextureType());
+ return cir::CUDADeviceTextureType::get(&cgm.getMLIRContext());
}
}
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 70fe6e22e77bc..4668aafe7872f 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -3694,6 +3694,9 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
assert(!cir::MissingFeatures::addressSpace());
return mlir::LLVM::LLVMPointerType::get(type.getContext());
});
+ converter.addConversion([&](cir::CUDADeviceTextureType type) -> mlir::Type {
+ return mlir::IntegerType::get(type.getContext(), 64);
+ });
converter.addConversion([&](cir::ArrayType type) -> mlir::Type {
mlir::Type ty =
convertTypeForMemory(converter, dataLayout, type.getElementType());
diff --git a/clang/test/CIR/CodeGenCUDA/texture.cu b/clang/test/CIR/CodeGenCUDA/texture.cu
new file mode 100644
index 0000000000000..3e669e365c201
--- /dev/null
+++ b/clang/test/CIR/CodeGenCUDA/texture.cu
@@ -0,0 +1,26 @@
+// REQUIRES: x86-registered-target
+// REQUIRES: nvptx-registered-target
+// RUN: %clang_cc1 -fclangir -std=c++11 -fcuda-is-device -triple nvptx64-nvidia-cuda -emit-cir -o - %s | FileCheck --check-prefix=CIR-DEVICE %s
+// RUN: %clang_cc1 -fclangir -std=c++11 -fcuda-is-device -triple nvptx64-nvidia-cuda -emit-llvm -o - %s | FileCheck --check-prefix=LLVM-DEVICE %s
+// RUN: %clang_cc1 -std=c++11 -fcuda-is-device -triple nvptx64-nvidia-cuda -emit-llvm -o - %s | FileCheck --check-prefix=OGCG-DEVICE %s
+
+struct textureReference {
+ int desc;
+};
+
+enum ReadMode {
+ ElementType = 0,
+ NormalizedFloat = 1
+};
+
+template <typename T, int dim = 1, ReadMode mode = ElementType>
+struct __attribute__((device_builtin_texture_type)) texture
+ : public textureReference {};
+
+texture<float, 2, NormalizedFloat> tex;
+
+// CIR-DEVICE: cir.global external target_address_space(1) @tex = #cir.undef : !cir.cuda_texture
+
+// CIR now matches OG CodeGen and emits undef for CUDA shadow variables.
+// LLVM-DEVICE: @tex ={{.*}} addrspace(1) externally_initialized global i64 undef
+// OGCG-DEVICE: @tex ={{.*}} addrspace(1) externally_initialized global i64 undef
More information about the cfe-commits
mailing list