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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
//===- LLVMSPIRVOpts.h - Specify options for translation --------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// Copyright (c) 2019 Intel Corporation. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal with the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimers in the documentation
// and/or other materials provided with the distribution.
// Neither the names of Advanced Micro Devices, Inc., nor the names of its
// contributors may be used to endorse or promote products derived from this
// Software without specific prior written permission.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
// THE SOFTWARE.
//
//===----------------------------------------------------------------------===//
/// \file LLVMSPIRVOpts.h
///
/// This files declares helper classes to handle SPIR-V versions and extensions.
///
//===----------------------------------------------------------------------===//
#ifndef SPIRV_LLVMSPIRVOPTS_H
#define SPIRV_LLVMSPIRVOPTS_H
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringRef.h>
#include <cassert>
#include <cstdint>
#include <map>
#include <optional>
#include <unordered_map>
#pragma GCC visibility push(default)
namespace llvm {
class IntrinsicInst;
} // namespace llvm
namespace SPIRV {
/// SPIR-V versions known to translator.
enum class VersionNumber : uint32_t {
// See section 2.3 of SPIR-V spec: Physical Layout of a SPIR_V Module and
// Instruction
SPIRV_1_0 = 0x00010000,
SPIRV_1_1 = 0x00010100,
SPIRV_1_2 = 0x00010200,
SPIRV_1_3 = 0x00010300,
SPIRV_1_4 = 0x00010400,
SPIRV_1_5 = 0x00010500,
SPIRV_1_6 = 0x00010600,
MinimumVersion = SPIRV_1_0,
MaximumVersion = SPIRV_1_6
};
inline constexpr std::string_view formatVersionNumber(uint32_t Version) {
switch (Version) {
case static_cast<uint32_t>(VersionNumber::SPIRV_1_0):
return "1.0";
case static_cast<uint32_t>(VersionNumber::SPIRV_1_1):
return "1.1";
case static_cast<uint32_t>(VersionNumber::SPIRV_1_2):
return "1.2";
case static_cast<uint32_t>(VersionNumber::SPIRV_1_3):
return "1.3";
case static_cast<uint32_t>(VersionNumber::SPIRV_1_4):
return "1.4";
case static_cast<uint32_t>(VersionNumber::SPIRV_1_5):
return "1.5";
case static_cast<uint32_t>(VersionNumber::SPIRV_1_6):
return "1.6";
}
return "unknown";
}
inline bool isSPIRVVersionKnown(VersionNumber Ver) {
return Ver >= VersionNumber::MinimumVersion &&
Ver <= VersionNumber::MaximumVersion;
}
enum class ExtensionID : uint32_t {
First,
#define EXT(X) X,
#include "LLVMSPIRVExtensions.inc"
#undef EXT
Last,
};
enum class ExtInst : uint32_t { None, OpenCL };
enum class BIsRepresentation : uint32_t { OpenCL12, OpenCL20, SPIRVFriendlyIR };
enum class FPContractMode : uint32_t { On, Off, Fast };
enum class DebugInfoEIS : uint32_t {
SPIRV_Debug,
OpenCL_DebugInfo_100,
NonSemantic_Shader_DebugInfo_100,
NonSemantic_Shader_DebugInfo_200
};
enum class BuiltinFormat : uint32_t { Function, Global };
/// \brief Helper class to manage SPIR-V translation
class TranslatorOpts {
public:
// Unset optional means not directly specified by user
using ExtensionsStatusMap = std::map<ExtensionID, std::optional<bool>>;
using ArgList = llvm::SmallVector<llvm::StringRef, 4>;
TranslatorOpts() = default;
TranslatorOpts(VersionNumber Max, const ExtensionsStatusMap &Map = {})
: MaxVersion(Max), ExtStatusMap(Map) {}
bool isAllowedToUseVersion(VersionNumber RequestedVersion) const {
return RequestedVersion <= MaxVersion;
}
bool isAllowedToUseExtension(ExtensionID Extension) const {
auto I = ExtStatusMap.find(Extension);
if (ExtStatusMap.end() == I)
return false;
return I->second && *I->second;
}
void setAllowedToUseExtension(ExtensionID Extension, bool Allow = true) {
// Only allow using the extension if it has not already been disabled
auto I = ExtStatusMap.find(Extension);
if (I == ExtStatusMap.end() || !I->second || (*I->second) == true)
ExtStatusMap[Extension] = Allow;
}
VersionNumber getMaxVersion() const { return MaxVersion; }
bool isGenArgNameMDEnabled() const { return GenKernelArgNameMD; }
bool isSPIRVMemToRegEnabled() const { return SPIRVMemToReg; }
void setMemToRegEnabled(bool Mem2Reg) { SPIRVMemToReg = Mem2Reg; }
bool preserveAuxData() const { return PreserveAuxData; }
void setPreserveAuxData(bool ArgValue) { PreserveAuxData = ArgValue; }
void setGenKernelArgNameMDEnabled(bool ArgNameMD) {
GenKernelArgNameMD = ArgNameMD;
}
void enableAllExtensions();
void enableGenArgNameMD() { GenKernelArgNameMD = true; }
void setSpecConst(uint32_t SpecId, uint64_t SpecValue) {
ExternalSpecialization[SpecId] = SpecValue;
}
bool getSpecializationConstant(uint32_t SpecId, uint64_t &Value) const {
auto It = ExternalSpecialization.find(SpecId);
if (It == ExternalSpecialization.end())
return false;
Value = It->second;
return true;
}
void setExtInst(ExtInst Value) {
// --spirv-ext-inst supersedes --spirv-replace-fmuladd-with-ocl-mad
ReplaceLLVMFmulAddWithOpenCLMad = false;
ExtInstValue = Value;
}
ExtInst getExtInst() const { return ExtInstValue; }
void setDesiredBIsRepresentation(BIsRepresentation Value) {
DesiredRepresentationOfBIs = Value;
}
BIsRepresentation getDesiredBIsRepresentation() const {
return DesiredRepresentationOfBIs;
}
void setFPContractMode(FPContractMode Mode) { FPCMode = Mode; }
FPContractMode getFPContractMode() const { return FPCMode; }
bool isUnknownIntrinsicAllowed(llvm::IntrinsicInst *II) const noexcept;
bool isSPIRVAllowUnknownIntrinsicsEnabled() const noexcept;
void setSPIRVAllowUnknownIntrinsics(ArgList IntrinsicPrefixList) noexcept;
bool allowExtraDIExpressions() const noexcept {
return AllowExtraDIExpressions;
}
void setAllowExtraDIExpressionsEnabled(bool Allow) noexcept {
AllowExtraDIExpressions = Allow;
}
DebugInfoEIS getDebugInfoEIS() const { return DebugInfoVersion; }
void setDebugInfoEIS(DebugInfoEIS EIS) { DebugInfoVersion = EIS; }
bool shouldReplaceLLVMFmulAddWithOpenCLMad() const noexcept {
return ReplaceLLVMFmulAddWithOpenCLMad;
}
void setReplaceLLVMFmulAddWithOpenCLMad(bool Value) noexcept {
ReplaceLLVMFmulAddWithOpenCLMad = Value;
}
bool shouldPreserveOCLKernelArgTypeMetadataThroughString() const noexcept {
return PreserveOCLKernelArgTypeMetadataThroughString;
}
void setPreserveOCLKernelArgTypeMetadataThroughString(bool Value) noexcept {
PreserveOCLKernelArgTypeMetadataThroughString = Value;
}
bool shouldEmitFunctionPtrAddrSpace() const noexcept {
return EmitFunctionPtrAddrSpace;
}
void setEmitFunctionPtrAddrSpace(bool Value) noexcept {
EmitFunctionPtrAddrSpace = Value;
}
void setBuiltinFormat(BuiltinFormat Value) noexcept {
SPIRVBuiltinFormat = Value;
}
BuiltinFormat getBuiltinFormat() const noexcept { return SPIRVBuiltinFormat; }
private:
// Common translation options
VersionNumber MaxVersion = VersionNumber::MaximumVersion;
ExtensionsStatusMap ExtStatusMap;
// SPIRVMemToReg option affects LLVM IR regularization phase
bool SPIRVMemToReg = false;
// SPIR-V to LLVM translation options
bool GenKernelArgNameMD = false;
std::unordered_map<uint32_t, uint64_t> ExternalSpecialization;
// Extended instruction set to use when translating from LLVM IR to SPIR-V
ExtInst ExtInstValue = ExtInst::None;
// Representation of built-ins, which should be used while translating from
// SPIR-V to back to LLVM IR
BIsRepresentation DesiredRepresentationOfBIs = BIsRepresentation::OpenCL12;
// Controls floating point contraction.
//
// - FPContractMode::On allows to choose a mode according to
// presence of fused LLVM intrinsics
//
// - FPContractMode::Off disables contratction for all entry points
//
// - FPContractMode::Fast allows *all* operations to be contracted
// for all entry points
FPContractMode FPCMode = FPContractMode::On;
// Unknown LLVM intrinsics will be translated as external function calls in
// SPIR-V
std::optional<ArgList> SPIRVAllowUnknownIntrinsics{};
// Enable support for extra DIExpression opcodes not listed in the SPIR-V
// DebugInfo specification.
bool AllowExtraDIExpressions = false;
DebugInfoEIS DebugInfoVersion = DebugInfoEIS::OpenCL_DebugInfo_100;
// Controls whether llvm.fmuladd.* should be replaced with mad from OpenCL
// extended instruction set or with a simple fmul + fadd
bool ReplaceLLVMFmulAddWithOpenCLMad = true;
// Add a workaround to preserve OpenCL kernel_arg_type and
// kernel_arg_type_qual metadata through OpString
bool PreserveOCLKernelArgTypeMetadataThroughString = false;
// Controls if CodeSectionINTEL can be emitted and consumed with a dedicated
// address space
bool EmitFunctionPtrAddrSpace = false;
bool PreserveAuxData = false;
BuiltinFormat SPIRVBuiltinFormat = BuiltinFormat::Function;
};
} // namespace SPIRV
#pragma GCC visibility pop
#endif // SPIRV_LLVMSPIRVOPTS_H
|