| 1 |
//=- OCLToSPIRV.h - OpenCL to SPIR-V builtin preprocessing pass -*- C++ -*-=// |
| 2 |
// |
| 3 |
// The LLVM/SPIR-V 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) 2022 The Khronos Group Inc. |
| 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 The Khronos Group, 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 implements preprocessing of OpenCL C built-in functions into SPIR-V |
| 36 |
// friendly IR form for further translation into SPIR-V |
| 37 |
// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#ifndef SPIRV_OCLTOSPIRV_H |
| 41 |
#define SPIRV_OCLTOSPIRV_H |
| 42 |
|
| 43 |
#include "OCLUtil.h" |
| 44 |
#include "SPIRVBuiltinHelper.h" |
| 45 |
|
| 46 |
#include "llvm/IR/InstVisitor.h" |
| 47 |
#include "llvm/IR/PassManager.h" |
| 48 |
#include "llvm/Pass.h" |
| 49 |
|
| 50 |
namespace SPIRV { |
| 51 |
|
| 52 |
class OCLTypeToSPIRVBase; |
| 53 |
|
| 54 |
class OCLToSPIRVBase : public InstVisitor<OCLToSPIRVBase>, BuiltinCallHelper { |
| 55 |
public: |
| 56 |
OCLToSPIRVBase() |
| 57 |
: BuiltinCallHelper(ManglingRules::SPIRV), Ctx(nullptr), CLVer(0), |
| 58 |
OCLTypeToSPIRVPtr(nullptr) {} |
| 59 |
virtual ~OCLToSPIRVBase() {} |
| 60 |
bool runOCLToSPIRV(Module &M); |
| 61 |
|
| 62 |
virtual void visitCallInst(CallInst &CI); |
| 63 |
|
| 64 |
/// Transform barrier/work_group_barrier/sub_group_barrier |
| 65 |
/// to __spirv_ControlBarrier. |
| 66 |
/// barrier(flag) => |
| 67 |
/// __spirv_ControlBarrier(workgroup, workgroup, map(flag)) |
| 68 |
/// work_group_barrier(scope, flag) => |
| 69 |
/// __spirv_ControlBarrier(workgroup, map(scope), map(flag)) |
| 70 |
/// sub_group_barrier(scope, flag) => |
| 71 |
/// __spirv_ControlBarrier(subgroup, map(scope), map(flag)) |
| 72 |
void visitCallBarrier(CallInst *CI); |
| 73 |
|
| 74 |
/// Erase useless convert functions. |
| 75 |
/// \return true if the call instruction is erased. |
| 76 |
bool eraseUselessConvert(CallInst *Call, StringRef MangledName, |
| 77 |
StringRef DeMangledName); |
| 78 |
|
| 79 |
/// Transform convert_ to |
| 80 |
/// __spirv_{CastOpName}_R{TargeTyName}{_sat}{_rt[p|n|z|e]} |
| 81 |
void visitCallConvert(CallInst *CI, StringRef MangledName, |
| 82 |
StringRef DemangledName); |
| 83 |
|
| 84 |
/// Transform async_work_group{_strided}_copy. |
| 85 |
/// async_work_group_copy(dst, src, n, event) |
| 86 |
/// => async_work_group_strided_copy(dst, src, n, 1, event) |
| 87 |
/// async_work_group_strided_copy(dst, src, n, stride, event) |
| 88 |
/// => __spirv_AsyncGroupCopy(ScopeWorkGroup, dst, src, n, stride, event) |
| 89 |
void visitCallAsyncWorkGroupCopy(CallInst *CI, StringRef DemangledName); |
| 90 |
|
| 91 |
/// Transform OCL builtin function to SPIR-V builtin function. |
| 92 |
void transBuiltin(CallInst *CI, OCLBuiltinTransInfo &Info); |
| 93 |
|
| 94 |
/// Transform atomic_work_item_fence/mem_fence to __spirv_MemoryBarrier. |
| 95 |
/// func(flag, order, scope) => |
| 96 |
/// __spirv_MemoryBarrier(map(scope), map(flag)|map(order)) |
| 97 |
void transMemoryBarrier(CallInst *CI, AtomicWorkItemFenceLiterals); |
| 98 |
|
| 99 |
/// Transform all to __spirv_Op(All|Any). Note that the types mismatch so |
| 100 |
// some extra code is emitted to convert between the two. |
| 101 |
void visitCallAllAny(spv::Op OC, CallInst *CI); |
| 102 |
|
| 103 |
/// Transform atomic_* to __spirv_Atomic*. |
| 104 |
/// atomic_x(ptr_arg, args, order, scope) => |
| 105 |
/// __spirv_AtomicY(ptr_arg, map(order), map(scope), args) |
| 106 |
void transAtomicBuiltin(CallInst *CI, OCLBuiltinTransInfo &Info); |
| 107 |
|
| 108 |
/// Transform atomic_work_item_fence to __spirv_MemoryBarrier. |
| 109 |
/// atomic_work_item_fence(flag, order, scope) => |
| 110 |
/// __spirv_MemoryBarrier(map(scope), map(flag)|map(order)) |
| 111 |
void visitCallAtomicWorkItemFence(CallInst *CI); |
| 112 |
|
| 113 |
/// Transform atomic_compare_exchange call. |
| 114 |
/// In atomic_compare_exchange, the expected value parameter is a pointer. |
| 115 |
/// However in SPIR-V it is a value. The transformation adds a load |
| 116 |
/// instruction, result of which is passed to atomic_compare_exchange as |
| 117 |
/// argument. |
| 118 |
/// The transformation adds a store instruction after the call, to update the |
| 119 |
/// value in expected with the value pointed to by object. Though, it is not |
| 120 |
/// necessary in case they are equal, this approach makes result code simpler. |
| 121 |
/// Also ICmp instruction is added, because the call must return result of |
| 122 |
/// comparison. |
| 123 |
/// \returns the call instruction of atomic_compare_exchange_strong. |
| 124 |
CallInst *visitCallAtomicCmpXchg(CallInst *CI); |
| 125 |
|
| 126 |
/// Transform atomic_init. |
| 127 |
/// atomic_init(p, x) => store p, x |
| 128 |
void visitCallAtomicInit(CallInst *CI); |
| 129 |
|
| 130 |
/// Transform legacy OCL 1.x atomic builtins to SPIR-V builtins for extensions |
| 131 |
/// cl_khr_int64_base_atomics |
| 132 |
/// cl_khr_int64_extended_atomics |
| 133 |
/// Do nothing if the called function is not a legacy atomic builtin. |
| 134 |
void visitCallAtomicLegacy(CallInst *CI, StringRef MangledName, |
| 135 |
StringRef DemangledName); |
| 136 |
|
| 137 |
/// Transform OCL 2.0 C++11 atomic builtins to SPIR-V builtins. |
| 138 |
/// Do nothing if the called function is not a C++11 atomic builtin. |
| 139 |
void visitCallAtomicCpp11(CallInst *CI, StringRef MangledName, |
| 140 |
StringRef DemangledName); |
| 141 |
|
| 142 |
/// Transform OCL builtin function to SPIR-V builtin function. |
| 143 |
/// Assuming there is a simple name mapping without argument changes. |
| 144 |
/// Should be called at last. |
| 145 |
void visitCallBuiltinSimple(CallInst *CI, StringRef MangledName, |
| 146 |
StringRef DemangledName); |
| 147 |
|
| 148 |
/// Transform get_image_{width|height|depth|dim}. |
| 149 |
/// get_image_xxx(...) => |
| 150 |
/// dimension = __spirv_ImageQuerySizeLod_R{ReturnType}(...); |
| 151 |
/// return dimension.{x|y|z}; |
| 152 |
void visitCallGetImageSize(CallInst *CI, StringRef DemangledName); |
| 153 |
|
| 154 |
/// Transform {work|sub}_group_x => |
| 155 |
/// __spirv_{OpName} |
| 156 |
/// |
| 157 |
/// Special handling of work_group_broadcast. |
| 158 |
/// work_group_broadcast(a, x, y, z) |
| 159 |
/// => |
| 160 |
/// __spirv_GroupBroadcast(a, vec3(x, y, z)) |
| 161 |
|
| 162 |
void visitCallGroupBuiltin(CallInst *CI, StringRef DemangledName); |
| 163 |
|
| 164 |
/// Transform mem_fence to __spirv_MemoryBarrier. |
| 165 |
/// mem_fence(flag) => __spirv_MemoryBarrier(Workgroup, map(flag)) |
| 166 |
void visitCallMemFence(CallInst *CI, StringRef DemangledName); |
| 167 |
|
| 168 |
void visitCallNDRange(CallInst *CI, StringRef DemangledName); |
| 169 |
|
| 170 |
/// Transform read_image with sampler arguments. |
| 171 |
/// read_image(image, sampler, ...) => |
| 172 |
/// sampled_image = __spirv_SampledImage(image, sampler); |
| 173 |
/// return __spirv_ImageSampleExplicitLod_R{ReturnType}(sampled_image, ...); |
| 174 |
void visitCallReadImageWithSampler(CallInst *CI, StringRef MangledName, |
| 175 |
StringRef DemangledName); |
| 176 |
|
| 177 |
/// Transform read_image with msaa image arguments. |
| 178 |
/// Sample argument must be acoded as Image Operand. |
| 179 |
void visitCallReadImageMSAA(CallInst *CI, StringRef MangledName); |
| 180 |
|
| 181 |
/// Transform {read|write}_image without sampler arguments. |
| 182 |
void visitCallReadWriteImage(CallInst *CI, StringRef DemangledName); |
| 183 |
|
| 184 |
/// Transform to_{global|local|private}. |
| 185 |
/// |
| 186 |
/// T* a = ...; |
| 187 |
/// addr T* b = to_addr(a); |
| 188 |
/// => |
| 189 |
/// i8* x = cast<i8*>(a); |
| 190 |
/// addr i8* y = __spirv_GenericCastToPtr_ToAddr(x); |
| 191 |
/// addr T* b = cast<addr T*>(y); |
| 192 |
void visitCallToAddr(CallInst *CI, StringRef DemangledName); |
| 193 |
|
| 194 |
/// Transform return type of relatinal built-in functions like isnan, isfinite |
| 195 |
/// to boolean values. |
| 196 |
void visitCallRelational(CallInst *CI, StringRef DemangledName); |
| 197 |
|
| 198 |
/// Transform vector load/store functions to SPIR-V extended builtin |
| 199 |
/// functions |
| 200 |
/// {vload|vstore{a}}{_half}{n}{_rte|_rtz|_rtp|_rtn} => |
| 201 |
/// __spirv_ocl_{ExtendedInstructionOpCodeName}__R{ReturnType} |
| 202 |
void visitCallVecLoadStore(CallInst *CI, StringRef MangledName, |
| 203 |
StringRef DemangledName); |
| 204 |
|
| 205 |
/// Transforms get_mem_fence built-in to SPIR-V function and aligns result |
| 206 |
/// values with SPIR 1.2. get_mem_fence(ptr) => __spirv_GenericPtrMemSemantics |
| 207 |
/// GenericPtrMemSemantics valid values are 0x100, 0x200 and 0x300, where is |
| 208 |
/// SPIR 1.2 defines them as 0x1, 0x2 and 0x3, so this function adjusts |
| 209 |
/// GenericPtrMemSemantics results to SPIR 1.2 values. |
| 210 |
void visitCallGetFence(CallInst *CI, StringRef DemangledName); |
| 211 |
|
| 212 |
/// Transforms OpDot instructions with a scalar type to a fmul instruction |
| 213 |
void visitCallDot(CallInst *CI); |
| 214 |
|
| 215 |
/// Transforms OpDot instructions with a vector or scalar (packed vector) type |
| 216 |
/// to dot or dot_acc_sat instructions |
| 217 |
void visitCallDot(CallInst *CI, StringRef MangledName, |
| 218 |
StringRef DemangledName); |
| 219 |
|
| 220 |
/// Fixes for built-in functions with vector+scalar arguments that are |
| 221 |
/// translated to the SPIR-V instructions where all arguments must have the |
| 222 |
/// same type. |
| 223 |
void visitCallScalToVec(CallInst *CI, StringRef MangledName, |
| 224 |
StringRef DemangledName); |
| 225 |
|
| 226 |
/// Transform get_image_channel_{order|data_type} built-in functions to |
| 227 |
/// __spirv_ocl_{ImageQueryOrder|ImageQueryFormat} |
| 228 |
void visitCallGetImageChannel(CallInst *CI, StringRef DemangledName, |
| 229 |
unsigned int Offset); |
| 230 |
|
| 231 |
/// Transform enqueue_kernel and kernel query built-in functions to |
| 232 |
/// spirv-friendly format filling arguments, required for device-side enqueue |
| 233 |
/// instructions, but missed in the original call |
| 234 |
void visitCallEnqueueKernel(CallInst *CI, StringRef DemangledName); |
| 235 |
void visitCallKernelQuery(CallInst *CI, StringRef DemangledName); |
| 236 |
|
| 237 |
/// For cl_intel_subgroups block read built-ins: |
| 238 |
void visitSubgroupBlockReadINTEL(CallInst *CI); |
| 239 |
|
| 240 |
/// For cl_intel_subgroups block write built-ins: |
| 241 |
void visitSubgroupBlockWriteINTEL(CallInst *CI); |
| 242 |
|
| 243 |
/// For cl_intel_media_block_io built-ins: |
| 244 |
void visitSubgroupImageMediaBlockINTEL(CallInst *CI, StringRef DemangledName); |
| 245 |
// For cl_intel_device_side_avc_motion_estimation built-ins |
| 246 |
void visitSubgroupAVCBuiltinCall(CallInst *CI, StringRef DemangledName); |
| 247 |
void visitSubgroupAVCWrapperBuiltinCall(CallInst *CI, Op WrappedOC, |
| 248 |
StringRef DemangledName); |
| 249 |
void visitSubgroupAVCBuiltinCallWithSampler(CallInst *CI, |
| 250 |
StringRef DemangledName); |
| 251 |
|
| 252 |
/// For cl_intel_split_work_group_barrier built-ins: |
| 253 |
void visitCallSplitBarrierINTEL(CallInst *CI, StringRef DemangledName); |
| 254 |
|
| 255 |
void visitCallLdexp(CallInst *CI, StringRef MangledName, |
| 256 |
StringRef DemangledName); |
| 257 |
|
| 258 |
/// For cl_intel_convert_bfloat16_as_ushort |
| 259 |
void visitCallConvertBFloat16AsUshort(CallInst *CI, StringRef DemangledName); |
| 260 |
/// For cl_intel_convert_as_bfloat16_float |
| 261 |
void visitCallConvertAsBFloat16Float(CallInst *CI, StringRef DemangledName); |
| 262 |
|
| 263 |
void setOCLTypeToSPIRV(OCLTypeToSPIRVBase *OCLTypeToSPIRV) { |
| 264 |
OCLTypeToSPIRVPtr = OCLTypeToSPIRV; |
| 265 |
} |
| 266 |
OCLTypeToSPIRVBase *getOCLTypeToSPIRV() { return OCLTypeToSPIRVPtr; } |
| 267 |
|
| 268 |
private: |
| 269 |
LLVMContext *Ctx; |
| 270 |
unsigned CLVer; /// OpenCL version as major*10+minor |
| 271 |
std::set<Value *> ValuesToDelete; |
| 272 |
OCLTypeToSPIRVBase *OCLTypeToSPIRVPtr; |
| 273 |
|
| 274 |
ConstantInt *addInt32(int I) { return getInt32(M, I); } |
| 275 |
ConstantInt *addSizet(uint64_t I) { return getSizet(M, I); } |
| 276 |
|
| 277 |
/// Get vector width from OpenCL vload* function name. |
| 278 |
SPIRVWord getVecLoadWidth(const std::string &DemangledName); |
| 279 |
|
| 280 |
/// Transform OpenCL vload/vstore function name. |
| 281 |
void transVecLoadStoreName(std::string &DemangledName, |
| 282 |
const std::string &Stem, bool AlwaysN); |
| 283 |
|
| 284 |
void processSubgroupBlockReadWriteINTEL(CallInst *CI, |
| 285 |
OCLBuiltinTransInfo &Info, |
| 286 |
const Type *DataTy); |
| 287 |
}; |
| 288 |
|
| 289 |
class OCLToSPIRVLegacy : public OCLToSPIRVBase, public llvm::ModulePass { |
| 290 |
public: |
| 291 |
OCLToSPIRVLegacy() : ModulePass(ID) { |
| 292 |
initializeOCLToSPIRVLegacyPass(*PassRegistry::getPassRegistry()); |
| 293 |
} |
| 294 |
|
| 295 |
bool runOnModule(Module &M) override; |
| 296 |
|
| 297 |
void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 298 |
|
| 299 |
static char ID; |
| 300 |
}; |
| 301 |
|
| 302 |
class OCLToSPIRVPass : public OCLToSPIRVBase, |
| 303 |
public llvm::PassInfoMixin<OCLToSPIRVPass> { |
| 304 |
public: |
| 305 |
llvm::PreservedAnalyses run(llvm::Module &M, |
| 306 |
llvm::ModuleAnalysisManager &MAM); |
| 307 |
}; |
| 308 |
|
| 309 |
} // namespace SPIRV |
| 310 |
|
| 311 |
#endif // SPIRV_OCLTOSPIRV_H |