| 1 | //===- SPIRV.cpp - SPIR-V ABI Implementation -----------------------------===// |
| 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 | #include "llvm/ABI/DefaultTargetInfo.h" |
| 10 | #include "llvm/ABI/FunctionInfo.h" |
| 11 | #include "llvm/ABI/TargetInfo.h" |
| 12 | #include "llvm/ABI/Types.h" |
| 13 | #include "llvm/IR/CallingConv.h" |
| 14 | #include "llvm/Support/Casting.h" |
| 15 | #include "llvm/Support/SPIRVAddrSpace.h" |
| 16 | |
| 17 | namespace llvm::abi { |
| 18 | |
| 19 | // The default classification applies, except for SPIR_KERNEL arguments, which |
| 20 | // SPIR-V restricts in which address spaces they may point to and requires to be |
| 21 | // copied when they are aggregates. |
| 22 | class SPIRVTargetInfo final : public DefaultTargetInfo { |
| 23 | private: |
| 24 | ABICompatInfo CompatInfo; |
| 25 | |
| 26 | /// Target address space of the language's default address space. |
| 27 | unsigned DefaultAddrSpace; |
| 28 | |
| 29 | /// Pass kernel aggregates byval. Set by the front end for device |
| 30 | /// compilations. |
| 31 | bool KernelPassAggregatesIndirect; |
| 32 | |
| 33 | ArgInfo classifyKernelArgumentType(const Type *ArgTy) const { |
| 34 | // Kernel arguments may not be default address space pointers, so pass them |
| 35 | // as CrossWorkgroup pointers. An atomic pointer of the same size is passed |
| 36 | // as the pointer itself. |
| 37 | const Type *Ty = ArgTy; |
| 38 | if (const auto *AT = dyn_cast<AtomicType>(Val: Ty); |
| 39 | AT && AT->getSizeInBits() == AT->getValueType()->getSizeInBits()) |
| 40 | Ty = AT->getValueType(); |
| 41 | if (const auto *PtrTy = dyn_cast<PointerType>(Val: Ty); |
| 42 | PtrTy && PtrTy->getAddrSpace() == DefaultAddrSpace) { |
| 43 | const Type *Coerced = TB.getPointerType( |
| 44 | Size: PtrTy->getSizeInBits().getFixedValue(), Align: PtrTy->getAlignment(), |
| 45 | Addrspace: SPIRVAS::CrossWorkgroup, /*HasAddressSpaceQualifier=*/true); |
| 46 | return ArgInfo::getDirect(T: Coerced, /*Offset=*/0, /*Align=*/std::nullopt, |
| 47 | /*CanBeFlattened=*/false); |
| 48 | } |
| 49 | |
| 50 | // Force copying aggregate type in kernel arguments by value. This is |
| 51 | // required for the object copied to be valid on the device. TODO: |
| 52 | // hardcoding to 0 should be revisited if HIPSPV / byval starts making use |
| 53 | // of the AS of an indirect arg. |
| 54 | if (KernelPassAggregatesIndirect && isAggregateTypeForABI(Ty: ArgTy)) |
| 55 | return getNaturalAlignIndirect(Ty: ArgTy, /*AddrSpace=*/0, /*ByVal=*/true); |
| 56 | |
| 57 | return classifyArgumentType(Ty: ArgTy); |
| 58 | } |
| 59 | |
| 60 | public: |
| 61 | SPIRVTargetInfo(TypeBuilder &Builder, unsigned DefaultAddrSpace, |
| 62 | bool KernelPassAggregatesIndirect) |
| 63 | : DefaultTargetInfo(Builder), DefaultAddrSpace(DefaultAddrSpace), |
| 64 | KernelPassAggregatesIndirect(KernelPassAggregatesIndirect) {} |
| 65 | |
| 66 | const ABICompatInfo &getABICompatInfo() const override { return CompatInfo; } |
| 67 | |
| 68 | bool hasInt128Type() const override { return false; } |
| 69 | |
| 70 | // A record that cannot be copied is constructed in place, so its sret |
| 71 | // pointer follows the default address space like a "this" pointer. |
| 72 | unsigned getSRetAddrSpace() const override { return DefaultAddrSpace; } |
| 73 | |
| 74 | void computeInfo(FunctionInfo &FI) const override { |
| 75 | // The logic is the same as in DefaultTargetInfo, except for kernel |
| 76 | // arguments. |
| 77 | if (!maybeCommonClassifyReturnType(FI)) |
| 78 | FI.getReturnInfo() = classifyReturnType(RetTy: FI.getReturnType()); |
| 79 | |
| 80 | bool IsKernel = FI.getCallingConvention() == CallingConv::SPIR_KERNEL; |
| 81 | for (auto &I : FI.arguments()) |
| 82 | I.Info = IsKernel ? classifyKernelArgumentType(ArgTy: I.ABIType) |
| 83 | : classifyArgumentType(Ty: I.ABIType); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | std::unique_ptr<TargetInfo> |
| 88 | createSPIRVTargetInfo(TypeBuilder &TB, unsigned DefaultAddrSpace, |
| 89 | bool KernelPassAggregatesIndirect) { |
| 90 | return std::make_unique<SPIRVTargetInfo>(args&: TB, args&: DefaultAddrSpace, |
| 91 | args&: KernelPassAggregatesIndirect); |
| 92 | } |
| 93 | |
| 94 | } // namespace llvm::abi |
| 95 | |