1349cc55cSDimitry Andric //===--- SPIR.h - Declare SPIR and SPIR-V target feature support *- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 9349cc55cSDimitry Andric // This file declares SPIR and SPIR-V TargetInfo objects. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_SPIR_H 140b57cec5SDimitry Andric #define LLVM_CLANG_LIB_BASIC_TARGETS_SPIR_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h" 170b57cec5SDimitry Andric #include "clang/Basic/TargetOptions.h" 180b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 190b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric namespace clang { 220b57cec5SDimitry Andric namespace targets { 230b57cec5SDimitry Andric 24349cc55cSDimitry Andric // Used by both the SPIR and SPIR-V targets. 25fe6060f1SDimitry Andric static const unsigned SPIRDefIsPrivMap[] = { 260b57cec5SDimitry Andric 0, // Default 270b57cec5SDimitry Andric 1, // opencl_global 280b57cec5SDimitry Andric 3, // opencl_local 290b57cec5SDimitry Andric 2, // opencl_constant 300b57cec5SDimitry Andric 0, // opencl_private 310b57cec5SDimitry Andric 4, // opencl_generic 32e8d8bef9SDimitry Andric 5, // opencl_global_device 33e8d8bef9SDimitry Andric 6, // opencl_global_host 340b57cec5SDimitry Andric 0, // cuda_device 350b57cec5SDimitry Andric 0, // cuda_constant 36480093f4SDimitry Andric 0, // cuda_shared 37fe6060f1SDimitry Andric // SYCL address space values for this map are dummy 38fe6060f1SDimitry Andric 0, // sycl_global 39fe6060f1SDimitry Andric 0, // sycl_global_device 40fe6060f1SDimitry Andric 0, // sycl_global_host 41fe6060f1SDimitry Andric 0, // sycl_local 42fe6060f1SDimitry Andric 0, // sycl_private 43fe6060f1SDimitry Andric 0, // ptr32_sptr 44fe6060f1SDimitry Andric 0, // ptr32_uptr 45fe6060f1SDimitry Andric 0 // ptr64 46fe6060f1SDimitry Andric }; 47fe6060f1SDimitry Andric 48349cc55cSDimitry Andric // Used by both the SPIR and SPIR-V targets. 49fe6060f1SDimitry Andric static const unsigned SPIRDefIsGenMap[] = { 50fe6060f1SDimitry Andric 4, // Default 51fe6060f1SDimitry Andric // OpenCL address space values for this map are dummy and they can't be used 52fe6060f1SDimitry Andric 0, // opencl_global 53fe6060f1SDimitry Andric 0, // opencl_local 54fe6060f1SDimitry Andric 0, // opencl_constant 55fe6060f1SDimitry Andric 0, // opencl_private 56fe6060f1SDimitry Andric 0, // opencl_generic 57fe6060f1SDimitry Andric 0, // opencl_global_device 58fe6060f1SDimitry Andric 0, // opencl_global_host 594824e7fdSDimitry Andric // cuda_* address space mapping is intended for HIPSPV (HIP to SPIR-V 604824e7fdSDimitry Andric // translation). This mapping is enabled when the language mode is HIP. 614824e7fdSDimitry Andric 1, // cuda_device 624824e7fdSDimitry Andric // cuda_constant pointer can be casted to default/"flat" pointer, but in 634824e7fdSDimitry Andric // SPIR-V casts between constant and generic pointers are not allowed. For 644824e7fdSDimitry Andric // this reason cuda_constant is mapped to SPIR-V CrossWorkgroup. 654824e7fdSDimitry Andric 1, // cuda_constant 664824e7fdSDimitry Andric 3, // cuda_shared 67fe6060f1SDimitry Andric 1, // sycl_global 68fe6060f1SDimitry Andric 5, // sycl_global_device 69fe6060f1SDimitry Andric 6, // sycl_global_host 70fe6060f1SDimitry Andric 3, // sycl_local 71fe6060f1SDimitry Andric 0, // sycl_private 72480093f4SDimitry Andric 0, // ptr32_sptr 73480093f4SDimitry Andric 0, // ptr32_uptr 74480093f4SDimitry Andric 0 // ptr64 750b57cec5SDimitry Andric }; 760b57cec5SDimitry Andric 77349cc55cSDimitry Andric // Base class for SPIR and SPIR-V target info. 78349cc55cSDimitry Andric class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo { 79349cc55cSDimitry Andric protected: 80349cc55cSDimitry Andric BaseSPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &) 810b57cec5SDimitry Andric : TargetInfo(Triple) { 824824e7fdSDimitry Andric assert((Triple.isSPIR() || Triple.isSPIRV()) && 834824e7fdSDimitry Andric "Invalid architecture for SPIR or SPIR-V."); 840b57cec5SDimitry Andric assert(getTriple().getOS() == llvm::Triple::UnknownOS && 85349cc55cSDimitry Andric "SPIR(-V) target must use unknown OS"); 860b57cec5SDimitry Andric assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment && 87349cc55cSDimitry Andric "SPIR(-V) target must use unknown environment type"); 880b57cec5SDimitry Andric TLSSupported = false; 890b57cec5SDimitry Andric VLASupported = false; 900b57cec5SDimitry Andric LongWidth = LongAlign = 64; 91fe6060f1SDimitry Andric AddrSpaceMap = &SPIRDefIsPrivMap; 920b57cec5SDimitry Andric UseAddrSpaceMapMangling = true; 930b57cec5SDimitry Andric HasLegalHalfType = true; 940b57cec5SDimitry Andric HasFloat16 = true; 950b57cec5SDimitry Andric // Define available target features 960b57cec5SDimitry Andric // These must be defined in sorted order! 970b57cec5SDimitry Andric NoAsmVariants = true; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 100349cc55cSDimitry Andric public: 1010b57cec5SDimitry Andric // SPIR supports the half type and the only llvm intrinsic allowed in SPIR is 1020b57cec5SDimitry Andric // memcpy as per section 3 of the SPIR spec. 1030b57cec5SDimitry Andric bool useFP16ConversionIntrinsics() const override { return false; } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric ArrayRef<Builtin::Info> getTargetBuiltins() const override { return None; } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric const char *getClobbers() const override { return ""; } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric ArrayRef<const char *> getGCCRegNames() const override { return None; } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric bool validateAsmConstraint(const char *&Name, 1120b57cec5SDimitry Andric TargetInfo::ConstraintInfo &info) const override { 1130b57cec5SDimitry Andric return true; 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override { 1170b57cec5SDimitry Andric return None; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric BuiltinVaListKind getBuiltinVaListKind() const override { 1210b57cec5SDimitry Andric return TargetInfo::VoidPtrBuiltinVaList; 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 124fe6060f1SDimitry Andric Optional<unsigned> 125fe6060f1SDimitry Andric getDWARFAddressSpace(unsigned AddressSpace) const override { 126fe6060f1SDimitry Andric return AddressSpace; 127fe6060f1SDimitry Andric } 128fe6060f1SDimitry Andric 1290b57cec5SDimitry Andric CallingConvCheckResult checkCallingConvention(CallingConv CC) const override { 1300b57cec5SDimitry Andric return (CC == CC_SpirFunction || CC == CC_OpenCLKernel) ? CCCR_OK 1310b57cec5SDimitry Andric : CCCR_Warning; 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric CallingConv getDefaultCallingConv() const override { 1350b57cec5SDimitry Andric return CC_SpirFunction; 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric 138fe6060f1SDimitry Andric void setAddressSpaceMap(bool DefaultIsGeneric) { 139fe6060f1SDimitry Andric AddrSpaceMap = DefaultIsGeneric ? &SPIRDefIsGenMap : &SPIRDefIsPrivMap; 140fe6060f1SDimitry Andric } 141fe6060f1SDimitry Andric 142fe6060f1SDimitry Andric void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override { 143fe6060f1SDimitry Andric TargetInfo::adjust(Diags, Opts); 144fe6060f1SDimitry Andric // FIXME: SYCL specification considers unannotated pointers and references 145fe6060f1SDimitry Andric // to be pointing to the generic address space. See section 5.9.3 of 146fe6060f1SDimitry Andric // SYCL 2020 specification. 1474824e7fdSDimitry Andric // Currently, there is no way of representing SYCL's and HIP's default 1484824e7fdSDimitry Andric // address space language semantic along with the semantics of embedded C's 1494824e7fdSDimitry Andric // default address space in the same address space map. Hence the map needs 1504824e7fdSDimitry Andric // to be reset to allow mapping to the desired value of 'Default' entry for 1514824e7fdSDimitry Andric // SYCL and HIP. 1524824e7fdSDimitry Andric setAddressSpaceMap( 1534824e7fdSDimitry Andric /*DefaultIsGeneric=*/Opts.SYCLIsDevice || 1544824e7fdSDimitry Andric // The address mapping from HIP language for device code is only defined 1554824e7fdSDimitry Andric // for SPIR-V. 1564824e7fdSDimitry Andric (getTriple().isSPIRV() && Opts.HIP && Opts.CUDAIsDevice)); 157fe6060f1SDimitry Andric } 158fe6060f1SDimitry Andric 1590b57cec5SDimitry Andric void setSupportedOpenCLOpts() override { 1600b57cec5SDimitry Andric // Assume all OpenCL extensions and optional core features are supported 161349cc55cSDimitry Andric // for SPIR and SPIR-V since they are generic targets. 162e8d8bef9SDimitry Andric supportAllOpenCLOpts(); 1630b57cec5SDimitry Andric } 1645ffd83dbSDimitry Andric 165*0eae32dcSDimitry Andric bool hasBitIntType() const override { return true; } 166e8d8bef9SDimitry Andric 167e8d8bef9SDimitry Andric bool hasInt128Type() const override { return false; } 1680b57cec5SDimitry Andric }; 169fe6060f1SDimitry Andric 170349cc55cSDimitry Andric class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public BaseSPIRTargetInfo { 171349cc55cSDimitry Andric public: 172349cc55cSDimitry Andric SPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) 173349cc55cSDimitry Andric : BaseSPIRTargetInfo(Triple, Opts) { 1744824e7fdSDimitry Andric assert(Triple.isSPIR() && "Invalid architecture for SPIR."); 175349cc55cSDimitry Andric assert(getTriple().getOS() == llvm::Triple::UnknownOS && 176349cc55cSDimitry Andric "SPIR target must use unknown OS"); 177349cc55cSDimitry Andric assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment && 178349cc55cSDimitry Andric "SPIR target must use unknown environment type"); 179349cc55cSDimitry Andric } 180349cc55cSDimitry Andric 181349cc55cSDimitry Andric void getTargetDefines(const LangOptions &Opts, 182349cc55cSDimitry Andric MacroBuilder &Builder) const override; 183349cc55cSDimitry Andric 184349cc55cSDimitry Andric bool hasFeature(StringRef Feature) const override { 185349cc55cSDimitry Andric return Feature == "spir"; 186349cc55cSDimitry Andric } 187349cc55cSDimitry Andric }; 188349cc55cSDimitry Andric 1890b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo { 1900b57cec5SDimitry Andric public: 1910b57cec5SDimitry Andric SPIR32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) 1920b57cec5SDimitry Andric : SPIRTargetInfo(Triple, Opts) { 1934824e7fdSDimitry Andric assert(Triple.getArch() == llvm::Triple::spir && 1944824e7fdSDimitry Andric "Invalid architecture for 32-bit SPIR."); 1950b57cec5SDimitry Andric PointerWidth = PointerAlign = 32; 1960b57cec5SDimitry Andric SizeType = TargetInfo::UnsignedInt; 1970b57cec5SDimitry Andric PtrDiffType = IntPtrType = TargetInfo::SignedInt; 1980b57cec5SDimitry Andric resetDataLayout("e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-" 1990b57cec5SDimitry Andric "v96:128-v192:256-v256:256-v512:512-v1024:1024"); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric void getTargetDefines(const LangOptions &Opts, 2030b57cec5SDimitry Andric MacroBuilder &Builder) const override; 2040b57cec5SDimitry Andric }; 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY SPIR64TargetInfo : public SPIRTargetInfo { 2070b57cec5SDimitry Andric public: 2080b57cec5SDimitry Andric SPIR64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) 2090b57cec5SDimitry Andric : SPIRTargetInfo(Triple, Opts) { 2104824e7fdSDimitry Andric assert(Triple.getArch() == llvm::Triple::spir64 && 2114824e7fdSDimitry Andric "Invalid architecture for 64-bit SPIR."); 2120b57cec5SDimitry Andric PointerWidth = PointerAlign = 64; 2130b57cec5SDimitry Andric SizeType = TargetInfo::UnsignedLong; 2140b57cec5SDimitry Andric PtrDiffType = IntPtrType = TargetInfo::SignedLong; 2150b57cec5SDimitry Andric resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-" 2160b57cec5SDimitry Andric "v96:128-v192:256-v256:256-v512:512-v1024:1024"); 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric void getTargetDefines(const LangOptions &Opts, 2200b57cec5SDimitry Andric MacroBuilder &Builder) const override; 2210b57cec5SDimitry Andric }; 222349cc55cSDimitry Andric 223349cc55cSDimitry Andric class LLVM_LIBRARY_VISIBILITY SPIRVTargetInfo : public BaseSPIRTargetInfo { 224349cc55cSDimitry Andric public: 225349cc55cSDimitry Andric SPIRVTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) 226349cc55cSDimitry Andric : BaseSPIRTargetInfo(Triple, Opts) { 2274824e7fdSDimitry Andric assert(Triple.isSPIRV() && "Invalid architecture for SPIR-V."); 228349cc55cSDimitry Andric assert(getTriple().getOS() == llvm::Triple::UnknownOS && 229349cc55cSDimitry Andric "SPIR-V target must use unknown OS"); 230349cc55cSDimitry Andric assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment && 231349cc55cSDimitry Andric "SPIR-V target must use unknown environment type"); 232349cc55cSDimitry Andric } 233349cc55cSDimitry Andric 234349cc55cSDimitry Andric void getTargetDefines(const LangOptions &Opts, 235349cc55cSDimitry Andric MacroBuilder &Builder) const override; 236349cc55cSDimitry Andric 237349cc55cSDimitry Andric bool hasFeature(StringRef Feature) const override { 238349cc55cSDimitry Andric return Feature == "spirv"; 239349cc55cSDimitry Andric } 240349cc55cSDimitry Andric }; 241349cc55cSDimitry Andric 242349cc55cSDimitry Andric class LLVM_LIBRARY_VISIBILITY SPIRV32TargetInfo : public SPIRVTargetInfo { 243349cc55cSDimitry Andric public: 244349cc55cSDimitry Andric SPIRV32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) 245349cc55cSDimitry Andric : SPIRVTargetInfo(Triple, Opts) { 2464824e7fdSDimitry Andric assert(Triple.getArch() == llvm::Triple::spirv32 && 2474824e7fdSDimitry Andric "Invalid architecture for 32-bit SPIR-V."); 248349cc55cSDimitry Andric PointerWidth = PointerAlign = 32; 249349cc55cSDimitry Andric SizeType = TargetInfo::UnsignedInt; 250349cc55cSDimitry Andric PtrDiffType = IntPtrType = TargetInfo::SignedInt; 251349cc55cSDimitry Andric resetDataLayout("e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-" 252349cc55cSDimitry Andric "v96:128-v192:256-v256:256-v512:512-v1024:1024"); 253349cc55cSDimitry Andric } 254349cc55cSDimitry Andric 255349cc55cSDimitry Andric void getTargetDefines(const LangOptions &Opts, 256349cc55cSDimitry Andric MacroBuilder &Builder) const override; 257349cc55cSDimitry Andric }; 258349cc55cSDimitry Andric 259349cc55cSDimitry Andric class LLVM_LIBRARY_VISIBILITY SPIRV64TargetInfo : public SPIRVTargetInfo { 260349cc55cSDimitry Andric public: 261349cc55cSDimitry Andric SPIRV64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) 262349cc55cSDimitry Andric : SPIRVTargetInfo(Triple, Opts) { 2634824e7fdSDimitry Andric assert(Triple.getArch() == llvm::Triple::spirv64 && 2644824e7fdSDimitry Andric "Invalid architecture for 64-bit SPIR-V."); 265349cc55cSDimitry Andric PointerWidth = PointerAlign = 64; 266349cc55cSDimitry Andric SizeType = TargetInfo::UnsignedLong; 267349cc55cSDimitry Andric PtrDiffType = IntPtrType = TargetInfo::SignedLong; 268349cc55cSDimitry Andric resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-" 269349cc55cSDimitry Andric "v96:128-v192:256-v256:256-v512:512-v1024:1024"); 270349cc55cSDimitry Andric } 271349cc55cSDimitry Andric 272349cc55cSDimitry Andric void getTargetDefines(const LangOptions &Opts, 273349cc55cSDimitry Andric MacroBuilder &Builder) const override; 274349cc55cSDimitry Andric }; 275349cc55cSDimitry Andric 2760b57cec5SDimitry Andric } // namespace targets 2770b57cec5SDimitry Andric } // namespace clang 2780b57cec5SDimitry Andric #endif // LLVM_CLANG_LIB_BASIC_TARGETS_SPIR_H 279