1 //===-- SPIRVSubtarget.cpp - SPIR-V Subtarget Information ------*- C++ -*--===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the SPIR-V specific subclass of TargetSubtargetInfo.
11 //===----------------------------------------------------------------------===//
13 #include "SPIRVSubtarget.h"
15 #include "SPIRVCommandLine.h"
16 #include "SPIRVGlobalRegistry.h"
17 #include "SPIRVLegalizerInfo.h"
18 #include "SPIRVRegisterBankInfo.h"
19 #include "SPIRVTargetMachine.h"
20 #include "llvm/MC/TargetRegistry.h"
21 #include "llvm/TargetParser/Host.h"
25 #define DEBUG_TYPE "spirv-subtarget"
27 #define GET_SUBTARGETINFO_TARGET_DESC
28 #define GET_SUBTARGETINFO_CTOR
29 #include "SPIRVGenSubtargetInfo.inc"
32 SPVTranslatorCompat("translator-compatibility-mode",
33 cl::desc("SPIR-V Translator compatibility mode"),
34 cl::Optional
, cl::init(false));
36 static cl::opt
<std::set
<SPIRV::Extension::Extension
>, false,
37 SPIRVExtensionsParser
>
38 Extensions("spirv-ext",
39 cl::desc("Specify list of enabled SPIR-V extensions"));
41 // Compare version numbers, but allow 0 to mean unspecified.
42 static bool isAtLeastVer(VersionTuple Target
, VersionTuple VerToCompareTo
) {
43 return Target
.empty() || Target
>= VerToCompareTo
;
46 SPIRVSubtarget::SPIRVSubtarget(const Triple
&TT
, const std::string
&CPU
,
47 const std::string
&FS
,
48 const SPIRVTargetMachine
&TM
)
49 : SPIRVGenSubtargetInfo(TT
, CPU
, /*TuneCPU=*/CPU
, FS
),
50 PointerSize(TM
.getPointerSizeInBits(/* AS= */ 0)), InstrInfo(),
51 FrameLowering(initSubtargetDependencies(CPU
, FS
)), TLInfo(TM
, *this),
53 switch (TT
.getSubArch()) {
54 case Triple::SPIRVSubArch_v10
:
55 SPIRVVersion
= VersionTuple(1, 0);
57 case Triple::SPIRVSubArch_v11
:
58 SPIRVVersion
= VersionTuple(1, 1);
60 case Triple::SPIRVSubArch_v12
:
61 SPIRVVersion
= VersionTuple(1, 2);
63 case Triple::SPIRVSubArch_v13
:
64 SPIRVVersion
= VersionTuple(1, 3);
66 case Triple::SPIRVSubArch_v14
:
68 SPIRVVersion
= VersionTuple(1, 4);
70 case Triple::SPIRVSubArch_v15
:
71 SPIRVVersion
= VersionTuple(1, 5);
73 case Triple::SPIRVSubArch_v16
:
74 SPIRVVersion
= VersionTuple(1, 6);
77 OpenCLVersion
= VersionTuple(2, 2);
79 // The order of initialization is important.
80 initAvailableExtensions();
81 initAvailableExtInstSets();
83 GR
= std::make_unique
<SPIRVGlobalRegistry
>(PointerSize
);
84 CallLoweringInfo
= std::make_unique
<SPIRVCallLowering
>(TLInfo
, GR
.get());
85 InlineAsmInfo
= std::make_unique
<SPIRVInlineAsmLowering
>(TLInfo
);
86 Legalizer
= std::make_unique
<SPIRVLegalizerInfo
>(*this);
87 RegBankInfo
= std::make_unique
<SPIRVRegisterBankInfo
>();
89 createSPIRVInstructionSelector(TM
, *this, *RegBankInfo
.get()));
92 SPIRVSubtarget
&SPIRVSubtarget::initSubtargetDependencies(StringRef CPU
,
94 ParseSubtargetFeatures(CPU
, /*TuneCPU=*/CPU
, FS
);
98 bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E
) const {
99 return AvailableExtensions
.contains(E
);
102 bool SPIRVSubtarget::canUseExtInstSet(
103 SPIRV::InstructionSet::InstructionSet E
) const {
104 return AvailableExtInstSets
.contains(E
);
107 bool SPIRVSubtarget::isAtLeastSPIRVVer(VersionTuple VerToCompareTo
) const {
108 return isAtLeastVer(SPIRVVersion
, VerToCompareTo
);
111 bool SPIRVSubtarget::isAtLeastOpenCLVer(VersionTuple VerToCompareTo
) const {
114 return isAtLeastVer(OpenCLVersion
, VerToCompareTo
);
117 // If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
118 // In SPIR-V Translator compatibility mode this feature is not available.
119 bool SPIRVSubtarget::canDirectlyComparePointers() const {
120 return !SPVTranslatorCompat
&& isAtLeastVer(SPIRVVersion
, VersionTuple(1, 4));
123 void SPIRVSubtarget::initAvailableExtensions() {
124 AvailableExtensions
.clear();
128 AvailableExtensions
.insert(Extensions
.begin(), Extensions
.end());
131 // TODO: use command line args for this rather than just defaults.
132 // Must have called initAvailableExtensions first.
133 void SPIRVSubtarget::initAvailableExtInstSets() {
134 AvailableExtInstSets
.clear();
136 AvailableExtInstSets
.insert(SPIRV::InstructionSet::GLSL_std_450
);
138 AvailableExtInstSets
.insert(SPIRV::InstructionSet::OpenCL_std
);
140 // Handle extended instruction sets from extensions.
142 SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension
)) {
143 AvailableExtInstSets
.insert(
144 SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax
);