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