1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
//===-- SPIRVSubtarget.cpp - SPIR-V Subtarget Information ------*- C++ -*--===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the SPIR-V specific subclass of TargetSubtargetInfo.
//
//===----------------------------------------------------------------------===//
#include "SPIRVSubtarget.h"
#include "SPIRV.h"
#include "SPIRVCommandLine.h"
#include "SPIRVGlobalRegistry.h"
#include "SPIRVLegalizerInfo.h"
#include "SPIRVRegisterBankInfo.h"
#include "SPIRVTargetMachine.h"
#include "llvm/TargetParser/Host.h"
using namespace llvm;
#define DEBUG_TYPE "spirv-subtarget"
#define GET_SUBTARGETINFO_TARGET_DESC
#define GET_SUBTARGETINFO_CTOR
#include "SPIRVGenSubtargetInfo.inc"
static cl::opt<bool>
SPVTranslatorCompat("translator-compatibility-mode",
cl::desc("SPIR-V Translator compatibility mode"),
cl::Optional, cl::init(false));
static cl::opt<std::set<SPIRV::Extension::Extension>, false,
SPIRVExtensionsParser>
Extensions("spirv-ext",
cl::desc("Specify list of enabled SPIR-V extensions"));
// Provides access to the cl::opt<...> `Extensions` variable from outside of the
// module.
void SPIRVSubtarget::addExtensionsToClOpt(
const std::set<SPIRV::Extension::Extension> &AllowList) {
Extensions.insert(AllowList.begin(), AllowList.end());
}
// Compare version numbers, but allow 0 to mean unspecified.
static bool isAtLeastVer(VersionTuple Target, VersionTuple VerToCompareTo) {
return Target.empty() || Target >= VerToCompareTo;
}
SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU,
const std::string &FS,
const SPIRVTargetMachine &TM)
: SPIRVGenSubtargetInfo(TT, CPU, /*TuneCPU=*/CPU, FS),
PointerSize(TM.getPointerSizeInBits(/* AS= */ 0)), InstrInfo(),
FrameLowering(initSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
TargetTriple(TT) {
switch (TT.getSubArch()) {
case Triple::SPIRVSubArch_v10:
SPIRVVersion = VersionTuple(1, 0);
break;
case Triple::SPIRVSubArch_v11:
SPIRVVersion = VersionTuple(1, 1);
break;
case Triple::SPIRVSubArch_v12:
SPIRVVersion = VersionTuple(1, 2);
break;
case Triple::SPIRVSubArch_v13:
SPIRVVersion = VersionTuple(1, 3);
break;
case Triple::SPIRVSubArch_v14:
default:
SPIRVVersion = VersionTuple(1, 4);
break;
case Triple::SPIRVSubArch_v15:
SPIRVVersion = VersionTuple(1, 5);
break;
case Triple::SPIRVSubArch_v16:
SPIRVVersion = VersionTuple(1, 6);
break;
}
OpenCLVersion = VersionTuple(2, 2);
// Set the environment based on the target triple.
if (TargetTriple.getOS() == Triple::Vulkan)
Env = Shader;
else if (TargetTriple.getEnvironment() == Triple::OpenCL)
Env = Kernel;
else
Env = Unknown;
// The order of initialization is important.
initAvailableExtensions(Extensions);
initAvailableExtInstSets();
GR = std::make_unique<SPIRVGlobalRegistry>(PointerSize);
CallLoweringInfo = std::make_unique<SPIRVCallLowering>(TLInfo, GR.get());
InlineAsmInfo = std::make_unique<SPIRVInlineAsmLowering>(TLInfo);
Legalizer = std::make_unique<SPIRVLegalizerInfo>(*this);
RegBankInfo = std::make_unique<SPIRVRegisterBankInfo>();
InstSelector.reset(createSPIRVInstructionSelector(TM, *this, *RegBankInfo));
}
SPIRVSubtarget &SPIRVSubtarget::initSubtargetDependencies(StringRef CPU,
StringRef FS) {
ParseSubtargetFeatures(CPU, /*TuneCPU=*/CPU, FS);
return *this;
}
bool SPIRVSubtarget::canUseExtension(SPIRV::Extension::Extension E) const {
return AvailableExtensions.contains(E);
}
bool SPIRVSubtarget::canUseExtInstSet(
SPIRV::InstructionSet::InstructionSet E) const {
return AvailableExtInstSets.contains(E);
}
SPIRV::InstructionSet::InstructionSet
SPIRVSubtarget::getPreferredInstructionSet() const {
if (isShader())
return SPIRV::InstructionSet::GLSL_std_450;
else
return SPIRV::InstructionSet::OpenCL_std;
}
bool SPIRVSubtarget::isAtLeastSPIRVVer(VersionTuple VerToCompareTo) const {
return isAtLeastVer(SPIRVVersion, VerToCompareTo);
}
bool SPIRVSubtarget::isAtLeastOpenCLVer(VersionTuple VerToCompareTo) const {
if (isShader())
return false;
return isAtLeastVer(OpenCLVersion, VerToCompareTo);
}
// If the SPIR-V version is >= 1.4 we can call OpPtrEqual and OpPtrNotEqual.
// In SPIR-V Translator compatibility mode this feature is not available.
bool SPIRVSubtarget::canDirectlyComparePointers() const {
return !SPVTranslatorCompat && isAtLeastVer(SPIRVVersion, VersionTuple(1, 4));
}
void SPIRVSubtarget::accountForAMDShaderTrinaryMinmax() {
if (canUseExtension(
SPIRV::Extension::SPV_AMD_shader_trinary_minmax_extension)) {
AvailableExtInstSets.insert(
SPIRV::InstructionSet::SPV_AMD_shader_trinary_minmax);
}
}
// TODO: use command line args for this rather than just defaults.
// Must have called initAvailableExtensions first.
void SPIRVSubtarget::initAvailableExtInstSets() {
AvailableExtInstSets.clear();
if (isShader())
AvailableExtInstSets.insert(SPIRV::InstructionSet::GLSL_std_450);
else
AvailableExtInstSets.insert(SPIRV::InstructionSet::OpenCL_std);
// Handle extended instruction sets from extensions.
accountForAMDShaderTrinaryMinmax();
}
// Set available extensions after SPIRVSubtarget is created.
void SPIRVSubtarget::initAvailableExtensions(
const std::set<SPIRV::Extension::Extension> &AllowedExtIds) {
AvailableExtensions.clear();
AvailableExtensions.insert_range(AllowedExtIds);
accountForAMDShaderTrinaryMinmax();
}
|