| 1 | //===---- SPIRV.cpp - SPIR-V-specific CIR CodeGen -------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This provides SPIR/SPIRV-specific CIR CodeGen logic for function attributes. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "../CIRGenModule.h" |
| 14 | #include "../TargetInfo.h" |
| 15 | |
| 16 | #include "clang/AST/Attr.h" |
| 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 19 | #include "clang/CodeGenUtils/TargetUtils.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | using namespace clang::CIRGen; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class CommonSPIRABIInfo : public ABIInfo { |
| 27 | public: |
| 28 | CommonSPIRABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {} |
| 29 | }; |
| 30 | |
| 31 | class CommonSPIRTargetCIRGenInfo : public TargetCIRGenInfo { |
| 32 | public: |
| 33 | CommonSPIRTargetCIRGenInfo(CIRGenTypes &cgt) |
| 34 | : TargetCIRGenInfo(std::make_unique<CommonSPIRABIInfo>(args&: cgt)) {} |
| 35 | |
| 36 | mlir::ptr::MemorySpaceAttrInterface |
| 37 | getCIRAllocaAddressSpace() const override { |
| 38 | return cir::LangAddressSpaceAttr::get( |
| 39 | context: &getABIInfo().cgt.getMLIRContext(), |
| 40 | value: cir::LangAddressSpace::OffloadPrivate); |
| 41 | } |
| 42 | |
| 43 | mlir::ptr::MemorySpaceAttrInterface |
| 44 | getGlobalCIRAddressSpace(LangAS as) const override { |
| 45 | // The cir::LangAddressSpace fold loses the SPIRDefIsGenMap split between |
| 46 | // cuda_constant and opencl_constant, so use the target map like classic. |
| 47 | if (as == LangAS::cuda_device || as == LangAS::cuda_constant || |
| 48 | as == LangAS::cuda_shared) |
| 49 | return cir::TargetAddressSpaceAttr::get( |
| 50 | context: &getABIInfo().cgt.getMLIRContext(), |
| 51 | value: getABIInfo().cgt.getASTContext().getTargetAddressSpace(AS: as)); |
| 52 | return TargetCIRGenInfo::getGlobalCIRAddressSpace(as); |
| 53 | } |
| 54 | |
| 55 | void setTargetAttributes(const clang::Decl *decl, mlir::Operation *global, |
| 56 | CIRGenModule &cgm) const override { |
| 57 | auto func = mlir::dyn_cast<cir::FuncOp>(Val: global); |
| 58 | if (!func || func.isDeclaration()) |
| 59 | return; |
| 60 | |
| 61 | const auto *fd = dyn_cast_or_null<FunctionDecl>(Val: decl); |
| 62 | if (!fd) |
| 63 | return; |
| 64 | |
| 65 | if (!cgm.getLangOpts().HIP || !cgm.getTriple().isSPIRV() || |
| 66 | cgm.getTriple().getVendor() != llvm::Triple::AMD) |
| 67 | return; |
| 68 | |
| 69 | if (!fd->hasAttr<CUDAGlobalAttr>()) |
| 70 | return; |
| 71 | |
| 72 | unsigned n = cgm.getLangOpts().GPUMaxThreadsPerBlock; |
| 73 | if (const auto *flatWGS = fd->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 74 | n = flatWGS->getMax() |
| 75 | ->EvaluateKnownConstInt(Ctx: cgm.getASTContext()) |
| 76 | .getExtValue(); |
| 77 | } else if (const auto *lb = fd->getAttr<CUDALaunchBoundsAttr>()) { |
| 78 | if (uint64_t maxThreads = lb->getMaxThreads() |
| 79 | ->EvaluateKnownConstInt(Ctx: cgm.getASTContext()) |
| 80 | .getExtValue()) |
| 81 | n = maxThreads; |
| 82 | } |
| 83 | |
| 84 | // Only x carries the flat WG size, reverse translated for AMDGPU targets. |
| 85 | func->setAttr( |
| 86 | name: cir::CIRDialect::getMaxWorkGroupSizeAttrName(), |
| 87 | value: cir::MaxWorkGroupSizeAttr::get(context: func.getContext(), x: n, /*y=*/1, /*z=*/1)); |
| 88 | } |
| 89 | |
| 90 | cir::CallingConv getDeviceKernelCallingConv() const override { |
| 91 | return cir::CallingConv::SpirKernel; |
| 92 | } |
| 93 | |
| 94 | bool supportsLibCall() const override { |
| 95 | const llvm::Triple &triple = getABIInfo().cgt.getCGModule().getTriple(); |
| 96 | return !(triple.isSPIRV() && triple.getVendor() == llvm::Triple::AMD); |
| 97 | } |
| 98 | |
| 99 | void setCUDAKernelCallingConvention(const FunctionType *&ft) const override { |
| 100 | // Convert HIP kernels to SPIR-V kernels. |
| 101 | if (getABIInfo().cgt.getASTContext().getLangOpts().HIP) |
| 102 | ft = getABIInfo().cgt.getASTContext().adjustFunctionType( |
| 103 | Fn: ft, EInfo: ft->getExtInfo().withCallingConv(cc: CC_DeviceKernel)); |
| 104 | } |
| 105 | |
| 106 | mlir::Value getNullPointer(CIRGenModule &cgm, cir::PointerType ptrTy, |
| 107 | QualType qt, mlir::Location loc) const override; |
| 108 | }; |
| 109 | |
| 110 | } // namespace |
| 111 | |
| 112 | // The bit pattern of null in non-generic AS is unspecified for SPIR(-V), so |
| 113 | // materialize it via an address space cast from null in generic AS. |
| 114 | mlir::Value |
| 115 | CommonSPIRTargetCIRGenInfo::getNullPointer(CIRGenModule &cgm, |
| 116 | cir::PointerType ptrTy, QualType qt, |
| 117 | mlir::Location loc) const { |
| 118 | CIRGenBuilderTy &builder = cgm.getBuilder(); |
| 119 | if (!CodeGenUtils::spirNullPointerNeedsGenericCast(QT: qt, Triple: cgm.getTriple())) |
| 120 | return builder.getNullPtr(ty: ptrTy, loc); |
| 121 | |
| 122 | cir::PointerType genericPtrTy = |
| 123 | builder.getPointerTo(ty: ptrTy.getPointee(), langAS: LangAS::opencl_generic); |
| 124 | return builder.createAddrSpaceCast(loc, src: builder.getNullPtr(ty: genericPtrTy, loc), |
| 125 | newTy: ptrTy); |
| 126 | } |
| 127 | |
| 128 | std::unique_ptr<TargetCIRGenInfo> |
| 129 | clang::CIRGen::createCommonSPIRTargetCIRGenInfo(CIRGenTypes &cgt) { |
| 130 | return std::make_unique<CommonSPIRTargetCIRGenInfo>(args&: cgt); |
| 131 | } |
| 132 | |