18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Support/NVVMAttributes.h"
28static void handleCUDALaunchBoundsAttr(
const CUDALaunchBoundsAttr *
attr,
31 auto setNVVMAttr = [&](llvm::StringRef
name,
const llvm::APSInt &value) {
32 func->setAttr((
"cir." + name).str(),
33 builder.getStringAttr(llvm::utostr(value.getExtValue())));
36 llvm::APSInt maxThreads(32);
40 setNVVMAttr(llvm::NVVMAttr::MaxNTID, maxThreads);
45 if (
attr->getMinBlocks()) {
46 llvm::APSInt minBlocks(32);
50 setNVVMAttr(llvm::NVVMAttr::MinCTASm, minBlocks);
53 if (
attr->getMaxBlocks()) {
54 llvm::APSInt maxBlocks(32);
58 setNVVMAttr(llvm::NVVMAttr::MaxClusterRank, maxBlocks);
62class NVPTXABIInfo :
public ABIInfo {
64 NVPTXABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
69 NVPTXTargetCIRGenInfo(CIRGenTypes &cgt)
70 : TargetCIRGenInfo(std::make_unique<NVPTXABIInfo>(cgt)) {}
72 void setTargetAttributes(
const clang::Decl *
decl, mlir::Operation *global,
73 CIRGenModule &cgm)
const override {
74 auto globalValue = mlir::dyn_cast<cir::CIRGlobalValueInterface>(global);
75 if (globalValue && globalValue.isDeclaration())
78 const auto *vd = dyn_cast_or_null<VarDecl>(
decl);
81 if (vd->getType()->isCUDADeviceBuiltinSurfaceType() ||
82 vd->getType()->isCUDADeviceBuiltinTextureType())
88 const auto *fd = dyn_cast_or_null<FunctionDecl>(
decl);
92 auto func = mlir::cast<cir::FuncOp>(global);
98 if (fd->hasAttr<DeviceKernelAttr>() || fd->hasAttr<CUDAGlobalAttr>()) {
101 func.setInlineKind(cir::InlineKind::NoInline);
102 if (fd->hasAttr<CUDAGlobalAttr>()) {
103 func.setCallingConv(getDeviceKernelCallingConv());
106 if (
const auto *attr = fd->getAttr<CUDALaunchBoundsAttr>())
107 handleCUDALaunchBoundsAttr(attr, func, cgm, cgm.
getBuilder());
112 cir::CallingConv getDeviceKernelCallingConv()
const override {
113 return cir::CallingConv::PTXKernel;
116 mlir::Type getCUDADeviceBuiltinSurfaceDeviceType()
const override {
117 return cir::CUDADeviceSurfaceType::get(&getABIInfo().cgt.getMLIRContext());
120 mlir::Type getCUDADeviceBuiltinTextureDeviceType()
const override {
121 return cir::CUDADeviceTextureType::get(&getABIInfo().cgt.getMLIRContext());
127 mlir::Value getNullPointer(CIRGenModule &cgm, cir::PointerType ptrTy,
128 QualType qt, mlir::Location loc)
const override {
132 mlir::ptr::MemorySpaceAttrInterface addrSpace = ptrTy.getAddrSpace();
134 mlir::dyn_cast_if_present<cir::TargetAddressSpaceAttr>(addrSpace);
138 targetAS.getValue() ==
143 cir::PointerType genericPtrTy =
144 builder.
getPointerTo(ptrTy.getPointee(), LangAS::opencl_generic);
146 loc, builder.
getNullPtr(genericPtrTy, loc), ptrTy);
152std::unique_ptr<TargetCIRGenInfo>
154 return std::make_unique<NVPTXTargetCIRGenInfo>(cgt);
cir::PointerType getPointerTo(mlir::Type ty)
cir::ConstantOp getNullPtr(mlir::Type ty, mlir::Location loc)
mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src, mlir::Type newTy)
unsigned getTargetAddressSpace(LangAS AS) const
This class organizes the cross-function state that is used while generating CIR code.
clang::ASTContext & getASTContext() const
CIRGenBuilderTy & getBuilder()
const clang::LangOptions & getLangOpts() const
This class organizes the cross-module state that is used while lowering AST types to CIR types.
bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS, clang::LangAS as)
std::unique_ptr< TargetCIRGenInfo > createNVPTXTargetCIRGenInfo(CIRGenTypes &cgt)
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Top level wrappers for InstallAPI frontend operations.
static bool emitNVVMMetadata()
static bool opFuncParameterAttributes()