1 //===-- SPIRVSubtarget.h - SPIR-V Subtarget Information --------*- C++ -*--===// 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 file declares the SPIR-V specific subclass of TargetSubtargetInfo. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVSUBTARGET_H 14 #define LLVM_LIB_TARGET_SPIRV_SPIRVSUBTARGET_H 15 16 #include "SPIRVCallLowering.h" 17 #include "SPIRVFrameLowering.h" 18 #include "SPIRVISelLowering.h" 19 #include "SPIRVInstrInfo.h" 20 #include "llvm/ADT/SmallSet.h" 21 #include "llvm/CodeGen/GlobalISel/CallLowering.h" 22 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" 23 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" 24 #include "llvm/CodeGen/SelectionDAGTargetInfo.h" 25 #include "llvm/CodeGen/TargetSubtargetInfo.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/Target/TargetMachine.h" 28 #include "llvm/TargetParser/Triple.h" 29 30 #define GET_SUBTARGETINFO_HEADER 31 #include "SPIRVGenSubtargetInfo.inc" 32 33 namespace llvm { 34 class StringRef; 35 class SPIRVTargetMachine; 36 37 class SPIRVSubtarget : public SPIRVGenSubtargetInfo { 38 private: 39 const unsigned PointerSize; 40 uint32_t SPIRVVersion; 41 uint32_t OpenCLVersion; 42 43 SmallSet<SPIRV::Extension::Extension, 4> AvailableExtensions; 44 SmallSet<SPIRV::InstructionSet::InstructionSet, 4> AvailableExtInstSets; 45 std::unique_ptr<SPIRVGlobalRegistry> GR; 46 47 SPIRVInstrInfo InstrInfo; 48 SPIRVFrameLowering FrameLowering; 49 SPIRVTargetLowering TLInfo; 50 Triple TargetTriple; 51 52 // GlobalISel related APIs. 53 std::unique_ptr<CallLowering> CallLoweringInfo; 54 std::unique_ptr<RegisterBankInfo> RegBankInfo; 55 std::unique_ptr<LegalizerInfo> Legalizer; 56 std::unique_ptr<InstructionSelector> InstSelector; 57 58 // TODO: Initialise the available extensions, extended instruction sets 59 // based on the environment settings. 60 void initAvailableExtensions(); 61 void initAvailableExtInstSets(); 62 63 public: 64 // This constructor initializes the data members to match that 65 // of the specified triple. 66 SPIRVSubtarget(const Triple &TT, const std::string &CPU, 67 const std::string &FS, const SPIRVTargetMachine &TM); 68 SPIRVSubtarget &initSubtargetDependencies(StringRef CPU, StringRef FS); 69 70 // Parses features string setting specified subtarget options. 71 // The definition of this function is auto generated by tblgen. 72 void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS); 73 unsigned getPointerSize() const { return PointerSize; } 74 bool canDirectlyComparePointers() const; 75 // TODO: this environment is not implemented in Triple, we need to decide 76 // how to standardize its support. For now, let's assume SPIR-V with physical 77 // addressing is OpenCL, and Logical addressing is Vulkan. 78 bool isOpenCLEnv() const { 79 return TargetTriple.getArch() == Triple::spirv32 || 80 TargetTriple.getArch() == Triple::spirv64; 81 } 82 bool isVulkanEnv() const { return TargetTriple.getArch() == Triple::spirv; } 83 uint32_t getSPIRVVersion() const { return SPIRVVersion; }; 84 bool isAtLeastSPIRVVer(uint32_t VerToCompareTo) const; 85 bool isAtLeastOpenCLVer(uint32_t VerToCompareTo) const; 86 // TODO: implement command line args or other ways to determine this. 87 bool hasOpenCLFullProfile() const { return true; } 88 bool hasOpenCLImageSupport() const { return true; } 89 const SmallSet<SPIRV::Extension::Extension, 4> & 90 getAllAvailableExtensions() const { 91 return AvailableExtensions; 92 } 93 bool canUseExtension(SPIRV::Extension::Extension E) const; 94 bool canUseExtInstSet(SPIRV::InstructionSet::InstructionSet E) const; 95 96 SPIRVGlobalRegistry *getSPIRVGlobalRegistry() const { return GR.get(); } 97 98 const CallLowering *getCallLowering() const override { 99 return CallLoweringInfo.get(); 100 } 101 const RegisterBankInfo *getRegBankInfo() const override { 102 return RegBankInfo.get(); 103 } 104 const LegalizerInfo *getLegalizerInfo() const override { 105 return Legalizer.get(); 106 } 107 InstructionSelector *getInstructionSelector() const override { 108 return InstSelector.get(); 109 } 110 const SPIRVInstrInfo *getInstrInfo() const override { return &InstrInfo; } 111 const SPIRVFrameLowering *getFrameLowering() const override { 112 return &FrameLowering; 113 } 114 const SPIRVTargetLowering *getTargetLowering() const override { 115 return &TLInfo; 116 } 117 const SPIRVRegisterInfo *getRegisterInfo() const override { 118 return &InstrInfo.getRegisterInfo(); 119 } 120 121 static bool classof(const TargetSubtargetInfo *ST) { 122 return ST->getTargetTriple().isSPIRV(); 123 } 124 }; 125 } // namespace llvm 126 127 #endif // LLVM_LIB_TARGET_SPIRV_SPIRVSUBTARGET_H 128