Skip to content

Commit fd3f5f8

Browse files
authored
[CIR][CUDA] Support for inbuilt texture types (#1469)
I have now fixed the test. Earlier I made some commits with other changes because we were testing something on my fork. This should be resolved now
1 parent 33b9a2d commit fd3f5f8

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,11 @@ bool CIRGenModule::shouldEmitCUDAGlobalVar(const VarDecl *global) const {
569569
// size and host-side address in order to provide access to
570570
// their device-side incarnations.
571571

572-
if (global->getType()->isCUDADeviceBuiltinTextureType()) {
573-
llvm_unreachable("NYI");
574-
}
575-
576572
return !langOpts.CUDAIsDevice || global->hasAttr<CUDADeviceAttr>() ||
577573
global->hasAttr<CUDAConstantAttr>() ||
578574
global->hasAttr<CUDASharedAttr>() ||
579-
global->getType()->isCUDADeviceBuiltinSurfaceType();
575+
global->getType()->isCUDADeviceBuiltinSurfaceType() ||
576+
global->getType()->isCUDADeviceBuiltinTextureType();
580577
}
581578

582579
void CIRGenModule::emitGlobal(GlobalDecl gd) {
@@ -1212,7 +1209,6 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
12121209
// initializer.
12131210
if (gv.isDeclaration()) {
12141211
getTargetCIRGenInfo().setTargetAttributes(d, gv, *this);
1215-
12161212
// External HIP managed variables needed to be recorded for transformation
12171213
// in both device and host compilations.
12181214
if (getLangOpts().CUDA && d && d->hasAttr<HIPManagedAttr>() &&
@@ -1493,7 +1489,8 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
14931489
// because they must not be initialized.
14941490
if (linkage != cir::GlobalLinkageKind::InternalLinkage &&
14951491
(d->hasAttr<CUDADeviceAttr>() || d->hasAttr<CUDAConstantAttr>() ||
1496-
d->getType()->isCUDADeviceBuiltinSurfaceType())) {
1492+
d->getType()->isCUDADeviceBuiltinSurfaceType() ||
1493+
d->getType()->isCUDADeviceBuiltinTextureType())) {
14971494
gv->setAttr(CUDAExternallyInitializedAttr::getMnemonic(),
14981495
CUDAExternallyInitializedAttr::get(&getMLIRContext()));
14991496
}

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,11 @@ mlir::Type CIRGenTypes::convertType(QualType T) {
357357
if (mlir::Type Ty =
358358
CGM.getTargetCIRGenInfo().getCUDADeviceBuiltinSurfaceDeviceType())
359359
return Ty;
360+
llvm_unreachable("NYI");
360361
} else if (T->isCUDADeviceBuiltinTextureType()) {
362+
if (mlir::Type Ty =
363+
CGM.getTargetCIRGenInfo().getCUDADeviceBuiltinTextureDeviceType())
364+
return Ty;
361365
llvm_unreachable("NYI");
362366
}
363367
}

clang/lib/CIR/CodeGen/TargetInfo.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,16 @@ class NVPTXTargetCIRGenInfo : public TargetCIRGenInfo {
345345
public:
346346
NVPTXTargetCIRGenInfo(CIRGenTypes &cgt)
347347
: TargetCIRGenInfo(std::make_unique<NVPTXABIInfo>(cgt)) {}
348-
349348
mlir::Type getCUDADeviceBuiltinSurfaceDeviceType() const override {
349+
// On the device side, surface reference is represented as an object handle
350+
// in 64-bit integer.
351+
return cir::IntType::get(&getABIInfo().CGT.getMLIRContext(), 64, true);
352+
}
353+
mlir::Type getCUDADeviceBuiltinTextureDeviceType() const override {
350354
// On the device side, texture reference is represented as an object handle
351355
// in 64-bit integer.
352356
return cir::IntType::get(&getABIInfo().CGT.getMLIRContext(), 64, true);
353357
}
354-
355358
void setTargetAttributes(const clang::Decl *decl, mlir::Operation *global,
356359
CIRGenModule &cgm) const override {
357360
if (const auto *vd = clang::dyn_cast_or_null<clang::VarDecl>(decl)) {

clang/lib/CIR/CodeGen/TargetInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ class TargetCIRGenInfo {
132132
virtual mlir::Type getCUDADeviceBuiltinSurfaceDeviceType() const {
133133
return nullptr;
134134
}
135+
virtual mlir::Type getCUDADeviceBuiltinTextureDeviceType() const {
136+
return nullptr;
137+
}
135138
virtual ~TargetCIRGenInfo() {}
136139
};
137140

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// REQUIRES: x86-registered-target
2+
// REQUIRES: nvptx-registered-target
3+
4+
// RUN: %clang_cc1 -fclangir -std=c++11 -fcuda-is-device -triple nvptx64-nvidia-cuda -emit-llvm -o - %s | FileCheck --check-prefix=DEVICE-LLVM %s
5+
// RUN: %clang_cc1 -fclangir -std=c++11 -fcuda-is-device -triple nvptx64-nvidia-cuda -emit-cir -o - %s | FileCheck --check-prefix=DEVICE-CIR %s
6+
// RUN: echo "GPU binary would be here" > %t
7+
8+
struct textureReference {
9+
int desc;
10+
};
11+
12+
enum ReadMode {
13+
ElementType = 0,
14+
NormalizedFloat = 1
15+
};
16+
17+
template <typename T, int dim = 1, enum ReadMode mode = ElementType>
18+
struct __attribute__((device_builtin_texture_type)) texture : public textureReference {
19+
};
20+
21+
texture<float, 2, NormalizedFloat> tex;
22+
23+
// DEVICE-LLVM: @tex = addrspace(1) externally_initialized global i64 undef, align 4
24+
// DEVICE-CIR: cir.global external addrspace(offload_global) @tex = #cir.undef : !s64i {alignment = 4 : i64, cu.externally_initialized = #cir.cu.externally_initialized}

0 commit comments

Comments
 (0)