| 1 |
//===- OCLUtil.h - OCL Utilities declarations -------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// The LLVM/SPIRV Translator |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 11 |
// copy of this software and associated documentation files (the "Software"), |
| 12 |
// to deal with the Software without restriction, including without limitation |
| 13 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 15 |
// Software is furnished to do so, subject to the following conditions: |
| 16 |
// |
| 17 |
// Redistributions of source code must retain the above copyright notice, |
| 18 |
// this list of conditions and the following disclaimers. |
| 19 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
// this list of conditions and the following disclaimers in the documentation |
| 21 |
// and/or other materials provided with the distribution. |
| 22 |
// Neither the names of Advanced Micro Devices, Inc., nor the names of its |
| 23 |
// contributors may be used to endorse or promote products derived from this |
| 24 |
// Software without specific prior written permission. |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 31 |
// THE SOFTWARE. |
| 32 |
// |
| 33 |
//===----------------------------------------------------------------------===// |
| 34 |
// |
| 35 |
// This file declares OCL utility functions. |
| 36 |
// |
| 37 |
//===----------------------------------------------------------------------===// |
| 38 |
|
| 39 |
#ifndef SPIRV_OCLUTIL_H |
| 40 |
#define SPIRV_OCLUTIL_H |
| 41 |
|
| 42 |
#include "SPIRVInternal.h" |
| 43 |
#include "llvm/ADT/SmallString.h" |
| 44 |
#include "llvm/IR/IRBuilder.h" |
| 45 |
#include "llvm/IR/Instructions.h" |
| 46 |
#include "llvm/Support/Path.h" |
| 47 |
|
| 48 |
#include <atomic> |
| 49 |
#include <functional> |
| 50 |
#include <tuple> |
| 51 |
#include <type_traits> |
| 52 |
#include <utility> |
| 53 |
using namespace SPIRV; |
| 54 |
using namespace llvm; |
| 55 |
using namespace spv; |
| 56 |
|
| 57 |
namespace SPIRV { |
| 58 |
class BuiltinCallMutator; |
| 59 |
} // namespace SPIRV |
| 60 |
|
| 61 |
namespace OCLUtil { |
| 62 |
|
| 63 |
/////////////////////////////////////////////////////////////////////////////// |
| 64 |
// |
| 65 |
// Enums |
| 66 |
// |
| 67 |
/////////////////////////////////////////////////////////////////////////////// |
| 68 |
|
| 69 |
enum OCLMemFenceKind { |
| 70 |
OCLMF_Local = 1, |
| 71 |
OCLMF_Global = 2, |
| 72 |
OCLMF_Image = 4, |
| 73 |
}; |
| 74 |
|
| 75 |
// This enum declares extra constants for OpenCL mem_fence flag. It includes |
| 76 |
// combinations of local/global/image flags. |
| 77 |
enum OCLMemFenceExtendedKind { |
| 78 |
OCLMFEx_Local = OCLMF_Local, |
| 79 |
OCLMFEx_Global = OCLMF_Global, |
| 80 |
OCLMFEx_Local_Global = OCLMF_Global | OCLMF_Local, |
| 81 |
OCLMFEx_Image = OCLMF_Image, |
| 82 |
OCLMFEx_Image_Local = OCLMF_Image | OCLMF_Local, |
| 83 |
OCLMFEx_Image_Global = OCLMF_Image | OCLMF_Global, |
| 84 |
OCLMFEx_Image_Local_Global = OCLMF_Image | OCLMF_Global | OCLMF_Local, |
| 85 |
}; |
| 86 |
|
| 87 |
enum OCLScopeKind { |
| 88 |
OCLMS_work_item, |
| 89 |
OCLMS_work_group, |
| 90 |
OCLMS_device, |
| 91 |
OCLMS_all_svm_devices, |
| 92 |
OCLMS_sub_group, |
| 93 |
}; |
| 94 |
|
| 95 |
// The enum below declares constants corresponding to memory synchronization |
| 96 |
// operations constants defined in |
| 97 |
// https://www.khronos.org/registry/OpenCL/sdk/2.1/docs/man/xhtml/memory_order.html |
| 98 |
// To avoid any inconsistence here, constants are explicitly initialized with |
| 99 |
// the corresponding constants from 'std::memory_order' enum. |
| 100 |
enum OCLMemOrderKind { |
| 101 |
#if __cplusplus >= 202002L |
| 102 |
OCLMO_relaxed = static_cast<int>(std::memory_order::relaxed), |
| 103 |
OCLMO_acquire = static_cast<int>(std::memory_order::acquire), |
| 104 |
OCLMO_release = static_cast<int>(std::memory_order::release), |
| 105 |
OCLMO_acq_rel = static_cast<int>(std::memory_order::acq_rel), |
| 106 |
OCLMO_seq_cst = static_cast<int>(std::memory_order::seq_cst) |
| 107 |
#else |
| 108 |
OCLMO_relaxed = std::memory_order::memory_order_relaxed, |
| 109 |
OCLMO_acquire = std::memory_order::memory_order_acquire, |
| 110 |
OCLMO_release = std::memory_order::memory_order_release, |
| 111 |
OCLMO_acq_rel = std::memory_order::memory_order_acq_rel, |
| 112 |
OCLMO_seq_cst = std::memory_order::memory_order_seq_cst |
| 113 |
#endif |
| 114 |
}; |
| 115 |
|
| 116 |
enum IntelFPGAMemoryAccessesVal { |
| 117 |
BurstCoalesce = 0x1, |
| 118 |
CacheSizeFlag = 0x2, |
| 119 |
DontStaticallyCoalesce = 0x4, |
| 120 |
PrefetchFlag = 0x8 |
| 121 |
}; |
| 122 |
|
| 123 |
/////////////////////////////////////////////////////////////////////////////// |
| 124 |
// |
| 125 |
// Types |
| 126 |
// |
| 127 |
/////////////////////////////////////////////////////////////////////////////// |
| 128 |
|
| 129 |
typedef SPIRVMap<OCLMemFenceKind, MemorySemanticsMask> OCLMemFenceMap; |
| 130 |
|
| 131 |
typedef SPIRVMap<OCLMemFenceExtendedKind, MemorySemanticsMask> |
| 132 |
OCLMemFenceExtendedMap; |
| 133 |
|
| 134 |
typedef SPIRVMap<OCLMemOrderKind, unsigned, MemorySemanticsMask> OCLMemOrderMap; |
| 135 |
|
| 136 |
typedef SPIRVMap<OCLScopeKind, Scope> OCLMemScopeMap; |
| 137 |
|
| 138 |
typedef SPIRVMap<std::string, SPIRVGroupOperationKind> |
| 139 |
SPIRSPIRVGroupOperationMap; |
| 140 |
|
| 141 |
typedef SPIRVMap<std::string, SPIRVFPRoundingModeKind> |
| 142 |
SPIRSPIRVFPRoundingModeMap; |
| 143 |
|
| 144 |
typedef SPIRVMap<std::string, Op, SPIRVInstruction> OCLSPIRVBuiltinMap; |
| 145 |
|
| 146 |
class OCL12Builtin; |
| 147 |
typedef SPIRVMap<std::string, Op, OCL12Builtin> OCL12SPIRVBuiltinMap; |
| 148 |
|
| 149 |
typedef SPIRVMap<std::string, SPIRVBuiltinVariableKind> |
| 150 |
SPIRSPIRVBuiltinVariableMap; |
| 151 |
|
| 152 |
/// Tuple of literals for atomic_work_item_fence (flag, order, scope) |
| 153 |
typedef std::tuple<unsigned, OCLMemOrderKind, OCLScopeKind> |
| 154 |
AtomicWorkItemFenceLiterals; |
| 155 |
|
| 156 |
/// Tuple of literals for work_group_barrier or sub_group_barrier |
| 157 |
/// (flag, mem_scope, exec_scope) |
| 158 |
typedef std::tuple<unsigned, OCLScopeKind, OCLScopeKind> BarrierLiterals; |
| 159 |
|
| 160 |
class OCLOpaqueType; |
| 161 |
typedef SPIRVMap<std::string, Op, OCLOpaqueType> OCLOpaqueTypeOpCodeMap; |
| 162 |
|
| 163 |
/// Information for translating OCL builtin. |
| 164 |
struct OCLBuiltinTransInfo { |
| 165 |
std::string UniqName; |
| 166 |
std::string MangledName; |
| 167 |
std::string Postfix; // Postfix to be added |
| 168 |
/// Postprocessor of operands |
| 169 |
std::function<void(BuiltinCallMutator &)> PostProc; |
| 170 |
Type *RetTy; // Return type of the translated function |
| 171 |
bool IsRetSigned; // When RetTy is int, determines if extensions |
| 172 |
// on it should be a sext or zet. |
| 173 |
OCLBuiltinTransInfo() : RetTy(nullptr), IsRetSigned(false) { |
| 174 |
PostProc = [](BuiltinCallMutator &) {}; |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
/////////////////////////////////////////////////////////////////////////////// |
| 179 |
// |
| 180 |
// Constants |
| 181 |
// |
| 182 |
/////////////////////////////////////////////////////////////////////////////// |
| 183 |
namespace kOCLBuiltinName { |
| 184 |
const static char All[] = "all"; |
| 185 |
const static char Any[] = "any"; |
| 186 |
#define _SPIRV_OP(x, y) \ |
| 187 |
const static char ArbitraryFloat##x##INTEL[] = "intel_arbitrary_float_" #y; |
| 188 |
_SPIRV_OP(Cast, cast) |
| 189 |
_SPIRV_OP(CastFromInt, cast_from_int) |
| 190 |
_SPIRV_OP(CastToInt, cast_to_int) |
| 191 |
_SPIRV_OP(Add, add) |
| 192 |
_SPIRV_OP(Sub, sub) |
| 193 |
_SPIRV_OP(Mul, mul) |
| 194 |
_SPIRV_OP(Div, div) |
| 195 |
_SPIRV_OP(GT, gt) |
| 196 |
_SPIRV_OP(GE, ge) |
| 197 |
_SPIRV_OP(LT, lt) |
| 198 |
_SPIRV_OP(LE, le) |
| 199 |
_SPIRV_OP(EQ, eq) |
| 200 |
_SPIRV_OP(Recip, recip) |
| 201 |
_SPIRV_OP(RSqrt, rsqrt) |
| 202 |
_SPIRV_OP(Cbrt, cbrt) |
| 203 |
_SPIRV_OP(Hypot, hypot) |
| 204 |
_SPIRV_OP(Sqrt, sqrt) |
| 205 |
_SPIRV_OP(Log, log) |
| 206 |
_SPIRV_OP(Log2, log2) |
| 207 |
_SPIRV_OP(Log10, log10) |
| 208 |
_SPIRV_OP(Log1p, log1p) |
| 209 |
_SPIRV_OP(Exp, exp) |
| 210 |
_SPIRV_OP(Exp2, exp2) |
| 211 |
_SPIRV_OP(Exp10, exp10) |
| 212 |
_SPIRV_OP(Expm1, expm1) |
| 213 |
_SPIRV_OP(Sin, sin) |
| 214 |
_SPIRV_OP(Cos, cos) |
| 215 |
_SPIRV_OP(SinCos, sincos) |
| 216 |
_SPIRV_OP(SinPi, sinpi) |
| 217 |
_SPIRV_OP(CosPi, cospi) |
| 218 |
_SPIRV_OP(SinCosPi, sincospi) |
| 219 |
_SPIRV_OP(ASin, asin) |
| 220 |
_SPIRV_OP(ASinPi, asinpi) |
| 221 |
_SPIRV_OP(ACos, acos) |
| 222 |
_SPIRV_OP(ACosPi, acospi) |
| 223 |
_SPIRV_OP(ATan, atan) |
| 224 |
_SPIRV_OP(ATanPi, atanpi) |
| 225 |
_SPIRV_OP(ATan2, atan2) |
| 226 |
_SPIRV_OP(Pow, pow) |
| 227 |
_SPIRV_OP(PowR, powr) |
| 228 |
_SPIRV_OP(PowN, pown) |
| 229 |
#undef _SPIRV_OP |
| 230 |
const static char AsyncWorkGroupCopy[] = "async_work_group_copy"; |
| 231 |
const static char AsyncWorkGroupStridedCopy[] = "async_work_group_strided_copy"; |
| 232 |
const static char AtomPrefix[] = "atom_"; |
| 233 |
const static char AtomCmpXchg[] = "atom_cmpxchg"; |
| 234 |
const static char AtomicPrefix[] = "atomic_"; |
| 235 |
const static char AtomicCmpXchg[] = "atomic_cmpxchg"; |
| 236 |
const static char AtomicCmpXchgStrong[] = "atomic_compare_exchange_strong"; |
| 237 |
const static char AtomicCmpXchgStrongExplicit[] = |
| 238 |
"atomic_compare_exchange_strong_explicit"; |
| 239 |
const static char AtomicCmpXchgWeak[] = "atomic_compare_exchange_weak"; |
| 240 |
const static char AtomicCmpXchgWeakExplicit[] = |
| 241 |
"atomic_compare_exchange_weak_explicit"; |
| 242 |
const static char AtomicInit[] = "atomic_init"; |
| 243 |
const static char AtomicWorkItemFence[] = "atomic_work_item_fence"; |
| 244 |
const static char Barrier[] = "barrier"; |
| 245 |
const static char Clamp[] = "clamp"; |
| 246 |
const static char ConvertPrefix[] = "convert_"; |
| 247 |
const static char Dot[] = "dot"; |
| 248 |
const static char DotAccSat[] = "dot_acc_sat"; |
| 249 |
const static char Dot4x8PackedPrefix[] = "dot_4x8packed_"; |
| 250 |
const static char DotAccSat4x8PackedPrefix[] = "dot_acc_sat_4x8packed_"; |
| 251 |
const static char EnqueueKernel[] = "enqueue_kernel"; |
| 252 |
const static char FixedSqrtINTEL[] = "intel_arbitrary_fixed_sqrt"; |
| 253 |
const static char FixedRecipINTEL[] = "intel_arbitrary_fixed_recip"; |
| 254 |
const static char FixedRsqrtINTEL[] = "intel_arbitrary_fixed_rsqrt"; |
| 255 |
const static char FixedSinINTEL[] = "intel_arbitrary_fixed_sin"; |
| 256 |
const static char FixedCosINTEL[] = "intel_arbitrary_fixed_cos"; |
| 257 |
const static char FixedSinCosINTEL[] = "intel_arbitrary_fixed_sincos"; |
| 258 |
const static char FixedSinPiINTEL[] = "intel_arbitrary_fixed_sinpi"; |
| 259 |
const static char FixedCosPiINTEL[] = "intel_arbitrary_fixed_cospi"; |
| 260 |
const static char FixedSinCosPiINTEL[] = "intel_arbitrary_fixed_sincospi"; |
| 261 |
const static char FixedLogINTEL[] = "intel_arbitrary_fixed_log"; |
| 262 |
const static char FixedExpINTEL[] = "intel_arbitrary_fixed_exp"; |
| 263 |
const static char FMax[] = "fmax"; |
| 264 |
const static char FMin[] = "fmin"; |
| 265 |
const static char FPGARegIntel[] = "__builtin_intel_fpga_reg"; |
| 266 |
const static char GetFence[] = "get_fence"; |
| 267 |
const static char GetImageArraySize[] = "get_image_array_size"; |
| 268 |
const static char GetImageChannelOrder[] = "get_image_channel_order"; |
| 269 |
const static char GetImageChannelDataType[] = "get_image_channel_data_type"; |
| 270 |
const static char GetImageDepth[] = "get_image_depth"; |
| 271 |
const static char GetImageDim[] = "get_image_dim"; |
| 272 |
const static char GetImageHeight[] = "get_image_height"; |
| 273 |
const static char GetImageWidth[] = "get_image_width"; |
| 274 |
const static char IsFinite[] = "isfinite"; |
| 275 |
const static char IsNan[] = "isnan"; |
| 276 |
const static char IsNormal[] = "isnormal"; |
| 277 |
const static char IsInf[] = "isinf"; |
| 278 |
const static char Max[] = "max"; |
| 279 |
const static char MemFence[] = "mem_fence"; |
| 280 |
const static char ReadMemFence[] = "read_mem_fence"; |
| 281 |
const static char WriteMemFence[] = "write_mem_fence"; |
| 282 |
const static char Min[] = "min"; |
| 283 |
const static char Mix[] = "mix"; |
| 284 |
const static char NDRangePrefix[] = "ndrange_"; |
| 285 |
const static char Pipe[] = "pipe"; |
| 286 |
const static char ReadImage[] = "read_image"; |
| 287 |
const static char ReadPipe[] = "read_pipe"; |
| 288 |
const static char ReadPipeBlockingINTEL[] = "read_pipe_bl"; |
| 289 |
const static char RoundingPrefix[] = "_r"; |
| 290 |
const static char Sampled[] = "sampled_"; |
| 291 |
const static char SampledReadImage[] = "sampled_read_image"; |
| 292 |
const static char Signbit[] = "signbit"; |
| 293 |
const static char SmoothStep[] = "smoothstep"; |
| 294 |
const static char Step[] = "step"; |
| 295 |
const static char SubGroupPrefix[] = "sub_group_"; |
| 296 |
const static char SubGroupBarrier[] = "sub_group_barrier"; |
| 297 |
const static char SubPrefix[] = "sub_"; |
| 298 |
const static char ToGlobal[] = "to_global"; |
| 299 |
const static char ToLocal[] = "to_local"; |
| 300 |
const static char ToPrivate[] = "to_private"; |
| 301 |
const static char VLoadPrefix[] = "vload"; |
| 302 |
const static char VLoadAPrefix[] = "vloada"; |
| 303 |
const static char VLoadHalf[] = "vload_half"; |
| 304 |
const static char VStorePrefix[] = "vstore"; |
| 305 |
const static char VStoreAPrefix[] = "vstorea"; |
| 306 |
const static char WaitGroupEvent[] = "wait_group_events"; |
| 307 |
const static char WriteImage[] = "write_image"; |
| 308 |
const static char WorkGroupBarrier[] = "work_group_barrier"; |
| 309 |
const static char WritePipe[] = "write_pipe"; |
| 310 |
const static char WritePipeBlockingINTEL[] = "write_pipe_bl"; |
| 311 |
const static char WorkGroupPrefix[] = "work_group_"; |
| 312 |
const static char WorkGroupAll[] = "work_group_all"; |
| 313 |
const static char WorkGroupAny[] = "work_group_any"; |
| 314 |
const static char SubGroupAll[] = "sub_group_all"; |
| 315 |
const static char SubGroupAny[] = "sub_group_any"; |
| 316 |
const static char WorkPrefix[] = "work_"; |
| 317 |
const static char SubgroupBlockReadINTELPrefix[] = "intel_sub_group_block_read"; |
| 318 |
const static char SubgroupBlockWriteINTELPrefix[] = |
| 319 |
"intel_sub_group_block_write"; |
| 320 |
const static char SubgroupImageMediaBlockINTELPrefix[] = |
| 321 |
"intel_sub_group_media_block"; |
| 322 |
const static char SplitBarrierINTELPrefix[] = "intel_work_group_barrier_"; |
| 323 |
const static char LDEXP[] = "ldexp"; |
| 324 |
#define _SPIRV_OP(x) \ |
| 325 |
const static char ConvertBFloat16##x##AsUShort##x[] = \ |
| 326 |
"intel_convert_bfloat16" #x "_as_ushort" #x; |
| 327 |
_SPIRV_OP() |
| 328 |
_SPIRV_OP(2) |
| 329 |
_SPIRV_OP(3) |
| 330 |
_SPIRV_OP(4) |
| 331 |
_SPIRV_OP(8) |
| 332 |
_SPIRV_OP(16) |
| 333 |
#undef _SPIRV_OP |
| 334 |
#define _SPIRV_OP(x) \ |
| 335 |
const static char ConvertAsBFloat16##x##Float##x[] = \ |
| 336 |
"intel_convert_as_bfloat16" #x "_float" #x; |
| 337 |
_SPIRV_OP() |
| 338 |
_SPIRV_OP(2) |
| 339 |
_SPIRV_OP(3) |
| 340 |
_SPIRV_OP(4) |
| 341 |
_SPIRV_OP(8) |
| 342 |
_SPIRV_OP(16) |
| 343 |
#undef _SPIRV_OP |
| 344 |
} // namespace kOCLBuiltinName |
| 345 |
|
| 346 |
/// Offset for OpenCL image channel order enumeration values. |
| 347 |
const unsigned int OCLImageChannelOrderOffset = 0x10B0; |
| 348 |
|
| 349 |
/// Offset for OpenCL image channel data type enumeration values. |
| 350 |
const unsigned int OCLImageChannelDataTypeOffset = 0x10D0; |
| 351 |
|
| 352 |
/// OCL 1.x atomic memory order when translated to 2.0 atomics. |
| 353 |
const OCLMemOrderKind OCLLegacyAtomicMemOrder = OCLMO_relaxed; |
| 354 |
|
| 355 |
/// OCL 1.x atomic memory scope when translated to 2.0 atomics. |
| 356 |
const OCLScopeKind OCLLegacyAtomicMemScope = OCLMS_work_group; |
| 357 |
|
| 358 |
namespace kOCLVer { |
| 359 |
const unsigned CL12 = 102000; |
| 360 |
const unsigned CL20 = 200000; |
| 361 |
const unsigned CL21 = 201000; |
| 362 |
const unsigned CL30 = 300000; |
| 363 |
} // namespace kOCLVer |
| 364 |
|
| 365 |
namespace OclExt { |
| 366 |
// clang-format off |
| 367 |
enum Kind { |
| 368 |
#define _SPIRV_OP(x) x, |
| 369 |
_SPIRV_OP(cl_images) |
| 370 |
_SPIRV_OP(cl_doubles) |
| 371 |
_SPIRV_OP(cl_khr_int64_base_atomics) |
| 372 |
_SPIRV_OP(cl_khr_int64_extended_atomics) |
| 373 |
_SPIRV_OP(cl_khr_fp16) |
| 374 |
_SPIRV_OP(cl_khr_gl_sharing) |
| 375 |
_SPIRV_OP(cl_khr_gl_event) |
| 376 |
_SPIRV_OP(cl_khr_d3d10_sharing) |
| 377 |
_SPIRV_OP(cl_khr_media_sharing) |
| 378 |
_SPIRV_OP(cl_khr_d3d11_sharing) |
| 379 |
_SPIRV_OP(cl_khr_global_int32_base_atomics) |
| 380 |
_SPIRV_OP(cl_khr_global_int32_extended_atomics) |
| 381 |
_SPIRV_OP(cl_khr_local_int32_base_atomics) |
| 382 |
_SPIRV_OP(cl_khr_local_int32_extended_atomics) |
| 383 |
_SPIRV_OP(cl_khr_byte_addressable_store) |
| 384 |
_SPIRV_OP(cl_khr_3d_image_writes) |
| 385 |
_SPIRV_OP(cl_khr_gl_msaa_sharing) |
| 386 |
_SPIRV_OP(cl_khr_depth_images) |
| 387 |
_SPIRV_OP(cl_khr_gl_depth_images) |
| 388 |
_SPIRV_OP(cl_khr_subgroups) |
| 389 |
_SPIRV_OP(cl_khr_mipmap_image) |
| 390 |
_SPIRV_OP(cl_khr_mipmap_image_writes) |
| 391 |
_SPIRV_OP(cl_khr_egl_event) |
| 392 |
_SPIRV_OP(cl_khr_srgb_image_writes) |
| 393 |
_SPIRV_OP(cl_khr_extended_bit_ops) |
| 394 |
#undef _SPIRV_OP |
| 395 |
}; |
| 396 |
// clang-format on |
| 397 |
} // namespace OclExt |
| 398 |
namespace kOCLSubgroupsAVCIntel { |
| 399 |
const static char Prefix[] = "intel_sub_group_avc_"; |
| 400 |
const static char MCEPrefix[] = "intel_sub_group_avc_mce_"; |
| 401 |
const static char IMEPrefix[] = "intel_sub_group_avc_ime_"; |
| 402 |
const static char REFPrefix[] = "intel_sub_group_avc_ref_"; |
| 403 |
const static char SICPrefix[] = "intel_sub_group_avc_sic_"; |
| 404 |
const static char TypePrefix[] = "opencl.intel_sub_group_avc_"; |
| 405 |
} // namespace kOCLSubgroupsAVCIntel |
| 406 |
|
| 407 |
/////////////////////////////////////////////////////////////////////////////// |
| 408 |
// |
| 409 |
// Functions |
| 410 |
// |
| 411 |
/////////////////////////////////////////////////////////////////////////////// |
| 412 |
|
| 413 |
/// Get instruction index for SPIR-V extended instruction for OpenCL.std |
| 414 |
/// extended instruction set. |
| 415 |
/// \param MangledName The mangled name of OpenCL builtin function. |
| 416 |
/// \param DemangledName The demangled name of OpenCL builtin function if |
| 417 |
/// not empty. |
| 418 |
/// \return instruction index of extended instruction if the OpenCL builtin |
| 419 |
/// function is translated to an extended instruction, otherwise ~0U. |
| 420 |
unsigned getExtOp(StringRef MangledName, StringRef DemangledName = ""); |
| 421 |
|
| 422 |
/// Get literal arguments of call of atomic_work_item_fence. |
| 423 |
AtomicWorkItemFenceLiterals getAtomicWorkItemFenceLiterals(CallInst *CI); |
| 424 |
|
| 425 |
/// Get literal arguments of call of work_group_barrier or sub_group_barrier. |
| 426 |
BarrierLiterals getBarrierLiterals(CallInst *CI); |
| 427 |
|
| 428 |
/// Get number of memory order arguments for atomic builtin function. |
| 429 |
size_t getAtomicBuiltinNumMemoryOrderArgs(StringRef Name); |
| 430 |
|
| 431 |
/// Get number of memory order arguments for spirv atomic builtin function. |
| 432 |
size_t getSPIRVAtomicBuiltinNumMemoryOrderArgs(Op OC); |
| 433 |
|
| 434 |
/// Return true for OpenCL builtins which do compute operations |
| 435 |
/// (like add, sub, min, max, inc, dec, ...) atomically |
| 436 |
bool isComputeAtomicOCLBuiltin(StringRef DemangledName); |
| 437 |
|
| 438 |
/// Get OCL version from metadata opencl.ocl.version. |
| 439 |
/// \param AllowMulti Allows multiple operands if true. |
| 440 |
/// \return OCL version encoded as Major*10^5+Minor*10^3+Rev, |
| 441 |
/// e.g. 201000 for OCL 2.1, 200000 for OCL 2.0, 102000 for OCL 1.2, |
| 442 |
/// 0 if metadata not found. |
| 443 |
/// If there are multiple operands, check they are identical. |
| 444 |
unsigned getOCLVersion(Module *M, bool AllowMulti = false); |
| 445 |
|
| 446 |
/// Encode OpenCL version as Major*10^5+Minor*10^3+Rev. |
| 447 |
unsigned encodeOCLVer(unsigned short Major, unsigned char Minor, |
| 448 |
unsigned char Rev); |
| 449 |
|
| 450 |
/// Decode OpenCL version which is encoded as Major*10^5+Minor*10^3+Rev |
| 451 |
std::tuple<unsigned short, unsigned char, unsigned char> |
| 452 |
decodeOCLVer(unsigned Ver); |
| 453 |
|
| 454 |
/// Decode a MDNode assuming it contains three integer constants. |
| 455 |
SmallVector<unsigned, 3> decodeMDNode(MDNode *N); |
| 456 |
|
| 457 |
/// Get full path from debug info metadata |
| 458 |
/// Return empty string if the path is not available. |
| 459 |
template <typename T> std::string getFullPath(const T *Scope) { |
| 460 |
if (!Scope) |
| 461 |
return std::string(); |
| 462 |
StringRef Filename = Scope->getFilename(); |
| 463 |
auto Style = sys::path::Style::native; |
| 464 |
if (sys::path::is_absolute(Filename, Style)) |
| 465 |
return Filename.str(); |
| 466 |
SmallString<16> DirName = Scope->getDirectory(); |
| 467 |
sys::path::append(DirName, Style, Filename.str()); |
| 468 |
return DirName.str().str(); |
| 469 |
} |
| 470 |
|
| 471 |
/// Decode OpenCL vector type hint MDNode and encode it as SPIR-V execution |
| 472 |
/// mode VecTypeHint. |
| 473 |
unsigned transVecTypeHint(MDNode *Node); |
| 474 |
|
| 475 |
/// Decode SPIR-V encoding of vector type hint execution mode. |
| 476 |
Type *decodeVecTypeHint(LLVMContext &C, unsigned Code); |
| 477 |
|
| 478 |
SPIRAddressSpace getOCLOpaqueTypeAddrSpace(Op OpCode); |
| 479 |
SPIR::TypeAttributeEnum getOCLOpaqueTypeAddrSpace(SPIR::TypePrimitiveEnum Prim); |
| 480 |
|
| 481 |
inline unsigned mapOCLMemSemanticToSPIRV(unsigned MemFenceFlag, |
| 482 |
OCLMemOrderKind Order) { |
| 483 |
return OCLMemOrderMap::map(Order) | mapBitMask<OCLMemFenceMap>(MemFenceFlag); |
| 484 |
} |
| 485 |
|
| 486 |
inline unsigned mapOCLMemFenceFlagToSPIRV(unsigned MemFenceFlag) { |
| 487 |
return mapBitMask<OCLMemFenceMap>(MemFenceFlag); |
| 488 |
} |
| 489 |
|
| 490 |
inline std::pair<unsigned, OCLMemOrderKind> |
| 491 |
mapSPIRVMemSemanticToOCL(unsigned Sema) { |
| 492 |
return std::make_pair( |
| 493 |
rmapBitMask<OCLMemFenceMap>(Sema), |
| 494 |
OCLMemOrderMap::rmap(extractSPIRVMemOrderSemantic(Sema))); |
| 495 |
} |
| 496 |
|
| 497 |
inline OCLMemOrderKind mapSPIRVMemOrderToOCL(unsigned Sema) { |
| 498 |
return OCLMemOrderMap::rmap(extractSPIRVMemOrderSemantic(Sema)); |
| 499 |
} |
| 500 |
|
| 501 |
/// If the value is a special type initializer (something that bitcasts from |
| 502 |
/// spirv.ConstantSampler to spirv.Sampler or likewise for PipeStorage), get the |
| 503 |
/// original type initializer, unwrap the bitcast. Otherwise, return nullptr. |
| 504 |
Value *unwrapSpecialTypeInitializer(Value *V); |
| 505 |
|
| 506 |
bool isPipeOrAddressSpaceCastBI(const StringRef MangledName); |
| 507 |
bool isEnqueueKernelBI(const StringRef MangledName); |
| 508 |
bool isKernelQueryBI(const StringRef MangledName); |
| 509 |
|
| 510 |
/// Check that the type is the sampler_t |
| 511 |
bool isSamplerTy(Type *Ty); |
| 512 |
|
| 513 |
// Checks if the binary operator is an unfused fmul + fadd instruction. |
| 514 |
bool isUnfusedMulAdd(BinaryOperator *B); |
| 515 |
|
| 516 |
// Get data and vector size postfix for sugroup_block_{read|write} builtins |
| 517 |
// as specified by cl_intel_subgroups* extensions. |
| 518 |
// Scalar data assumed to be represented as vector of one element. |
| 519 |
std::string getIntelSubgroupBlockDataPostfix(unsigned ElementBitSize, |
| 520 |
unsigned VectorNumElements); |
| 521 |
|
| 522 |
void insertImageNameAccessQualifier(SPIRVAccessQualifierKind Acc, |
| 523 |
std::string &Name); |
| 524 |
|
| 525 |
std::unique_ptr<SPIRV::BuiltinFuncMangleInfo> makeMangler(Function &F); |
| 526 |
} // namespace OCLUtil |
| 527 |
|
| 528 |
using namespace OCLUtil; |
| 529 |
namespace SPIRV { |
| 530 |
|
| 531 |
template <class KeyTy, class ValTy, class Identifier = void> |
| 532 |
Instruction * |
| 533 |
getOrCreateSwitchFunc(StringRef MapName, Value *V, |
| 534 |
const SPIRVMap<KeyTy, ValTy, Identifier> &Map, |
| 535 |
bool IsReverse, std::optional<int> DefaultCase, |
| 536 |
Instruction *InsertPoint, int KeyMask = 0) { |
| 537 |
static_assert(std::is_convertible<KeyTy, int>::value && |
| 538 |
std::is_convertible<ValTy, int>::value, |
| 539 |
"Can map only integer values"); |
| 540 |
Type *Ty = V->getType(); |
| 541 |
assert(Ty && Ty->isIntegerTy() && "Can't map non-integer types"); |
| 542 |
Module *M = InsertPoint->getModule(); |
| 543 |
Function *F = getOrCreateFunction(M, Ty, Ty, MapName); |
| 544 |
if (!F->empty()) // The switch function already exists. just call it. |
| 545 |
return addCallInst(M, MapName, Ty, V, nullptr, InsertPoint); |
| 546 |
|
| 547 |
F->setLinkage(GlobalValue::PrivateLinkage); |
| 548 |
|
| 549 |
LLVMContext &Ctx = M->getContext(); |
| 550 |
BasicBlock *BB = BasicBlock::Create(Ctx, "entry", F); |
| 551 |
IRBuilder<> IRB(BB); |
| 552 |
SwitchInst *SI; |
| 553 |
F->arg_begin()->setName("key"); |
| 554 |
if (KeyMask) { |
| 555 |
Value *MaskV = ConstantInt::get(Type::getInt32Ty(Ctx), KeyMask); |
| 556 |
Value *NewKey = IRB.CreateAnd(MaskV, F->arg_begin()); |
| 557 |
NewKey->setName("key.masked"); |
| 558 |
SI = IRB.CreateSwitch(NewKey, BB); |
| 559 |
} else { |
| 560 |
SI = IRB.CreateSwitch(F->arg_begin(), BB); |
| 561 |
} |
| 562 |
|
| 563 |
if (!DefaultCase) { |
| 564 |
BasicBlock *DefaultBB = BasicBlock::Create(Ctx, "default", F); |
| 565 |
IRBuilder<> DefaultIRB(DefaultBB); |
| 566 |
DefaultIRB.CreateUnreachable(); |
| 567 |
SI->setDefaultDest(DefaultBB); |
| 568 |
} |
| 569 |
|
| 570 |
Map.foreach ([&](int Key, int Val) { |
| 571 |
if (IsReverse) |
| 572 |
std::swap(Key, Val); |
| 573 |
BasicBlock *CaseBB = BasicBlock::Create(Ctx, "case." + Twine(Key), F); |
| 574 |
IRBuilder<> CaseIRB(CaseBB); |
| 575 |
CaseIRB.CreateRet(CaseIRB.getInt32(Val)); |
| 576 |
SI->addCase(IRB.getInt32(Key), CaseBB); |
| 577 |
if (Key == DefaultCase) |
| 578 |
SI->setDefaultDest(CaseBB); |
| 579 |
}); |
| 580 |
assert(SI->getDefaultDest() != BB && "Invalid default destination in switch"); |
| 581 |
return addCallInst(M, MapName, Ty, V, nullptr, InsertPoint); |
| 582 |
} |
| 583 |
|
| 584 |
/// Performs conversion from OpenCL memory_scope into SPIR-V Scope. |
| 585 |
/// |
| 586 |
/// Supports both constant and non-constant values. To handle the latter case, |
| 587 |
/// function with switch..case statement will be inserted into module which |
| 588 |
/// \arg InsertBefore belongs to (in order to perform mapping at runtime) |
| 589 |
/// |
| 590 |
/// \param [in] MemScope memory_scope value which needs to be translated |
| 591 |
/// \param [in] DefaultCase default value for switch..case construct if |
| 592 |
/// dynamic mapping is used |
| 593 |
/// \param [in] InsertBefore insertion point for call into conversion function |
| 594 |
/// which is generated if \arg MemScope is not a constant |
| 595 |
/// \returns \c Value corresponding to SPIR-V Scope equivalent to OpenCL |
| 596 |
/// memory_scope passed in \arg MemScope |
| 597 |
Value *transOCLMemScopeIntoSPIRVScope(Value *MemScope, |
| 598 |
std::optional<int> DefaultCase, |
| 599 |
Instruction *InsertBefore); |
| 600 |
|
| 601 |
/// Performs conversion from OpenCL memory_order into SPIR-V Memory Semantics. |
| 602 |
/// |
| 603 |
/// Supports both constant and non-constant values. To handle the latter case, |
| 604 |
/// function with switch..case statement will be inserted into module which |
| 605 |
/// \arg InsertBefore belongs to (in order to perform mapping at runtime) |
| 606 |
/// |
| 607 |
/// \param [in] MemOrder memory_scope value which needs to be translated |
| 608 |
/// \param [in] DefaultCase default value for switch..case construct if |
| 609 |
/// dynamic mapping is used |
| 610 |
/// \param [in] InsertBefore insertion point for call into conversion function |
| 611 |
/// which is generated if \arg MemOrder is not a constant |
| 612 |
/// \returns \c Value corresponding to SPIR-V Memory Semantics equivalent to |
| 613 |
/// OpenCL memory_order passed in \arg MemOrder |
| 614 |
Value *transOCLMemOrderIntoSPIRVMemorySemantics(Value *MemOrder, |
| 615 |
std::optional<int> DefaultCase, |
| 616 |
Instruction *InsertBefore); |
| 617 |
|
| 618 |
/// Performs conversion from SPIR-V Scope into OpenCL memory_scope. |
| 619 |
/// |
| 620 |
/// Supports both constant and non-constant values. To handle the latter case, |
| 621 |
/// function with switch..case statement will be inserted into module which |
| 622 |
/// \arg InsertBefore belongs to (in order to perform mapping at runtime) |
| 623 |
/// |
| 624 |
/// \param [in] MemScope Scope value which needs to be translated |
| 625 |
/// \param [in] InsertBefore insertion point for call into conversion function |
| 626 |
/// which is generated if \arg MemScope is not a constant |
| 627 |
/// \returns \c Value corresponding to OpenCL memory_scope equivalent to SPIR-V |
| 628 |
/// Scope passed in \arg MemScope |
| 629 |
Value *transSPIRVMemoryScopeIntoOCLMemoryScope(Value *MemScope, |
| 630 |
Instruction *InsertBefore); |
| 631 |
|
| 632 |
/// Performs conversion from SPIR-V Memory Semantics into OpenCL memory_order. |
| 633 |
/// |
| 634 |
/// Supports both constant and non-constant values. To handle the latter case, |
| 635 |
/// function with switch..case statement will be inserted into module which |
| 636 |
/// \arg InsertBefore belongs to (in order to perform mapping at runtime) |
| 637 |
/// |
| 638 |
/// \param [in] MemorySemantics Memory Semantics value which needs to be |
| 639 |
/// translated |
| 640 |
/// \param [in] InsertBefore insertion point for call into conversion function |
| 641 |
/// which is generated if \arg MemorySemantics is not a constant |
| 642 |
/// \returns \c Value corresponding to OpenCL memory_order equivalent to SPIR-V |
| 643 |
/// Memory Semantics passed in \arg MemorySemantics |
| 644 |
Value *transSPIRVMemorySemanticsIntoOCLMemoryOrder(Value *MemorySemantics, |
| 645 |
Instruction *InsertBefore); |
| 646 |
|
| 647 |
/// Performs conversion from SPIR-V Memory Semantics into OpenCL |
| 648 |
/// mem_fence_flags. |
| 649 |
/// |
| 650 |
/// Supports both constant and non-constant values. To handle the latter case, |
| 651 |
/// function with switch..case statement will be inserted into module which |
| 652 |
/// \arg InsertBefore belongs to (in order to perform mapping at runtime) |
| 653 |
/// |
| 654 |
/// \param [in] MemorySemantics Memory Semantics value which needs to be |
| 655 |
/// translated |
| 656 |
/// \param [in] InsertBefore insertion point for call into conversion function |
| 657 |
/// which is generated if \arg MemorySemantics is not a constant |
| 658 |
/// \returns \c Value corresponding to OpenCL mem_fence_flags equivalent to |
| 659 |
/// SPIR-V Memory Semantics passed in \arg MemorySemantics |
| 660 |
Value *transSPIRVMemorySemanticsIntoOCLMemFenceFlags(Value *MemorySemantics, |
| 661 |
Instruction *InsertBefore); |
| 662 |
|
| 663 |
class SPIRVSubgroupsAVCIntelInst; |
| 664 |
typedef SPIRVMap<std::string, Op, SPIRVSubgroupsAVCIntelInst> |
| 665 |
OCLSPIRVSubgroupAVCIntelBuiltinMap; |
| 666 |
|
| 667 |
typedef SPIRVMap<AtomicRMWInst::BinOp, Op> LLVMSPIRVAtomicRmwOpCodeMap; |
| 668 |
|
| 669 |
class SPIRVFixedPointIntelInst; |
| 670 |
template <> |
| 671 |
inline void SPIRVMap<std::string, Op, SPIRVFixedPointIntelInst>::init() { |
| 672 |
#define _SPIRV_OP(x, y) add("intel_arbitrary_fixed_" #x, OpFixed##y##INTEL); |
| 673 |
_SPIRV_OP(sqrt, Sqrt) |
| 674 |
_SPIRV_OP(recip, Recip) |
| 675 |
_SPIRV_OP(rsqrt, Rsqrt) |
| 676 |
_SPIRV_OP(sin, Sin) |
| 677 |
_SPIRV_OP(cos, Cos) |
| 678 |
_SPIRV_OP(sincos, SinCos) |
| 679 |
_SPIRV_OP(sinpi, SinPi) |
| 680 |
_SPIRV_OP(cospi, CosPi) |
| 681 |
_SPIRV_OP(sincospi, SinCosPi) |
| 682 |
_SPIRV_OP(log, Log) |
| 683 |
_SPIRV_OP(exp, Exp) |
| 684 |
#undef _SPIRV_OP |
| 685 |
} |
| 686 |
typedef SPIRVMap<std::string, Op, SPIRVFixedPointIntelInst> |
| 687 |
SPIRVFixedPointIntelMap; |
| 688 |
|
| 689 |
class SPIRVArbFloatIntelInst; |
| 690 |
template <> |
| 691 |
inline void SPIRVMap<std::string, Op, SPIRVArbFloatIntelInst>::init() { |
| 692 |
#define _SPIRV_OP(x, y) \ |
| 693 |
add("intel_arbitrary_float_" #y, OpArbitraryFloat##x##INTEL); |
| 694 |
_SPIRV_OP(Cast, cast) |
| 695 |
_SPIRV_OP(CastFromInt, cast_from_int) |
| 696 |
_SPIRV_OP(CastToInt, cast_to_int) |
| 697 |
_SPIRV_OP(Add, add) |
| 698 |
_SPIRV_OP(Sub, sub) |
| 699 |
_SPIRV_OP(Mul, mul) |
| 700 |
_SPIRV_OP(Div, div) |
| 701 |
_SPIRV_OP(GT, gt) |
| 702 |
_SPIRV_OP(GE, ge) |
| 703 |
_SPIRV_OP(LT, lt) |
| 704 |
_SPIRV_OP(LE, le) |
| 705 |
_SPIRV_OP(EQ, eq) |
| 706 |
_SPIRV_OP(Recip, recip) |
| 707 |
_SPIRV_OP(RSqrt, rsqrt) |
| 708 |
_SPIRV_OP(Cbrt, cbrt) |
| 709 |
_SPIRV_OP(Hypot, hypot) |
| 710 |
_SPIRV_OP(Sqrt, sqrt) |
| 711 |
_SPIRV_OP(Log, log) |
| 712 |
_SPIRV_OP(Log2, log2) |
| 713 |
_SPIRV_OP(Log10, log10) |
| 714 |
_SPIRV_OP(Log1p, log1p) |
| 715 |
_SPIRV_OP(Exp, exp) |
| 716 |
_SPIRV_OP(Exp2, exp2) |
| 717 |
_SPIRV_OP(Exp10, exp10) |
| 718 |
_SPIRV_OP(Expm1, expm1) |
| 719 |
_SPIRV_OP(Sin, sin) |
| 720 |
_SPIRV_OP(Cos, cos) |
| 721 |
_SPIRV_OP(SinCos, sincos) |
| 722 |
_SPIRV_OP(SinPi, sinpi) |
| 723 |
_SPIRV_OP(CosPi, cospi) |
| 724 |
_SPIRV_OP(SinCosPi, sincospi) |
| 725 |
_SPIRV_OP(ASin, asin) |
| 726 |
_SPIRV_OP(ASinPi, asinpi) |
| 727 |
_SPIRV_OP(ACos, acos) |
| 728 |
_SPIRV_OP(ACosPi, acospi) |
| 729 |
_SPIRV_OP(ATan, atan) |
| 730 |
_SPIRV_OP(ATanPi, atanpi) |
| 731 |
_SPIRV_OP(ATan2, atan2) |
| 732 |
_SPIRV_OP(Pow, pow) |
| 733 |
_SPIRV_OP(PowR, powr) |
| 734 |
_SPIRV_OP(PowN, pown) |
| 735 |
#undef _SPIRV_OP |
| 736 |
} |
| 737 |
typedef SPIRVMap<std::string, Op, SPIRVArbFloatIntelInst> SPIRVArbFloatIntelMap; |
| 738 |
|
| 739 |
} // namespace SPIRV |
| 740 |
|
| 741 |
#endif // SPIRV_OCLUTIL_H |