| 1 |
//===- SPIRVReader.cpp - Converts SPIR-V to LLVM ----------------*- 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) 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 |
/// \file |
| 35 |
/// |
| 36 |
/// This file implements conversion of SPIR-V binary to LLVM IR. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
#include "SPIRVReader.h" |
| 40 |
#include "OCLUtil.h" |
| 41 |
#include "SPIRVAsm.h" |
| 42 |
#include "SPIRVBasicBlock.h" |
| 43 |
#include "SPIRVExtInst.h" |
| 44 |
#include "SPIRVFnVar.h" |
| 45 |
#include "SPIRVFunction.h" |
| 46 |
#include "SPIRVInstruction.h" |
| 47 |
#include "SPIRVInternal.h" |
| 48 |
#include "SPIRVMDBuilder.h" |
| 49 |
#include "SPIRVMemAliasingINTEL.h" |
| 50 |
#include "SPIRVModule.h" |
| 51 |
#include "SPIRVToLLVMDbgTran.h" |
| 52 |
#include "SPIRVToOCL.h" |
| 53 |
#include "SPIRVType.h" |
| 54 |
#include "SPIRVUtil.h" |
| 55 |
#include "SPIRVValue.h" |
| 56 |
#include "VectorComputeUtil.h" |
| 57 |
|
| 58 |
#include "llvm/ADT/DenseMap.h" |
| 59 |
#include "llvm/ADT/SmallSet.h" |
| 60 |
#include "llvm/Analysis/LoopInfo.h" |
| 61 |
#include "llvm/BinaryFormat/Dwarf.h" |
| 62 |
#include "llvm/IR/Attributes.h" |
| 63 |
#include "llvm/IR/Constants.h" |
| 64 |
#include "llvm/IR/DerivedTypes.h" |
| 65 |
#include "llvm/IR/Dominators.h" |
| 66 |
#include "llvm/IR/IRBuilder.h" |
| 67 |
#include "llvm/IR/InlineAsm.h" |
| 68 |
#include "llvm/IR/Instructions.h" |
| 69 |
#include "llvm/IR/IntrinsicInst.h" |
| 70 |
#include "llvm/IR/MDBuilder.h" |
| 71 |
#include "llvm/IR/Metadata.h" |
| 72 |
#include "llvm/IR/Module.h" |
| 73 |
#include "llvm/IR/Type.h" |
| 74 |
#include "llvm/IR/TypedPointerType.h" |
| 75 |
#include "llvm/Support/Casting.h" |
| 76 |
#include "llvm/Support/CommandLine.h" |
| 77 |
#include "llvm/Support/Debug.h" |
| 78 |
#include "llvm/Support/FileSystem.h" |
| 79 |
|
| 80 |
#include <algorithm> |
| 81 |
#include <cstdlib> |
| 82 |
#include <fstream> |
| 83 |
#include <functional> |
| 84 |
#include <iostream> |
| 85 |
#include <iterator> |
| 86 |
#include <map> |
| 87 |
#include <set> |
| 88 |
#include <sstream> |
| 89 |
#include <string> |
| 90 |
|
| 91 |
#define DEBUG_TYPE "spirv" |
| 92 |
|
| 93 |
using namespace std; |
| 94 |
using namespace llvm; |
| 95 |
using namespace SPIRV; |
| 96 |
using namespace OCLUtil; |
| 97 |
|
| 98 |
namespace SPIRV { |
| 99 |
|
| 100 |
cl::opt<bool> SPIRVEnableStepExpansion( |
| 101 |
"spirv-expand-step", cl::init(true), |
| 102 |
cl::desc("Enable expansion of OpenCL step and smoothstep function")); |
| 103 |
|
| 104 |
// Prefix for placeholder global variable name. |
| 105 |
const char *KPlaceholderPrefix = "placeholder."; |
| 106 |
|
| 107 |
// Save the translated LLVM before validation for debugging purpose. |
| 108 |
static bool DbgSaveTmpLLVM = false; |
| 109 |
static const char *DbgTmpLLVMFileName = "_tmp_llvmbil.ll"; |
| 110 |
|
| 111 |
namespace kOCLTypeQualifierName { |
| 112 |
const static char *Volatile = "volatile"; |
| 113 |
const static char *Restrict = "restrict"; |
| 114 |
const static char *Pipe = "pipe"; |
| 115 |
} // namespace kOCLTypeQualifierName |
| 116 |
|
| 117 |
static bool isKernel(SPIRVFunction *BF) { |
| 118 |
return BF->getModule()->isEntryPoint(ExecutionModelKernel, BF->getId()); |
| 119 |
} |
| 120 |
|
| 121 |
static void dumpLLVM(Module *M, const std::string &FName) { |
| 122 |
std::error_code EC; |
| 123 |
raw_fd_ostream FS(FName, EC, sys::fs::OF_None); |
| 124 |
if (!EC) { |
| 125 |
FS << *M; |
| 126 |
FS.close(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
static MDNode *getMDNodeStringIntVec(LLVMContext *Context, |
| 131 |
const std::vector<SPIRVWord> &IntVals) { |
| 132 |
std::vector<Metadata *> ValueVec; |
| 133 |
for (auto &I : IntVals) |
| 134 |
ValueVec.push_back(ConstantAsMetadata::get( |
| 135 |
ConstantInt::get(Type::getInt32Ty(*Context), I))); |
| 136 |
return MDNode::get(*Context, ValueVec); |
| 137 |
} |
| 138 |
|
| 139 |
static MDNode *getMDTwoInt(LLVMContext *Context, unsigned Int1, unsigned Int2) { |
| 140 |
std::vector<Metadata *> ValueVec; |
| 141 |
ValueVec.push_back(ConstantAsMetadata::get( |
| 142 |
ConstantInt::get(Type::getInt32Ty(*Context), Int1))); |
| 143 |
ValueVec.push_back(ConstantAsMetadata::get( |
| 144 |
ConstantInt::get(Type::getInt32Ty(*Context), Int2))); |
| 145 |
return MDNode::get(*Context, ValueVec); |
| 146 |
} |
| 147 |
|
| 148 |
static void addOCLVersionMetadata(LLVMContext *Context, Module *M, |
| 149 |
const std::string &MDName, unsigned Major, |
| 150 |
unsigned Minor) { |
| 151 |
NamedMDNode *NamedMD = M->getOrInsertNamedMetadata(MDName); |
| 152 |
NamedMD->addOperand(getMDTwoInt(Context, Major, Minor)); |
| 153 |
} |
| 154 |
|
| 155 |
static void addNamedMetadataStringSet(LLVMContext *Context, Module *M, |
| 156 |
const std::string &MDName, |
| 157 |
const std::set<std::string> &StrSet) { |
| 158 |
NamedMDNode *NamedMD = M->getOrInsertNamedMetadata(MDName); |
| 159 |
std::vector<Metadata *> ValueVec; |
| 160 |
for (auto &&Str : StrSet) { |
| 161 |
ValueVec.push_back(MDString::get(*Context, Str)); |
| 162 |
} |
| 163 |
NamedMD->addOperand(MDNode::get(*Context, ValueVec)); |
| 164 |
} |
| 165 |
|
| 166 |
static void addKernelArgumentMetadata( |
| 167 |
LLVMContext *Context, const std::string &MDName, SPIRVFunction *BF, |
| 168 |
llvm::Function *Fn, |
| 169 |
std::function<Metadata *(SPIRVFunctionParameter *)> ForeachFnArg) { |
| 170 |
std::vector<Metadata *> ValueVec; |
| 171 |
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) { |
| 172 |
ValueVec.push_back(ForeachFnArg(Arg)); |
| 173 |
}); |
| 174 |
Fn->setMetadata(MDName, MDNode::get(*Context, ValueVec)); |
| 175 |
} |
| 176 |
|
| 177 |
static void addBufferLocationMetadata( |
| 178 |
LLVMContext *Context, SPIRVFunction *BF, llvm::Function *Fn, |
| 179 |
std::function<Metadata *(SPIRVFunctionParameter *)> ForeachFnArg) { |
| 180 |
std::vector<Metadata *> ValueVec; |
| 181 |
bool DecorationFound = false; |
| 182 |
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) { |
| 183 |
if (Arg->getType()->isTypePointer() && |
| 184 |
Arg->hasDecorate(DecorationBufferLocationINTEL)) { |
| 185 |
DecorationFound = true; |
| 186 |
ValueVec.push_back(ForeachFnArg(Arg)); |
| 187 |
} else { |
| 188 |
llvm::Metadata *DefaultNode = ConstantAsMetadata::get( |
| 189 |
ConstantInt::get(Type::getInt32Ty(*Context), -1)); |
| 190 |
ValueVec.push_back(DefaultNode); |
| 191 |
} |
| 192 |
}); |
| 193 |
if (DecorationFound) |
| 194 |
Fn->setMetadata("kernel_arg_buffer_location", |
| 195 |
MDNode::get(*Context, ValueVec)); |
| 196 |
} |
| 197 |
|
| 198 |
static void addRuntimeAlignedMetadata( |
| 199 |
LLVMContext *Context, SPIRVFunction *BF, llvm::Function *Fn, |
| 200 |
std::function<Metadata *(SPIRVFunctionParameter *)> ForeachFnArg) { |
| 201 |
std::vector<Metadata *> ValueVec; |
| 202 |
bool RuntimeAlignedFound = false; |
| 203 |
[[maybe_unused]] llvm::Metadata *DefaultNode = |
| 204 |
ConstantAsMetadata::get(ConstantInt::get(Type::getInt1Ty(*Context), 0)); |
| 205 |
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) { |
| 206 |
if (Arg->hasAttr(FunctionParameterAttributeRuntimeAlignedINTEL) || |
| 207 |
Arg->hasDecorate(internal::DecorationRuntimeAlignedINTEL)) { |
| 208 |
RuntimeAlignedFound = true; |
| 209 |
ValueVec.push_back(ForeachFnArg(Arg)); |
| 210 |
} else { |
| 211 |
ValueVec.push_back(DefaultNode); |
| 212 |
} |
| 213 |
}); |
| 214 |
if (RuntimeAlignedFound) |
| 215 |
Fn->setMetadata("kernel_arg_runtime_aligned", |
| 216 |
MDNode::get(*Context, ValueVec)); |
| 217 |
} |
| 218 |
|
| 219 |
Value *SPIRVToLLVM::getTranslatedValue(SPIRVValue *BV) { |
| 220 |
auto Loc = ValueMap.find(BV); |
| 221 |
if (Loc != ValueMap.end()) |
| 222 |
return Loc->second; |
| 223 |
return nullptr; |
| 224 |
} |
| 225 |
|
| 226 |
static std::optional<llvm::Attribute> |
| 227 |
translateSEVMetadata(SPIRVValue *BV, llvm::LLVMContext &Context) { |
| 228 |
std::optional<llvm::Attribute> RetAttr; |
| 229 |
|
| 230 |
if (!BV->hasDecorate(DecorationSingleElementVectorINTEL)) |
| 231 |
return RetAttr; |
| 232 |
|
| 233 |
auto VecDecorateSEV = BV->getDecorations(DecorationSingleElementVectorINTEL); |
| 234 |
assert(VecDecorateSEV.size() == 1 && |
| 235 |
"Entry must have no more than one SingleElementVectorINTEL " |
| 236 |
"decoration"); |
| 237 |
auto *DecorateSEV = VecDecorateSEV.back(); |
| 238 |
auto LiteralCount = DecorateSEV->getLiteralCount(); |
| 239 |
assert(LiteralCount <= 1 && "SingleElementVectorINTEL decoration must " |
| 240 |
"have no more than one literal"); |
| 241 |
|
| 242 |
SPIRVWord IndirectLevelsOnElement = |
| 243 |
(LiteralCount == 1) ? DecorateSEV->getLiteral(0) : 0; |
| 244 |
|
| 245 |
RetAttr = Attribute::get(Context, kVCMetadata::VCSingleElementVector, |
| 246 |
std::to_string(IndirectLevelsOnElement)); |
| 247 |
return RetAttr; |
| 248 |
} |
| 249 |
|
| 250 |
IntrinsicInst *SPIRVToLLVM::getLifetimeStartIntrinsic(Instruction *I) { |
| 251 |
auto II = dyn_cast<IntrinsicInst>(I); |
| 252 |
if (II && II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 253 |
return II; |
| 254 |
// Bitcast might be inserted during translation of OpLifetimeStart |
| 255 |
auto BC = dyn_cast<BitCastInst>(I); |
| 256 |
if (BC) { |
| 257 |
for (const auto &U : BC->users()) { |
| 258 |
II = dyn_cast<IntrinsicInst>(U); |
| 259 |
if (II && II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 260 |
return II; |
| 261 |
; |
| 262 |
} |
| 263 |
} |
| 264 |
return nullptr; |
| 265 |
} |
| 266 |
|
| 267 |
SPIRVErrorLog &SPIRVToLLVM::getErrorLog() { return BM->getErrorLog(); } |
| 268 |
|
| 269 |
void SPIRVToLLVM::setCallingConv(CallInst *Call) { |
| 270 |
Function *F = Call->getCalledFunction(); |
| 271 |
assert(F && "Function pointers are not allowed in SPIRV"); |
| 272 |
Call->setCallingConv(F->getCallingConv()); |
| 273 |
} |
| 274 |
|
| 275 |
// For integer types shorter than 32 bit, unsigned/signedness can be inferred |
| 276 |
// from zext/sext attribute. |
| 277 |
MDString *SPIRVToLLVM::transOCLKernelArgTypeName(SPIRVFunctionParameter *Arg) { |
| 278 |
auto Ty = |
| 279 |
Arg->isByVal() ? Arg->getType()->getPointerElementType() : Arg->getType(); |
| 280 |
return MDString::get(*Context, transTypeToOCLTypeName(Ty, !Arg->isZext())); |
| 281 |
} |
| 282 |
|
| 283 |
Value *SPIRVToLLVM::mapFunction(SPIRVFunction *BF, Function *F) { |
| 284 |
SPIRVDBG(spvdbgs() << "[mapFunction] " << *BF << " -> "; |
| 285 |
dbgs() << *F << '\n';) |
| 286 |
FuncMap[BF] = F; |
| 287 |
return F; |
| 288 |
} |
| 289 |
|
| 290 |
std::optional<uint64_t> SPIRVToLLVM::transIdAsConstant(SPIRVId Id) { |
| 291 |
auto *V = BM->get<SPIRVValue>(Id); |
| 292 |
const auto *ConstValue = |
| 293 |
dyn_cast<ConstantInt>(transValue(V, nullptr, nullptr)); |
| 294 |
if (!ConstValue) |
| 295 |
return {}; |
| 296 |
return ConstValue->getZExtValue(); |
| 297 |
} |
| 298 |
|
| 299 |
std::optional<uint64_t> SPIRVToLLVM::getAlignment(SPIRVValue *V) { |
| 300 |
SPIRVWord AlignmentBytes = 0; |
| 301 |
if (V->hasAlignment(&AlignmentBytes)) { |
| 302 |
return AlignmentBytes; |
| 303 |
} |
| 304 |
|
| 305 |
// If there was no Alignment decoration, look for AlignmentId instead. |
| 306 |
SPIRVId AlignId; |
| 307 |
if (V->hasDecorateId(DecorationAlignmentId, 0, &AlignId)) { |
| 308 |
return transIdAsConstant(AlignId); |
| 309 |
} |
| 310 |
return {}; |
| 311 |
} |
| 312 |
|
| 313 |
Type *SPIRVToLLVM::transFPType(SPIRVType *T) { |
| 314 |
switch (T->getFloatBitWidth()) { |
| 315 |
case 4: |
| 316 |
// No LLVM IR counter part for FP4 - map it on i4. |
| 317 |
return Type::getIntNTy(*Context, 4); |
| 318 |
case 8: |
| 319 |
// No LLVM IR counter part for FP8 - map it on i8. |
| 320 |
return Type::getIntNTy(*Context, 8); |
| 321 |
case 16: |
| 322 |
if (T->isTypeFloat(16, FPEncodingBFloat16KHR)) |
| 323 |
return Type::getBFloatTy(*Context); |
| 324 |
return Type::getHalfTy(*Context); |
| 325 |
case 32: |
| 326 |
return Type::getFloatTy(*Context); |
| 327 |
case 64: |
| 328 |
return Type::getDoubleTy(*Context); |
| 329 |
default: |
| 330 |
llvm_unreachable("Invalid type"); |
| 331 |
return nullptr; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
std::string SPIRVToLLVM::transVCTypeName(SPIRVTypeBufferSurfaceINTEL *PST) { |
| 336 |
if (PST->hasAccessQualifier()) |
| 337 |
return VectorComputeUtil::getVCBufferSurfaceName(PST->getAccessQualifier()); |
| 338 |
return VectorComputeUtil::getVCBufferSurfaceName(); |
| 339 |
} |
| 340 |
|
| 341 |
template <typename ImageType> |
| 342 |
std::optional<SPIRVAccessQualifierKind> getAccessQualifier(ImageType *T) { |
| 343 |
if (!T->hasAccessQualifier()) |
| 344 |
return {}; |
| 345 |
return T->getAccessQualifier(); |
| 346 |
} |
| 347 |
|
| 348 |
Type *SPIRVToLLVM::transType(SPIRVType *T, bool UseTPT) { |
| 349 |
// Try to reuse a known type if it's already matched. However, if we want to |
| 350 |
// produce a TypedPointerType in lieu of a PointerType, we *do not* want to |
| 351 |
// pull a PointerType out of the type map, nor do we want to store a |
| 352 |
// TypedPointerType in there. This is generally safe to do, as types are |
| 353 |
// usually uniqued by LLVM, but we need to be cautious around struct types. |
| 354 |
auto Loc = TypeMap.find(T); |
| 355 |
if (Loc != TypeMap.end() && !UseTPT) |
| 356 |
return Loc->second; |
| 357 |
|
| 358 |
SPIRVDBG(spvdbgs() << "[transType] " << *T << " -> ";) |
| 359 |
T->validate(); |
| 360 |
switch (static_cast<SPIRVWord>(T->getOpCode())) { |
| 361 |
case OpTypeVoid: |
| 362 |
return mapType(T, Type::getVoidTy(*Context)); |
| 363 |
case OpTypeBool: |
| 364 |
return mapType(T, Type::getInt1Ty(*Context)); |
| 365 |
case OpTypeInt: |
| 366 |
return mapType(T, Type::getIntNTy(*Context, T->getIntegerBitWidth())); |
| 367 |
case OpTypeFloat: |
| 368 |
return mapType(T, transFPType(T)); |
| 369 |
case OpTypeArray: { |
| 370 |
// The length might be an OpSpecConstantOp, that needs to be specialized |
| 371 |
// and evaluated before the LLVM ArrayType can be constructed. |
| 372 |
auto *LenExpr = static_cast<const SPIRVTypeArray *>(T)->getLength(); |
| 373 |
auto *LenValue = cast<ConstantInt>(transValue(LenExpr, nullptr, nullptr)); |
| 374 |
return mapType(T, ArrayType::get(transType(T->getArrayElementType()), |
| 375 |
LenValue->getZExtValue())); |
| 376 |
} |
| 377 |
case internal::OpTypeTokenINTEL: |
| 378 |
return mapType(T, Type::getTokenTy(*Context)); |
| 379 |
case OpTypePointer: { |
| 380 |
unsigned AS = SPIRSPIRVAddrSpaceMap::rmap(T->getPointerStorageClass()); |
| 381 |
if (AS == SPIRAS_CodeSectionINTEL && !BM->shouldEmitFunctionPtrAddrSpace()) |
| 382 |
AS = SPIRAS_Private; |
| 383 |
if (BM->shouldEmitFunctionPtrAddrSpace() && |
| 384 |
T->getPointerElementType()->getOpCode() == OpTypeFunction) |
| 385 |
AS = SPIRAS_CodeSectionINTEL; |
| 386 |
Type *ElementTy = transType(T->getPointerElementType(), UseTPT); |
| 387 |
if (UseTPT) |
| 388 |
return TypedPointerType::get(ElementTy, AS); |
| 389 |
return mapType(T, PointerType::get(ElementTy, AS)); |
| 390 |
} |
| 391 |
case OpTypeVector: |
| 392 |
return mapType(T, |
| 393 |
FixedVectorType::get(transType(T->getVectorComponentType()), |
| 394 |
T->getVectorComponentCount())); |
| 395 |
case OpTypeMatrix: |
| 396 |
return mapType(T, ArrayType::get(transType(T->getMatrixColumnType()), |
| 397 |
T->getMatrixColumnCount())); |
| 398 |
case OpTypeOpaque: |
| 399 |
return mapType(T, StructType::create(*Context, T->getName())); |
| 400 |
case OpTypeFunction: { |
| 401 |
auto FT = static_cast<SPIRVTypeFunction *>(T); |
| 402 |
auto RT = transType(FT->getReturnType()); |
| 403 |
std::vector<Type *> PT; |
| 404 |
for (size_t I = 0, E = FT->getNumParameters(); I != E; ++I) |
| 405 |
PT.push_back(transType(FT->getParameterType(I))); |
| 406 |
return mapType(T, FunctionType::get(RT, PT, false)); |
| 407 |
} |
| 408 |
case OpTypeImage: { |
| 409 |
auto ST = static_cast<SPIRVTypeImage *>(T); |
| 410 |
if (ST->isOCLImage()) |
| 411 |
return mapType(T, |
| 412 |
getSPIRVType(OpTypeImage, transType(ST->getSampledType()), |
| 413 |
ST->getDescriptor(), getAccessQualifier(ST), |
| 414 |
!UseTPT)); |
| 415 |
else |
| 416 |
llvm_unreachable("Unsupported image type"); |
| 417 |
return nullptr; |
| 418 |
} |
| 419 |
case OpTypeSampledImage: { |
| 420 |
const auto *ST = static_cast<SPIRVTypeSampledImage *>(T)->getImageType(); |
| 421 |
return mapType( |
| 422 |
T, getSPIRVType(OpTypeSampledImage, transType(ST->getSampledType()), |
| 423 |
ST->getDescriptor(), getAccessQualifier(ST), !UseTPT)); |
| 424 |
} |
| 425 |
case OpTypeStruct: { |
| 426 |
// We do not generate structs with any TypedPointerType members. To ensure |
| 427 |
// that uniqueness of struct types is maintained, reuse an existing struct |
| 428 |
// type in the type map, even if UseTPT is true. |
| 429 |
if (Loc != TypeMap.end()) |
| 430 |
return Loc->second; |
| 431 |
auto ST = static_cast<SPIRVTypeStruct *>(T); |
| 432 |
auto Name = ST->getName(); |
| 433 |
if (!Name.empty()) { |
| 434 |
if (auto OldST = StructType::getTypeByName(*Context, Name)) |
| 435 |
OldST->setName(""); |
| 436 |
} else { |
| 437 |
Name = "structtype"; |
| 438 |
} |
| 439 |
auto *StructTy = StructType::create(*Context, Name); |
| 440 |
mapType(ST, StructTy); |
| 441 |
SmallVector<Type *, 4> MT; |
| 442 |
for (size_t I = 0, E = ST->getMemberCount(); I != E; ++I) |
| 443 |
MT.push_back(transType(ST->getMemberType(I))); |
| 444 |
for (auto &CI : ST->getContinuedInstructions()) |
| 445 |
for (size_t I = 0, E = CI->getNumElements(); I != E; ++I) |
| 446 |
MT.push_back(transType(CI->getMemberType(I))); |
| 447 |
StructTy->setBody(MT, ST->isPacked()); |
| 448 |
return StructTy; |
| 449 |
} |
| 450 |
case OpTypePipe: { |
| 451 |
auto PT = static_cast<SPIRVTypePipe *>(T); |
| 452 |
return mapType(T, |
| 453 |
getSPIRVType(OpTypePipe, PT->getAccessQualifier(), !UseTPT)); |
| 454 |
} |
| 455 |
case OpTypePipeStorage: { |
| 456 |
return mapType(T, getSPIRVType(OpTypePipeStorage, !UseTPT)); |
| 457 |
} |
| 458 |
case OpTypeVmeImageINTEL: { |
| 459 |
auto *VT = static_cast<SPIRVTypeVmeImageINTEL *>(T)->getImageType(); |
| 460 |
return mapType( |
| 461 |
T, getSPIRVType(OpTypeVmeImageINTEL, transType(VT->getSampledType()), |
| 462 |
VT->getDescriptor(), getAccessQualifier(VT), !UseTPT)); |
| 463 |
} |
| 464 |
case OpTypeBufferSurfaceINTEL: { |
| 465 |
auto *PST = static_cast<SPIRVTypeBufferSurfaceINTEL *>(T); |
| 466 |
Type *Ty = nullptr; |
| 467 |
if (UseTPT) { |
| 468 |
Type *StructTy = getOrCreateOpaqueStructType(M, transVCTypeName(PST)); |
| 469 |
Ty = TypedPointerType::get(StructTy, SPIRAS_Global); |
| 470 |
} else { |
| 471 |
std::vector<unsigned> Params; |
| 472 |
if (PST->hasAccessQualifier()) { |
| 473 |
unsigned Access = static_cast<unsigned>(PST->getAccessQualifier()); |
| 474 |
Params.push_back(Access); |
| 475 |
} |
| 476 |
Ty = TargetExtType::get(*Context, "spirv.BufferSurfaceINTEL", {}, Params); |
| 477 |
} |
| 478 |
return mapType(T, Ty); |
| 479 |
} |
| 480 |
case internal::OpTypeJointMatrixINTEL: { |
| 481 |
auto *MT = static_cast<SPIRVTypeJointMatrixINTEL *>(T); |
| 482 |
auto R = static_cast<SPIRVConstant *>(MT->getRows())->getZExtIntValue(); |
| 483 |
auto C = static_cast<SPIRVConstant *>(MT->getColumns())->getZExtIntValue(); |
| 484 |
auto L = static_cast<SPIRVConstant *>(MT->getLayout())->getZExtIntValue(); |
| 485 |
auto S = static_cast<SPIRVConstant *>(MT->getScope())->getZExtIntValue(); |
| 486 |
SmallVector<unsigned, 5> Params = {(unsigned)R, (unsigned)C, (unsigned)L, |
| 487 |
(unsigned)S}; |
| 488 |
if (auto *Use = MT->getUse()) |
| 489 |
Params.push_back(static_cast<SPIRVConstant *>(Use)->getZExtIntValue()); |
| 490 |
return mapType(T, getSPIRVType(internal::OpTypeJointMatrixINTEL, |
| 491 |
transTypeToOCLTypeName(MT->getCompType()), |
| 492 |
Params, !UseTPT)); |
| 493 |
} |
| 494 |
case OpTypeCooperativeMatrixKHR: { |
| 495 |
auto *MT = static_cast<SPIRVTypeCooperativeMatrixKHR *>(T); |
| 496 |
unsigned Scope = |
| 497 |
static_cast<SPIRVConstant *>(MT->getScope())->getZExtIntValue(); |
| 498 |
unsigned Rows = |
| 499 |
static_cast<SPIRVConstant *>(MT->getRows())->getZExtIntValue(); |
| 500 |
unsigned Cols = |
| 501 |
static_cast<SPIRVConstant *>(MT->getColumns())->getZExtIntValue(); |
| 502 |
unsigned Use = |
| 503 |
static_cast<SPIRVConstant *>(MT->getUse())->getZExtIntValue(); |
| 504 |
|
| 505 |
std::vector<unsigned> Params = {Scope, Rows, Cols, Use}; |
| 506 |
return mapType( |
| 507 |
T, llvm::TargetExtType::get(*Context, "spirv.CooperativeMatrixKHR", |
| 508 |
transType(MT->getCompType()), Params)); |
| 509 |
} |
| 510 |
case OpTypeForwardPointer: { |
| 511 |
SPIRVTypeForwardPointer *FP = |
| 512 |
static_cast<SPIRVTypeForwardPointer *>(static_cast<SPIRVEntry *>(T)); |
| 513 |
return mapType(T, transType(static_cast<SPIRVType *>( |
| 514 |
BM->getEntry(FP->getPointerId())))); |
| 515 |
} |
| 516 |
|
| 517 |
default: { |
| 518 |
auto OC = T->getOpCode(); |
| 519 |
if (isOpaqueGenericTypeOpCode(OC) || isSubgroupAvcINTELTypeOpCode(OC)) { |
| 520 |
return mapType(T, getSPIRVType(OC, !UseTPT)); |
| 521 |
} |
| 522 |
llvm_unreachable("Not implemented!"); |
| 523 |
} |
| 524 |
} |
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
std::string SPIRVToLLVM::transTypeToOCLTypeName(SPIRVType *T, bool IsSigned) { |
| 529 |
switch (T->getOpCode()) { |
| 530 |
case OpTypeVoid: |
| 531 |
return "void"; |
| 532 |
case OpTypeBool: |
| 533 |
return "bool"; |
| 534 |
case OpTypeInt: { |
| 535 |
std::string Prefix = IsSigned ? "" : "u"; |
| 536 |
switch (T->getIntegerBitWidth()) { |
| 537 |
case 8: |
| 538 |
return Prefix + "char"; |
| 539 |
case 16: |
| 540 |
return Prefix + "short"; |
| 541 |
case 32: |
| 542 |
return Prefix + "int"; |
| 543 |
case 64: |
| 544 |
return Prefix + "long"; |
| 545 |
default: |
| 546 |
// Arbitrary precision integer |
| 547 |
return Prefix + std::string("int") + T->getIntegerBitWidth() + "_t"; |
| 548 |
} |
| 549 |
} break; |
| 550 |
case OpTypeFloat: |
| 551 |
switch (T->getFloatBitWidth()) { |
| 552 |
case 16: |
| 553 |
return "half"; |
| 554 |
case 32: |
| 555 |
return "float"; |
| 556 |
case 64: |
| 557 |
return "double"; |
| 558 |
default: |
| 559 |
llvm_unreachable("invalid floating pointer bitwidth"); |
| 560 |
return std::string("float") + T->getFloatBitWidth() + "_t"; |
| 561 |
} |
| 562 |
break; |
| 563 |
case OpTypeArray: |
| 564 |
return "array"; |
| 565 |
case OpTypePointer: { |
| 566 |
SPIRVType *ET = T->getPointerElementType(); |
| 567 |
if (isa<OpTypeFunction>(ET)) { |
| 568 |
SPIRVTypeFunction *TF = static_cast<SPIRVTypeFunction *>(ET); |
| 569 |
std::string name = transTypeToOCLTypeName(TF->getReturnType()); |
| 570 |
name += " (*)("; |
| 571 |
for (unsigned I = 0, E = TF->getNumParameters(); I < E; ++I) |
| 572 |
name += transTypeToOCLTypeName(TF->getParameterType(I)) + ','; |
| 573 |
name.back() = ')'; // replace the last comma with a closing brace. |
| 574 |
return name; |
| 575 |
} |
| 576 |
return transTypeToOCLTypeName(ET) + "*"; |
| 577 |
} |
| 578 |
case OpTypeVector: |
| 579 |
return transTypeToOCLTypeName(T->getVectorComponentType()) + |
| 580 |
T->getVectorComponentCount(); |
| 581 |
case OpTypeMatrix: |
| 582 |
return transTypeToOCLTypeName(T->getMatrixColumnType()) + |
| 583 |
T->getMatrixColumnCount(); |
| 584 |
case OpTypeOpaque: |
| 585 |
return T->getName(); |
| 586 |
case OpTypeFunction: |
| 587 |
llvm_unreachable("Unsupported"); |
| 588 |
return "function"; |
| 589 |
case OpTypeStruct: { |
| 590 |
auto Name = T->getName(); |
| 591 |
if (Name.find("struct.") == 0) |
| 592 |
Name[6] = ' '; |
| 593 |
else if (Name.find("union.") == 0) |
| 594 |
Name[5] = ' '; |
| 595 |
return Name; |
| 596 |
} |
| 597 |
case OpTypePipe: |
| 598 |
return "pipe"; |
| 599 |
case OpTypeSampler: |
| 600 |
return "sampler_t"; |
| 601 |
case OpTypeImage: { |
| 602 |
std::string Name; |
| 603 |
Name = rmap<std::string>(static_cast<SPIRVTypeImage *>(T)->getDescriptor()); |
| 604 |
return Name; |
| 605 |
} |
| 606 |
default: |
| 607 |
if (isOpaqueGenericTypeOpCode(T->getOpCode())) { |
| 608 |
return OCLOpaqueTypeOpCodeMap::rmap(T->getOpCode()); |
| 609 |
} |
| 610 |
llvm_unreachable("Not implemented"); |
| 611 |
return "unknown"; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
std::vector<Type *> |
| 616 |
SPIRVToLLVM::transTypeVector(const std::vector<SPIRVType *> &BT, bool UseTPT) { |
| 617 |
std::vector<Type *> T; |
| 618 |
for (auto I : BT) |
| 619 |
T.push_back(transType(I, UseTPT)); |
| 620 |
return T; |
| 621 |
} |
| 622 |
|
| 623 |
static Type *opaquifyType(Type *Ty) { |
| 624 |
if (auto *TPT = dyn_cast<TypedPointerType>(Ty)) { |
| 625 |
Ty = PointerType::get(opaquifyType(TPT->getElementType()), |
| 626 |
TPT->getAddressSpace()); |
| 627 |
} |
| 628 |
return Ty; |
| 629 |
} |
| 630 |
|
| 631 |
static void opaquifyTypedPointers(MutableArrayRef<Type *> Types) { |
| 632 |
for (Type *&Ty : Types) { |
| 633 |
Ty = opaquifyType(Ty); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
std::vector<Value *> |
| 638 |
SPIRVToLLVM::transValue(const std::vector<SPIRVValue *> &BV, Function *F, |
| 639 |
BasicBlock *BB) { |
| 640 |
std::vector<Value *> V; |
| 641 |
for (auto I : BV) |
| 642 |
V.push_back(transValue(I, F, BB)); |
| 643 |
return V; |
| 644 |
} |
| 645 |
|
| 646 |
void SPIRVToLLVM::setName(llvm::Value *V, SPIRVValue *BV) { |
| 647 |
auto Name = BV->getName(); |
| 648 |
if (!Name.empty() && (!V->hasName() || Name != V->getName())) |
| 649 |
V->setName(Name); |
| 650 |
} |
| 651 |
|
| 652 |
inline llvm::Metadata *SPIRVToLLVM::getMetadataFromName(std::string Name) { |
| 653 |
return llvm::MDNode::get(*Context, llvm::MDString::get(*Context, Name)); |
| 654 |
} |
| 655 |
|
| 656 |
inline std::vector<llvm::Metadata *> |
| 657 |
SPIRVToLLVM::getMetadataFromNameAndParameter(std::string Name, |
| 658 |
SPIRVWord Parameter) { |
| 659 |
return {MDString::get(*Context, Name), |
| 660 |
ConstantAsMetadata::get( |
| 661 |
ConstantInt::get(Type::getInt32Ty(*Context), Parameter))}; |
| 662 |
} |
| 663 |
|
| 664 |
inline llvm::MDNode * |
| 665 |
SPIRVToLLVM::getMetadataFromNameAndParameter(std::string Name, |
| 666 |
int64_t Parameter) { |
| 667 |
std::vector<llvm::Metadata *> Metadata = { |
| 668 |
MDString::get(*Context, Name), |
| 669 |
ConstantAsMetadata::get( |
| 670 |
ConstantInt::get(Type::getInt64Ty(*Context), Parameter))}; |
| 671 |
return llvm::MDNode::get(*Context, Metadata); |
| 672 |
} |
| 673 |
|
| 674 |
template <typename LoopInstType> |
| 675 |
void SPIRVToLLVM::setLLVMLoopMetadata(const LoopInstType *LM, |
| 676 |
const Loop *LoopObj) { |
| 677 |
if (!LM) |
| 678 |
return; |
| 679 |
|
| 680 |
auto Temp = MDNode::getTemporary(*Context, std::nullopt); |
| 681 |
auto Self = MDNode::get(*Context, Temp.get()); |
| 682 |
Self->replaceOperandWith(0, Self); |
| 683 |
SPIRVWord LC = LM->getLoopControl(); |
| 684 |
if (LC == LoopControlMaskNone) { |
| 685 |
LoopObj->setLoopID(Self); |
| 686 |
return; |
| 687 |
} |
| 688 |
|
| 689 |
unsigned NumParam = 0; |
| 690 |
std::vector<llvm::Metadata *> Metadata; |
| 691 |
std::vector<SPIRVWord> LoopControlParameters = LM->getLoopControlParameters(); |
| 692 |
Metadata.push_back(llvm::MDNode::get(*Context, Self)); |
| 693 |
|
| 694 |
// To correctly decode loop control parameters, order of checks for loop |
| 695 |
// control masks must match with the order given in the spec (see 3.23), |
| 696 |
// i.e. check smaller-numbered bits first. |
| 697 |
// Unroll and UnrollCount loop controls can't be applied simultaneously with |
| 698 |
// DontUnroll loop control. |
| 699 |
if (LC & LoopControlUnrollMask && !(LC & LoopControlPartialCountMask)) |
| 700 |
Metadata.push_back(getMetadataFromName("llvm.loop.unroll.enable")); |
| 701 |
else if (LC & LoopControlDontUnrollMask) |
| 702 |
Metadata.push_back(getMetadataFromName("llvm.loop.unroll.disable")); |
| 703 |
if (LC & LoopControlDependencyInfiniteMask) |
| 704 |
Metadata.push_back(getMetadataFromName("llvm.loop.ivdep.enable")); |
| 705 |
if (LC & LoopControlDependencyLengthMask) { |
| 706 |
Metadata.push_back(llvm::MDNode::get( |
| 707 |
*Context, |
| 708 |
getMetadataFromNameAndParameter("llvm.loop.ivdep.safelen", |
| 709 |
LoopControlParameters[NumParam]))); |
| 710 |
++NumParam; |
| 711 |
// TODO: Fix the increment/assertion logic in all of the conditions |
| 712 |
assert(NumParam <= LoopControlParameters.size() && |
| 713 |
"Missing loop control parameter!"); |
| 714 |
} |
| 715 |
// Placeholder for LoopControls added in SPIR-V 1.4 spec (see 3.23) |
| 716 |
if (LC & LoopControlMinIterationsMask) { |
| 717 |
++NumParam; |
| 718 |
assert(NumParam <= LoopControlParameters.size() && |
| 719 |
"Missing loop control parameter!"); |
| 720 |
} |
| 721 |
if (LC & LoopControlMaxIterationsMask) { |
| 722 |
++NumParam; |
| 723 |
assert(NumParam <= LoopControlParameters.size() && |
| 724 |
"Missing loop control parameter!"); |
| 725 |
} |
| 726 |
if (LC & LoopControlIterationMultipleMask) { |
| 727 |
++NumParam; |
| 728 |
assert(NumParam <= LoopControlParameters.size() && |
| 729 |
"Missing loop control parameter!"); |
| 730 |
} |
| 731 |
if (LC & LoopControlPeelCountMask) { |
| 732 |
++NumParam; |
| 733 |
assert(NumParam <= LoopControlParameters.size() && |
| 734 |
"Missing loop control parameter!"); |
| 735 |
} |
| 736 |
if (LC & LoopControlPartialCountMask && !(LC & LoopControlDontUnrollMask)) { |
| 737 |
// If unroll factor is set as '1' and Unroll mask is applied attempt to do |
| 738 |
// full unrolling and disable it if the trip count is not known at compile |
| 739 |
// time. |
| 740 |
if (1 == LoopControlParameters[NumParam] && (LC & LoopControlUnrollMask)) |
| 741 |
Metadata.push_back(getMetadataFromName("llvm.loop.unroll.full")); |
| 742 |
else |
| 743 |
Metadata.push_back(llvm::MDNode::get( |
| 744 |
*Context, |
| 745 |
getMetadataFromNameAndParameter("llvm.loop.unroll.count", |
| 746 |
LoopControlParameters[NumParam]))); |
| 747 |
++NumParam; |
| 748 |
assert(NumParam <= LoopControlParameters.size() && |
| 749 |
"Missing loop control parameter!"); |
| 750 |
} |
| 751 |
if (LC & LoopControlInitiationIntervalINTELMask) { |
| 752 |
Metadata.push_back(llvm::MDNode::get( |
| 753 |
*Context, getMetadataFromNameAndParameter( |
| 754 |
"llvm.loop.ii.count", LoopControlParameters[NumParam]))); |
| 755 |
++NumParam; |
| 756 |
assert(NumParam <= LoopControlParameters.size() && |
| 757 |
"Missing loop control parameter!"); |
| 758 |
} |
| 759 |
if (LC & LoopControlMaxConcurrencyINTELMask) { |
| 760 |
Metadata.push_back(llvm::MDNode::get( |
| 761 |
*Context, |
| 762 |
getMetadataFromNameAndParameter("llvm.loop.max_concurrency.count", |
| 763 |
LoopControlParameters[NumParam]))); |
| 764 |
++NumParam; |
| 765 |
assert(NumParam <= LoopControlParameters.size() && |
| 766 |
"Missing loop control parameter!"); |
| 767 |
} |
| 768 |
if (LC & LoopControlDependencyArrayINTELMask) { |
| 769 |
// Collect pointer variable <-> safelen information |
| 770 |
std::map<Value *, unsigned> PointerSflnMap; |
| 771 |
unsigned NumOperandPairs = LoopControlParameters[NumParam]; |
| 772 |
unsigned OperandsEndIndex = NumParam + NumOperandPairs * 2; |
| 773 |
assert(OperandsEndIndex <= LoopControlParameters.size() && |
| 774 |
"Missing loop control parameter!"); |
| 775 |
SPIRVModule *M = LM->getModule(); |
| 776 |
while (NumParam < OperandsEndIndex) { |
| 777 |
SPIRVId ArraySPIRVId = LoopControlParameters[++NumParam]; |
| 778 |
Value *PointerVar = ValueMap[M->getValue(ArraySPIRVId)]; |
| 779 |
unsigned Safelen = LoopControlParameters[++NumParam]; |
| 780 |
PointerSflnMap.emplace(PointerVar, Safelen); |
| 781 |
} |
| 782 |
|
| 783 |
// A single run over the loop to retrieve all GetElementPtr instructions |
| 784 |
// that access relevant array variables |
| 785 |
std::map<Value *, std::vector<GetElementPtrInst *>> ArrayGEPMap; |
| 786 |
for (const auto &BB : LoopObj->blocks()) { |
| 787 |
for (Instruction &I : *BB) { |
| 788 |
auto *GEP = dyn_cast<GetElementPtrInst>(&I); |
| 789 |
if (!GEP) |
| 790 |
continue; |
| 791 |
|
| 792 |
Value *AccessedPointer = GEP->getPointerOperand(); |
| 793 |
if (auto *LI = dyn_cast<LoadInst>(AccessedPointer)) |
| 794 |
AccessedPointer = LI->getPointerOperand(); |
| 795 |
auto PointerSflnIt = PointerSflnMap.find(AccessedPointer); |
| 796 |
if (PointerSflnIt != PointerSflnMap.end()) { |
| 797 |
ArrayGEPMap[AccessedPointer].push_back(GEP); |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
// Create index group metadata nodes - one per each of the array |
| 803 |
// variables. Mark each GEP accessing a particular array variable |
| 804 |
// into a corresponding index group |
| 805 |
std::map<unsigned, SmallSet<MDNode *, 4>> SafelenIdxGroupMap; |
| 806 |
// Whenever a kernel closure field access is pointed to instead of |
| 807 |
// an array/pointer variable, ensure that all GEPs to that memory |
| 808 |
// share the same index group by hashing the newly added index groups. |
| 809 |
// "Memory offset info" represents a handle to the whole closure block |
| 810 |
// + an integer offset to a particular captured parameter. |
| 811 |
using MemoryOffsetInfo = std::pair<Value *, unsigned>; |
| 812 |
std::map<MemoryOffsetInfo, MDNode *> OffsetIdxGroupMap; |
| 813 |
|
| 814 |
for (auto &ArrayGEPIt : ArrayGEPMap) { |
| 815 |
MDNode *CurrentDepthIdxGroup = nullptr; |
| 816 |
if (auto *PrecedingGEP = dyn_cast<GetElementPtrInst>(ArrayGEPIt.first)) { |
| 817 |
Value *ClosureFieldPointer = PrecedingGEP->getPointerOperand(); |
| 818 |
unsigned Offset = |
| 819 |
cast<ConstantInt>(PrecedingGEP->getOperand(2))->getZExtValue(); |
| 820 |
MemoryOffsetInfo Info{ClosureFieldPointer, Offset}; |
| 821 |
auto OffsetIdxGroupIt = OffsetIdxGroupMap.find(Info); |
| 822 |
if (OffsetIdxGroupIt == OffsetIdxGroupMap.end()) { |
| 823 |
// This is the first GEP encountered for this closure field. |
| 824 |
// Emit a distinct index group that will be referenced from |
| 825 |
// llvm.loop.parallel_access_indices metadata; hash the new |
| 826 |
// MDNode for future accesses to the same memory. |
| 827 |
CurrentDepthIdxGroup = |
| 828 |
llvm::MDNode::getDistinct(*Context, std::nullopt); |
| 829 |
OffsetIdxGroupMap.emplace(Info, CurrentDepthIdxGroup); |
| 830 |
} else { |
| 831 |
// Previous accesses to that field have already been indexed, |
| 832 |
// just use the already-existing metadata. |
| 833 |
CurrentDepthIdxGroup = OffsetIdxGroupIt->second; |
| 834 |
} |
| 835 |
} else /* Regular kernel-scope array/pointer variable */ { |
| 836 |
// Emit a distinct index group that will be referenced from |
| 837 |
// llvm.loop.parallel_access_indices metadata |
| 838 |
CurrentDepthIdxGroup = |
| 839 |
llvm::MDNode::getDistinct(*Context, std::nullopt); |
| 840 |
} |
| 841 |
|
| 842 |
unsigned Safelen = PointerSflnMap.find(ArrayGEPIt.first)->second; |
| 843 |
SafelenIdxGroupMap[Safelen].insert(CurrentDepthIdxGroup); |
| 844 |
for (auto *GEP : ArrayGEPIt.second) { |
| 845 |
StringRef IdxGroupMDName("llvm.index.group"); |
| 846 |
llvm::MDNode *PreviousIdxGroup = GEP->getMetadata(IdxGroupMDName); |
| 847 |
if (!PreviousIdxGroup) { |
| 848 |
GEP->setMetadata(IdxGroupMDName, CurrentDepthIdxGroup); |
| 849 |
continue; |
| 850 |
} |
| 851 |
|
| 852 |
// If we're dealing with an embedded loop, it may be the case |
| 853 |
// that GEP instructions for some of the arrays were already |
| 854 |
// marked by the algorithm when it went over the outer level loops. |
| 855 |
// In order to retain the IVDep information for each "loop |
| 856 |
// dimension", we will mark such GEP's into a separate joined node |
| 857 |
// that will refer to the previous levels' index groups AND to the |
| 858 |
// index group specific to the current loop. |
| 859 |
std::vector<llvm::Metadata *> CurrentDepthOperands( |
| 860 |
PreviousIdxGroup->op_begin(), PreviousIdxGroup->op_end()); |
| 861 |
if (CurrentDepthOperands.empty()) |
| 862 |
CurrentDepthOperands.push_back(PreviousIdxGroup); |
| 863 |
CurrentDepthOperands.push_back(CurrentDepthIdxGroup); |
| 864 |
auto *JointIdxGroup = llvm::MDNode::get(*Context, CurrentDepthOperands); |
| 865 |
GEP->setMetadata(IdxGroupMDName, JointIdxGroup); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
for (auto &SflnIdxGroupIt : SafelenIdxGroupMap) { |
| 870 |
auto *Name = MDString::get(*Context, "llvm.loop.parallel_access_indices"); |
| 871 |
unsigned SflnValue = SflnIdxGroupIt.first; |
| 872 |
llvm::Metadata *SafelenMDOp = |
| 873 |
SflnValue ? ConstantAsMetadata::get(ConstantInt::get( |
| 874 |
Type::getInt32Ty(*Context), SflnValue)) |
| 875 |
: nullptr; |
| 876 |
std::vector<llvm::Metadata *> Parameters{Name}; |
| 877 |
for (auto *Node : SflnIdxGroupIt.second) |
| 878 |
Parameters.push_back(Node); |
| 879 |
if (SafelenMDOp) |
| 880 |
Parameters.push_back(SafelenMDOp); |
| 881 |
Metadata.push_back(llvm::MDNode::get(*Context, Parameters)); |
| 882 |
} |
| 883 |
++NumParam; |
| 884 |
} |
| 885 |
if (LC & LoopControlPipelineEnableINTELMask) { |
| 886 |
Metadata.push_back(llvm::MDNode::get( |
| 887 |
*Context, |
| 888 |
getMetadataFromNameAndParameter("llvm.loop.intel.pipelining.enable", |
| 889 |
LoopControlParameters[NumParam++]))); |
| 890 |
assert(NumParam <= LoopControlParameters.size() && |
| 891 |
"Missing loop control parameter!"); |
| 892 |
} |
| 893 |
if (LC & LoopControlLoopCoalesceINTELMask) { |
| 894 |
// If LoopCoalesce has a parameter of '0' |
| 895 |
if (!LoopControlParameters[NumParam]) { |
| 896 |
Metadata.push_back(llvm::MDNode::get( |
| 897 |
*Context, getMetadataFromName("llvm.loop.coalesce.enable"))); |
| 898 |
} else { |
| 899 |
Metadata.push_back(llvm::MDNode::get( |
| 900 |
*Context, |
| 901 |
getMetadataFromNameAndParameter("llvm.loop.coalesce.count", |
| 902 |
LoopControlParameters[NumParam]))); |
| 903 |
} |
| 904 |
++NumParam; |
| 905 |
assert(NumParam <= LoopControlParameters.size() && |
| 906 |
"Missing loop control parameter!"); |
| 907 |
} |
| 908 |
if (LC & LoopControlMaxInterleavingINTELMask) { |
| 909 |
Metadata.push_back(llvm::MDNode::get( |
| 910 |
*Context, |
| 911 |
getMetadataFromNameAndParameter("llvm.loop.max_interleaving.count", |
| 912 |
LoopControlParameters[NumParam++]))); |
| 913 |
assert(NumParam <= LoopControlParameters.size() && |
| 914 |
"Missing loop control parameter!"); |
| 915 |
} |
| 916 |
if (LC & LoopControlSpeculatedIterationsINTELMask) { |
| 917 |
Metadata.push_back(llvm::MDNode::get( |
| 918 |
*Context, getMetadataFromNameAndParameter( |
| 919 |
"llvm.loop.intel.speculated.iterations.count", |
| 920 |
LoopControlParameters[NumParam++]))); |
| 921 |
assert(NumParam <= LoopControlParameters.size() && |
| 922 |
"Missing loop control parameter!"); |
| 923 |
} |
| 924 |
if (LC & LoopControlNoFusionINTELMask) |
| 925 |
Metadata.push_back(getMetadataFromName("llvm.loop.fusion.disable")); |
| 926 |
if (LC & spv::LoopControlLoopCountINTELMask) { |
| 927 |
// LoopCountINTELMask parameters are int64 and each parameter is stored |
| 928 |
// as 2 SPIRVWords (int32) |
| 929 |
assert(NumParam + 6 <= LoopControlParameters.size() && |
| 930 |
"Missing loop control parameter!"); |
| 931 |
|
| 932 |
uint64_t LoopCountMin = |
| 933 |
static_cast<uint64_t>(LoopControlParameters[NumParam++]); |
| 934 |
LoopCountMin |= static_cast<uint64_t>(LoopControlParameters[NumParam++]) |
| 935 |
<< 32; |
| 936 |
if (static_cast<int64_t>(LoopCountMin) >= 0) { |
| 937 |
Metadata.push_back(getMetadataFromNameAndParameter( |
| 938 |
"llvm.loop.intel.loopcount_min", static_cast<int64_t>(LoopCountMin))); |
| 939 |
} |
| 940 |
|
| 941 |
uint64_t LoopCountMax = |
| 942 |
static_cast<uint64_t>(LoopControlParameters[NumParam++]); |
| 943 |
LoopCountMax |= static_cast<uint64_t>(LoopControlParameters[NumParam++]) |
| 944 |
<< 32; |
| 945 |
if (static_cast<int64_t>(LoopCountMax) >= 0) { |
| 946 |
Metadata.push_back(getMetadataFromNameAndParameter( |
| 947 |
"llvm.loop.intel.loopcount_max", static_cast<int64_t>(LoopCountMax))); |
| 948 |
} |
| 949 |
|
| 950 |
uint64_t LoopCountAvg = |
| 951 |
static_cast<uint64_t>(LoopControlParameters[NumParam++]); |
| 952 |
LoopCountAvg |= static_cast<uint64_t>(LoopControlParameters[NumParam++]) |
| 953 |
<< 32; |
| 954 |
if (static_cast<int64_t>(LoopCountAvg) >= 0) { |
| 955 |
Metadata.push_back(getMetadataFromNameAndParameter( |
| 956 |
"llvm.loop.intel.loopcount_avg", static_cast<int64_t>(LoopCountAvg))); |
| 957 |
} |
| 958 |
} |
| 959 |
if (LC & spv::LoopControlMaxReinvocationDelayINTELMask) { |
| 960 |
Metadata.push_back(llvm::MDNode::get( |
| 961 |
*Context, getMetadataFromNameAndParameter( |
| 962 |
"llvm.loop.intel.max_reinvocation_delay.count", |
| 963 |
LoopControlParameters[NumParam++]))); |
| 964 |
assert(NumParam <= LoopControlParameters.size() && |
| 965 |
"Missing loop control parameter!"); |
| 966 |
} |
| 967 |
llvm::MDNode *Node = llvm::MDNode::get(*Context, Metadata); |
| 968 |
|
| 969 |
// Set the first operand to refer itself |
| 970 |
Node->replaceOperandWith(0, Node); |
| 971 |
LoopObj->setLoopID(Node); |
| 972 |
} |
| 973 |
|
| 974 |
void SPIRVToLLVM::transLLVMLoopMetadata(const Function *F) { |
| 975 |
assert(F); |
| 976 |
|
| 977 |
if (FuncLoopMetadataMap.empty()) |
| 978 |
return; |
| 979 |
|
| 980 |
// Function declaration doesn't contain loop metadata. |
| 981 |
if (F->isDeclaration()) |
| 982 |
return; |
| 983 |
|
| 984 |
DominatorTree DomTree(*(const_cast<Function *>(F))); |
| 985 |
LoopInfo LI(DomTree); |
| 986 |
|
| 987 |
// In SPIRV loop metadata is linked to a header basic block of a loop |
| 988 |
// whilst in LLVM IR it is linked to a latch basic block (the one |
| 989 |
// whose back edge goes to a header basic block) of the loop. |
| 990 |
// To ensure consistent behaviour, we can rely on the `llvm::Loop` |
| 991 |
// class to handle the metadata placement |
| 992 |
for (const auto *LoopObj : LI.getLoopsInPreorder()) { |
| 993 |
// Check that loop header BB contains loop metadata. |
| 994 |
const auto LMDItr = FuncLoopMetadataMap.find(LoopObj->getHeader()); |
| 995 |
if (LMDItr == FuncLoopMetadataMap.end()) |
| 996 |
continue; |
| 997 |
|
| 998 |
const auto *LMD = LMDItr->second; |
| 999 |
if (LMD->getOpCode() == OpLoopMerge) { |
| 1000 |
const auto *LM = static_cast<const SPIRVLoopMerge *>(LMD); |
| 1001 |
setLLVMLoopMetadata<SPIRVLoopMerge>(LM, LoopObj); |
| 1002 |
} else if (LMD->getOpCode() == OpLoopControlINTEL) { |
| 1003 |
const auto *LCI = static_cast<const SPIRVLoopControlINTEL *>(LMD); |
| 1004 |
setLLVMLoopMetadata<SPIRVLoopControlINTEL>(LCI, LoopObj); |
| 1005 |
} |
| 1006 |
|
| 1007 |
FuncLoopMetadataMap.erase(LMDItr); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
Value *SPIRVToLLVM::transValue(SPIRVValue *BV, Function *F, BasicBlock *BB, |
| 1012 |
bool CreatePlaceHolder) { |
| 1013 |
SPIRVToLLVMValueMap::iterator Loc = ValueMap.find(BV); |
| 1014 |
if (Loc != ValueMap.end() && (!PlaceholderMap.count(BV) || CreatePlaceHolder)) |
| 1015 |
return Loc->second; |
| 1016 |
|
| 1017 |
SPIRVDBG(spvdbgs() << "[transValue] " << *BV << " -> ";) |
| 1018 |
BV->validate(); |
| 1019 |
|
| 1020 |
auto V = transValueWithoutDecoration(BV, F, BB, CreatePlaceHolder); |
| 1021 |
if (!V) { |
| 1022 |
SPIRVDBG(dbgs() << " Warning ! nullptr\n";) |
| 1023 |
return nullptr; |
| 1024 |
} |
| 1025 |
setName(V, BV); |
| 1026 |
if (!transDecoration(BV, V)) { |
| 1027 |
assert(0 && "trans decoration fail"); |
| 1028 |
return nullptr; |
| 1029 |
} |
| 1030 |
|
| 1031 |
SPIRVDBG(dbgs() << *V << '\n';) |
| 1032 |
|
| 1033 |
return V; |
| 1034 |
} |
| 1035 |
|
| 1036 |
Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, |
| 1037 |
BasicBlock *BB) { |
| 1038 |
SPIRVUnary *BC = static_cast<SPIRVUnary *>(BV); |
| 1039 |
auto Src = transValue(BC->getOperand(0), F, BB, BB ? true : false); |
| 1040 |
auto Dst = transType(BC->getType()); |
| 1041 |
CastInst::CastOps CO = Instruction::BitCast; |
| 1042 |
bool IsExt = |
| 1043 |
Dst->getScalarSizeInBits() > Src->getType()->getScalarSizeInBits(); |
| 1044 |
|
| 1045 |
auto GetFPEncoding = [](SPIRVType *Ty) -> FPEncodingWrap { |
| 1046 |
if (Ty->isTypeFloat()) { |
| 1047 |
unsigned Enc = |
| 1048 |
static_cast<SPIRVTypeFloat *>(Ty)->getFloatingPointEncoding(); |
| 1049 |
return static_cast<FPEncodingWrap>(Enc); |
| 1050 |
} |
| 1051 |
if (Ty->isTypeInt()) |
| 1052 |
return FPEncodingWrap::Integer; |
| 1053 |
return FPEncodingWrap::IEEE754; |
| 1054 |
}; |
| 1055 |
|
| 1056 |
auto IsFP4OrFP8Encoding = [](FPEncodingWrap Encoding) -> bool { |
| 1057 |
return Encoding == FPEncodingWrap::E4M3 || |
| 1058 |
Encoding == FPEncodingWrap::E5M2 || Encoding == FPEncodingWrap::E2M1; |
| 1059 |
}; |
| 1060 |
|
| 1061 |
switch (static_cast<unsigned>(BC->getOpCode())) { |
| 1062 |
case OpPtrCastToGeneric: |
| 1063 |
case OpGenericCastToPtr: |
| 1064 |
case OpPtrCastToCrossWorkgroupINTEL: |
| 1065 |
case OpCrossWorkgroupCastToPtrINTEL: { |
| 1066 |
// If module has pointers with DeviceOnlyINTEL and HostOnlyINTEL storage |
| 1067 |
// classes there will be a situation, when global_device/global_host |
| 1068 |
// address space will be lowered to just global address space. If there also |
| 1069 |
// is an addrspacecast - we need to replace it with source pointer. |
| 1070 |
if (Src->getType()->getPointerAddressSpace() == |
| 1071 |
Dst->getPointerAddressSpace()) |
| 1072 |
return Src; |
| 1073 |
CO = Instruction::AddrSpaceCast; |
| 1074 |
break; |
| 1075 |
} |
| 1076 |
case OpSConvert: |
| 1077 |
CO = IsExt ? Instruction::SExt : Instruction::Trunc; |
| 1078 |
break; |
| 1079 |
case OpUConvert: |
| 1080 |
CO = IsExt ? Instruction::ZExt : Instruction::Trunc; |
| 1081 |
break; |
| 1082 |
case internal::OpClampConvertFToFINTEL: |
| 1083 |
case internal::OpClampConvertFToSINTEL: |
| 1084 |
case internal::OpStochasticRoundFToFINTEL: |
| 1085 |
case internal::OpClampStochasticRoundFToFINTEL: |
| 1086 |
case internal::OpClampStochasticRoundFToSINTEL: |
| 1087 |
case OpConvertSToF: |
| 1088 |
case OpConvertFToS: |
| 1089 |
case OpConvertUToF: |
| 1090 |
case OpConvertFToU: |
| 1091 |
case OpFConvert: { |
| 1092 |
const auto OC = BC->getOpCode(); |
| 1093 |
{ |
| 1094 |
auto SPVOps = BC->getOperands(); |
| 1095 |
auto *SPVSrcTy = SPVOps[0]->getType(); |
| 1096 |
auto *SPVDstTy = BC->getType(); |
| 1097 |
|
| 1098 |
auto GetEncodingAndUpdateType = |
| 1099 |
[GetFPEncoding](SPIRVType *&SPVTy) -> FPEncodingWrap { |
| 1100 |
if (SPVTy->isTypeVector()) { |
| 1101 |
SPVTy = SPVTy->getVectorComponentType(); |
| 1102 |
} else if (SPVTy->isTypeCooperativeMatrixKHR()) { |
| 1103 |
auto *MT = static_cast<SPIRVTypeCooperativeMatrixKHR *>(SPVTy); |
| 1104 |
SPVTy = MT->getCompType(); |
| 1105 |
} |
| 1106 |
return GetFPEncoding(SPVTy); |
| 1107 |
}; |
| 1108 |
|
| 1109 |
FPEncodingWrap SrcEnc = GetEncodingAndUpdateType(SPVSrcTy); |
| 1110 |
FPEncodingWrap DstEnc = GetEncodingAndUpdateType(SPVDstTy); |
| 1111 |
if (IsFP4OrFP8Encoding(SrcEnc) || IsFP4OrFP8Encoding(DstEnc) || |
| 1112 |
SPVSrcTy->isTypeInt(4) || SPVDstTy->isTypeInt(4)) { |
| 1113 |
FPConversionDesc FPDesc = {SrcEnc, DstEnc, BC->getOpCode()}; |
| 1114 |
auto Conv = SPIRV::FPConvertToEncodingMap::rmap(FPDesc); |
| 1115 |
std::vector<Value *> Ops = {Src}; |
| 1116 |
std::vector<Type *> OpsTys = {Src->getType()}; |
| 1117 |
|
| 1118 |
std::string BuiltinName = |
| 1119 |
kSPIRVName::InternalBuiltinPrefix + std::string(Conv); |
| 1120 |
BuiltinFuncMangleInfo Info; |
| 1121 |
std::string MangledName; |
| 1122 |
// Translate additional Ops for stochastic conversions. |
| 1123 |
if (OC == internal::OpStochasticRoundFToFINTEL || |
| 1124 |
OC == internal::OpClampStochasticRoundFToFINTEL || |
| 1125 |
OC == internal::OpClampStochasticRoundFToSINTEL) { |
| 1126 |
// Seed. |
| 1127 |
Ops.emplace_back(transValue(SPVOps[1], F, BB, true)); |
| 1128 |
OpsTys.emplace_back(Ops[1]->getType()); |
| 1129 |
constexpr unsigned MaxOpsSize = 3; |
| 1130 |
if (SPVOps.size() == MaxOpsSize) { |
| 1131 |
// New Seed. |
| 1132 |
Ops.emplace_back(transValue(SPVOps[2], F, BB, true)); |
| 1133 |
|
| 1134 |
// The following mess is needed to create a function with correct |
| 1135 |
// mangling. |
| 1136 |
SPIRVType *PtrTy = SPVOps[2]->getType(); |
| 1137 |
const unsigned AS = |
| 1138 |
SPIRSPIRVAddrSpaceMap::rmap(PtrTy->getPointerStorageClass()); |
| 1139 |
Type *ElementTy = transType(PtrTy->getPointerElementType()); |
| 1140 |
OpsTys.emplace_back(TypedPointerType::get(ElementTy, AS)); |
| 1141 |
MangledName = mangleBuiltin(BuiltinName, OpsTys, &Info); |
| 1142 |
// But to create function itself we need untyped pointer type. |
| 1143 |
OpsTys[2] = opaquifyType(OpsTys[2]); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
if (MangledName.empty()) |
| 1148 |
MangledName = mangleBuiltin(BuiltinName, OpsTys, &Info); |
| 1149 |
|
| 1150 |
FunctionType *FTy = FunctionType::get(Dst, OpsTys, false); |
| 1151 |
FunctionCallee Func = M->getOrInsertFunction(MangledName, FTy); |
| 1152 |
return CallInst::Create(Func, Ops, "", BB); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
// These conversions can be done without __builtin_spirv prefixed functions |
| 1156 |
// as their operand and result types have native representation in LLVM IR. |
| 1157 |
if (OC == internal::OpClampConvertFToFINTEL || |
| 1158 |
OC == internal::OpStochasticRoundFToFINTEL || |
| 1159 |
OC == internal::OpClampStochasticRoundFToFINTEL) |
| 1160 |
return mapValue(BV, transSPIRVBuiltinFromInst( |
| 1161 |
static_cast<SPIRVInstruction *>(BV), BB)); |
| 1162 |
|
| 1163 |
if (OC == OpFConvert) { |
| 1164 |
CO = IsExt ? Instruction::FPExt : Instruction::FPTrunc; |
| 1165 |
break; |
| 1166 |
} |
| 1167 |
CO = static_cast<CastInst::CastOps>(OpCodeMap::rmap(OC)); |
| 1168 |
break; |
| 1169 |
} |
| 1170 |
case OpBitcast: |
| 1171 |
if (!Dst->isPointerTy() && Dst == Src->getType()) |
| 1172 |
return Src; |
| 1173 |
// OpBitcast need to be handled as a special-case when the source is a |
| 1174 |
// pointer and the destination is not a pointer, and where the source is not |
| 1175 |
// a pointer and the destination is a pointer. This is supported by the |
| 1176 |
// SPIR-V bitcast, but not by the LLVM bitcast. |
| 1177 |
CO = Instruction::BitCast; |
| 1178 |
if (Src->getType()->isPointerTy() && !Dst->isPointerTy()) { |
| 1179 |
if (auto *DstVecTy = dyn_cast<FixedVectorType>(Dst)) { |
| 1180 |
unsigned TotalBitWidth = |
| 1181 |
DstVecTy->getElementType()->getIntegerBitWidth() * |
| 1182 |
DstVecTy->getNumElements(); |
| 1183 |
auto *IntTy = Type::getIntNTy(BB->getContext(), TotalBitWidth); |
| 1184 |
if (BB) { |
| 1185 |
Src = CastInst::CreatePointerCast(Src, IntTy, "", BB); |
| 1186 |
} else { |
| 1187 |
Src = ConstantExpr::getPointerCast(dyn_cast<Constant>(Src), IntTy); |
| 1188 |
} |
| 1189 |
} else { |
| 1190 |
CO = Instruction::PtrToInt; |
| 1191 |
} |
| 1192 |
} else if (!Src->getType()->isPointerTy() && Dst->isPointerTy()) { |
| 1193 |
if (auto *SrcVecTy = dyn_cast<FixedVectorType>(Src->getType())) { |
| 1194 |
unsigned TotalBitWidth = |
| 1195 |
SrcVecTy->getElementType()->getIntegerBitWidth() * |
| 1196 |
SrcVecTy->getNumElements(); |
| 1197 |
auto *IntTy = Type::getIntNTy(BB->getContext(), TotalBitWidth); |
| 1198 |
if (BB) { |
| 1199 |
Src = CastInst::Create(Instruction::BitCast, Src, IntTy, "", BB); |
| 1200 |
} else { |
| 1201 |
Src = ConstantExpr::getBitCast(dyn_cast<Constant>(Src), IntTy); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
CO = Instruction::IntToPtr; |
| 1205 |
} |
| 1206 |
break; |
| 1207 |
// TODO: remove this as it's invalid SPIR-V. |
| 1208 |
case OpConvertPtrToU: { |
| 1209 |
if (Src->getType()->isTargetExtTy()) |
| 1210 |
return transSPIRVBuiltinFromInst(BC, BB); |
| 1211 |
[[fallthrough]]; |
| 1212 |
} |
| 1213 |
default: |
| 1214 |
CO = static_cast<CastInst::CastOps>(OpCodeMap::rmap(BC->getOpCode())); |
| 1215 |
} |
| 1216 |
assert(CastInst::isCast(CO) && "Invalid cast op code"); |
| 1217 |
SPIRVDBG(if (!CastInst::castIsValid(CO, Src, Dst)) { |
| 1218 |
spvdbgs() << "Invalid cast: " << *BV << " -> "; |
| 1219 |
dbgs() << "Op = " << CO << ", Src = " << *Src << " Dst = " << *Dst << '\n'; |
| 1220 |
}) |
| 1221 |
if (BB) |
| 1222 |
return CastInst::Create(CO, Src, Dst, BV->getName(), BB); |
| 1223 |
return ConstantExpr::getCast(CO, dyn_cast<Constant>(Src), Dst); |
| 1224 |
} |
| 1225 |
|
| 1226 |
static void applyNoIntegerWrapDecorations(const SPIRVValue *BV, |
| 1227 |
Instruction *Inst) { |
| 1228 |
if (BV->hasDecorate(DecorationNoSignedWrap)) { |
| 1229 |
Inst->setHasNoSignedWrap(true); |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (BV->hasDecorate(DecorationNoUnsignedWrap)) { |
| 1233 |
Inst->setHasNoUnsignedWrap(true); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
static void applyFPFastMathModeDecorations(const SPIRVValue *BV, |
| 1238 |
Instruction *Inst) { |
| 1239 |
SPIRVWord V; |
| 1240 |
FastMathFlags FMF; |
| 1241 |
if (BV->hasDecorate(DecorationFPFastMathMode, 0, &V)) { |
| 1242 |
if (V & FPFastMathModeNotNaNMask) |
| 1243 |
FMF.setNoNaNs(); |
| 1244 |
if (V & FPFastMathModeNotInfMask) |
| 1245 |
FMF.setNoInfs(); |
| 1246 |
if (V & FPFastMathModeNSZMask) |
| 1247 |
FMF.setNoSignedZeros(); |
| 1248 |
if (V & FPFastMathModeAllowRecipMask) |
| 1249 |
FMF.setAllowReciprocal(); |
| 1250 |
if (V & FPFastMathModeAllowContractFastINTELMask) |
| 1251 |
FMF.setAllowContract(); |
| 1252 |
if (V & FPFastMathModeAllowReassocINTELMask) |
| 1253 |
FMF.setAllowReassoc(); |
| 1254 |
if (V & FPFastMathModeFastMask) |
| 1255 |
FMF.setFast(); |
| 1256 |
Inst->setFastMathFlags(FMF); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
Value *SPIRVToLLVM::transShiftLogicalBitwiseInst(SPIRVValue *BV, BasicBlock *BB, |
| 1261 |
Function *F) { |
| 1262 |
SPIRVBinary *BBN = static_cast<SPIRVBinary *>(BV); |
| 1263 |
if (BV->getType()->isTypeCooperativeMatrixKHR()) { |
| 1264 |
return mapValue(BV, transSPIRVBuiltinFromInst(BBN, BB)); |
| 1265 |
} |
| 1266 |
Instruction::BinaryOps BO; |
| 1267 |
auto OP = BBN->getOpCode(); |
| 1268 |
if (isLogicalOpCode(OP)) |
| 1269 |
OP = IntBoolOpMap::rmap(OP); |
| 1270 |
BO = static_cast<Instruction::BinaryOps>(OpCodeMap::rmap(OP)); |
| 1271 |
|
| 1272 |
Value *Op0 = transValue(BBN->getOperand(0), F, BB); |
| 1273 |
Value *Op1 = transValue(BBN->getOperand(1), F, BB); |
| 1274 |
|
| 1275 |
IRBuilder<> Builder(*Context); |
| 1276 |
if (BB) { |
| 1277 |
Builder.SetInsertPoint(BB); |
| 1278 |
} |
| 1279 |
|
| 1280 |
Value *NewOp = Builder.CreateBinOp(BO, Op0, Op1, BV->getName()); |
| 1281 |
if (auto *Inst = dyn_cast<Instruction>(NewOp)) { |
| 1282 |
applyNoIntegerWrapDecorations(BV, Inst); |
| 1283 |
applyFPFastMathModeDecorations(BV, Inst); |
| 1284 |
} |
| 1285 |
return NewOp; |
| 1286 |
} |
| 1287 |
|
| 1288 |
Value *SPIRVToLLVM::transCmpInst(SPIRVValue *BV, BasicBlock *BB, Function *F) { |
| 1289 |
SPIRVCompare *BC = static_cast<SPIRVCompare *>(BV); |
| 1290 |
SPIRVType *BT = BC->getOperand(0)->getType(); |
| 1291 |
Value *Inst = nullptr; |
| 1292 |
auto OP = BC->getOpCode(); |
| 1293 |
if (isLogicalOpCode(OP)) |
| 1294 |
OP = IntBoolOpMap::rmap(OP); |
| 1295 |
|
| 1296 |
Value *Op0 = transValue(BC->getOperand(0), F, BB); |
| 1297 |
Value *Op1 = transValue(BC->getOperand(1), F, BB); |
| 1298 |
|
| 1299 |
IRBuilder<> Builder(*Context); |
| 1300 |
if (BB) { |
| 1301 |
Builder.SetInsertPoint(BB); |
| 1302 |
} |
| 1303 |
|
| 1304 |
if (OP == OpLessOrGreater) |
| 1305 |
OP = OpFOrdNotEqual; |
| 1306 |
|
| 1307 |
if (BT->isTypeVectorOrScalarInt() || BT->isTypeVectorOrScalarBool() || |
| 1308 |
BT->isTypePointer()) |
| 1309 |
Inst = Builder.CreateICmp(CmpMap::rmap(OP), Op0, Op1); |
| 1310 |
else if (BT->isTypeVectorOrScalarFloat()) |
| 1311 |
Inst = Builder.CreateFCmp(CmpMap::rmap(OP), Op0, Op1); |
| 1312 |
assert(Inst && "not implemented"); |
| 1313 |
applyFPFastMathModeDecorations(BV, static_cast<Instruction *>(Inst)); |
| 1314 |
return Inst; |
| 1315 |
} |
| 1316 |
|
| 1317 |
Type *SPIRVToLLVM::mapType(SPIRVType *BT, Type *T) { |
| 1318 |
SPIRVDBG(dbgs() << *T << '\n';) |
| 1319 |
// We don't want to store a TypedPointerType in the type map, since we can't |
| 1320 |
// actually use it in LLVM IR directly. Note that in the cases where we do |
| 1321 |
// want to construct TypedPointerType, we don't check the type map here. |
| 1322 |
if (!isa<TypedPointerType>(T)) |
| 1323 |
TypeMap[BT] = T; |
| 1324 |
return T; |
| 1325 |
} |
| 1326 |
|
| 1327 |
Value *SPIRVToLLVM::mapValue(SPIRVValue *BV, Value *V) { |
| 1328 |
auto Loc = ValueMap.find(BV); |
| 1329 |
if (Loc != ValueMap.end()) { |
| 1330 |
if (Loc->second == V) |
| 1331 |
return V; |
| 1332 |
auto LD = dyn_cast<LoadInst>(Loc->second); |
| 1333 |
auto Placeholder = dyn_cast<GlobalVariable>(LD->getPointerOperand()); |
| 1334 |
assert(LD && Placeholder && |
| 1335 |
Placeholder->getName().startswith(KPlaceholderPrefix) && |
| 1336 |
"A value is translated twice"); |
| 1337 |
// Replaces placeholders for PHI nodes |
| 1338 |
LD->replaceAllUsesWith(V); |
| 1339 |
LD->eraseFromParent(); |
| 1340 |
Placeholder->eraseFromParent(); |
| 1341 |
} |
| 1342 |
ValueMap[BV] = V; |
| 1343 |
return V; |
| 1344 |
} |
| 1345 |
|
| 1346 |
CallInst * |
| 1347 |
SPIRVToLLVM::expandOCLBuiltinWithScalarArg(CallInst *CI, |
| 1348 |
const std::string &FuncName) { |
| 1349 |
if (!CI->getOperand(0)->getType()->isVectorTy() && |
| 1350 |
CI->getOperand(1)->getType()->isVectorTy()) { |
| 1351 |
auto VecElemCount = |
| 1352 |
cast<VectorType>(CI->getOperand(1)->getType())->getElementCount(); |
| 1353 |
auto Mutator = mutateCallInst(CI, FuncName); |
| 1354 |
Mutator.mapArg(0, [=](Value *Arg) { |
| 1355 |
Value *NewVec = nullptr; |
| 1356 |
if (auto *CA = dyn_cast<Constant>(Arg)) |
| 1357 |
NewVec = ConstantVector::getSplat(VecElemCount, CA); |
| 1358 |
else { |
| 1359 |
NewVec = ConstantVector::getSplat( |
| 1360 |
VecElemCount, Constant::getNullValue(Arg->getType())); |
| 1361 |
NewVec = InsertElementInst::Create(NewVec, Arg, getInt32(M, 0), "", CI); |
| 1362 |
NewVec = new ShuffleVectorInst( |
| 1363 |
NewVec, NewVec, |
| 1364 |
ConstantVector::getSplat(VecElemCount, getInt32(M, 0)), "", CI); |
| 1365 |
} |
| 1366 |
NewVec->takeName(Arg); |
| 1367 |
return NewVec; |
| 1368 |
}); |
| 1369 |
return cast<CallInst>(Mutator.getMutated()); |
| 1370 |
} |
| 1371 |
return CI; |
| 1372 |
} |
| 1373 |
|
| 1374 |
std::string |
| 1375 |
SPIRVToLLVM::transOCLPipeTypeAccessQualifier(SPIRV::SPIRVTypePipe *ST) { |
| 1376 |
return SPIRSPIRVAccessQualifierMap::rmap(ST->getAccessQualifier()); |
| 1377 |
} |
| 1378 |
|
| 1379 |
void SPIRVToLLVM::transGeneratorMD() { |
| 1380 |
SPIRVMDBuilder B(*M); |
| 1381 |
B.addNamedMD(kSPIRVMD::Generator) |
| 1382 |
.addOp() |
| 1383 |
.addU16(BM->getGeneratorId()) |
| 1384 |
.addU16(BM->getGeneratorVer()) |
| 1385 |
.done(); |
| 1386 |
} |
| 1387 |
|
| 1388 |
Value *SPIRVToLLVM::oclTransConstantSampler(SPIRV::SPIRVConstantSampler *BCS, |
| 1389 |
BasicBlock *BB) { |
| 1390 |
auto *SamplerT = getSPIRVType(OpTypeSampler, true); |
| 1391 |
auto *I32Ty = IntegerType::getInt32Ty(*Context); |
| 1392 |
auto *FTy = FunctionType::get(SamplerT, {I32Ty}, false); |
| 1393 |
|
| 1394 |
FunctionCallee Func = M->getOrInsertFunction(SAMPLER_INIT, FTy); |
| 1395 |
|
| 1396 |
auto Lit = (BCS->getAddrMode() << 1) | BCS->getNormalized() | |
| 1397 |
((BCS->getFilterMode() + 1) << 4); |
| 1398 |
|
| 1399 |
return CallInst::Create(Func, {ConstantInt::get(I32Ty, Lit)}, "", BB); |
| 1400 |
} |
| 1401 |
|
| 1402 |
Value *SPIRVToLLVM::oclTransConstantPipeStorage( |
| 1403 |
SPIRV::SPIRVConstantPipeStorage *BCPS) { |
| 1404 |
|
| 1405 |
string CPSName = string(kSPIRVTypeName::PrefixAndDelim) + |
| 1406 |
kSPIRVTypeName::ConstantPipeStorage; |
| 1407 |
|
| 1408 |
auto Int32Ty = IntegerType::getInt32Ty(*Context); |
| 1409 |
auto CPSTy = StructType::getTypeByName(*Context, CPSName); |
| 1410 |
if (!CPSTy) { |
| 1411 |
Type *CPSElemsTy[] = {Int32Ty, Int32Ty, Int32Ty}; |
| 1412 |
CPSTy = StructType::create(*Context, CPSElemsTy, CPSName); |
| 1413 |
} |
| 1414 |
|
| 1415 |
assert(CPSTy != nullptr && "Could not create spirv.ConstantPipeStorage"); |
| 1416 |
|
| 1417 |
Constant *CPSElems[] = {ConstantInt::get(Int32Ty, BCPS->getPacketSize()), |
| 1418 |
ConstantInt::get(Int32Ty, BCPS->getPacketAlign()), |
| 1419 |
ConstantInt::get(Int32Ty, BCPS->getCapacity())}; |
| 1420 |
|
| 1421 |
return new GlobalVariable(*M, CPSTy, false, GlobalValue::LinkOnceODRLinkage, |
| 1422 |
ConstantStruct::get(CPSTy, CPSElems), |
| 1423 |
BCPS->getName(), nullptr, |
| 1424 |
GlobalValue::NotThreadLocal, SPIRAS_Global); |
| 1425 |
} |
| 1426 |
|
| 1427 |
// A pointer annotation may have been generated for the operand. If the operand |
| 1428 |
// is used further in IR, it should be replaced with the intrinsic call result. |
| 1429 |
// Otherwise, the generated pointer annotation call is left unused. |
| 1430 |
static void replaceOperandWithAnnotationIntrinsicCallResult(Value *&V) { |
| 1431 |
if (Use *SingleUse = V->getSingleUndroppableUse()) { |
| 1432 |
if (auto *II = dyn_cast<IntrinsicInst>(SingleUse->getUser())) { |
| 1433 |
if (II->getIntrinsicID() == Intrinsic::ptr_annotation && |
| 1434 |
II->getType() == V->getType()) |
| 1435 |
// Overwrite the future operand with the intrinsic call result. |
| 1436 |
V = II; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
// Translate aliasing memory access masks for SPIRVLoad and SPIRVStore |
| 1442 |
// instructions. These masks are mapped on alias.scope and noalias |
| 1443 |
// metadata in LLVM. Translation of optional string operand isn't yet supported |
| 1444 |
// in the translator. |
| 1445 |
template <typename SPIRVInstType> |
| 1446 |
void SPIRVToLLVM::transAliasingMemAccess(SPIRVInstType *BI, Instruction *I) { |
| 1447 |
static_assert(std::is_same<SPIRVInstType, SPIRVStore>::value || |
| 1448 |
std::is_same<SPIRVInstType, SPIRVLoad>::value, |
| 1449 |
"Only stores and loads can be aliased by memory access mask"); |
| 1450 |
if (BI->SPIRVMemoryAccess::isNoAlias()) |
| 1451 |
addMemAliasMetadata(I, BI->SPIRVMemoryAccess::getNoAliasInstID(), |
| 1452 |
LLVMContext::MD_noalias); |
| 1453 |
if (BI->SPIRVMemoryAccess::isAliasScope()) |
| 1454 |
addMemAliasMetadata(I, BI->SPIRVMemoryAccess::getAliasScopeInstID(), |
| 1455 |
LLVMContext::MD_alias_scope); |
| 1456 |
} |
| 1457 |
|
| 1458 |
// Create and apply alias.scope/noalias metadata |
| 1459 |
void SPIRVToLLVM::addMemAliasMetadata(Instruction *I, SPIRVId AliasListId, |
| 1460 |
uint32_t AliasMDKind) { |
| 1461 |
SPIRVAliasScopeListDeclINTEL *AliasList = |
| 1462 |
BM->get<SPIRVAliasScopeListDeclINTEL>(AliasListId); |
| 1463 |
std::vector<SPIRVId> AliasScopeIds = AliasList->getArguments(); |
| 1464 |
MDBuilder MDB(*Context); |
| 1465 |
SmallVector<Metadata *, 4> MDScopes; |
| 1466 |
for (const auto ScopeId : AliasScopeIds) { |
| 1467 |
SPIRVAliasScopeDeclINTEL *AliasScope = |
| 1468 |
BM->get<SPIRVAliasScopeDeclINTEL>(ScopeId); |
| 1469 |
std::vector<SPIRVId> AliasDomainIds = AliasScope->getArguments(); |
| 1470 |
// Currently we expect exactly one argument for aliasing scope |
| 1471 |
// instruction. |
| 1472 |
// TODO: add translation of string scope and domain operand. |
| 1473 |
assert(AliasDomainIds.size() == 1 && |
| 1474 |
"AliasScopeDeclINTEL must have exactly one argument"); |
| 1475 |
SPIRVId AliasDomainId = AliasDomainIds[0]; |
| 1476 |
// Create and store unique domain and scope metadata |
| 1477 |
MDAliasDomainMap.emplace(AliasDomainId, |
| 1478 |
MDB.createAnonymousAliasScopeDomain()); |
| 1479 |
MDAliasScopeMap.emplace(ScopeId, MDB.createAnonymousAliasScope( |
| 1480 |
MDAliasDomainMap[AliasDomainId])); |
| 1481 |
MDScopes.emplace_back(MDAliasScopeMap[ScopeId]); |
| 1482 |
} |
| 1483 |
// Create and store unique alias.scope/noalias metadata |
| 1484 |
MDAliasListMap.emplace(AliasListId, |
| 1485 |
MDNode::concatenate(I->getMetadata(AliasMDKind), |
| 1486 |
MDNode::get(*Context, MDScopes))); |
| 1487 |
I->setMetadata(AliasMDKind, MDAliasListMap[AliasListId]); |
| 1488 |
} |
| 1489 |
|
| 1490 |
void SPIRVToLLVM::transFunctionPointerCallArgumentAttributes( |
| 1491 |
SPIRVValue *BV, CallInst *CI, SPIRVTypeFunction *CalledFnTy) { |
| 1492 |
std::vector<SPIRVDecorate const *> ArgumentAttributes = |
| 1493 |
BV->getDecorations(internal::DecorationArgumentAttributeINTEL); |
| 1494 |
|
| 1495 |
for (const auto *Dec : ArgumentAttributes) { |
| 1496 |
std::vector<SPIRVWord> Literals = Dec->getVecLiteral(); |
| 1497 |
SPIRVWord ArgNo = Literals[0]; |
| 1498 |
SPIRVWord SpirvAttr = Literals[1]; |
| 1499 |
Attribute::AttrKind LlvmAttrKind = SPIRSPIRVFuncParamAttrMap::rmap( |
| 1500 |
static_cast<SPIRVFuncParamAttrKind>(SpirvAttr)); |
| 1501 |
auto LlvmAttr = |
| 1502 |
Attribute::isTypeAttrKind(LlvmAttrKind) |
| 1503 |
? Attribute::get(CI->getContext(), LlvmAttrKind, |
| 1504 |
transType(CalledFnTy->getParameterType(ArgNo) |
| 1505 |
->getPointerElementType())) |
| 1506 |
: Attribute::get(CI->getContext(), LlvmAttrKind); |
| 1507 |
CI->addParamAttr(ArgNo, LlvmAttr); |
| 1508 |
} |
| 1509 |
} |
| 1510 |
|
| 1511 |
/// For instructions, this function assumes they are created in order |
| 1512 |
/// and appended to the given basic block. An instruction may use a |
| 1513 |
/// instruction from another BB which has not been translated. Such |
| 1514 |
/// instructions should be translated to place holders at the point |
| 1515 |
/// of first use, then replaced by real instructions when they are |
| 1516 |
/// created. |
| 1517 |
/// |
| 1518 |
/// When CreatePlaceHolder is true, create a load instruction of a |
| 1519 |
/// global variable as placeholder for SPIRV instruction. Otherwise, |
| 1520 |
/// create instruction and replace placeholder if there is one. |
| 1521 |
Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F, |
| 1522 |
BasicBlock *BB, |
| 1523 |
bool CreatePlaceHolder) { |
| 1524 |
|
| 1525 |
auto OC = BV->getOpCode(); |
| 1526 |
IntBoolOpMap::rfind(OC, &OC); |
| 1527 |
|
| 1528 |
// Translation of non-instruction values |
| 1529 |
switch (OC) { |
| 1530 |
case OpConstant: |
| 1531 |
case OpSpecConstant: { |
| 1532 |
SPIRVConstant *BConst = static_cast<SPIRVConstant *>(BV); |
| 1533 |
SPIRVType *BT = BV->getType(); |
| 1534 |
Type *LT = transType(BT); |
| 1535 |
uint64_t ConstValue = BConst->getZExtIntValue(); |
| 1536 |
SPIRVWord SpecId = 0; |
| 1537 |
if (OC == OpSpecConstant && BV->hasDecorate(DecorationSpecId, 0, &SpecId)) { |
| 1538 |
// Update the value with possibly provided external specialization. |
| 1539 |
if (BM->getSpecializationConstant(SpecId, ConstValue)) { |
| 1540 |
assert( |
| 1541 |
(BT->getBitWidth() == 64 || |
| 1542 |
(ConstValue >> BT->getBitWidth()) == 0) && |
| 1543 |
"Size of externally provided specialization constant value doesn't" |
| 1544 |
"fit into the specialization constant type"); |
| 1545 |
} |
| 1546 |
} |
| 1547 |
switch (BT->getOpCode()) { |
| 1548 |
case OpTypeBool: |
| 1549 |
case OpTypeInt: { |
| 1550 |
const unsigned NumBits = BT->getBitWidth(); |
| 1551 |
if (NumBits > 64) { |
| 1552 |
// Translate huge arbitrary precision integer constants |
| 1553 |
const unsigned RawDataNumWords = BConst->getNumWords(); |
| 1554 |
const unsigned BigValNumWords = (RawDataNumWords + 1) / 2; |
| 1555 |
std::vector<uint64_t> BigValVec(BigValNumWords); |
| 1556 |
const std::vector<SPIRVWord> &RawData = BConst->getSPIRVWords(); |
| 1557 |
// SPIRV words are integers of 32-bit width, meanwhile llvm::APInt |
| 1558 |
// is storing data using an array of 64-bit words. Here we pack SPIRV |
| 1559 |
// words into 64-bit integer array. |
| 1560 |
for (size_t I = 0; I != RawDataNumWords / 2; ++I) |
| 1561 |
BigValVec[I] = |
| 1562 |
(static_cast<uint64_t>(RawData[2 * I + 1]) << SpirvWordBitWidth) | |
| 1563 |
RawData[2 * I]; |
| 1564 |
if (RawDataNumWords % 2) |
| 1565 |
BigValVec.back() = RawData.back(); |
| 1566 |
return mapValue(BV, ConstantInt::get(LT, APInt(NumBits, BigValVec))); |
| 1567 |
} |
| 1568 |
return mapValue( |
| 1569 |
BV, ConstantInt::get(LT, ConstValue, |
| 1570 |
static_cast<SPIRVTypeInt *>(BT)->isSigned())); |
| 1571 |
} |
| 1572 |
case OpTypeFloat: { |
| 1573 |
const llvm::fltSemantics *FS = nullptr; |
| 1574 |
switch (BT->getFloatBitWidth()) { |
| 1575 |
case 16: |
| 1576 |
FS = |
| 1577 |
(BT->isTypeFloat(16, FPEncodingBFloat16KHR) ? &APFloat::BFloat() |
| 1578 |
: &APFloat::IEEEhalf()); |
| 1579 |
break; |
| 1580 |
case 32: |
| 1581 |
FS = &APFloat::IEEEsingle(); |
| 1582 |
break; |
| 1583 |
case 64: |
| 1584 |
FS = &APFloat::IEEEdouble(); |
| 1585 |
break; |
| 1586 |
default: |
| 1587 |
llvm_unreachable("invalid floating-point type"); |
| 1588 |
} |
| 1589 |
APFloat FPConstValue(*FS, APInt(BT->getFloatBitWidth(), ConstValue)); |
| 1590 |
return mapValue(BV, ConstantFP::get(*Context, FPConstValue)); |
| 1591 |
} |
| 1592 |
default: |
| 1593 |
llvm_unreachable("Not implemented"); |
| 1594 |
return nullptr; |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
case OpConstantTrue: |
| 1599 |
return mapValue(BV, ConstantInt::getTrue(*Context)); |
| 1600 |
|
| 1601 |
case OpConstantFalse: |
| 1602 |
return mapValue(BV, ConstantInt::getFalse(*Context)); |
| 1603 |
|
| 1604 |
case OpSpecConstantTrue: |
| 1605 |
case OpSpecConstantFalse: { |
| 1606 |
bool IsTrue = OC == OpSpecConstantTrue; |
| 1607 |
SPIRVWord SpecId = 0; |
| 1608 |
if (BV->hasDecorate(DecorationSpecId, 0, &SpecId)) { |
| 1609 |
uint64_t ConstValue = 0; |
| 1610 |
if (BM->getSpecializationConstant(SpecId, ConstValue)) { |
| 1611 |
IsTrue = ConstValue; |
| 1612 |
} |
| 1613 |
} |
| 1614 |
return mapValue(BV, IsTrue ? ConstantInt::getTrue(*Context) |
| 1615 |
: ConstantInt::getFalse(*Context)); |
| 1616 |
} |
| 1617 |
|
| 1618 |
case OpConstantNull: { |
| 1619 |
auto LT = transType(BV->getType()); |
| 1620 |
return mapValue(BV, Constant::getNullValue(LT)); |
| 1621 |
} |
| 1622 |
|
| 1623 |
case OpConstantComposite: |
| 1624 |
case OpSpecConstantComposite: { |
| 1625 |
auto BCC = static_cast<SPIRVConstantComposite *>(BV); |
| 1626 |
std::vector<Constant *> CV; |
| 1627 |
for (auto &I : BCC->getElements()) |
| 1628 |
CV.push_back(dyn_cast<Constant>(transValue(I, F, BB))); |
| 1629 |
for (auto &CI : BCC->getContinuedInstructions()) { |
| 1630 |
for (auto &I : CI->getElements()) |
| 1631 |
CV.push_back(dyn_cast<Constant>(transValue(I, F, BB))); |
| 1632 |
} |
| 1633 |
switch (BV->getType()->getOpCode()) { |
| 1634 |
case OpTypeVector: |
| 1635 |
return mapValue(BV, ConstantVector::get(CV)); |
| 1636 |
case OpTypeMatrix: |
| 1637 |
case OpTypeArray: { |
| 1638 |
auto *AT = cast<ArrayType>(transType(BCC->getType())); |
| 1639 |
for (size_t I = 0; I != AT->getNumElements(); ++I) { |
| 1640 |
auto *ElemTy = AT->getElementType(); |
| 1641 |
if (auto *ElemPtrTy = dyn_cast<PointerType>(ElemTy)) { |
| 1642 |
assert(isa<PointerType>(CV[I]->getType()) && |
| 1643 |
"Constant type doesn't match constexpr array element type"); |
| 1644 |
if (ElemPtrTy->getAddressSpace() != |
| 1645 |
cast<PointerType>(CV[I]->getType())->getAddressSpace()) |
| 1646 |
CV[I] = ConstantExpr::getAddrSpaceCast(CV[I], AT->getElementType()); |
| 1647 |
} |
| 1648 |
} |
| 1649 |
|
| 1650 |
return mapValue(BV, ConstantArray::get(AT, CV)); |
| 1651 |
} |
| 1652 |
case OpTypeStruct: { |
| 1653 |
auto *BCCTy = cast<StructType>(transType(BCC->getType())); |
| 1654 |
auto Members = BCCTy->getNumElements(); |
| 1655 |
auto Constants = CV.size(); |
| 1656 |
// if we try to initialize constant TypeStruct, add bitcasts |
| 1657 |
// if src and dst types are both pointers but to different types |
| 1658 |
if (Members == Constants) { |
| 1659 |
for (unsigned I = 0; I < Members; ++I) { |
| 1660 |
if (CV[I]->getType() == BCCTy->getElementType(I)) |
| 1661 |
continue; |
| 1662 |
if (!CV[I]->getType()->isPointerTy() || |
| 1663 |
!BCCTy->getElementType(I)->isPointerTy()) |
| 1664 |
continue; |
| 1665 |
|
| 1666 |
if (cast<PointerType>(CV[I]->getType())->getAddressSpace() != |
| 1667 |
cast<PointerType>(BCCTy->getElementType(I))->getAddressSpace()) |
| 1668 |
CV[I] = |
| 1669 |
ConstantExpr::getAddrSpaceCast(CV[I], BCCTy->getElementType(I)); |
| 1670 |
else |
| 1671 |
CV[I] = ConstantExpr::getBitCast(CV[I], BCCTy->getElementType(I)); |
| 1672 |
} |
| 1673 |
} |
| 1674 |
|
| 1675 |
return mapValue(BV, ConstantStruct::get(BCCTy, CV)); |
| 1676 |
} |
| 1677 |
case OpTypeCooperativeMatrixKHR: { |
| 1678 |
assert(CV.size() == 1 && |
| 1679 |
"expecting exactly one operand for cooperative matrix types"); |
| 1680 |
llvm::Type *RetTy = transType(BCC->getType()); |
| 1681 |
llvm::Type *EltTy = transType( |
| 1682 |
static_cast<const SPIRVTypeCooperativeMatrixKHR *>(BV->getType()) |
| 1683 |
->getCompType()); |
| 1684 |
auto *FTy = FunctionType::get(RetTy, {EltTy}, false); |
| 1685 |
FunctionCallee Func = |
| 1686 |
M->getOrInsertFunction(getSPIRVFuncName(OC, RetTy), FTy); |
| 1687 |
IRBuilder<> Builder(BB); |
| 1688 |
CallInst *Call = Builder.CreateCall(Func, CV.front()); |
| 1689 |
Call->setCallingConv(CallingConv::SPIR_FUNC); |
| 1690 |
return Call; |
| 1691 |
} |
| 1692 |
default: |
| 1693 |
llvm_unreachable("not implemented"); |
| 1694 |
return nullptr; |
| 1695 |
} |
| 1696 |
} |
| 1697 |
|
| 1698 |
case OpConstantSampler: { |
| 1699 |
auto BCS = static_cast<SPIRVConstantSampler *>(BV); |
| 1700 |
// Intentially do not map this value. We want to generate constant |
| 1701 |
// sampler initializer every time constant sampler is used, otherwise |
| 1702 |
// initializer may not dominate all its uses. |
| 1703 |
return oclTransConstantSampler(BCS, BB); |
| 1704 |
} |
| 1705 |
|
| 1706 |
case OpConstantPipeStorage: { |
| 1707 |
auto BCPS = static_cast<SPIRVConstantPipeStorage *>(BV); |
| 1708 |
return mapValue(BV, oclTransConstantPipeStorage(BCPS)); |
| 1709 |
} |
| 1710 |
|
| 1711 |
case OpSpecConstantOp: { |
| 1712 |
auto BI = |
| 1713 |
createInstFromSpecConstantOp(static_cast<SPIRVSpecConstantOp *>(BV)); |
| 1714 |
return mapValue(BV, transValue(BI, nullptr, nullptr, false)); |
| 1715 |
} |
| 1716 |
|
| 1717 |
case OpConstantFunctionPointerINTEL: { |
| 1718 |
SPIRVConstantFunctionPointerINTEL *BC = |
| 1719 |
static_cast<SPIRVConstantFunctionPointerINTEL *>(BV); |
| 1720 |
SPIRVFunction *F = BC->getFunction(); |
| 1721 |
BV->setName(F->getName()); |
| 1722 |
const unsigned AS = BM->shouldEmitFunctionPtrAddrSpace() |
| 1723 |
? SPIRAS_CodeSectionINTEL |
| 1724 |
: SPIRAS_Private; |
| 1725 |
return mapValue(BV, transFunction(F, AS)); |
| 1726 |
} |
| 1727 |
|
| 1728 |
case OpUndef: |
| 1729 |
return mapValue(BV, UndefValue::get(transType(BV->getType()))); |
| 1730 |
|
| 1731 |
case OpSizeOf: { |
| 1732 |
Type *ResTy = transType(BV->getType()); |
| 1733 |
auto *BI = static_cast<SPIRVSizeOf *>(BV); |
| 1734 |
SPIRVType *TypeArg = reinterpret_cast<SPIRVType *>(BI->getOpValue(0)); |
| 1735 |
Type *EltTy = transType(TypeArg->getPointerElementType()); |
| 1736 |
uint64_t Size = M->getDataLayout().getTypeStoreSize(EltTy).getFixedValue(); |
| 1737 |
return mapValue(BV, ConstantInt::get(ResTy, Size)); |
| 1738 |
} |
| 1739 |
|
| 1740 |
case OpVariable: { |
| 1741 |
auto BVar = static_cast<SPIRVVariable *>(BV); |
| 1742 |
auto *PreTransTy = BVar->getType()->getPointerElementType(); |
| 1743 |
auto *Ty = transType(PreTransTy); |
| 1744 |
bool IsConst = BVar->isConstant(); |
| 1745 |
llvm::GlobalValue::LinkageTypes LinkageTy = transLinkageType(BVar); |
| 1746 |
SPIRVStorageClassKind BS = BVar->getStorageClass(); |
| 1747 |
SPIRVValue *Init = BVar->getInitializer(); |
| 1748 |
|
| 1749 |
if (PreTransTy->isTypeSampler() && BS == StorageClassUniformConstant) { |
| 1750 |
// Skip generating llvm code during translation of a variable definition, |
| 1751 |
// generate code only for its uses |
| 1752 |
if (!BB) |
| 1753 |
return nullptr; |
| 1754 |
|
| 1755 |
assert(Init && "UniformConstant OpVariable with sampler type must have " |
| 1756 |
"an initializer!"); |
| 1757 |
return transValue(Init, F, BB); |
| 1758 |
} |
| 1759 |
|
| 1760 |
if (BS == StorageClassFunction && !Init) { |
| 1761 |
assert(BB && "Invalid BB"); |
| 1762 |
return mapValue(BV, new AllocaInst(Ty, 0, BV->getName(), BB)); |
| 1763 |
} |
| 1764 |
|
| 1765 |
SPIRAddressSpace AddrSpace; |
| 1766 |
bool IsVectorCompute = |
| 1767 |
BVar->hasDecorate(DecorationVectorComputeVariableINTEL); |
| 1768 |
Constant *Initializer = nullptr; |
| 1769 |
if (IsVectorCompute) { |
| 1770 |
AddrSpace = VectorComputeUtil::getVCGlobalVarAddressSpace(BS); |
| 1771 |
Initializer = UndefValue::get(Ty); |
| 1772 |
} else |
| 1773 |
AddrSpace = SPIRSPIRVAddrSpaceMap::rmap(BS); |
| 1774 |
// Force SPIRV BuiltIn variable's name to be __spirv_BuiltInXXXX. |
| 1775 |
// No matter what BV's linkage name is. |
| 1776 |
SPIRVBuiltinVariableKind BVKind; |
| 1777 |
if (BVar->isBuiltin(&BVKind)) |
| 1778 |
BV->setName(prefixSPIRVName(SPIRVBuiltInNameMap::map(BVKind))); |
| 1779 |
auto LVar = new GlobalVariable(*M, Ty, IsConst, LinkageTy, |
| 1780 |
/*Initializer=*/nullptr, BV->getName(), 0, |
| 1781 |
GlobalVariable::NotThreadLocal, AddrSpace); |
| 1782 |
auto Res = mapValue(BV, LVar); |
| 1783 |
if (Init) |
| 1784 |
Initializer = dyn_cast<Constant>(transValue(Init, F, BB, false)); |
| 1785 |
else if (LinkageTy == GlobalValue::CommonLinkage) |
| 1786 |
// In LLVM, variables with common linkage type must be initialized to 0. |
| 1787 |
Initializer = Constant::getNullValue(Ty); |
| 1788 |
else if (BS == SPIRVStorageClassKind::StorageClassWorkgroup && |
| 1789 |
LinkageTy != GlobalValue::ExternalLinkage) |
| 1790 |
Initializer = dyn_cast<Constant>(UndefValue::get(Ty)); |
| 1791 |
else if ((LinkageTy != GlobalValue::ExternalLinkage) && |
| 1792 |
(BS == SPIRVStorageClassKind::StorageClassCrossWorkgroup)) |
| 1793 |
Initializer = Constant::getNullValue(Ty); |
| 1794 |
|
| 1795 |
LVar->setUnnamedAddr((IsConst && Ty->isArrayTy() && |
| 1796 |
Ty->getArrayElementType()->isIntegerTy(8)) |
| 1797 |
? GlobalValue::UnnamedAddr::Global |
| 1798 |
: GlobalValue::UnnamedAddr::None); |
| 1799 |
LVar->setInitializer(Initializer); |
| 1800 |
|
| 1801 |
if (IsVectorCompute) { |
| 1802 |
LVar->addAttribute(kVCMetadata::VCGlobalVariable); |
| 1803 |
SPIRVWord Offset; |
| 1804 |
if (BVar->hasDecorate(DecorationGlobalVariableOffsetINTEL, 0, &Offset)) |
| 1805 |
LVar->addAttribute(kVCMetadata::VCByteOffset, utostr(Offset)); |
| 1806 |
if (BVar->hasDecorate(DecorationVolatile)) |
| 1807 |
LVar->addAttribute(kVCMetadata::VCVolatile); |
| 1808 |
auto SEVAttr = translateSEVMetadata(BVar, LVar->getContext()); |
| 1809 |
if (SEVAttr) |
| 1810 |
LVar->addAttribute(SEVAttr.value().getKindAsString(), |
| 1811 |
SEVAttr.value().getValueAsString()); |
| 1812 |
} |
| 1813 |
|
| 1814 |
return Res; |
| 1815 |
} |
| 1816 |
|
| 1817 |
case OpFunctionParameter: { |
| 1818 |
auto BA = static_cast<SPIRVFunctionParameter *>(BV); |
| 1819 |
assert(F && "Invalid function"); |
| 1820 |
unsigned ArgNo = 0; |
| 1821 |
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; |
| 1822 |
++I, ++ArgNo) { |
| 1823 |
if (ArgNo == BA->getArgNo()) |
| 1824 |
return mapValue(BV, &(*I)); |
| 1825 |
} |
| 1826 |
llvm_unreachable("Invalid argument"); |
| 1827 |
return nullptr; |
| 1828 |
} |
| 1829 |
|
| 1830 |
case OpFunction: |
| 1831 |
return mapValue(BV, transFunction(static_cast<SPIRVFunction *>(BV))); |
| 1832 |
|
| 1833 |
case OpAsmINTEL: |
| 1834 |
return mapValue(BV, transAsmINTEL(static_cast<SPIRVAsmINTEL *>(BV))); |
| 1835 |
|
| 1836 |
case OpLabel: |
| 1837 |
return mapValue(BV, BasicBlock::Create(*Context, BV->getName(), F)); |
| 1838 |
|
| 1839 |
case OpSpecConstantArchitectureINTEL: |
| 1840 |
llvm_unreachable( |
| 1841 |
"Encountered non-specialized OpSpecConstantArchitectureINTEL"); |
| 1842 |
return nullptr; |
| 1843 |
|
| 1844 |
case OpSpecConstantTargetINTEL: |
| 1845 |
llvm_unreachable("Encountered non-specialized OpSpecConstantTargetINTEL"); |
| 1846 |
return nullptr; |
| 1847 |
|
| 1848 |
case OpSpecConstantCapabilitiesINTEL: |
| 1849 |
llvm_unreachable( |
| 1850 |
"Encountered non-specialized OpSpecConstantCapabilitiesINTEL"); |
| 1851 |
return nullptr; |
| 1852 |
|
| 1853 |
default: |
| 1854 |
// do nothing |
| 1855 |
break; |
| 1856 |
} |
| 1857 |
|
| 1858 |
// During translation of OpSpecConstantOp we create an instruction |
| 1859 |
// corresponding to the Opcode operand and then translate this instruction. |
| 1860 |
// For such instruction BB and F should be nullptr, because it is a constant |
| 1861 |
// expression declared out of scope of any basic block or function. |
| 1862 |
// All other values require valid BB pointer. |
| 1863 |
assert(((isSpecConstantOpAllowedOp(OC) && !F && !BB) || BB) && "Invalid BB"); |
| 1864 |
|
| 1865 |
// Creation of place holder |
| 1866 |
if (CreatePlaceHolder) { |
| 1867 |
auto *Ty = transType(BV->getType()); |
| 1868 |
auto GV = |
| 1869 |
new GlobalVariable(*M, Ty, false, GlobalValue::PrivateLinkage, nullptr, |
| 1870 |
std::string(KPlaceholderPrefix) + BV->getName(), 0, |
| 1871 |
GlobalVariable::NotThreadLocal, 0); |
| 1872 |
auto LD = new LoadInst(Ty, GV, BV->getName(), BB); |
| 1873 |
PlaceholderMap[BV] = LD; |
| 1874 |
return mapValue(BV, LD); |
| 1875 |
} |
| 1876 |
|
| 1877 |
// Translation of instructions |
| 1878 |
int OpCode = BV->getOpCode(); |
| 1879 |
switch (OpCode) { |
| 1880 |
case OpVariableLengthArrayINTEL: { |
| 1881 |
auto *VLA = static_cast<SPIRVVariableLengthArrayINTEL *>(BV); |
| 1882 |
llvm::Type *Ty = transType(BV->getType()->getPointerElementType()); |
| 1883 |
llvm::Value *ArrSize = transValue(VLA->getOperand(0), F, BB); |
| 1884 |
return mapValue( |
| 1885 |
BV, new AllocaInst(Ty, SPIRAS_Private, ArrSize, BV->getName(), BB)); |
| 1886 |
} |
| 1887 |
|
| 1888 |
case OpRestoreMemoryINTEL: { |
| 1889 |
auto *Restore = static_cast<SPIRVRestoreMemoryINTEL *>(BV); |
| 1890 |
llvm::Value *Ptr = transValue(Restore->getOperand(0), F, BB); |
| 1891 |
Function *StackRestore = |
| 1892 |
Intrinsic::getDeclaration(M, Intrinsic::stackrestore); |
| 1893 |
return mapValue(BV, CallInst::Create(StackRestore, {Ptr}, "", BB)); |
| 1894 |
} |
| 1895 |
|
| 1896 |
case OpSaveMemoryINTEL: { |
| 1897 |
Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave); |
| 1898 |
return mapValue(BV, CallInst::Create(StackSave, "", BB)); |
| 1899 |
} |
| 1900 |
|
| 1901 |
case OpBranch: { |
| 1902 |
auto *BR = static_cast<SPIRVBranch *>(BV); |
| 1903 |
auto *BI = BranchInst::Create( |
| 1904 |
cast<BasicBlock>(transValue(BR->getTargetLabel(), F, BB)), BB); |
| 1905 |
// Loop metadata will be translated in the end of function translation. |
| 1906 |
return mapValue(BV, BI); |
| 1907 |
} |
| 1908 |
|
| 1909 |
case OpBranchConditional: { |
| 1910 |
auto *BR = static_cast<SPIRVBranchConditional *>(BV); |
| 1911 |
auto *BC = BranchInst::Create( |
| 1912 |
cast<BasicBlock>(transValue(BR->getTrueLabel(), F, BB)), |
| 1913 |
cast<BasicBlock>(transValue(BR->getFalseLabel(), F, BB)), |
| 1914 |
transValue(BR->getCondition(), F, BB), BB); |
| 1915 |
// Loop metadata will be translated in the end of function translation. |
| 1916 |
return mapValue(BV, BC); |
| 1917 |
} |
| 1918 |
|
| 1919 |
case OpPhi: { |
| 1920 |
auto Phi = static_cast<SPIRVPhi *>(BV); |
| 1921 |
auto LPhi = dyn_cast<PHINode>(mapValue( |
| 1922 |
BV, PHINode::Create(transType(Phi->getType()), |
| 1923 |
Phi->getPairs().size() / 2, Phi->getName(), BB))); |
| 1924 |
Phi->foreachPair([&](SPIRVValue *IncomingV, SPIRVBasicBlock *IncomingBB, |
| 1925 |
size_t Index) { |
| 1926 |
auto Translated = transValue(IncomingV, F, BB); |
| 1927 |
LPhi->addIncoming(Translated, |
| 1928 |
dyn_cast<BasicBlock>(transValue(IncomingBB, F, BB))); |
| 1929 |
}); |
| 1930 |
return LPhi; |
| 1931 |
} |
| 1932 |
|
| 1933 |
case OpUnreachable: |
| 1934 |
return mapValue(BV, new UnreachableInst(*Context, BB)); |
| 1935 |
|
| 1936 |
case OpReturn: |
| 1937 |
return mapValue(BV, ReturnInst::Create(*Context, BB)); |
| 1938 |
|
| 1939 |
case OpReturnValue: { |
| 1940 |
auto RV = static_cast<SPIRVReturnValue *>(BV); |
| 1941 |
return mapValue( |
| 1942 |
BV, ReturnInst::Create(*Context, |
| 1943 |
transValue(RV->getReturnValue(), F, BB), BB)); |
| 1944 |
} |
| 1945 |
|
| 1946 |
case OpLifetimeStart: { |
| 1947 |
SPIRVLifetimeStart *LTStart = static_cast<SPIRVLifetimeStart *>(BV); |
| 1948 |
IRBuilder<> Builder(BB); |
| 1949 |
SPIRVWord Size = LTStart->getSize(); |
| 1950 |
ConstantInt *S = nullptr; |
| 1951 |
if (Size) |
| 1952 |
S = Builder.getInt64(Size); |
| 1953 |
Value *Var = transValue(LTStart->getObject(), F, BB); |
| 1954 |
CallInst *Start = Builder.CreateLifetimeStart(Var, S); |
| 1955 |
return mapValue(BV, Start); |
| 1956 |
} |
| 1957 |
|
| 1958 |
case OpLifetimeStop: { |
| 1959 |
SPIRVLifetimeStop *LTStop = static_cast<SPIRVLifetimeStop *>(BV); |
| 1960 |
IRBuilder<> Builder(BB); |
| 1961 |
SPIRVWord Size = LTStop->getSize(); |
| 1962 |
ConstantInt *S = nullptr; |
| 1963 |
if (Size) |
| 1964 |
S = Builder.getInt64(Size); |
| 1965 |
auto Var = transValue(LTStop->getObject(), F, BB); |
| 1966 |
for (const auto &I : Var->users()) |
| 1967 |
if (auto II = getLifetimeStartIntrinsic(dyn_cast<Instruction>(I))) |
| 1968 |
return mapValue(BV, Builder.CreateLifetimeEnd(II->getOperand(1), S)); |
| 1969 |
return mapValue(BV, Builder.CreateLifetimeEnd(Var, S)); |
| 1970 |
} |
| 1971 |
|
| 1972 |
case OpStore: { |
| 1973 |
SPIRVStore *BS = static_cast<SPIRVStore *>(BV); |
| 1974 |
StoreInst *SI = nullptr; |
| 1975 |
auto *Src = transValue(BS->getSrc(), F, BB); |
| 1976 |
auto *Dst = transValue(BS->getDst(), F, BB); |
| 1977 |
// A ptr.annotation may have been generated for the source variable. |
| 1978 |
replaceOperandWithAnnotationIntrinsicCallResult(Src); |
| 1979 |
// A ptr.annotation may have been generated for the destination variable. |
| 1980 |
replaceOperandWithAnnotationIntrinsicCallResult(Dst); |
| 1981 |
|
| 1982 |
bool isVolatile = BS->SPIRVMemoryAccess::isVolatile(); |
| 1983 |
uint64_t AlignValue = BS->SPIRVMemoryAccess::getAlignment(); |
| 1984 |
if (0 == AlignValue) |
| 1985 |
SI = new StoreInst(Src, Dst, isVolatile, BB); |
| 1986 |
else |
| 1987 |
SI = new StoreInst(Src, Dst, isVolatile, Align(AlignValue), BB); |
| 1988 |
if (BS->SPIRVMemoryAccess::isNonTemporal()) |
| 1989 |
transNonTemporalMetadata(SI); |
| 1990 |
transAliasingMemAccess<SPIRVStore>(BS, SI); |
| 1991 |
return mapValue(BV, SI); |
| 1992 |
} |
| 1993 |
|
| 1994 |
case OpLoad: { |
| 1995 |
SPIRVLoad *BL = static_cast<SPIRVLoad *>(BV); |
| 1996 |
auto *V = transValue(BL->getSrc(), F, BB); |
| 1997 |
// A ptr.annotation may have been generated for the source variable. |
| 1998 |
replaceOperandWithAnnotationIntrinsicCallResult(V); |
| 1999 |
|
| 2000 |
Type *Ty = transType(BL->getType()); |
| 2001 |
LoadInst *LI = nullptr; |
| 2002 |
uint64_t AlignValue = BL->SPIRVMemoryAccess::getAlignment(); |
| 2003 |
if (0 == AlignValue) { |
| 2004 |
LI = new LoadInst(Ty, V, BV->getName(), |
| 2005 |
BL->SPIRVMemoryAccess::isVolatile(), BB); |
| 2006 |
} else { |
| 2007 |
LI = new LoadInst(Ty, V, BV->getName(), |
| 2008 |
BL->SPIRVMemoryAccess::isVolatile(), Align(AlignValue), |
| 2009 |
BB); |
| 2010 |
} |
| 2011 |
if (BL->SPIRVMemoryAccess::isNonTemporal()) |
| 2012 |
transNonTemporalMetadata(LI); |
| 2013 |
transAliasingMemAccess<SPIRVLoad>(BL, LI); |
| 2014 |
return mapValue(BV, LI); |
| 2015 |
} |
| 2016 |
|
| 2017 |
case OpCopyMemory: { |
| 2018 |
auto *BC = static_cast<SPIRVCopyMemory *>(BV); |
| 2019 |
llvm::Value *Dst = transValue(BC->getTarget(), F, BB); |
| 2020 |
MaybeAlign Align(BC->getAlignment()); |
| 2021 |
MaybeAlign SrcAlign = |
| 2022 |
BC->getSrcAlignment() ? MaybeAlign(BC->getSrcAlignment()) : Align; |
| 2023 |
Type *EltTy = |
| 2024 |
transType(BC->getSource()->getType()->getPointerElementType()); |
| 2025 |
uint64_t Size = M->getDataLayout().getTypeStoreSize(EltTy).getFixedValue(); |
| 2026 |
bool IsVolatile = BC->SPIRVMemoryAccess::isVolatile(); |
| 2027 |
IRBuilder<> Builder(BB); |
| 2028 |
|
| 2029 |
llvm::Value *Src = transValue(BC->getSource(), F, BB); |
| 2030 |
CallInst *CI = |
| 2031 |
Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile); |
| 2032 |
if (isFuncNoUnwind()) |
| 2033 |
CI->getFunction()->addFnAttr(Attribute::NoUnwind); |
| 2034 |
return mapValue(BV, CI); |
| 2035 |
} |
| 2036 |
|
| 2037 |
case OpCopyMemorySized: { |
| 2038 |
SPIRVCopyMemorySized *BC = static_cast<SPIRVCopyMemorySized *>(BV); |
| 2039 |
llvm::Value *Dst = transValue(BC->getTarget(), F, BB); |
| 2040 |
MaybeAlign Align(BC->getAlignment()); |
| 2041 |
MaybeAlign SrcAlign = |
| 2042 |
BC->getSrcAlignment() ? MaybeAlign(BC->getSrcAlignment()) : Align; |
| 2043 |
llvm::Value *Size = transValue(BC->getSize(), F, BB); |
| 2044 |
bool IsVolatile = BC->SPIRVMemoryAccess::isVolatile(); |
| 2045 |
IRBuilder<> Builder(BB); |
| 2046 |
|
| 2047 |
llvm::Value *Src = transValue(BC->getSource(), F, BB); |
| 2048 |
CallInst *CI = |
| 2049 |
Builder.CreateMemCpy(Dst, Align, Src, SrcAlign, Size, IsVolatile); |
| 2050 |
if (isFuncNoUnwind()) |
| 2051 |
CI->getFunction()->addFnAttr(Attribute::NoUnwind); |
| 2052 |
return mapValue(BV, CI); |
| 2053 |
} |
| 2054 |
|
| 2055 |
case OpSelect: { |
| 2056 |
SPIRVSelect *BS = static_cast<SPIRVSelect *>(BV); |
| 2057 |
IRBuilder<> Builder(*Context); |
| 2058 |
if (BB) { |
| 2059 |
Builder.SetInsertPoint(BB); |
| 2060 |
} |
| 2061 |
return mapValue(BV, |
| 2062 |
Builder.CreateSelect(transValue(BS->getCondition(), F, BB), |
| 2063 |
transValue(BS->getTrueValue(), F, BB), |
| 2064 |
transValue(BS->getFalseValue(), F, BB), |
| 2065 |
BV->getName())); |
| 2066 |
} |
| 2067 |
|
| 2068 |
case OpLine: |
| 2069 |
case OpSelectionMerge: // OpenCL Compiler does not use this instruction |
| 2070 |
return nullptr; |
| 2071 |
|
| 2072 |
case OpLoopMerge: // Will be translated after all other function's |
| 2073 |
case OpLoopControlINTEL: // instructions are translated. |
| 2074 |
FuncLoopMetadataMap[BB] = BV; |
| 2075 |
return nullptr; |
| 2076 |
|
| 2077 |
case OpSwitch: { |
| 2078 |
auto BS = static_cast<SPIRVSwitch *>(BV); |
| 2079 |
auto Select = transValue(BS->getSelect(), F, BB); |
| 2080 |
auto LS = SwitchInst::Create( |
| 2081 |
Select, dyn_cast<BasicBlock>(transValue(BS->getDefault(), F, BB)), |
| 2082 |
BS->getNumPairs(), BB); |
| 2083 |
BS->foreachPair( |
| 2084 |
[&](SPIRVSwitch::LiteralTy Literals, SPIRVBasicBlock *Label) { |
| 2085 |
assert(!Literals.empty() && "Literals should not be empty"); |
| 2086 |
assert(Literals.size() <= 2 && |
| 2087 |
"Number of literals should not be more then two"); |
| 2088 |
uint64_t Literal = uint64_t(Literals.at(0)); |
| 2089 |
if (Literals.size() == 2) { |
| 2090 |
Literal += uint64_t(Literals.at(1)) << 32; |
| 2091 |
} |
| 2092 |
LS->addCase( |
| 2093 |
ConstantInt::get(cast<IntegerType>(Select->getType()), Literal), |
| 2094 |
cast<BasicBlock>(transValue(Label, F, BB))); |
| 2095 |
}); |
| 2096 |
return mapValue(BV, LS); |
| 2097 |
} |
| 2098 |
|
| 2099 |
case OpVectorTimesScalar: { |
| 2100 |
auto VTS = static_cast<SPIRVVectorTimesScalar *>(BV); |
| 2101 |
IRBuilder<> Builder(BB); |
| 2102 |
auto Scalar = transValue(VTS->getScalar(), F, BB); |
| 2103 |
auto Vector = transValue(VTS->getVector(), F, BB); |
| 2104 |
auto *VecTy = cast<FixedVectorType>(Vector->getType()); |
| 2105 |
unsigned VecSize = VecTy->getNumElements(); |
| 2106 |
auto NewVec = Builder.CreateVectorSplat(VecSize, Scalar, Scalar->getName()); |
| 2107 |
NewVec->takeName(Scalar); |
| 2108 |
auto Scale = Builder.CreateFMul(Vector, NewVec, "scale"); |
| 2109 |
return mapValue(BV, Scale); |
| 2110 |
} |
| 2111 |
|
| 2112 |
case OpVectorTimesMatrix: { |
| 2113 |
auto *VTM = static_cast<SPIRVVectorTimesMatrix *>(BV); |
| 2114 |
IRBuilder<> Builder(BB); |
| 2115 |
Value *Mat = transValue(VTM->getMatrix(), F, BB); |
| 2116 |
Value *Vec = transValue(VTM->getVector(), F, BB); |
| 2117 |
|
| 2118 |
// Vec is of N elements. |
| 2119 |
// Mat is of M columns and N rows. |
| 2120 |
// Mat consists of vectors: V_1, V_2, ..., V_M |
| 2121 |
// |
| 2122 |
// The product is: |
| 2123 |
// |
| 2124 |
// |------- M ----------| |
| 2125 |
// Result = sum ( {Vec_1, Vec_1, ..., Vec_1} * {V_1_1, V_2_1, ..., V_M_1}, |
| 2126 |
// {Vec_2, Vec_2, ..., Vec_2} * {V_1_2, V_2_2, ..., V_M_2}, |
| 2127 |
// ... |
| 2128 |
// {Vec_N, Vec_N, ..., Vec_N} * {V_1_N, V_2_N, ..., V_M_N}); |
| 2129 |
|
| 2130 |
unsigned M = Mat->getType()->getArrayNumElements(); |
| 2131 |
|
| 2132 |
auto *VecTy = cast<FixedVectorType>(Vec->getType()); |
| 2133 |
FixedVectorType *VTy = FixedVectorType::get(VecTy->getElementType(), M); |
| 2134 |
auto ETy = VTy->getElementType(); |
| 2135 |
unsigned N = VecTy->getNumElements(); |
| 2136 |
Value *V = Builder.CreateVectorSplat(M, ConstantFP::get(ETy, 0.0)); |
| 2137 |
|
| 2138 |
for (unsigned Idx = 0; Idx != N; ++Idx) { |
| 2139 |
Value *S = Builder.CreateExtractElement(Vec, Builder.getInt32(Idx)); |
| 2140 |
Value *Lhs = Builder.CreateVectorSplat(M, S); |
| 2141 |
Value *Rhs = UndefValue::get(VTy); |
| 2142 |
for (unsigned Idx2 = 0; Idx2 != M; ++Idx2) { |
| 2143 |
Value *Vx = Builder.CreateExtractValue(Mat, Idx2); |
| 2144 |
Value *Vxi = Builder.CreateExtractElement(Vx, Builder.getInt32(Idx)); |
| 2145 |
Rhs = Builder.CreateInsertElement(Rhs, Vxi, Builder.getInt32(Idx2)); |
| 2146 |
} |
| 2147 |
Value *Mul = Builder.CreateFMul(Lhs, Rhs); |
| 2148 |
V = Builder.CreateFAdd(V, Mul); |
| 2149 |
} |
| 2150 |
|
| 2151 |
return mapValue(BV, V); |
| 2152 |
} |
| 2153 |
|
| 2154 |
case OpMatrixTimesScalar: { |
| 2155 |
auto MTS = static_cast<SPIRVMatrixTimesScalar *>(BV); |
| 2156 |
IRBuilder<> Builder(BB); |
| 2157 |
auto Scalar = transValue(MTS->getScalar(), F, BB); |
| 2158 |
auto Matrix = transValue(MTS->getMatrix(), F, BB); |
| 2159 |
uint64_t ColNum = Matrix->getType()->getArrayNumElements(); |
| 2160 |
auto ColType = cast<ArrayType>(Matrix->getType())->getElementType(); |
| 2161 |
auto VecSize = cast<FixedVectorType>(ColType)->getNumElements(); |
| 2162 |
auto NewVec = Builder.CreateVectorSplat(VecSize, Scalar, Scalar->getName()); |
| 2163 |
NewVec->takeName(Scalar); |
| 2164 |
|
| 2165 |
Value *V = UndefValue::get(Matrix->getType()); |
| 2166 |
for (uint64_t Idx = 0; Idx != ColNum; Idx++) { |
| 2167 |
auto Col = Builder.CreateExtractValue(Matrix, Idx); |
| 2168 |
auto I = Builder.CreateFMul(Col, NewVec); |
| 2169 |
V = Builder.CreateInsertValue(V, I, Idx); |
| 2170 |
} |
| 2171 |
|
| 2172 |
return mapValue(BV, V); |
| 2173 |
} |
| 2174 |
|
| 2175 |
case OpMatrixTimesVector: { |
| 2176 |
auto *MTV = static_cast<SPIRVMatrixTimesVector *>(BV); |
| 2177 |
IRBuilder<> Builder(BB); |
| 2178 |
Value *Mat = transValue(MTV->getMatrix(), F, BB); |
| 2179 |
Value *Vec = transValue(MTV->getVector(), F, BB); |
| 2180 |
|
| 2181 |
// Result is similar to Matrix * Matrix |
| 2182 |
// Mat is of M columns and N rows. |
| 2183 |
// Mat consists of vectors: V_1, V_2, ..., V_M |
| 2184 |
// where each vector is of size N. |
| 2185 |
// |
| 2186 |
// Vec is of size M. |
| 2187 |
// The product is a vector of size N. |
| 2188 |
// |
| 2189 |
// |------- N ----------| |
| 2190 |
// Result = sum ( {Vec_1, Vec_1, ..., Vec_1} * V_1, |
| 2191 |
// {Vec_2, Vec_2, ..., Vec_2} * V_2, |
| 2192 |
// ... |
| 2193 |
// {Vec_M, Vec_M, ..., Vec_M} * V_N ); |
| 2194 |
// |
| 2195 |
// where sum is defined as vector sum. |
| 2196 |
|
| 2197 |
unsigned M = Mat->getType()->getArrayNumElements(); |
| 2198 |
FixedVectorType *VTy = cast<FixedVectorType>( |
| 2199 |
cast<ArrayType>(Mat->getType())->getElementType()); |
| 2200 |
unsigned N = VTy->getNumElements(); |
| 2201 |
auto ETy = VTy->getElementType(); |
| 2202 |
Value *V = Builder.CreateVectorSplat(N, ConstantFP::get(ETy, 0.0)); |
| 2203 |
|
| 2204 |
for (unsigned Idx = 0; Idx != M; ++Idx) { |
| 2205 |
Value *S = Builder.CreateExtractElement(Vec, Builder.getInt32(Idx)); |
| 2206 |
Value *Lhs = Builder.CreateVectorSplat(N, S); |
| 2207 |
Value *Vx = Builder.CreateExtractValue(Mat, Idx); |
| 2208 |
Value *Mul = Builder.CreateFMul(Lhs, Vx); |
| 2209 |
V = Builder.CreateFAdd(V, Mul); |
| 2210 |
} |
| 2211 |
|
| 2212 |
return mapValue(BV, V); |
| 2213 |
} |
| 2214 |
|
| 2215 |
case OpMatrixTimesMatrix: { |
| 2216 |
auto *MTM = static_cast<SPIRVMatrixTimesMatrix *>(BV); |
| 2217 |
IRBuilder<> Builder(BB); |
| 2218 |
Value *M1 = transValue(MTM->getLeftMatrix(), F, BB); |
| 2219 |
Value *M2 = transValue(MTM->getRightMatrix(), F, BB); |
| 2220 |
|
| 2221 |
// Each matrix consists of a list of columns. |
| 2222 |
// M1 (the left matrix) is of C1 columns and R1 rows. |
| 2223 |
// M1 consists of a list of vectors: V_1, V_2, ..., V_C1 |
| 2224 |
// where V_x are vectors of size R1. |
| 2225 |
// |
| 2226 |
// M2 (the right matrix) is of C2 columns and R2 rows. |
| 2227 |
// M2 consists of a list of vectors: U_1, U_2, ..., U_C2 |
| 2228 |
// where U_x are vectors of size R2. |
| 2229 |
// |
| 2230 |
// Now M1 * M2 requires C1 == R2. |
| 2231 |
// The result is a matrix of C2 columns and R1 rows. |
| 2232 |
// That is, consists of C2 vectors of size R1. |
| 2233 |
// |
| 2234 |
// M1 * M2 algorithm is as below: |
| 2235 |
// |
| 2236 |
// Result = { dot_product(U_1, M1), |
| 2237 |
// dot_product(U_2, M1), |
| 2238 |
// ... |
| 2239 |
// dot_product(U_C2, M1) }; |
| 2240 |
// where |
| 2241 |
// dot_product (U, M) is defined as: |
| 2242 |
// |
| 2243 |
// |-------- C1 ------| |
| 2244 |
// Result = sum ( {U[1], U[1], ..., U[1]} * V_1, |
| 2245 |
// {U[2], U[2], ..., U[2]} * V_2, |
| 2246 |
// ... |
| 2247 |
// {U[R2], U[R2], ..., U[R2]} * V_C1 ); |
| 2248 |
// Note that C1 == R2 |
| 2249 |
// sum is defined as vector sum. |
| 2250 |
|
| 2251 |
unsigned C1 = M1->getType()->getArrayNumElements(); |
| 2252 |
unsigned C2 = M2->getType()->getArrayNumElements(); |
| 2253 |
FixedVectorType *V1Ty = |
| 2254 |
cast<FixedVectorType>(cast<ArrayType>(M1->getType())->getElementType()); |
| 2255 |
FixedVectorType *V2Ty = |
| 2256 |
cast<FixedVectorType>(cast<ArrayType>(M2->getType())->getElementType()); |
| 2257 |
unsigned R1 = V1Ty->getNumElements(); |
| 2258 |
unsigned R2 = V2Ty->getNumElements(); |
| 2259 |
auto ETy = V1Ty->getElementType(); |
| 2260 |
|
| 2261 |
(void)C1; |
| 2262 |
assert(C1 == R2 && "Unmatched matrix"); |
| 2263 |
|
| 2264 |
auto VTy = FixedVectorType::get(ETy, R1); |
| 2265 |
auto ResultTy = ArrayType::get(VTy, C2); |
| 2266 |
|
| 2267 |
Value *Res = UndefValue::get(ResultTy); |
| 2268 |
|
| 2269 |
for (unsigned Idx = 0; Idx != C2; ++Idx) { |
| 2270 |
Value *U = Builder.CreateExtractValue(M2, Idx); |
| 2271 |
|
| 2272 |
// Calculate dot_product(U, M1) |
| 2273 |
Value *Dot = Builder.CreateVectorSplat(R1, ConstantFP::get(ETy, 0.0)); |
| 2274 |
|
| 2275 |
for (unsigned Idx2 = 0; Idx2 != R2; ++Idx2) { |
| 2276 |
Value *Ux = Builder.CreateExtractElement(U, Builder.getInt32(Idx2)); |
| 2277 |
Value *Lhs = Builder.CreateVectorSplat(R1, Ux); |
| 2278 |
Value *Rhs = Builder.CreateExtractValue(M1, Idx2); |
| 2279 |
Value *Mul = Builder.CreateFMul(Lhs, Rhs); |
| 2280 |
Dot = Builder.CreateFAdd(Dot, Mul); |
| 2281 |
} |
| 2282 |
|
| 2283 |
Res = Builder.CreateInsertValue(Res, Dot, Idx); |
| 2284 |
} |
| 2285 |
|
| 2286 |
return mapValue(BV, Res); |
| 2287 |
} |
| 2288 |
|
| 2289 |
case OpTranspose: { |
| 2290 |
auto TR = static_cast<SPIRVTranspose *>(BV); |
| 2291 |
IRBuilder<> Builder(BB); |
| 2292 |
auto Matrix = transValue(TR->getMatrix(), F, BB); |
| 2293 |
unsigned ColNum = Matrix->getType()->getArrayNumElements(); |
| 2294 |
FixedVectorType *ColTy = cast<FixedVectorType>( |
| 2295 |
cast<ArrayType>(Matrix->getType())->getElementType()); |
| 2296 |
unsigned RowNum = ColTy->getNumElements(); |
| 2297 |
|
| 2298 |
auto VTy = FixedVectorType::get(ColTy->getElementType(), ColNum); |
| 2299 |
auto ResultTy = ArrayType::get(VTy, RowNum); |
| 2300 |
Value *V = UndefValue::get(ResultTy); |
| 2301 |
|
| 2302 |
SmallVector<Value *, 16> MCache; |
| 2303 |
MCache.reserve(ColNum); |
| 2304 |
for (unsigned Idx = 0; Idx != ColNum; ++Idx) |
| 2305 |
MCache.push_back(Builder.CreateExtractValue(Matrix, Idx)); |
| 2306 |
|
| 2307 |
if (ColNum == RowNum) { |
| 2308 |
// Fastpath |
| 2309 |
switch (ColNum) { |
| 2310 |
case 2: { |
| 2311 |
Value *V1 = Builder.CreateShuffleVector(MCache[0], MCache[1], |
| 2312 |
ArrayRef<int>{0, 2}); |
| 2313 |
V = Builder.CreateInsertValue(V, V1, 0); |
| 2314 |
Value *V2 = Builder.CreateShuffleVector(MCache[0], MCache[1], |
| 2315 |
ArrayRef<int>{1, 3}); |
| 2316 |
V = Builder.CreateInsertValue(V, V2, 1); |
| 2317 |
return mapValue(BV, V); |
| 2318 |
} |
| 2319 |
|
| 2320 |
case 4: { |
| 2321 |
for (int Idx = 0; Idx < 4; ++Idx) { |
| 2322 |
Value *V1 = Builder.CreateShuffleVector( |
| 2323 |
MCache[0], MCache[1], ArrayRef<int>{Idx, Idx + 4}); |
| 2324 |
Value *V2 = Builder.CreateShuffleVector( |
| 2325 |
MCache[2], MCache[3], ArrayRef<int>{Idx, Idx + 4}); |
| 2326 |
Value *V3 = |
| 2327 |
Builder.CreateShuffleVector(V1, V2, ArrayRef<int>{0, 1, 2, 3}); |
| 2328 |
V = Builder.CreateInsertValue(V, V3, Idx); |
| 2329 |
} |
| 2330 |
return mapValue(BV, V); |
| 2331 |
} |
| 2332 |
|
| 2333 |
default: |
| 2334 |
break; |
| 2335 |
} |
| 2336 |
} |
| 2337 |
|
| 2338 |
// Slowpath |
| 2339 |
for (unsigned Idx = 0; Idx != RowNum; ++Idx) { |
| 2340 |
Value *Vec = UndefValue::get(VTy); |
| 2341 |
|
| 2342 |
for (unsigned Idx2 = 0; Idx2 != ColNum; ++Idx2) { |
| 2343 |
Value *S = |
| 2344 |
Builder.CreateExtractElement(MCache[Idx2], Builder.getInt32(Idx)); |
| 2345 |
Vec = Builder.CreateInsertElement(Vec, S, Idx2); |
| 2346 |
} |
| 2347 |
|
| 2348 |
V = Builder.CreateInsertValue(V, Vec, Idx); |
| 2349 |
} |
| 2350 |
|
| 2351 |
return mapValue(BV, V); |
| 2352 |
} |
| 2353 |
|
| 2354 |
case OpCopyObject: { |
| 2355 |
SPIRVCopyObject *CO = static_cast<SPIRVCopyObject *>(BV); |
| 2356 |
auto *Ty = transType(CO->getOperand()->getType()); |
| 2357 |
AllocaInst *AI = new AllocaInst(Ty, 0, "", BB); |
| 2358 |
new StoreInst(transValue(CO->getOperand(), F, BB), AI, BB); |
| 2359 |
LoadInst *LI = new LoadInst(Ty, AI, "", BB); |
| 2360 |
return mapValue(BV, LI); |
| 2361 |
} |
| 2362 |
case OpCopyLogical: { |
| 2363 |
SPIRVCopyLogical *CL = static_cast<SPIRVCopyLogical *>(BV); |
| 2364 |
|
| 2365 |
auto *SrcTy = transType(CL->getOperand()->getType()); |
| 2366 |
auto *DstTy = transType(CL->getType()); |
| 2367 |
|
| 2368 |
assert(M->getDataLayout().getTypeStoreSize(SrcTy).getFixedValue() == |
| 2369 |
M->getDataLayout().getTypeStoreSize(DstTy).getFixedValue() && |
| 2370 |
"Size mismatch in OpCopyLogical"); |
| 2371 |
|
| 2372 |
IRBuilder<> Builder(BB); |
| 2373 |
|
| 2374 |
auto *SrcAI = Builder.CreateAlloca(SrcTy); |
| 2375 |
Builder.CreateAlignedStore(transValue(CL->getOperand(), F, BB), SrcAI, |
| 2376 |
SrcAI->getAlign()); |
| 2377 |
|
| 2378 |
auto *LI = Builder.CreateAlignedLoad(DstTy, SrcAI, SrcAI->getAlign()); |
| 2379 |
return mapValue(BV, LI); |
| 2380 |
} |
| 2381 |
|
| 2382 |
case OpAccessChain: |
| 2383 |
case OpInBoundsAccessChain: |
| 2384 |
case OpPtrAccessChain: |
| 2385 |
case OpInBoundsPtrAccessChain: { |
| 2386 |
auto AC = static_cast<SPIRVAccessChainBase *>(BV); |
| 2387 |
auto Base = transValue(AC->getBase(), F, BB); |
| 2388 |
SPIRVType *BaseSPVTy = AC->getBase()->getType(); |
| 2389 |
if (BaseSPVTy->isTypePointer() && |
| 2390 |
BaseSPVTy->getPointerElementType()->isTypeCooperativeMatrixKHR()) { |
| 2391 |
return mapValue(BV, transSPIRVBuiltinFromInst(AC, BB)); |
| 2392 |
} |
| 2393 |
Type *BaseTy = |
| 2394 |
BaseSPVTy->isTypeVector() |
| 2395 |
? transType( |
| 2396 |
BaseSPVTy->getVectorComponentType()->getPointerElementType()) |
| 2397 |
: transType(BaseSPVTy->getPointerElementType()); |
| 2398 |
auto Index = transValue(AC->getIndices(), F, BB); |
| 2399 |
if (!AC->hasPtrIndex()) |
| 2400 |
Index.insert(Index.begin(), getInt32(M, 0)); |
| 2401 |
auto IsInbound = AC->isInBounds(); |
| 2402 |
Value *V = nullptr; |
| 2403 |
|
| 2404 |
if (GEPOrUseMap.count(Base)) { |
| 2405 |
auto IdxToInstMap = GEPOrUseMap[Base]; |
| 2406 |
auto Idx = AC->getIndices(); |
| 2407 |
|
| 2408 |
// In transIntelFPGADecorations we generated GEPs only for the fields of |
| 2409 |
// structure, meaning that GEP to `0` accesses the Structure itself, and |
| 2410 |
// the second `Id` is a Key in the map. |
| 2411 |
if (Idx.size() == 2) { |
| 2412 |
unsigned Idx1 = static_cast<ConstantInt *>(getTranslatedValue(Idx[0])) |
| 2413 |
->getZExtValue(); |
| 2414 |
if (Idx1 == 0) { |
| 2415 |
unsigned Idx2 = static_cast<ConstantInt *>(getTranslatedValue(Idx[1])) |
| 2416 |
->getZExtValue(); |
| 2417 |
|
| 2418 |
// If we already have the instruction in a map, use it. |
| 2419 |
if (IdxToInstMap.count(Idx2)) |
| 2420 |
return mapValue(BV, IdxToInstMap[Idx2]); |
| 2421 |
} |
| 2422 |
} |
| 2423 |
} |
| 2424 |
|
| 2425 |
if (BB) { |
| 2426 |
auto GEP = |
| 2427 |
GetElementPtrInst::Create(BaseTy, Base, Index, BV->getName(), BB); |
| 2428 |
GEP->setIsInBounds(IsInbound); |
| 2429 |
V = GEP; |
| 2430 |
} else { |
| 2431 |
V = ConstantExpr::getGetElementPtr(BaseTy, dyn_cast<Constant>(Base), |
| 2432 |
Index, IsInbound); |
| 2433 |
} |
| 2434 |
return mapValue(BV, V); |
| 2435 |
} |
| 2436 |
|
| 2437 |
case OpPtrEqual: |
| 2438 |
case OpPtrNotEqual: { |
| 2439 |
auto *BC = static_cast<SPIRVBinary *>(BV); |
| 2440 |
auto Ops = transValue(BC->getOperands(), F, BB); |
| 2441 |
|
| 2442 |
IRBuilder<> Builder(BB); |
| 2443 |
Value *Op1 = Builder.CreatePtrToInt(Ops[0], Type::getInt64Ty(*Context)); |
| 2444 |
Value *Op2 = Builder.CreatePtrToInt(Ops[1], Type::getInt64Ty(*Context)); |
| 2445 |
CmpInst::Predicate P = |
| 2446 |
OC == OpPtrEqual ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
| 2447 |
Value *V = Builder.CreateICmp(P, Op1, Op2); |
| 2448 |
return mapValue(BV, V); |
| 2449 |
} |
| 2450 |
|
| 2451 |
case OpPtrDiff: { |
| 2452 |
auto *BC = static_cast<SPIRVBinary *>(BV); |
| 2453 |
auto Ops = transValue(BC->getOperands(), F, BB); |
| 2454 |
IRBuilder<> Builder(BB); |
| 2455 |
Type *ElemTy = |
| 2456 |
transType(BC->getOperands()[0]->getType()->getPointerElementType()); |
| 2457 |
Value *V = Builder.CreatePtrDiff(ElemTy, Ops[0], Ops[1]); |
| 2458 |
return mapValue(BV, V); |
| 2459 |
} |
| 2460 |
|
| 2461 |
case OpCompositeConstruct: { |
| 2462 |
auto CC = static_cast<SPIRVCompositeConstruct *>(BV); |
| 2463 |
auto Constituents = transValue(CC->getOperands(), F, BB); |
| 2464 |
std::vector<Constant *> CV; |
| 2465 |
bool HasRtValues = false; |
| 2466 |
for (const auto &I : Constituents) { |
| 2467 |
auto *C = dyn_cast<Constant>(I); |
| 2468 |
CV.push_back(C); |
| 2469 |
if (!HasRtValues && C == nullptr) |
| 2470 |
HasRtValues = true; |
| 2471 |
} |
| 2472 |
|
| 2473 |
switch (static_cast<size_t>(BV->getType()->getOpCode())) { |
| 2474 |
case OpTypeVector: { |
| 2475 |
if (!HasRtValues) |
| 2476 |
return mapValue(BV, ConstantVector::get(CV)); |
| 2477 |
|
| 2478 |
auto *VT = cast<FixedVectorType>(transType(CC->getType())); |
| 2479 |
Value *NewVec = ConstantVector::getSplat( |
| 2480 |
VT->getElementCount(), PoisonValue::get(VT->getElementType())); |
| 2481 |
|
| 2482 |
for (size_t I = 0; I < Constituents.size(); I++) { |
| 2483 |
NewVec = InsertElementInst::Create(NewVec, Constituents[I], |
| 2484 |
getInt32(M, I), "", BB); |
| 2485 |
} |
| 2486 |
return mapValue(BV, NewVec); |
| 2487 |
} |
| 2488 |
case OpTypeArray: { |
| 2489 |
auto *AT = cast<ArrayType>(transType(CC->getType())); |
| 2490 |
if (!HasRtValues) |
| 2491 |
return mapValue(BV, ConstantArray::get(AT, CV)); |
| 2492 |
|
| 2493 |
AllocaInst *Alloca = new AllocaInst(AT, SPIRAS_Private, "", BB); |
| 2494 |
|
| 2495 |
// get pointer to the element of the array |
| 2496 |
// store the result of argument |
| 2497 |
for (size_t I = 0; I < Constituents.size(); I++) { |
| 2498 |
auto *GEP = GetElementPtrInst::Create( |
| 2499 |
AT, Alloca, {getInt32(M, 0), getInt32(M, I)}, "gep", BB); |
| 2500 |
GEP->setIsInBounds(true); |
| 2501 |
new StoreInst(Constituents[I], GEP, false, BB); |
| 2502 |
} |
| 2503 |
|
| 2504 |
auto *Load = new LoadInst(AT, Alloca, "load", false, BB); |
| 2505 |
return mapValue(BV, Load); |
| 2506 |
} |
| 2507 |
case OpTypeStruct: { |
| 2508 |
auto *ST = cast<StructType>(transType(CC->getType())); |
| 2509 |
if (!HasRtValues) |
| 2510 |
return mapValue(BV, ConstantStruct::get(ST, CV)); |
| 2511 |
|
| 2512 |
AllocaInst *Alloca = new AllocaInst(ST, SPIRAS_Private, "", BB); |
| 2513 |
|
| 2514 |
// get pointer to the element of structure |
| 2515 |
// store the result of argument |
| 2516 |
for (size_t I = 0; I < Constituents.size(); I++) { |
| 2517 |
auto *GEP = GetElementPtrInst::Create( |
| 2518 |
ST, Alloca, {getInt32(M, 0), getInt32(M, I)}, "gep", BB); |
| 2519 |
GEP->setIsInBounds(true); |
| 2520 |
new StoreInst(Constituents[I], GEP, false, BB); |
| 2521 |
} |
| 2522 |
|
| 2523 |
auto *Load = new LoadInst(ST, Alloca, "load", false, BB); |
| 2524 |
return mapValue(BV, Load); |
| 2525 |
} |
| 2526 |
case internal::OpTypeJointMatrixINTEL: |
| 2527 |
case OpTypeCooperativeMatrixKHR: |
| 2528 |
return mapValue(BV, transSPIRVBuiltinFromInst(CC, BB)); |
| 2529 |
default: |
| 2530 |
llvm_unreachable("Unhandled type!"); |
| 2531 |
} |
| 2532 |
} |
| 2533 |
|
| 2534 |
case OpCompositeExtract: { |
| 2535 |
SPIRVCompositeExtract *CE = static_cast<SPIRVCompositeExtract *>(BV); |
| 2536 |
IRBuilder<> Builder(*Context); |
| 2537 |
if (BB) { |
| 2538 |
Builder.SetInsertPoint(BB); |
| 2539 |
} |
| 2540 |
if (CE->getComposite()->getType()->isTypeVector()) { |
| 2541 |
assert(CE->getIndices().size() == 1 && "Invalid index"); |
| 2542 |
return mapValue( |
| 2543 |
BV, Builder.CreateExtractElement( |
| 2544 |
transValue(CE->getComposite(), F, BB), |
| 2545 |
ConstantInt::get(*Context, APInt(32, CE->getIndices()[0])), |
| 2546 |
BV->getName())); |
| 2547 |
} |
| 2548 |
return mapValue( |
| 2549 |
BV, Builder.CreateExtractValue(transValue(CE->getComposite(), F, BB), |
| 2550 |
CE->getIndices(), BV->getName())); |
| 2551 |
} |
| 2552 |
|
| 2553 |
case OpVectorExtractDynamic: { |
| 2554 |
auto *VED = static_cast<SPIRVVectorExtractDynamic *>(BV); |
| 2555 |
SPIRVValue *Vec = VED->getVector(); |
| 2556 |
if (Vec->getType()->getOpCode() == internal::OpTypeJointMatrixINTEL) { |
| 2557 |
return mapValue(BV, transSPIRVBuiltinFromInst(VED, BB)); |
| 2558 |
} |
| 2559 |
return mapValue( |
| 2560 |
BV, ExtractElementInst::Create(transValue(Vec, F, BB), |
| 2561 |
transValue(VED->getIndex(), F, BB), |
| 2562 |
BV->getName(), BB)); |
| 2563 |
} |
| 2564 |
|
| 2565 |
case OpCompositeInsert: { |
| 2566 |
auto CI = static_cast<SPIRVCompositeInsert *>(BV); |
| 2567 |
IRBuilder<> Builder(*Context); |
| 2568 |
if (BB) { |
| 2569 |
Builder.SetInsertPoint(BB); |
| 2570 |
} |
| 2571 |
if (CI->getComposite()->getType()->isTypeVector()) { |
| 2572 |
assert(CI->getIndices().size() == 1 && "Invalid index"); |
| 2573 |
return mapValue( |
| 2574 |
BV, Builder.CreateInsertElement( |
| 2575 |
transValue(CI->getComposite(), F, BB), |
| 2576 |
transValue(CI->getObject(), F, BB), |
| 2577 |
ConstantInt::get(*Context, APInt(32, CI->getIndices()[0])), |
| 2578 |
BV->getName())); |
| 2579 |
} |
| 2580 |
return mapValue( |
| 2581 |
BV, Builder.CreateInsertValue(transValue(CI->getComposite(), F, BB), |
| 2582 |
transValue(CI->getObject(), F, BB), |
| 2583 |
CI->getIndices(), BV->getName())); |
| 2584 |
} |
| 2585 |
|
| 2586 |
case OpVectorInsertDynamic: { |
| 2587 |
auto *VID = static_cast<SPIRVVectorInsertDynamic *>(BV); |
| 2588 |
SPIRVValue *Vec = VID->getVector(); |
| 2589 |
if (Vec->getType()->getOpCode() == internal::OpTypeJointMatrixINTEL) { |
| 2590 |
return mapValue(BV, transSPIRVBuiltinFromInst(VID, BB)); |
| 2591 |
} |
| 2592 |
return mapValue( |
| 2593 |
BV, InsertElementInst::Create( |
| 2594 |
transValue(Vec, F, BB), transValue(VID->getComponent(), F, BB), |
| 2595 |
transValue(VID->getIndex(), F, BB), BV->getName(), BB)); |
| 2596 |
} |
| 2597 |
|
| 2598 |
case OpVectorShuffle: { |
| 2599 |
auto VS = static_cast<SPIRVVectorShuffle *>(BV); |
| 2600 |
std::vector<Constant *> Components; |
| 2601 |
IntegerType *Int32Ty = IntegerType::get(*Context, 32); |
| 2602 |
for (auto I : VS->getComponents()) { |
| 2603 |
if (I == static_cast<SPIRVWord>(-1)) |
| 2604 |
Components.push_back(UndefValue::get(Int32Ty)); |
| 2605 |
else |
| 2606 |
Components.push_back(ConstantInt::get(Int32Ty, I)); |
| 2607 |
} |
| 2608 |
IRBuilder<> Builder(*Context); |
| 2609 |
if (BB) { |
| 2610 |
Builder.SetInsertPoint(BB); |
| 2611 |
} |
| 2612 |
Value *Vec1 = transValue(VS->getVector1(), F, BB); |
| 2613 |
Value *Vec2 = transValue(VS->getVector2(), F, BB); |
| 2614 |
auto *Vec1Ty = cast<FixedVectorType>(Vec1->getType()); |
| 2615 |
auto *Vec2Ty = cast<FixedVectorType>(Vec2->getType()); |
| 2616 |
if (Vec1Ty->getNumElements() != Vec2Ty->getNumElements()) { |
| 2617 |
// LLVM's shufflevector requires that the two vector operands have the |
| 2618 |
// same type; SPIR-V's OpVectorShuffle allows the vector operands to |
| 2619 |
// differ in the number of components. Adjust for that by extending |
| 2620 |
// the smaller vector. |
| 2621 |
if (Vec1Ty->getNumElements() < Vec2Ty->getNumElements()) { |
| 2622 |
Vec1 = extendVector(Vec1, Vec2Ty, Builder); |
| 2623 |
// Extending Vec1 requires offsetting any Vec2 indices in Components by |
| 2624 |
// the number of new elements. |
| 2625 |
unsigned Offset = Vec2Ty->getNumElements() - Vec1Ty->getNumElements(); |
| 2626 |
unsigned Vec2Start = Vec1Ty->getNumElements(); |
| 2627 |
for (auto &C : Components) { |
| 2628 |
if (auto *CI = dyn_cast<ConstantInt>(C)) { |
| 2629 |
uint64_t V = CI->getZExtValue(); |
| 2630 |
if (V >= Vec2Start) { |
| 2631 |
// This is a Vec2 index; add the offset to it. |
| 2632 |
C = ConstantInt::get(Int32Ty, V + Offset); |
| 2633 |
} |
| 2634 |
} |
| 2635 |
} |
| 2636 |
} else { |
| 2637 |
Vec2 = extendVector(Vec2, Vec1Ty, Builder); |
| 2638 |
} |
| 2639 |
} |
| 2640 |
return mapValue( |
| 2641 |
BV, Builder.CreateShuffleVector( |
| 2642 |
Vec1, Vec2, ConstantVector::get(Components), BV->getName())); |
| 2643 |
} |
| 2644 |
|
| 2645 |
case OpBitReverse: { |
| 2646 |
auto *BR = static_cast<SPIRVUnary *>(BV); |
| 2647 |
auto Ty = transType(BV->getType()); |
| 2648 |
Function *intr = |
| 2649 |
Intrinsic::getDeclaration(M, llvm::Intrinsic::bitreverse, Ty); |
| 2650 |
auto *Call = CallInst::Create(intr, transValue(BR->getOperand(0), F, BB), |
| 2651 |
BR->getName(), BB); |
| 2652 |
return mapValue(BV, Call); |
| 2653 |
} |
| 2654 |
|
| 2655 |
case OpFunctionCall: { |
| 2656 |
SPIRVFunctionCall *BC = static_cast<SPIRVFunctionCall *>(BV); |
| 2657 |
auto Call = CallInst::Create(transFunction(BC->getFunction()), |
| 2658 |
transValue(BC->getArgumentValues(), F, BB), |
| 2659 |
BC->getName(), BB); |
| 2660 |
setCallingConv(Call); |
| 2661 |
setAttrByCalledFunc(Call); |
| 2662 |
return mapValue(BV, Call); |
| 2663 |
} |
| 2664 |
|
| 2665 |
case OpAsmCallINTEL: |
| 2666 |
return mapValue( |
| 2667 |
BV, transAsmCallINTEL(static_cast<SPIRVAsmCallINTEL *>(BV), F, BB)); |
| 2668 |
|
| 2669 |
case OpFunctionPointerCallINTEL: { |
| 2670 |
SPIRVFunctionPointerCallINTEL *BC = |
| 2671 |
static_cast<SPIRVFunctionPointerCallINTEL *>(BV); |
| 2672 |
auto *V = transValue(BC->getCalledValue(), F, BB); |
| 2673 |
auto *SpirvFnTy = BC->getCalledValue()->getType()->getPointerElementType(); |
| 2674 |
auto *FnTy = cast<FunctionType>(transType(SpirvFnTy)); |
| 2675 |
auto *Call = CallInst::Create( |
| 2676 |
FnTy, V, transValue(BC->getArgumentValues(), F, BB), BC->getName(), BB); |
| 2677 |
transFunctionPointerCallArgumentAttributes( |
| 2678 |
BV, Call, static_cast<SPIRVTypeFunction *>(SpirvFnTy)); |
| 2679 |
// Assuming we are calling a regular device function |
| 2680 |
Call->setCallingConv(CallingConv::SPIR_FUNC); |
| 2681 |
// Don't set attributes, because at translation time we don't know which |
| 2682 |
// function exactly we are calling. |
| 2683 |
return mapValue(BV, Call); |
| 2684 |
} |
| 2685 |
|
| 2686 |
case OpAssumeTrueKHR: { |
| 2687 |
IRBuilder<> Builder(BB); |
| 2688 |
SPIRVAssumeTrueKHR *BC = static_cast<SPIRVAssumeTrueKHR *>(BV); |
| 2689 |
Value *Condition = transValue(BC->getCondition(), F, BB); |
| 2690 |
return mapValue(BV, Builder.CreateAssumption(Condition)); |
| 2691 |
} |
| 2692 |
|
| 2693 |
case OpExpectKHR: { |
| 2694 |
IRBuilder<> Builder(BB); |
| 2695 |
SPIRVExpectKHRInstBase *BC = static_cast<SPIRVExpectKHRInstBase *>(BV); |
| 2696 |
Type *RetTy = transType(BC->getType()); |
| 2697 |
Value *Val = transValue(BC->getOperand(0), F, BB); |
| 2698 |
Value *ExpVal = transValue(BC->getOperand(1), F, BB); |
| 2699 |
return mapValue( |
| 2700 |
BV, Builder.CreateIntrinsic(Intrinsic::expect, RetTy, {Val, ExpVal})); |
| 2701 |
} |
| 2702 |
|
| 2703 |
case OpExtInst: { |
| 2704 |
auto *ExtInst = static_cast<SPIRVExtInst *>(BV); |
| 2705 |
switch (ExtInst->getExtSetKind()) { |
| 2706 |
case SPIRVEIS_OpenCL: { |
| 2707 |
auto *V = mapValue(BV, transOCLBuiltinFromExtInst(ExtInst, BB)); |
| 2708 |
applyFPFastMathModeDecorations(BV, static_cast<Instruction *>(V)); |
| 2709 |
return V; |
| 2710 |
} |
| 2711 |
case SPIRVEIS_Debug: |
| 2712 |
case SPIRVEIS_OpenCL_DebugInfo_100: |
| 2713 |
case SPIRVEIS_NonSemantic_Shader_DebugInfo_100: |
| 2714 |
case SPIRVEIS_NonSemantic_Shader_DebugInfo_200: |
| 2715 |
return mapValue(BV, DbgTran->transDebugIntrinsic(ExtInst, BB)); |
| 2716 |
default: |
| 2717 |
llvm_unreachable("Unknown extended instruction set!"); |
| 2718 |
} |
| 2719 |
} |
| 2720 |
|
| 2721 |
case OpSNegate: { |
| 2722 |
IRBuilder<> Builder(*Context); |
| 2723 |
if (BB) { |
| 2724 |
Builder.SetInsertPoint(BB); |
| 2725 |
} |
| 2726 |
SPIRVUnary *BC = static_cast<SPIRVUnary *>(BV); |
| 2727 |
if (BV->getType()->isTypeCooperativeMatrixKHR()) { |
| 2728 |
return mapValue(BV, transSPIRVBuiltinFromInst(BC, BB)); |
| 2729 |
} |
| 2730 |
auto Neg = |
| 2731 |
Builder.CreateNeg(transValue(BC->getOperand(0), F, BB), BV->getName()); |
| 2732 |
if (auto *NegInst = dyn_cast<Instruction>(Neg)) { |
| 2733 |
applyNoIntegerWrapDecorations(BV, NegInst); |
| 2734 |
} |
| 2735 |
return mapValue(BV, Neg); |
| 2736 |
} |
| 2737 |
|
| 2738 |
case OpFMod: { |
| 2739 |
// translate OpFMod(a, b) to: |
| 2740 |
// r = frem(a, b) |
| 2741 |
// c = copysign(r, b) |
| 2742 |
// needs_fixing = islessgreater(r, c) |
| 2743 |
// result = needs_fixing ? r + b : c |
| 2744 |
IRBuilder<> Builder(BB); |
| 2745 |
SPIRVFMod *FMod = static_cast<SPIRVFMod *>(BV); |
| 2746 |
auto Dividend = transValue(FMod->getOperand(0), F, BB); |
| 2747 |
auto Divisor = transValue(FMod->getOperand(1), F, BB); |
| 2748 |
auto FRem = Builder.CreateFRem(Dividend, Divisor, "frem.res"); |
| 2749 |
auto CopySign = Builder.CreateBinaryIntrinsic( |
| 2750 |
llvm::Intrinsic::copysign, FRem, Divisor, nullptr, "copysign.res"); |
| 2751 |
auto FAdd = Builder.CreateFAdd(FRem, Divisor, "fadd.res"); |
| 2752 |
auto Cmp = Builder.CreateFCmpONE(FRem, CopySign, "cmp.res"); |
| 2753 |
auto Select = Builder.CreateSelect(Cmp, FAdd, CopySign); |
| 2754 |
return mapValue(BV, Select); |
| 2755 |
} |
| 2756 |
|
| 2757 |
case OpSMod: { |
| 2758 |
// translate OpSMod(a, b) to: |
| 2759 |
// r = srem(a, b) |
| 2760 |
// needs_fixing = ((a < 0) != (b < 0) && r != 0) |
| 2761 |
// result = needs_fixing ? r + b : r |
| 2762 |
IRBuilder<> Builder(*Context); |
| 2763 |
if (BB) { |
| 2764 |
Builder.SetInsertPoint(BB); |
| 2765 |
} |
| 2766 |
SPIRVSMod *SMod = static_cast<SPIRVSMod *>(BV); |
| 2767 |
auto Dividend = transValue(SMod->getOperand(0), F, BB); |
| 2768 |
auto Divisor = transValue(SMod->getOperand(1), F, BB); |
| 2769 |
auto SRem = Builder.CreateSRem(Dividend, Divisor, "srem.res"); |
| 2770 |
auto Xor = Builder.CreateXor(Dividend, Divisor, "xor.res"); |
| 2771 |
auto Zero = ConstantInt::getNullValue(Dividend->getType()); |
| 2772 |
auto CmpSign = Builder.CreateICmpSLT(Xor, Zero, "cmpsign.res"); |
| 2773 |
auto CmpSRem = Builder.CreateICmpNE(SRem, Zero, "cmpsrem.res"); |
| 2774 |
auto Add = Builder.CreateNSWAdd(SRem, Divisor, "add.res"); |
| 2775 |
auto Cmp = Builder.CreateAnd(CmpSign, CmpSRem, "cmp.res"); |
| 2776 |
auto Select = Builder.CreateSelect(Cmp, Add, SRem); |
| 2777 |
return mapValue(BV, Select); |
| 2778 |
} |
| 2779 |
|
| 2780 |
case OpFNegate: { |
| 2781 |
SPIRVUnary *BC = static_cast<SPIRVUnary *>(BV); |
| 2782 |
if (BV->getType()->isTypeCooperativeMatrixKHR()) { |
| 2783 |
return mapValue(BV, transSPIRVBuiltinFromInst(BC, BB)); |
| 2784 |
} |
| 2785 |
auto Neg = UnaryOperator::CreateFNeg(transValue(BC->getOperand(0), F, BB), |
| 2786 |
BV->getName(), BB); |
| 2787 |
applyFPFastMathModeDecorations(BV, Neg); |
| 2788 |
return mapValue(BV, Neg); |
| 2789 |
} |
| 2790 |
|
| 2791 |
case OpNot: |
| 2792 |
case OpLogicalNot: { |
| 2793 |
IRBuilder<> Builder(*Context); |
| 2794 |
if (BB) { |
| 2795 |
Builder.SetInsertPoint(BB); |
| 2796 |
} |
| 2797 |
SPIRVUnary *BC = static_cast<SPIRVUnary *>(BV); |
| 2798 |
return mapValue(BV, Builder.CreateNot(transValue(BC->getOperand(0), F, BB), |
| 2799 |
BV->getName())); |
| 2800 |
} |
| 2801 |
|
| 2802 |
case OpAll: |
| 2803 |
case OpAny: |
| 2804 |
return mapValue(BV, transAllAny(static_cast<SPIRVInstruction *>(BV), BB)); |
| 2805 |
|
| 2806 |
case OpIsFinite: |
| 2807 |
case OpIsInf: |
| 2808 |
case OpIsNan: |
| 2809 |
case OpIsNormal: |
| 2810 |
case OpSignBitSet: |
| 2811 |
return mapValue(BV, |
| 2812 |
transRelational(static_cast<SPIRVInstruction *>(BV), BB)); |
| 2813 |
case OpIAddCarry: { |
| 2814 |
auto *BC = static_cast<SPIRVBinary *>(BV); |
| 2815 |
return mapValue(BV, transBuiltinFromInst("__spirv_IAddCarry", BC, BB)); |
| 2816 |
} |
| 2817 |
case OpISubBorrow: { |
| 2818 |
auto *BC = static_cast<SPIRVBinary *>(BV); |
| 2819 |
return mapValue(BV, transBuiltinFromInst("__spirv_ISubBorrow", BC, BB)); |
| 2820 |
} |
| 2821 |
case OpSMulExtended: { |
| 2822 |
auto *BC = static_cast<SPIRVBinary *>(BV); |
| 2823 |
return mapValue(BV, transBuiltinFromInst("__spirv_SMulExtended", BC, BB)); |
| 2824 |
} |
| 2825 |
case OpUMulExtended: { |
| 2826 |
auto *BC = static_cast<SPIRVBinary *>(BV); |
| 2827 |
return mapValue(BV, transBuiltinFromInst("__spirv_UMulExtended", BC, BB)); |
| 2828 |
} |
| 2829 |
case OpGetKernelWorkGroupSize: |
| 2830 |
case OpGetKernelPreferredWorkGroupSizeMultiple: |
| 2831 |
return mapValue( |
| 2832 |
BV, transWGSizeQueryBI(static_cast<SPIRVInstruction *>(BV), BB)); |
| 2833 |
case OpGetKernelNDrangeMaxSubGroupSize: |
| 2834 |
case OpGetKernelNDrangeSubGroupCount: |
| 2835 |
return mapValue( |
| 2836 |
BV, transSGSizeQueryBI(static_cast<SPIRVInstruction *>(BV), BB)); |
| 2837 |
case OpFPGARegINTEL: { |
| 2838 |
IRBuilder<> Builder(BB); |
| 2839 |
|
| 2840 |
SPIRVFPGARegINTELInstBase *BC = |
| 2841 |
static_cast<SPIRVFPGARegINTELInstBase *>(BV); |
| 2842 |
|
| 2843 |
PointerType *Int8PtrTyPrivate = |
| 2844 |
Type::getInt8PtrTy(*Context, SPIRAS_Private); |
| 2845 |
IntegerType *Int32Ty = Type::getInt32Ty(*Context); |
| 2846 |
|
| 2847 |
Value *UndefInt8Ptr = UndefValue::get(Int8PtrTyPrivate); |
| 2848 |
Value *UndefInt32 = UndefValue::get(Int32Ty); |
| 2849 |
|
| 2850 |
Constant *GS = Builder.CreateGlobalStringPtr(kOCLBuiltinName::FPGARegIntel); |
| 2851 |
|
| 2852 |
Type *Ty = transType(BC->getType()); |
| 2853 |
Value *Val = transValue(BC->getOperand(0), F, BB); |
| 2854 |
|
| 2855 |
Value *ValAsArg = Val; |
| 2856 |
Type *RetTy = Ty; |
| 2857 |
auto IID = Intrinsic::annotation; |
| 2858 |
if (!isa<IntegerType>(Ty)) { |
| 2859 |
// All scalar types can be bitcasted to a same-sized integer |
| 2860 |
if (!isa<PointerType>(Ty) && !isa<StructType>(Ty)) { |
| 2861 |
RetTy = IntegerType::get(*Context, Ty->getPrimitiveSizeInBits()); |
| 2862 |
ValAsArg = Builder.CreateBitCast(Val, RetTy); |
| 2863 |
} |
| 2864 |
// If pointer type or struct type |
| 2865 |
else { |
| 2866 |
IID = Intrinsic::ptr_annotation; |
| 2867 |
auto *PtrTy = dyn_cast<PointerType>(Ty); |
| 2868 |
if (PtrTy && |
| 2869 |
(PtrTy->isOpaque() || |
| 2870 |
isa<IntegerType>(PtrTy->getNonOpaquePointerElementType()))) |
| 2871 |
RetTy = PtrTy; |
| 2872 |
// Whether a struct or a pointer to some other type, |
| 2873 |
// bitcast to i8* |
| 2874 |
else { |
| 2875 |
RetTy = Int8PtrTyPrivate; |
| 2876 |
ValAsArg = Builder.CreateBitCast(Val, RetTy); |
| 2877 |
} |
| 2878 |
Value *Args[] = {ValAsArg, GS, UndefInt8Ptr, UndefInt32, UndefInt8Ptr}; |
| 2879 |
auto *IntrinsicCall = |
| 2880 |
Builder.CreateIntrinsic(IID, {RetTy, GS->getType()}, Args); |
| 2881 |
return mapValue(BV, IntrinsicCall); |
| 2882 |
} |
| 2883 |
} |
| 2884 |
|
| 2885 |
Value *Args[] = {ValAsArg, GS, UndefInt8Ptr, UndefInt32}; |
| 2886 |
auto *IntrinsicCall = |
| 2887 |
Builder.CreateIntrinsic(IID, {RetTy, GS->getType()}, Args); |
| 2888 |
return mapValue(BV, IntrinsicCall); |
| 2889 |
} |
| 2890 |
|
| 2891 |
case OpFixedSqrtINTEL: |
| 2892 |
case OpFixedRecipINTEL: |
| 2893 |
case OpFixedRsqrtINTEL: |
| 2894 |
case OpFixedSinINTEL: |
| 2895 |
case OpFixedCosINTEL: |
| 2896 |
case OpFixedSinCosINTEL: |
| 2897 |
case OpFixedSinPiINTEL: |
| 2898 |
case OpFixedCosPiINTEL: |
| 2899 |
case OpFixedSinCosPiINTEL: |
| 2900 |
case OpFixedLogINTEL: |
| 2901 |
case OpFixedExpINTEL: |
| 2902 |
return mapValue( |
| 2903 |
BV, transFixedPointInst(static_cast<SPIRVInstruction *>(BV), BB)); |
| 2904 |
|
| 2905 |
case OpArbitraryFloatCastINTEL: |
| 2906 |
case OpArbitraryFloatCastFromIntINTEL: |
| 2907 |
case OpArbitraryFloatCastToIntINTEL: |
| 2908 |
case OpArbitraryFloatRecipINTEL: |
| 2909 |
case OpArbitraryFloatRSqrtINTEL: |
| 2910 |
case OpArbitraryFloatCbrtINTEL: |
| 2911 |
case OpArbitraryFloatSqrtINTEL: |
| 2912 |
case OpArbitraryFloatLogINTEL: |
| 2913 |
case OpArbitraryFloatLog2INTEL: |
| 2914 |
case OpArbitraryFloatLog10INTEL: |
| 2915 |
case OpArbitraryFloatLog1pINTEL: |
| 2916 |
case OpArbitraryFloatExpINTEL: |
| 2917 |
case OpArbitraryFloatExp2INTEL: |
| 2918 |
case OpArbitraryFloatExp10INTEL: |
| 2919 |
case OpArbitraryFloatExpm1INTEL: |
| 2920 |
case OpArbitraryFloatSinINTEL: |
| 2921 |
case OpArbitraryFloatCosINTEL: |
| 2922 |
case OpArbitraryFloatSinCosINTEL: |
| 2923 |
case OpArbitraryFloatSinPiINTEL: |
| 2924 |
case OpArbitraryFloatCosPiINTEL: |
| 2925 |
case OpArbitraryFloatSinCosPiINTEL: |
| 2926 |
case OpArbitraryFloatASinINTEL: |
| 2927 |
case OpArbitraryFloatASinPiINTEL: |
| 2928 |
case OpArbitraryFloatACosINTEL: |
| 2929 |
case OpArbitraryFloatACosPiINTEL: |
| 2930 |
case OpArbitraryFloatATanINTEL: |
| 2931 |
case OpArbitraryFloatATanPiINTEL: |
| 2932 |
return mapValue(BV, |
| 2933 |
transArbFloatInst(static_cast<SPIRVInstruction *>(BV), BB)); |
| 2934 |
|
| 2935 |
case OpArbitraryFloatAddINTEL: |
| 2936 |
case OpArbitraryFloatSubINTEL: |
| 2937 |
case OpArbitraryFloatMulINTEL: |
| 2938 |
case OpArbitraryFloatDivINTEL: |
| 2939 |
case OpArbitraryFloatGTINTEL: |
| 2940 |
case OpArbitraryFloatGEINTEL: |
| 2941 |
case OpArbitraryFloatLTINTEL: |
| 2942 |
case OpArbitraryFloatLEINTEL: |
| 2943 |
case OpArbitraryFloatEQINTEL: |
| 2944 |
case OpArbitraryFloatHypotINTEL: |
| 2945 |
case OpArbitraryFloatATan2INTEL: |
| 2946 |
case OpArbitraryFloatPowINTEL: |
| 2947 |
case OpArbitraryFloatPowRINTEL: |
| 2948 |
case OpArbitraryFloatPowNINTEL: |
| 2949 |
return mapValue( |
| 2950 |
BV, transArbFloatInst(static_cast<SPIRVInstruction *>(BV), BB, true)); |
| 2951 |
|
| 2952 |
case OpArithmeticFenceEXT: { |
| 2953 |
IRBuilder<> Builder(BB); |
| 2954 |
auto *BC = static_cast<SPIRVUnary *>(BV); |
| 2955 |
Type *RetTy = transType(BC->getType()); |
| 2956 |
Value *Val = transValue(BC->getOperand(0), F, BB); |
| 2957 |
return mapValue( |
| 2958 |
BV, Builder.CreateIntrinsic(Intrinsic::arithmetic_fence, RetTy, Val)); |
| 2959 |
} |
| 2960 |
case OpFmaKHR: { |
| 2961 |
IRBuilder<> Builder(BB); |
| 2962 |
auto *BC = static_cast<SPIRVFmaKHR *>(BV); |
| 2963 |
return mapValue( |
| 2964 |
BV, Builder.CreateIntrinsic(Intrinsic::fma, transType(BC->getType()), |
| 2965 |
{transValue(BC->getOperand(0), F, BB), |
| 2966 |
transValue(BC->getOperand(1), F, BB), |
| 2967 |
transValue(BC->getOperand(2), F, BB)})); |
| 2968 |
} |
| 2969 |
case internal::OpMaskedGatherINTEL: { |
| 2970 |
IRBuilder<> Builder(BB); |
| 2971 |
auto *Inst = static_cast<SPIRVMaskedGatherINTELInst *>(BV); |
| 2972 |
Type *RetTy = transType(Inst->getType()); |
| 2973 |
Value *PtrVector = transValue(Inst->getOperand(0), F, BB); |
| 2974 |
uint32_t Alignment = Inst->getOpWord(1); |
| 2975 |
Value *Mask = transValue(Inst->getOperand(2), F, BB); |
| 2976 |
Value *FillEmpty = transValue(Inst->getOperand(3), F, BB); |
| 2977 |
return mapValue(BV, Builder.CreateMaskedGather(RetTy, PtrVector, |
| 2978 |
Align(Alignment), Mask, |
| 2979 |
FillEmpty)); |
| 2980 |
} |
| 2981 |
|
| 2982 |
case internal::OpMaskedScatterINTEL: { |
| 2983 |
IRBuilder<> Builder(BB); |
| 2984 |
auto *Inst = static_cast<SPIRVMaskedScatterINTELInst *>(BV); |
| 2985 |
Value *InputVector = transValue(Inst->getOperand(0), F, BB); |
| 2986 |
Value *PtrVector = transValue(Inst->getOperand(1), F, BB); |
| 2987 |
uint32_t Alignment = Inst->getOpWord(2); |
| 2988 |
Value *Mask = transValue(Inst->getOperand(3), F, BB); |
| 2989 |
return mapValue(BV, Builder.CreateMaskedScatter(InputVector, PtrVector, |
| 2990 |
Align(Alignment), Mask)); |
| 2991 |
} |
| 2992 |
|
| 2993 |
default: { |
| 2994 |
auto OC = BV->getOpCode(); |
| 2995 |
if (isCmpOpCode(OC)) |
| 2996 |
return mapValue(BV, transCmpInst(BV, BB, F)); |
| 2997 |
|
| 2998 |
if (OCLSPIRVBuiltinMap::rfind(OC, nullptr)) |
| 2999 |
return mapValue(BV, transSPIRVBuiltinFromInst( |
| 3000 |
static_cast<SPIRVInstruction *>(BV), BB)); |
| 3001 |
|
| 3002 |
if (isBinaryShiftLogicalBitwiseOpCode(OC) || isLogicalOpCode(OC)) |
| 3003 |
return mapValue(BV, transShiftLogicalBitwiseInst(BV, BB, F)); |
| 3004 |
|
| 3005 |
if (isCvtOpCode(OC) && OC != OpGenericCastToPtrExplicit) { |
| 3006 |
auto BI = static_cast<SPIRVInstruction *>(BV); |
| 3007 |
Value *Inst = nullptr; |
| 3008 |
if (BI->hasFPRoundingMode() || BI->isSaturatedConversion()) { |
| 3009 |
Inst = transSPIRVBuiltinFromInst(BI, BB); |
| 3010 |
} else if (BI->getType()->isTypeCooperativeMatrixKHR()) { |
| 3011 |
// For cooperative matrix conversions generate __builtin_spirv |
| 3012 |
// conversions instead of __spirv_FConvert in case of mini-float |
| 3013 |
// type element type. |
| 3014 |
auto *OutMatrixElementTy = |
| 3015 |
static_cast<SPIRVTypeCooperativeMatrixKHR *>(BI->getType()) |
| 3016 |
->getCompType(); |
| 3017 |
auto *InMatrixElementTy = |
| 3018 |
static_cast<SPIRVTypeCooperativeMatrixKHR *>( |
| 3019 |
static_cast<SPIRVUnary *>(BI)->getOperand(0)->getType()) |
| 3020 |
->getCompType(); |
| 3021 |
if (OutMatrixElementTy->isTypeFloat(8, FPEncodingFloat8E4M3EXT) || |
| 3022 |
OutMatrixElementTy->isTypeFloat(8, FPEncodingFloat8E5M2EXT) || |
| 3023 |
InMatrixElementTy->isTypeFloat(8, FPEncodingFloat8E4M3EXT) || |
| 3024 |
InMatrixElementTy->isTypeFloat(8, FPEncodingFloat8E5M2EXT) || |
| 3025 |
OutMatrixElementTy->isTypeFloat( |
| 3026 |
4, internal::FPEncodingFloat4E2M1INTEL) || |
| 3027 |
InMatrixElementTy->isTypeFloat(4, |
| 3028 |
internal::FPEncodingFloat4E2M1INTEL)) |
| 3029 |
Inst = transConvertInst(BV, F, BB); |
| 3030 |
else |
| 3031 |
Inst = transSPIRVBuiltinFromInst(BI, BB); |
| 3032 |
} else { |
| 3033 |
Inst = transConvertInst(BV, F, BB); |
| 3034 |
} |
| 3035 |
return mapValue(BV, Inst); |
| 3036 |
} |
| 3037 |
if (isIntelCvtOpCode(OC)) |
| 3038 |
return mapValue(BV, transConvertInst(BV, F, BB)); |
| 3039 |
return mapValue( |
| 3040 |
BV, transSPIRVBuiltinFromInst(static_cast<SPIRVInstruction *>(BV), BB)); |
| 3041 |
} |
| 3042 |
} |
| 3043 |
} |
| 3044 |
|
| 3045 |
// Get meaningful suffix for adding at the end of the function name to avoid |
| 3046 |
// ascending numerical suffixes. It is useful in situations, where the same |
| 3047 |
// function is called twice or more in one basic block. So, the function name is |
| 3048 |
// formed in the following way: [FuncName].[ReturnTy].[InputTy] |
| 3049 |
static std::string getFuncAPIntSuffix(const Type *RetTy, const Type *In1Ty, |
| 3050 |
const Type *In2Ty = nullptr) { |
| 3051 |
std::stringstream Suffix; |
| 3052 |
Suffix << ".i" << RetTy->getIntegerBitWidth() << ".i" |
| 3053 |
<< In1Ty->getIntegerBitWidth(); |
| 3054 |
if (In2Ty) |
| 3055 |
Suffix << ".i" << In2Ty->getIntegerBitWidth(); |
| 3056 |
return Suffix.str(); |
| 3057 |
} |
| 3058 |
|
| 3059 |
Value *SPIRVToLLVM::transFixedPointInst(SPIRVInstruction *BI, BasicBlock *BB) { |
| 3060 |
// LLVM fixed point functions return value: |
| 3061 |
// iN (arbitrary precision integer of N bits length) |
| 3062 |
// Arguments: |
| 3063 |
// A(iN), S(i1), I(i32), rI(i32), Quantization(i32), Overflow(i32) |
| 3064 |
// If return value wider than 64 bit: |
| 3065 |
// iN addrspace(4)* sret(iN), A(iN), S(i1), I(i32), rI(i32), |
| 3066 |
// Quantization(i32), Overflow(i32) |
| 3067 |
|
| 3068 |
// SPIR-V fixed point instruction contains: |
| 3069 |
// <id>ResTy Res<id> In<id> Literal S Literal I Literal rI Literal Q Literal O |
| 3070 |
|
| 3071 |
Type *RetTy = transType(BI->getType()); |
| 3072 |
|
| 3073 |
auto Inst = static_cast<SPIRVFixedPointIntelInst *>(BI); |
| 3074 |
Type *InTy = transType(Inst->getOperand(0)->getType()); |
| 3075 |
|
| 3076 |
IntegerType *Int32Ty = IntegerType::get(*Context, 32); |
| 3077 |
IntegerType *Int1Ty = IntegerType::get(*Context, 1); |
| 3078 |
|
| 3079 |
SmallVector<Type *, 8> ArgTys; |
| 3080 |
std::vector<Value *> Args; |
| 3081 |
Args.reserve(8); |
| 3082 |
if (RetTy->getIntegerBitWidth() > 64) { |
| 3083 |
llvm::PointerType *RetPtrTy = llvm::PointerType::get(RetTy, SPIRAS_Generic); |
| 3084 |
Value *Alloca = new AllocaInst(RetTy, SPIRAS_Private, "", BB); |
| 3085 |
Value *RetValPtr = new AddrSpaceCastInst(Alloca, RetPtrTy, "", BB); |
| 3086 |
ArgTys.emplace_back(RetPtrTy); |
| 3087 |
Args.emplace_back(RetValPtr); |
| 3088 |
} |
| 3089 |
|
| 3090 |
ArgTys.insert(ArgTys.end(), |
| 3091 |
{InTy, Int1Ty, Int32Ty, Int32Ty, Int32Ty, Int32Ty}); |
| 3092 |
|
| 3093 |
auto Words = Inst->getOpWords(); |
| 3094 |
Args.emplace_back(transValue(Inst->getOperand(0), BB->getParent(), BB)); |
| 3095 |
Args.emplace_back(ConstantInt::get(Int1Ty, Words[1])); |
| 3096 |
for (int I = 2; I <= 5; I++) |
| 3097 |
Args.emplace_back(ConstantInt::get(Int32Ty, Words[I])); |
| 3098 |
|
| 3099 |
Type *FuncRetTy = |
| 3100 |
(RetTy->getIntegerBitWidth() <= 64) ? RetTy : Type::getVoidTy(*Context); |
| 3101 |
FunctionType *FT = FunctionType::get(FuncRetTy, ArgTys, false); |
| 3102 |
|
| 3103 |
Op OpCode = Inst->getOpCode(); |
| 3104 |
std::string FuncName = |
| 3105 |
SPIRVFixedPointIntelMap::rmap(OpCode) + getFuncAPIntSuffix(RetTy, InTy); |
| 3106 |
|
| 3107 |
FunctionCallee FCallee = M->getOrInsertFunction(FuncName, FT); |
| 3108 |
|
| 3109 |
auto *Func = cast<Function>(FCallee.getCallee()); |
| 3110 |
Func->setCallingConv(CallingConv::SPIR_FUNC); |
| 3111 |
if (isFuncNoUnwind()) |
| 3112 |
Func->addFnAttr(Attribute::NoUnwind); |
| 3113 |
|
| 3114 |
if (RetTy->getIntegerBitWidth() <= 64) |
| 3115 |
return CallInst::Create(FCallee, Args, "", BB); |
| 3116 |
|
| 3117 |
Func->addParamAttr( |
| 3118 |
0, Attribute::get(*Context, Attribute::AttrKind::StructRet, RetTy)); |
| 3119 |
CallInst *APIntInst = CallInst::Create(FCallee, Args, "", BB); |
| 3120 |
APIntInst->addParamAttr( |
| 3121 |
0, Attribute::get(*Context, Attribute::AttrKind::StructRet, RetTy)); |
| 3122 |
|
| 3123 |
return static_cast<Value *>(new LoadInst(RetTy, Args[0], "", false, BB)); |
| 3124 |
} |
| 3125 |
|
| 3126 |
Value *SPIRVToLLVM::transArbFloatInst(SPIRVInstruction *BI, BasicBlock *BB, |
| 3127 |
bool IsBinaryInst) { |
| 3128 |
// Format of instructions Add, Sub, Mul, Div, Hypot, ATan2, Pow, PowR: |
| 3129 |
// LLVM arbitrary floating point functions return value: |
| 3130 |
// iN (arbitrary precision integer of N bits length) |
| 3131 |
// Arguments: A(iN), MA(i32), B(iN), MB(i32), Mout(i32), |
| 3132 |
// EnableSubnormals(i32), RoundingMode(i32), |
| 3133 |
// RoundingAccuracy(i32) |
| 3134 |
// where A, B and return values are of arbitrary precision integer type. |
| 3135 |
// SPIR-V arbitrary floating point instruction layout: |
| 3136 |
// <id>ResTy Res<id> A<id> Literal MA B<id> Literal MB Literal Mout |
| 3137 |
// Literal EnableSubnormals Literal RoundingMode |
| 3138 |
// Literal RoundingAccuracy |
| 3139 |
|
| 3140 |
// Format of instructions GT, GE, LT, LE, EQ: |
| 3141 |
// LLVM arbitrary floating point functions return value: Bool |
| 3142 |
// Arguments: A(iN), MA(i32), B(iN), MB(i32) |
| 3143 |
// where A and B are of arbitrary precision integer type. |
| 3144 |
// SPIR-V arbitrary floating point instruction layout: |
| 3145 |
// <id>ResTy Res<id> A<id> Literal MA B<id> Literal MB |
| 3146 |
|
| 3147 |
// Format of instruction PowN: |
| 3148 |
// LLVM arbitrary floating point functions return value: iN |
| 3149 |
// Arguments: A(iN), MA(i32), B(iN), SignOfB(i1), Mout(i32), |
| 3150 |
// EnableSubnormals(i32), RoundingMode(i32), |
| 3151 |
// RoundingAccuracy(i32) |
| 3152 |
// where A, B and return values are of arbitrary precision integer type. |
| 3153 |
// SPIR-V arbitrary floating point instruction layout: |
| 3154 |
// <id>ResTy Res<id> A<id> Literal MA B<id> Literal SignOfB Literal Mout |
| 3155 |
// Literal EnableSubnormals Literal RoundingMode |
| 3156 |
// Literal RoundingAccuracy |
| 3157 |
|
| 3158 |
// Format of instruction CastFromInt: |
| 3159 |
// LLVM arbitrary floating point functions return value: iN |
| 3160 |
// Arguments: A(iN), Mout(i32), FromSign(bool), EnableSubnormals(i32), |
| 3161 |
// RoundingMode(i32), RoundingAccuracy(i32) |
| 3162 |
// where A and return values are of arbitrary precision integer type. |
| 3163 |
// SPIR-V arbitrary floating point instruction layout: |
| 3164 |
// <id>ResTy Res<id> A<id> Literal Mout Literal FromSign |
| 3165 |
// Literal EnableSubnormals Literal RoundingMode |
| 3166 |
// Literal RoundingAccuracy |
| 3167 |
|
| 3168 |
// Format of instruction CastToInt: |
| 3169 |
// LLVM arbitrary floating point functions return value: iN |
| 3170 |
// Arguments: A(iN), MA(i32), ToSign(bool), EnableSubnormals(i32), |
| 3171 |
// RoundingMode(i32), RoundingAccuracy(i32) |
| 3172 |
// where A and return values are of arbitrary precision integer type. |
| 3173 |
// SPIR-V arbitrary floating point instruction layout: |
| 3174 |
// <id>ResTy Res<id> A<id> Literal MA Literal ToSign |
| 3175 |
// Literal EnableSubnormals Literal RoundingMode |
| 3176 |
// Literal RoundingAccuracy |
| 3177 |
|
| 3178 |
// Format of other instructions: |
| 3179 |
// LLVM arbitrary floating point functions return value: iN |
| 3180 |
// Arguments: A(iN), MA(i32), Mout(i32), EnableSubnormals(i32), |
| 3181 |
// RoundingMode(i32), RoundingAccuracy(i32) |
| 3182 |
// where A and return values are of arbitrary precision integer type. |
| 3183 |
// SPIR-V arbitrary floating point instruction layout: |
| 3184 |
// <id>ResTy Res<id> A<id> Literal MA Literal Mout Literal EnableSubnormals |
| 3185 |
// Literal RoundingMode Literal RoundingAccuracy |
| 3186 |
|
| 3187 |
Type *RetTy = transType(BI->getType()); |
| 3188 |
IntegerType *Int1Ty = Type::getInt1Ty(*Context); |
| 3189 |
IntegerType *Int32Ty = Type::getInt32Ty(*Context); |
| 3190 |
|
| 3191 |
auto Inst = static_cast<SPIRVArbFloatIntelInst *>(BI); |
| 3192 |
|
| 3193 |
Type *ATy = transType(Inst->getOperand(0)->getType()); |
| 3194 |
Type *BTy = nullptr; |
| 3195 |
|
| 3196 |
// Words contain: |
| 3197 |
// A<id> [Literal MA] [B<id>] [Literal MB] [Literal Mout] [Literal Sign] |
| 3198 |
// [Literal EnableSubnormals Literal RoundingMode Literal RoundingAccuracy] |
| 3199 |
const std::vector<SPIRVWord> Words = Inst->getOpWords(); |
| 3200 |
auto WordsItr = Words.begin() + 1; /* Skip word for A input id */ |
| 3201 |
|
| 3202 |
SmallVector<Type *, 8> ArgTys; |
| 3203 |
std::vector<Value *> Args; |
| 3204 |
|
| 3205 |
if (RetTy->getIntegerBitWidth() > 64) { |
| 3206 |
llvm::PointerType *RetPtrTy = llvm::PointerType::get(RetTy, SPIRAS_Generic); |
| 3207 |
ArgTys.push_back(RetPtrTy); |
| 3208 |
Value *Alloca = new AllocaInst(RetTy, SPIRAS_Private, "", BB); |
| 3209 |
Value *RetValPtr = new AddrSpaceCastInst(Alloca, RetPtrTy, "", BB); |
| 3210 |
Args.push_back(RetValPtr); |
| 3211 |
} |
| 3212 |
|
| 3213 |
ArgTys.insert(ArgTys.end(), {ATy, Int32Ty}); |
| 3214 |
// A - input |
| 3215 |
Args.emplace_back(transValue(Inst->getOperand(0), BB->getParent(), BB)); |
| 3216 |
// MA/Mout - width of mantissa |
| 3217 |
Args.emplace_back(ConstantInt::get(Int32Ty, *WordsItr++)); |
| 3218 |
|
| 3219 |
Op OC = Inst->getOpCode(); |
| 3220 |
if (OC == OpArbitraryFloatCastFromIntINTEL || |
| 3221 |
OC == OpArbitraryFloatCastToIntINTEL) { |
| 3222 |
ArgTys.push_back(Int1Ty); |
| 3223 |
Args.push_back(ConstantInt::get(Int1Ty, *WordsItr++)); /* ToSign/FromSign */ |
| 3224 |
} |
| 3225 |
|
| 3226 |
if (IsBinaryInst) { |
| 3227 |
/* B - input */ |
| 3228 |
BTy = transType(Inst->getOperand(2)->getType()); |
| 3229 |
ArgTys.push_back(BTy); |
| 3230 |
Args.push_back(transValue(Inst->getOperand(2), BB->getParent(), BB)); |
| 3231 |
++WordsItr; /* Skip word for B input id */ |
| 3232 |
if (OC == OpArbitraryFloatPowNINTEL) { |
| 3233 |
ArgTys.push_back(Int1Ty); |
| 3234 |
Args.push_back(ConstantInt::get(Int1Ty, *WordsItr++)); /* SignOfB */ |
| 3235 |
} |
| 3236 |
} |
| 3237 |
|
| 3238 |
std::fill_n(std::back_inserter(ArgTys), Words.end() - WordsItr, Int32Ty); |
| 3239 |
std::transform(WordsItr, Words.end(), std::back_inserter(Args), |
| 3240 |
[Int32Ty](const SPIRVWord &Word) { |
| 3241 |
return ConstantInt::get(Int32Ty, Word); |
| 3242 |
}); |
| 3243 |
|
| 3244 |
std::string FuncName = |
| 3245 |
SPIRVArbFloatIntelMap::rmap(OC) + getFuncAPIntSuffix(RetTy, ATy, BTy); |
| 3246 |
|
| 3247 |
Type *FuncRetTy = |
| 3248 |
(RetTy->getIntegerBitWidth() <= 64) ? RetTy : Type::getVoidTy(*Context); |
| 3249 |
FunctionType *FT = FunctionType::get(FuncRetTy, ArgTys, false); |
| 3250 |
FunctionCallee FCallee = M->getOrInsertFunction(FuncName, FT); |
| 3251 |
|
| 3252 |
auto *Func = cast<Function>(FCallee.getCallee()); |
| 3253 |
Func->setCallingConv(CallingConv::SPIR_FUNC); |
| 3254 |
if (isFuncNoUnwind()) |
| 3255 |
Func->addFnAttr(Attribute::NoUnwind); |
| 3256 |
|
| 3257 |
if (RetTy->getIntegerBitWidth() <= 64) |
| 3258 |
return CallInst::Create(Func, Args, "", BB); |
| 3259 |
|
| 3260 |
Func->addParamAttr( |
| 3261 |
0, Attribute::get(*Context, Attribute::AttrKind::StructRet, RetTy)); |
| 3262 |
CallInst *APFloatInst = CallInst::Create(FCallee, Args, "", BB); |
| 3263 |
APFloatInst->addParamAttr( |
| 3264 |
0, Attribute::get(*Context, Attribute::AttrKind::StructRet, RetTy)); |
| 3265 |
|
| 3266 |
return static_cast<Value *>(new LoadInst(RetTy, Args[0], "", false, BB)); |
| 3267 |
} |
| 3268 |
|
| 3269 |
template <class SourceTy, class FuncTy> |
| 3270 |
bool SPIRVToLLVM::foreachFuncCtlMask(SourceTy Source, FuncTy Func) { |
| 3271 |
SPIRVWord FCM = Source->getFuncCtlMask(); |
| 3272 |
SPIRSPIRVFuncCtlMaskMap::foreach ( |
| 3273 |
[&](Attribute::AttrKind Attr, SPIRVFunctionControlMaskKind Mask) { |
| 3274 |
if (FCM & Mask) |
| 3275 |
Func(Attr); |
| 3276 |
}); |
| 3277 |
return true; |
| 3278 |
} |
| 3279 |
|
| 3280 |
void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) { |
| 3281 |
if (BF->hasDecorate(DecorationReferencedIndirectlyINTEL)) |
| 3282 |
F->addFnAttr("referenced-indirectly"); |
| 3283 |
if (isFuncNoUnwind()) |
| 3284 |
F->addFnAttr(Attribute::NoUnwind); |
| 3285 |
foreachFuncCtlMask(BF, [&](Attribute::AttrKind Attr) { F->addFnAttr(Attr); }); |
| 3286 |
|
| 3287 |
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; |
| 3288 |
++I) { |
| 3289 |
auto *BA = BF->getArgument(I->getArgNo()); |
| 3290 |
mapValue(BA, &(*I)); |
| 3291 |
setName(&(*I), BA); |
| 3292 |
AttributeMask IllegalAttrs = AttributeFuncs::typeIncompatible(I->getType()); |
| 3293 |
BA->foreachAttr([&](SPIRVFuncParamAttrKind Kind) { |
| 3294 |
// Skip this function parameter attribute as it will translated among |
| 3295 |
// OpenCL metadata |
| 3296 |
if (Kind == FunctionParameterAttributeRuntimeAlignedINTEL) |
| 3297 |
return; |
| 3298 |
Attribute::AttrKind LLVMKind = SPIRSPIRVFuncParamAttrMap::rmap(Kind); |
| 3299 |
if (IllegalAttrs.contains(LLVMKind)) |
| 3300 |
return; |
| 3301 |
Type *AttrTy = nullptr; |
| 3302 |
switch (LLVMKind) { |
| 3303 |
case Attribute::AttrKind::ByVal: |
| 3304 |
case Attribute::AttrKind::StructRet: |
| 3305 |
AttrTy = transType(BA->getType()->getPointerElementType()); |
| 3306 |
break; |
| 3307 |
default: |
| 3308 |
break; // do nothing |
| 3309 |
} |
| 3310 |
// Make sure to use a correct constructor for a typed/typeless attribute |
| 3311 |
auto A = AttrTy ? Attribute::get(*Context, LLVMKind, AttrTy) |
| 3312 |
: Attribute::get(*Context, LLVMKind); |
| 3313 |
I->addAttr(A); |
| 3314 |
}); |
| 3315 |
|
| 3316 |
AttrBuilder Builder(*Context); |
| 3317 |
SPIRVWord MaxOffset = 0; |
| 3318 |
if (BA->hasDecorate(DecorationMaxByteOffset, 0, &MaxOffset)) |
| 3319 |
Builder.addDereferenceableAttr(MaxOffset); |
| 3320 |
else { |
| 3321 |
SPIRVId MaxOffsetId; |
| 3322 |
if (BA->hasDecorateId(DecorationMaxByteOffsetId, 0, &MaxOffsetId)) { |
| 3323 |
if (auto MaxOffsetVal = transIdAsConstant(MaxOffsetId)) { |
| 3324 |
Builder.addDereferenceableAttr(*MaxOffsetVal); |
| 3325 |
} |
| 3326 |
} |
| 3327 |
} |
| 3328 |
if (auto Alignment = getAlignment(BA)) { |
| 3329 |
Builder.addAlignmentAttr(*Alignment); |
| 3330 |
} |
| 3331 |
I->addAttrs(Builder); |
| 3332 |
} |
| 3333 |
BF->foreachReturnValueAttr([&](SPIRVFuncParamAttrKind Kind) { |
| 3334 |
if (Kind == FunctionParameterAttributeNoWrite) |
| 3335 |
return; |
| 3336 |
F->addRetAttr(SPIRSPIRVFuncParamAttrMap::rmap(Kind)); |
| 3337 |
}); |
| 3338 |
} |
| 3339 |
|
| 3340 |
namespace { |
| 3341 |
// One basic block can be a predecessor to another basic block more than |
| 3342 |
// once (https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2702). |
| 3343 |
// This function fixes any PHIs that break this rule. |
| 3344 |
static void validatePhiPredecessors(Function *F) { |
| 3345 |
for (BasicBlock &BB : *F) { |
| 3346 |
bool UniquePreds = true; |
| 3347 |
DenseMap<BasicBlock *, unsigned> PredsCnt; |
| 3348 |
for (BasicBlock *PredBB : predecessors(&BB)) { |
| 3349 |
auto It = PredsCnt.try_emplace(PredBB, 1); |
| 3350 |
if (!It.second) { |
| 3351 |
UniquePreds = false; |
| 3352 |
++It.first->second; |
| 3353 |
} |
| 3354 |
} |
| 3355 |
if (UniquePreds) |
| 3356 |
continue; |
| 3357 |
// `phi` requires an incoming value per each predecessor instance, even |
| 3358 |
// it's the same basic block that has been already inserted as an incoming |
| 3359 |
// value of the `phi`. |
| 3360 |
for (PHINode &Phi : BB.phis()) { |
| 3361 |
SmallVector<Value *> Vs; |
| 3362 |
SmallVector<BasicBlock *> Bs; |
| 3363 |
SmallPtrSet<BasicBlock *, 8> UsedB; |
| 3364 |
for (auto [V, B] : zip(Phi.incoming_values(), Phi.blocks())) { |
| 3365 |
if (!UsedB.insert(B).second) |
| 3366 |
continue; |
| 3367 |
unsigned N = PredsCnt[B]; |
| 3368 |
Vs.insert(Vs.end(), N, V); |
| 3369 |
Bs.insert(Bs.end(), N, B); |
| 3370 |
} |
| 3371 |
unsigned I = 0; |
| 3372 |
for (unsigned N = Phi.getNumIncomingValues(); I < N; ++I) { |
| 3373 |
Phi.setIncomingValue(I, Vs[I]); |
| 3374 |
Phi.setIncomingBlock(I, Bs[I]); |
| 3375 |
} |
| 3376 |
for (unsigned N = Vs.size(); I < N; ++I) |
| 3377 |
Phi.addIncoming(Vs[I], Bs[I]); |
| 3378 |
} |
| 3379 |
} |
| 3380 |
} |
| 3381 |
} // namespace |
| 3382 |
|
| 3383 |
Function *SPIRVToLLVM::transFunction(SPIRVFunction *BF, unsigned AS) { |
| 3384 |
auto Loc = FuncMap.find(BF); |
| 3385 |
if (Loc != FuncMap.end()) |
| 3386 |
return Loc->second; |
| 3387 |
|
| 3388 |
auto IsKernel = isKernel(BF); |
| 3389 |
|
| 3390 |
if (IsKernel) { |
| 3391 |
// search for a previous function with the same name |
| 3392 |
// upgrade it to a kernel and drop this if it's found |
| 3393 |
for (auto &I : FuncMap) { |
| 3394 |
auto BFName = I.getFirst()->getName(); |
| 3395 |
if (BF->getName() == BFName) { |
| 3396 |
auto *F = I.getSecond(); |
| 3397 |
F->setCallingConv(CallingConv::SPIR_KERNEL); |
| 3398 |
F->setLinkage(GlobalValue::ExternalLinkage); |
| 3399 |
F->setDSOLocal(false); |
| 3400 |
F = cast<Function>(mapValue(BF, F)); |
| 3401 |
mapFunction(BF, F); |
| 3402 |
transFunctionAttrs(BF, F); |
| 3403 |
return F; |
| 3404 |
} |
| 3405 |
} |
| 3406 |
} |
| 3407 |
|
| 3408 |
auto Linkage = IsKernel ? GlobalValue::ExternalLinkage : transLinkageType(BF); |
| 3409 |
FunctionType *FT = dyn_cast<FunctionType>(transType(BF->getFunctionType())); |
| 3410 |
std::string FuncName = BF->getName(); |
| 3411 |
StringRef FuncNameRef(FuncName); |
| 3412 |
// Transform "@spirv.llvm_memset_p0i8_i32.volatile" to @llvm.memset.p0i8.i32 |
| 3413 |
// assuming llvm.memset is supported by the device compiler. If this |
| 3414 |
// assumption is not safe, we should have a command line option to control |
| 3415 |
// this behavior. |
| 3416 |
if (FuncNameRef.startswith("spirv.llvm_memset_p")) { |
| 3417 |
// We can't guarantee that the name is correctly mangled due to opaque |
| 3418 |
// pointers. Derive the correct name from the function type. |
| 3419 |
FuncName = |
| 3420 |
Intrinsic::getDeclaration(M, Intrinsic::memset, |
| 3421 |
{FT->getParamType(0), FT->getParamType(2)}) |
| 3422 |
->getName(); |
| 3423 |
} |
| 3424 |
|
| 3425 |
// Special handling for spirv.llvm_umul_with_overflow_* functions |
| 3426 |
// These were created during forward translation by lowering intrinsics. |
| 3427 |
// During reverse translation, we replace them with intrinsic calls. |
| 3428 |
if (FuncNameRef.starts_with("spirv.llvm_umul_with_overflow_")) { |
| 3429 |
Type *OverloadTy = FT->getParamType(0); |
| 3430 |
Function *F = Intrinsic::getDeclaration(M, Intrinsic::umul_with_overflow, |
| 3431 |
{OverloadTy}); |
| 3432 |
F = cast<Function>(mapValue(BF, F)); |
| 3433 |
mapFunction(BF, F); |
| 3434 |
return F; // Skip body translation - intrinsic will be used instead |
| 3435 |
} |
| 3436 |
|
| 3437 |
// Normal function handling. |
| 3438 |
if (FuncNameRef.consume_front("spirv.")) { |
| 3439 |
FuncNameRef.consume_back(".volatile"); |
| 3440 |
FuncName = FuncNameRef.str(); |
| 3441 |
std::replace(FuncName.begin(), FuncName.end(), '_', '.'); |
| 3442 |
} |
| 3443 |
Function *F = M->getFunction(FuncName); |
| 3444 |
if (!F) |
| 3445 |
F = Function::Create(FT, Linkage, AS, FuncName, M); |
| 3446 |
|
| 3447 |
F = cast<Function>(mapValue(BF, F)); |
| 3448 |
mapFunction(BF, F); |
| 3449 |
|
| 3450 |
if (F->isIntrinsic()) |
| 3451 |
return F; |
| 3452 |
|
| 3453 |
F->setCallingConv(IsKernel ? CallingConv::SPIR_KERNEL |
| 3454 |
: CallingConv::SPIR_FUNC); |
| 3455 |
transFunctionAttrs(BF, F); |
| 3456 |
|
| 3457 |
// Creating all basic blocks before creating instructions. |
| 3458 |
for (size_t I = 0, E = BF->getNumBasicBlock(); I != E; ++I) { |
| 3459 |
transValue(BF->getBasicBlock(I), F, nullptr); |
| 3460 |
} |
| 3461 |
|
| 3462 |
for (size_t I = 0, E = BF->getNumBasicBlock(); I != E; ++I) { |
| 3463 |
SPIRVBasicBlock *BBB = BF->getBasicBlock(I); |
| 3464 |
BasicBlock *BB = dyn_cast<BasicBlock>(transValue(BBB, F, nullptr)); |
| 3465 |
for (size_t BI = 0, BE = BBB->getNumInst(); BI != BE; ++BI) { |
| 3466 |
SPIRVInstruction *BInst = BBB->getInst(BI); |
| 3467 |
transValue(BInst, F, BB, false); |
| 3468 |
} |
| 3469 |
} |
| 3470 |
|
| 3471 |
validatePhiPredecessors(F); |
| 3472 |
transLLVMLoopMetadata(F); |
| 3473 |
|
| 3474 |
return F; |
| 3475 |
} |
| 3476 |
|
| 3477 |
Value *SPIRVToLLVM::transAsmINTEL(SPIRVAsmINTEL *BA) { |
| 3478 |
assert(BA); |
| 3479 |
bool HasSideEffect = BA->hasDecorate(DecorationSideEffectsINTEL); |
| 3480 |
return InlineAsm::get( |
| 3481 |
cast<FunctionType>(transType(BA->getFunctionType())), |
| 3482 |
BA->getInstructions(), BA->getConstraints(), HasSideEffect, |
| 3483 |
/* IsAlignStack */ false, InlineAsm::AsmDialect::AD_ATT); |
| 3484 |
} |
| 3485 |
|
| 3486 |
CallInst *SPIRVToLLVM::transAsmCallINTEL(SPIRVAsmCallINTEL *BI, Function *F, |
| 3487 |
BasicBlock *BB) { |
| 3488 |
assert(BI); |
| 3489 |
auto *IA = cast<InlineAsm>(transValue(BI->getAsm(), F, BB)); |
| 3490 |
auto Args = transValue(BM->getValues(BI->getArguments()), F, BB); |
| 3491 |
return CallInst::Create(cast<FunctionType>(IA->getFunctionType()), IA, Args, |
| 3492 |
BI->getName(), BB); |
| 3493 |
} |
| 3494 |
|
| 3495 |
/// LLVM convert builtin functions is translated to two instructions: |
| 3496 |
/// y = i32 islessgreater(float x, float z) -> |
| 3497 |
/// y = i32 ZExt(bool LessOrGreater(float x, float z)) |
| 3498 |
/// When translating back, for simplicity, a trunc instruction is inserted |
| 3499 |
/// w = bool LessOrGreater(float x, float z) -> |
| 3500 |
/// w = bool Trunc(i32 islessgreater(float x, float z)) |
| 3501 |
/// Optimizer should be able to remove the redundant trunc/zext |
| 3502 |
void SPIRVToLLVM::transOCLBuiltinFromInstPreproc( |
| 3503 |
SPIRVInstruction *BI, Type *&RetTy, std::vector<SPIRVValue *> &Args) { |
| 3504 |
if (!BI->hasType()) |
| 3505 |
return; |
| 3506 |
auto BT = BI->getType(); |
| 3507 |
if (isCmpOpCode(BI->getOpCode())) { |
| 3508 |
if (BT->isTypeBool()) |
| 3509 |
RetTy = IntegerType::getInt32Ty(*Context); |
| 3510 |
else if (BT->isTypeVectorBool()) |
| 3511 |
RetTy = FixedVectorType::get( |
| 3512 |
IntegerType::get( |
| 3513 |
*Context, |
| 3514 |
Args[0]->getType()->getVectorComponentType()->getBitWidth()), |
| 3515 |
BT->getVectorComponentCount()); |
| 3516 |
else |
| 3517 |
llvm_unreachable("invalid compare instruction"); |
| 3518 |
} |
| 3519 |
} |
| 3520 |
|
| 3521 |
Instruction * |
| 3522 |
SPIRVToLLVM::transOCLBuiltinPostproc(SPIRVInstruction *BI, CallInst *CI, |
| 3523 |
BasicBlock *BB, |
| 3524 |
const std::string &DemangledName) { |
| 3525 |
auto OC = BI->getOpCode(); |
| 3526 |
if (isCmpOpCode(OC) && BI->getType()->isTypeVectorOrScalarBool()) { |
| 3527 |
return CastInst::Create(Instruction::Trunc, CI, transType(BI->getType()), |
| 3528 |
"cvt", BB); |
| 3529 |
} |
| 3530 |
if (SPIRVEnableStepExpansion && |
| 3531 |
(DemangledName == "smoothstep" || DemangledName == "step")) |
| 3532 |
return expandOCLBuiltinWithScalarArg(CI, DemangledName); |
| 3533 |
return CI; |
| 3534 |
} |
| 3535 |
|
| 3536 |
Value *SPIRVToLLVM::transBlockInvoke(SPIRVValue *Invoke, BasicBlock *BB) { |
| 3537 |
auto *TranslatedInvoke = transFunction(static_cast<SPIRVFunction *>(Invoke)); |
| 3538 |
auto *Int8PtrTyGen = Type::getInt8PtrTy(*Context, SPIRAS_Generic); |
| 3539 |
return CastInst::CreatePointerBitCastOrAddrSpaceCast(TranslatedInvoke, |
| 3540 |
Int8PtrTyGen, "", BB); |
| 3541 |
} |
| 3542 |
|
| 3543 |
Instruction *SPIRVToLLVM::transWGSizeQueryBI(SPIRVInstruction *BI, |
| 3544 |
BasicBlock *BB) { |
| 3545 |
std::string FName = |
| 3546 |
(BI->getOpCode() == OpGetKernelWorkGroupSize) |
| 3547 |
? "__get_kernel_work_group_size_impl" |
| 3548 |
: "__get_kernel_preferred_work_group_size_multiple_impl"; |
| 3549 |
|
| 3550 |
Function *F = M->getFunction(FName); |
| 3551 |
if (!F) { |
| 3552 |
auto Int8PtrTyGen = Type::getInt8PtrTy(*Context, SPIRAS_Generic); |
| 3553 |
FunctionType *FT = FunctionType::get(Type::getInt32Ty(*Context), |
| 3554 |
{Int8PtrTyGen, Int8PtrTyGen}, false); |
| 3555 |
F = Function::Create(FT, GlobalValue::ExternalLinkage, FName, M); |
| 3556 |
if (isFuncNoUnwind()) |
| 3557 |
F->addFnAttr(Attribute::NoUnwind); |
| 3558 |
} |
| 3559 |
auto Ops = BI->getOperands(); |
| 3560 |
SmallVector<Value *, 2> Args = {transBlockInvoke(Ops[0], BB), |
| 3561 |
transValue(Ops[1], F, BB, false)}; |
| 3562 |
auto Call = CallInst::Create(F, Args, "", BB); |
| 3563 |
setName(Call, BI); |
| 3564 |
setAttrByCalledFunc(Call); |
| 3565 |
return Call; |
| 3566 |
} |
| 3567 |
|
| 3568 |
Instruction *SPIRVToLLVM::transSGSizeQueryBI(SPIRVInstruction *BI, |
| 3569 |
BasicBlock *BB) { |
| 3570 |
std::string FName = (BI->getOpCode() == OpGetKernelNDrangeMaxSubGroupSize) |
| 3571 |
? "__get_kernel_max_sub_group_size_for_ndrange_impl" |
| 3572 |
: "__get_kernel_sub_group_count_for_ndrange_impl"; |
| 3573 |
|
| 3574 |
auto Ops = BI->getOperands(); |
| 3575 |
Function *F = M->getFunction(FName); |
| 3576 |
if (!F) { |
| 3577 |
auto Int8PtrTyGen = Type::getInt8PtrTy(*Context, SPIRAS_Generic); |
| 3578 |
SmallVector<Type *, 3> Tys = { |
| 3579 |
transType(Ops[0]->getType()), // ndrange |
| 3580 |
Int8PtrTyGen, // block_invoke |
| 3581 |
Int8PtrTyGen // block_literal |
| 3582 |
}; |
| 3583 |
auto *FT = FunctionType::get(Type::getInt32Ty(*Context), Tys, false); |
| 3584 |
F = Function::Create(FT, GlobalValue::ExternalLinkage, FName, M); |
| 3585 |
if (isFuncNoUnwind()) |
| 3586 |
F->addFnAttr(Attribute::NoUnwind); |
| 3587 |
} |
| 3588 |
SmallVector<Value *, 2> Args = { |
| 3589 |
transValue(Ops[0], F, BB, false), // ndrange |
| 3590 |
transBlockInvoke(Ops[1], BB), // block_invoke |
| 3591 |
transValue(Ops[2], F, BB, false) // block_literal |
| 3592 |
}; |
| 3593 |
auto Call = CallInst::Create(F, Args, "", BB); |
| 3594 |
setName(Call, BI); |
| 3595 |
setAttrByCalledFunc(Call); |
| 3596 |
return Call; |
| 3597 |
} |
| 3598 |
|
| 3599 |
Instruction *SPIRVToLLVM::transBuiltinFromInst(const std::string &FuncName, |
| 3600 |
SPIRVInstruction *BI, |
| 3601 |
BasicBlock *BB) { |
| 3602 |
std::string MangledName; |
| 3603 |
auto Ops = BI->getOperands(); |
| 3604 |
Type *RetTy = |
| 3605 |
BI->hasType() ? transType(BI->getType()) : Type::getVoidTy(*Context); |
| 3606 |
transOCLBuiltinFromInstPreproc(BI, RetTy, Ops); |
| 3607 |
std::vector<Type *> ArgTys = |
| 3608 |
transTypeVector(SPIRVInstruction::getOperandTypes(Ops), true); |
| 3609 |
for (auto &I : ArgTys) { |
| 3610 |
if (isa<FunctionType>(I)) { |
| 3611 |
I = TypedPointerType::get(I, SPIRAS_Private); |
| 3612 |
} |
| 3613 |
} |
| 3614 |
|
| 3615 |
if (BM->getDesiredBIsRepresentation() != BIsRepresentation::SPIRVFriendlyIR) |
| 3616 |
mangleOpenClBuiltin(FuncName, ArgTys, MangledName); |
| 3617 |
else |
| 3618 |
MangledName = |
| 3619 |
getSPIRVFriendlyIRFunctionName(FuncName, BI->getOpCode(), ArgTys, Ops); |
| 3620 |
|
| 3621 |
opaquifyTypedPointers(ArgTys); |
| 3622 |
|
| 3623 |
Function *Func = M->getFunction(MangledName); |
| 3624 |
FunctionType *FT = FunctionType::get(RetTy, ArgTys, false); |
| 3625 |
// ToDo: Some intermediate functions have duplicate names with |
| 3626 |
// different function types. This is OK if the function name |
| 3627 |
// is used internally and finally translated to unique function |
| 3628 |
// names. However it is better to have a way to differentiate |
| 3629 |
// between intermidiate functions and final functions and make |
| 3630 |
// sure final functions have unique names. |
| 3631 |
SPIRVDBG(if (Func && Func->getFunctionType() != FT) { |
| 3632 |
dbgs() << "Warning: Function name conflict:\n" |
| 3633 |
<< *Func << '\n' |
| 3634 |
<< " => " << *FT << '\n'; |
| 3635 |
}) |
| 3636 |
if (!Func || Func->getFunctionType() != FT) { |
| 3637 |
LLVM_DEBUG(for (auto &I : ArgTys) { dbgs() << *I << '\n'; }); |
| 3638 |
Func = Function::Create(FT, GlobalValue::ExternalLinkage, MangledName, M); |
| 3639 |
Func->setCallingConv(CallingConv::SPIR_FUNC); |
| 3640 |
if (isFuncNoUnwind()) |
| 3641 |
Func->addFnAttr(Attribute::NoUnwind); |
| 3642 |
auto OC = BI->getOpCode(); |
| 3643 |
if (isGroupOpCode(OC) || isIntelSubgroupOpCode(OC) || |
| 3644 |
isSplitBarrierINTELOpCode(OC) || OC == OpControlBarrier) |
| 3645 |
Func->addFnAttr(Attribute::Convergent); |
| 3646 |
} |
| 3647 |
CallInst *Call; |
| 3648 |
if (BI->getOpCode() == OpCooperativeMatrixLengthKHR && |
| 3649 |
Ops[0]->getOpCode() == OpTypeCooperativeMatrixKHR) { |
| 3650 |
// OpCooperativeMatrixLengthKHR needs special handling as its operand is |
| 3651 |
// a Type instead of a Value. |
| 3652 |
llvm::Type *MatTy = transType(reinterpret_cast<SPIRVType *>(Ops[0])); |
| 3653 |
Call = CallInst::Create(Func, Constant::getNullValue(MatTy), "", BB); |
| 3654 |
} else { |
| 3655 |
Call = CallInst::Create(Func, transValue(Ops, BB->getParent(), BB), "", BB); |
| 3656 |
} |
| 3657 |
setName(Call, BI); |
| 3658 |
setAttrByCalledFunc(Call); |
| 3659 |
SPIRVDBG(spvdbgs() << "[transInstToBuiltinCall] " << *BI << " -> "; |
| 3660 |
dbgs() << *Call << '\n';) |
| 3661 |
Instruction *Inst = transOCLBuiltinPostproc(BI, Call, BB, FuncName); |
| 3662 |
return Inst; |
| 3663 |
} |
| 3664 |
|
| 3665 |
SPIRVToLLVM::SPIRVToLLVM(Module *LLVMModule, SPIRVModule *TheSPIRVModule) |
| 3666 |
: BuiltinCallHelper(ManglingRules::OpenCL), M(LLVMModule), |
| 3667 |
BM(TheSPIRVModule) { |
| 3668 |
assert(M && "Initialization without an LLVM module is not allowed"); |
| 3669 |
initialize(*M); |
| 3670 |
Context = &M->getContext(); |
| 3671 |
if (BM->getDesiredBIsRepresentation() == BIsRepresentation::SPIRVFriendlyIR) |
| 3672 |
UseTargetTypes = !Context->supportsTypedPointers(); |
| 3673 |
DbgTran.reset(new SPIRVToLLVMDbgTran(TheSPIRVModule, LLVMModule, this)); |
| 3674 |
} |
| 3675 |
|
| 3676 |
std::string getSPIRVFuncSuffix(SPIRVInstruction *BI) { |
| 3677 |
string Suffix = ""; |
| 3678 |
if (BI->getOpCode() == OpCreatePipeFromPipeStorage) { |
| 3679 |
auto CPFPS = static_cast<SPIRVCreatePipeFromPipeStorage *>(BI); |
| 3680 |
assert(CPFPS->getType()->isTypePipe() && |
| 3681 |
"Invalid type of CreatePipeFromStorage"); |
| 3682 |
auto PipeType = static_cast<SPIRVTypePipe *>(CPFPS->getType()); |
| 3683 |
switch (PipeType->getAccessQualifier()) { |
| 3684 |
default: |
| 3685 |
case AccessQualifierReadOnly: |
| 3686 |
Suffix = "_read"; |
| 3687 |
break; |
| 3688 |
case AccessQualifierWriteOnly: |
| 3689 |
Suffix = "_write"; |
| 3690 |
break; |
| 3691 |
case AccessQualifierReadWrite: |
| 3692 |
Suffix = "_read_write"; |
| 3693 |
break; |
| 3694 |
} |
| 3695 |
} |
| 3696 |
if (BI->hasDecorate(DecorationSaturatedConversion)) { |
| 3697 |
Suffix += kSPIRVPostfix::Divider; |
| 3698 |
Suffix += kSPIRVPostfix::Sat; |
| 3699 |
} |
| 3700 |
SPIRVFPRoundingModeKind Kind; |
| 3701 |
if (BI->hasFPRoundingMode(&Kind)) { |
| 3702 |
Suffix += kSPIRVPostfix::Divider; |
| 3703 |
Suffix += SPIRSPIRVFPRoundingModeMap::rmap(Kind); |
| 3704 |
} |
| 3705 |
if (BI->getOpCode() == OpGenericCastToPtrExplicit) { |
| 3706 |
Suffix += kSPIRVPostfix::Divider; |
| 3707 |
auto *Ty = BI->getType(); |
| 3708 |
auto GenericCastToPtrInst = |
| 3709 |
Ty->isTypeVectorPointer() |
| 3710 |
? Ty->getVectorComponentType()->getPointerStorageClass() |
| 3711 |
: Ty->getPointerStorageClass(); |
| 3712 |
switch (GenericCastToPtrInst) { |
| 3713 |
case StorageClassCrossWorkgroup: |
| 3714 |
Suffix += std::string(kSPIRVPostfix::ToGlobal); |
| 3715 |
break; |
| 3716 |
case StorageClassWorkgroup: |
| 3717 |
Suffix += std::string(kSPIRVPostfix::ToLocal); |
| 3718 |
break; |
| 3719 |
case StorageClassFunction: |
| 3720 |
Suffix += std::string(kSPIRVPostfix::ToPrivate); |
| 3721 |
break; |
| 3722 |
default: |
| 3723 |
llvm_unreachable("Invalid address space"); |
| 3724 |
} |
| 3725 |
} |
| 3726 |
if (BI->getOpCode() == OpBuildNDRange) { |
| 3727 |
Suffix += kSPIRVPostfix::Divider; |
| 3728 |
auto *NDRangeInst = static_cast<SPIRVBuildNDRange *>(BI); |
| 3729 |
auto *EleTy = ((NDRangeInst->getOperands())[0])->getType(); |
| 3730 |
int Dim = EleTy->isTypeArray() ? EleTy->getArrayLength() : 1; |
| 3731 |
assert((EleTy->isTypeInt() && Dim == 1) || |
| 3732 |
(EleTy->isTypeArray() && Dim >= 2 && Dim <= 3)); |
| 3733 |
ostringstream OS; |
| 3734 |
OS << Dim; |
| 3735 |
Suffix += OS.str() + "D"; |
| 3736 |
} |
| 3737 |
return Suffix; |
| 3738 |
} |
| 3739 |
|
| 3740 |
Instruction *SPIRVToLLVM::transSPIRVBuiltinFromInst(SPIRVInstruction *BI, |
| 3741 |
BasicBlock *BB) { |
| 3742 |
assert(BB && "Invalid BB"); |
| 3743 |
const auto OC = BI->getOpCode(); |
| 3744 |
|
| 3745 |
bool AddRetTypePostfix = false; |
| 3746 |
switch (static_cast<size_t>(OC)) { |
| 3747 |
case OpImageQuerySizeLod: |
| 3748 |
case OpImageQuerySize: |
| 3749 |
case OpImageRead: |
| 3750 |
case OpSubgroupImageBlockReadINTEL: |
| 3751 |
case OpSubgroupImageMediaBlockReadINTEL: |
| 3752 |
case OpSubgroupBlockReadINTEL: |
| 3753 |
case OpImageSampleExplicitLod: |
| 3754 |
case OpSDotKHR: |
| 3755 |
case OpUDotKHR: |
| 3756 |
case OpSUDotKHR: |
| 3757 |
case OpSDotAccSatKHR: |
| 3758 |
case OpUDotAccSatKHR: |
| 3759 |
case OpSUDotAccSatKHR: |
| 3760 |
case OpReadClockKHR: |
| 3761 |
case OpFmaKHR: |
| 3762 |
case internal::OpJointMatrixLoadINTEL: |
| 3763 |
case OpCooperativeMatrixLoadKHR: |
| 3764 |
case internal::OpCooperativeMatrixLoadCheckedINTEL: |
| 3765 |
case internal::OpConvertHandleToImageINTEL: |
| 3766 |
case internal::OpConvertHandleToSampledImageINTEL: |
| 3767 |
case internal::OpClampConvertFToFINTEL: |
| 3768 |
case internal::OpClampConvertFToSINTEL: |
| 3769 |
case internal::OpStochasticRoundFToFINTEL: |
| 3770 |
case internal::OpClampStochasticRoundFToFINTEL: |
| 3771 |
case internal::OpClampStochasticRoundFToSINTEL: |
| 3772 |
AddRetTypePostfix = true; |
| 3773 |
break; |
| 3774 |
default: { |
| 3775 |
if (isCvtOpCode(OC) && OC != OpGenericCastToPtrExplicit) |
| 3776 |
AddRetTypePostfix = true; |
| 3777 |
break; |
| 3778 |
} |
| 3779 |
} |
| 3780 |
|
| 3781 |
bool IsRetSigned = true; |
| 3782 |
switch (OC) { |
| 3783 |
case OpConvertFToU: |
| 3784 |
case OpSatConvertSToU: |
| 3785 |
case OpUConvert: |
| 3786 |
case OpUDotKHR: |
| 3787 |
case OpUDotAccSatKHR: |
| 3788 |
case OpReadClockKHR: |
| 3789 |
IsRetSigned = false; |
| 3790 |
break; |
| 3791 |
case OpImageRead: |
| 3792 |
case OpImageSampleExplicitLod: { |
| 3793 |
size_t Idx = getImageOperandsIndex(OC); |
| 3794 |
if (auto Ops = BI->getOperands(); Ops.size() > Idx) { |
| 3795 |
auto ImOp = static_cast<SPIRVConstant *>(Ops[Idx])->getZExtIntValue(); |
| 3796 |
IsRetSigned = !(ImOp & ImageOperandsMask::ImageOperandsZeroExtendMask); |
| 3797 |
} |
| 3798 |
break; |
| 3799 |
} |
| 3800 |
default: |
| 3801 |
break; |
| 3802 |
} |
| 3803 |
|
| 3804 |
if (AddRetTypePostfix) { |
| 3805 |
const Type *RetTy = BI->hasType() ? transType(BI->getType(), true) |
| 3806 |
: Type::getVoidTy(*Context); |
| 3807 |
Type *PET = nullptr; |
| 3808 |
if (auto *TPT = dyn_cast<TypedPointerType>(RetTy)) |
| 3809 |
PET = TPT->getElementType(); |
| 3810 |
return transBuiltinFromInst(getSPIRVFuncName(OC, RetTy, IsRetSigned, PET) + |
| 3811 |
getSPIRVFuncSuffix(BI), |
| 3812 |
BI, BB); |
| 3813 |
} |
| 3814 |
return transBuiltinFromInst(getSPIRVFuncName(OC, getSPIRVFuncSuffix(BI)), BI, |
| 3815 |
BB); |
| 3816 |
} |
| 3817 |
|
| 3818 |
bool SPIRVToLLVM::translate() { |
| 3819 |
if (!transAddressingModel()) |
| 3820 |
return false; |
| 3821 |
|
| 3822 |
// Entry Points should be translated before all debug intrinsics. |
| 3823 |
for (SPIRVExtInst *EI : BM->getDebugInstVec()) { |
| 3824 |
if (EI->getExtOp() == SPIRVDebug::EntryPoint) |
| 3825 |
DbgTran->transDebugInst(EI); |
| 3826 |
} |
| 3827 |
|
| 3828 |
// Compile unit might be needed during translation of debug intrinsics. |
| 3829 |
for (SPIRVExtInst *EI : BM->getDebugInstVec()) { |
| 3830 |
// Translate Compile Units first. |
| 3831 |
if (EI->getExtOp() == SPIRVDebug::CompilationUnit) |
| 3832 |
DbgTran->transDebugInst(EI); |
| 3833 |
} |
| 3834 |
|
| 3835 |
for (unsigned I = 0, E = BM->getNumVariables(); I != E; ++I) { |
| 3836 |
auto *BV = BM->getVariable(I); |
| 3837 |
if (BV->getStorageClass() != StorageClassFunction) |
| 3838 |
transValue(BV, nullptr, nullptr); |
| 3839 |
else |
| 3840 |
transGlobalCtorDtors(BV); |
| 3841 |
} |
| 3842 |
|
| 3843 |
// Then translate all debug instructions. |
| 3844 |
for (SPIRVExtInst *EI : BM->getDebugInstVec()) { |
| 3845 |
DbgTran->transDebugInst(EI); |
| 3846 |
} |
| 3847 |
|
| 3848 |
for (auto *FP : BM->getFunctionPointers()) { |
| 3849 |
SPIRVConstantFunctionPointerINTEL *BC = |
| 3850 |
static_cast<SPIRVConstantFunctionPointerINTEL *>(FP); |
| 3851 |
SPIRVFunction *F = BC->getFunction(); |
| 3852 |
FP->setName(F->getName()); |
| 3853 |
const unsigned AS = BM->shouldEmitFunctionPtrAddrSpace() |
| 3854 |
? SPIRAS_CodeSectionINTEL |
| 3855 |
: SPIRAS_Private; |
| 3856 |
mapValue(FP, transFunction(F, AS)); |
| 3857 |
} |
| 3858 |
|
| 3859 |
for (unsigned I = 0, E = BM->getNumFunctions(); I != E; ++I) { |
| 3860 |
transFunction(BM->getFunction(I)); |
| 3861 |
transUserSemantic(BM->getFunction(I)); |
| 3862 |
} |
| 3863 |
|
| 3864 |
transGlobalAnnotations(); |
| 3865 |
|
| 3866 |
if (!transMetadata()) |
| 3867 |
return false; |
| 3868 |
if (!transFPContractMetadata()) |
| 3869 |
return false; |
| 3870 |
transSourceLanguage(); |
| 3871 |
if (!transSourceExtension()) |
| 3872 |
return false; |
| 3873 |
transGeneratorMD(); |
| 3874 |
if (!lowerBuiltins(BM, M)) |
| 3875 |
return false; |
| 3876 |
if (BM->getDesiredBIsRepresentation() == BIsRepresentation::SPIRVFriendlyIR) { |
| 3877 |
SPIRVWord SrcLangVer = 0; |
| 3878 |
BM->getSourceLanguage(&SrcLangVer); |
| 3879 |
bool IsCpp = SrcLangVer == kOCLVer::CL21; |
| 3880 |
if (!postProcessBuiltinsReturningStruct(M, IsCpp)) |
| 3881 |
return false; |
| 3882 |
} |
| 3883 |
|
| 3884 |
for (SPIRVExtInst *EI : BM->getAuxDataInstVec()) { |
| 3885 |
transAuxDataInst(EI); |
| 3886 |
} |
| 3887 |
|
| 3888 |
eraseUselessFunctions(M); |
| 3889 |
|
| 3890 |
DbgTran->addDbgInfoVersion(); |
| 3891 |
DbgTran->finalize(); |
| 3892 |
|
| 3893 |
return true; |
| 3894 |
} |
| 3895 |
|
| 3896 |
bool SPIRVToLLVM::transAddressingModel() { |
| 3897 |
switch (BM->getAddressingModel()) { |
| 3898 |
case AddressingModelPhysical64: |
| 3899 |
M->setTargetTriple(SPIR_TARGETTRIPLE64); |
| 3900 |
M->setDataLayout(SPIR_DATALAYOUT64); |
| 3901 |
break; |
| 3902 |
case AddressingModelPhysical32: |
| 3903 |
M->setTargetTriple(SPIR_TARGETTRIPLE32); |
| 3904 |
M->setDataLayout(SPIR_DATALAYOUT32); |
| 3905 |
break; |
| 3906 |
case AddressingModelLogical: |
| 3907 |
// Do not set target triple and data layout |
| 3908 |
break; |
| 3909 |
default: |
| 3910 |
SPIRVCKRT(0, InvalidAddressingModel, |
| 3911 |
"Actual addressing mode is " + |
| 3912 |
std::to_string(BM->getAddressingModel())); |
| 3913 |
} |
| 3914 |
return true; |
| 3915 |
} |
| 3916 |
|
| 3917 |
void generateIntelFPGAAnnotation( |
| 3918 |
const SPIRVEntry *E, std::vector<llvm::SmallString<256>> &AnnotStrVec) { |
| 3919 |
llvm::SmallString<256> AnnotStr; |
| 3920 |
llvm::raw_svector_ostream Out(AnnotStr); |
| 3921 |
if (E->hasDecorate(DecorationRegisterINTEL)) |
| 3922 |
Out << "{register:1}"; |
| 3923 |
|
| 3924 |
SPIRVWord Result = 0; |
| 3925 |
if (E->hasDecorate(DecorationMemoryINTEL)) |
| 3926 |
Out << "{memory:" |
| 3927 |
<< E->getDecorationStringLiteral(DecorationMemoryINTEL).front() << '}'; |
| 3928 |
if (E->hasDecorate(DecorationBankwidthINTEL, 0, &Result)) |
| 3929 |
Out << "{bankwidth:" << Result << '}'; |
| 3930 |
if (E->hasDecorate(DecorationNumbanksINTEL, 0, &Result)) |
| 3931 |
Out << "{numbanks:" << Result << '}'; |
| 3932 |
if (E->hasDecorate(DecorationMaxPrivateCopiesINTEL, 0, &Result)) |
| 3933 |
Out << "{private_copies:" << Result << '}'; |
| 3934 |
if (E->hasDecorate(DecorationSinglepumpINTEL)) |
| 3935 |
Out << "{pump:1}"; |
| 3936 |
if (E->hasDecorate(DecorationDoublepumpINTEL)) |
| 3937 |
Out << "{pump:2}"; |
| 3938 |
if (E->hasDecorate(DecorationMaxReplicatesINTEL, 0, &Result)) |
| 3939 |
Out << "{max_replicates:" << Result << '}'; |
| 3940 |
if (E->hasDecorate(DecorationSimpleDualPortINTEL)) |
| 3941 |
Out << "{simple_dual_port:1}"; |
| 3942 |
if (E->hasDecorate(DecorationMergeINTEL)) { |
| 3943 |
Out << "{merge"; |
| 3944 |
for (const auto &Str : E->getDecorationStringLiteral(DecorationMergeINTEL)) |
| 3945 |
Out << ":" << Str; |
| 3946 |
Out << '}'; |
| 3947 |
} |
| 3948 |
if (E->hasDecorate(DecorationBankBitsINTEL)) { |
| 3949 |
Out << "{bank_bits:"; |
| 3950 |
auto Literals = E->getDecorationLiterals(DecorationBankBitsINTEL); |
| 3951 |
for (size_t I = 0; I < Literals.size() - 1; ++I) |
| 3952 |
Out << Literals[I] << ","; |
| 3953 |
Out << Literals.back() << '}'; |
| 3954 |
} |
| 3955 |
if (E->hasDecorate(DecorationForcePow2DepthINTEL, 0, &Result)) |
| 3956 |
Out << "{force_pow2_depth:" << Result << '}'; |
| 3957 |
|
| 3958 |
unsigned LSUParamsBitmask = 0; |
| 3959 |
llvm::SmallString<32> AdditionalParamsStr; |
| 3960 |
llvm::raw_svector_ostream ParamsOut(AdditionalParamsStr); |
| 3961 |
if (E->hasDecorate(DecorationBurstCoalesceINTEL, 0)) |
| 3962 |
LSUParamsBitmask |= IntelFPGAMemoryAccessesVal::BurstCoalesce; |
| 3963 |
if (E->hasDecorate(DecorationCacheSizeINTEL, 0, &Result)) { |
| 3964 |
LSUParamsBitmask |= IntelFPGAMemoryAccessesVal::CacheSizeFlag; |
| 3965 |
ParamsOut << "{cache-size:" << Result << "}"; |
| 3966 |
} |
| 3967 |
if (E->hasDecorate(DecorationDontStaticallyCoalesceINTEL, 0)) |
| 3968 |
LSUParamsBitmask |= IntelFPGAMemoryAccessesVal::DontStaticallyCoalesce; |
| 3969 |
if (E->hasDecorate(DecorationPrefetchINTEL, 0, &Result)) { |
| 3970 |
LSUParamsBitmask |= IntelFPGAMemoryAccessesVal::PrefetchFlag; |
| 3971 |
// TODO: Enable prefetch size backwards translation |
| 3972 |
// once it is supported |
| 3973 |
} |
| 3974 |
if (LSUParamsBitmask) |
| 3975 |
Out << "{params:" << LSUParamsBitmask << "}" << AdditionalParamsStr; |
| 3976 |
if (!AnnotStr.empty()) |
| 3977 |
AnnotStrVec.emplace_back(AnnotStr); |
| 3978 |
|
| 3979 |
if (E->hasDecorate(DecorationUserSemantic)) { |
| 3980 |
auto Annotations = |
| 3981 |
E->getAllDecorationStringLiterals(DecorationUserSemantic); |
| 3982 |
for (size_t I = 0; I != Annotations.size(); ++I) { |
| 3983 |
// UserSemantic has a single literal string |
| 3984 |
llvm::SmallString<256> UserSemanticStr; |
| 3985 |
llvm::raw_svector_ostream UserSemanticOut(UserSemanticStr); |
| 3986 |
for (const auto &Str : Annotations[I]) |
| 3987 |
UserSemanticOut << Str; |
| 3988 |
AnnotStrVec.emplace_back(UserSemanticStr); |
| 3989 |
} |
| 3990 |
} |
| 3991 |
} |
| 3992 |
|
| 3993 |
void generateIntelFPGAAnnotationForStructMember( |
| 3994 |
const SPIRVEntry *E, SPIRVWord MemberNumber, |
| 3995 |
std::vector<llvm::SmallString<256>> &AnnotStrVec) { |
| 3996 |
llvm::SmallString<256> AnnotStr; |
| 3997 |
llvm::raw_svector_ostream Out(AnnotStr); |
| 3998 |
if (E->hasMemberDecorate(DecorationRegisterINTEL, 0, MemberNumber)) |
| 3999 |
Out << "{register:1}"; |
| 4000 |
|
| 4001 |
SPIRVWord Result = 0; |
| 4002 |
if (E->hasMemberDecorate(DecorationMemoryINTEL, 0, MemberNumber, &Result)) |
| 4003 |
Out << "{memory:" |
| 4004 |
<< E->getMemberDecorationStringLiteral(DecorationMemoryINTEL, |
| 4005 |
MemberNumber) |
| 4006 |
.front() |
| 4007 |
<< '}'; |
| 4008 |
if (E->hasMemberDecorate(DecorationBankwidthINTEL, 0, MemberNumber, &Result)) |
| 4009 |
Out << "{bankwidth:" << Result << '}'; |
| 4010 |
if (E->hasMemberDecorate(DecorationNumbanksINTEL, 0, MemberNumber, &Result)) |
| 4011 |
Out << "{numbanks:" << Result << '}'; |
| 4012 |
if (E->hasMemberDecorate(DecorationMaxPrivateCopiesINTEL, 0, MemberNumber, |
| 4013 |
&Result)) |
| 4014 |
Out << "{private_copies:" << Result << '}'; |
| 4015 |
if (E->hasMemberDecorate(DecorationSinglepumpINTEL, 0, MemberNumber)) |
| 4016 |
Out << "{pump:1}"; |
| 4017 |
if (E->hasMemberDecorate(DecorationDoublepumpINTEL, 0, MemberNumber)) |
| 4018 |
Out << "{pump:2}"; |
| 4019 |
if (E->hasMemberDecorate(DecorationMaxReplicatesINTEL, 0, MemberNumber, |
| 4020 |
&Result)) |
| 4021 |
Out << "{max_replicates:" << Result << '}'; |
| 4022 |
if (E->hasMemberDecorate(DecorationSimpleDualPortINTEL, 0, MemberNumber)) |
| 4023 |
Out << "{simple_dual_port:1}"; |
| 4024 |
if (E->hasMemberDecorate(DecorationMergeINTEL, 0, MemberNumber)) { |
| 4025 |
Out << "{merge"; |
| 4026 |
for (const auto &Str : E->getMemberDecorationStringLiteral( |
| 4027 |
DecorationMergeINTEL, MemberNumber)) |
| 4028 |
Out << ":" << Str; |
| 4029 |
Out << '}'; |
| 4030 |
} |
| 4031 |
if (E->hasMemberDecorate(DecorationBankBitsINTEL, 0, MemberNumber)) { |
| 4032 |
Out << "{bank_bits:"; |
| 4033 |
auto Literals = |
| 4034 |
E->getMemberDecorationLiterals(DecorationBankBitsINTEL, MemberNumber); |
| 4035 |
for (size_t I = 0; I < Literals.size() - 1; ++I) |
| 4036 |
Out << Literals[I] << ","; |
| 4037 |
Out << Literals.back() << '}'; |
| 4038 |
} |
| 4039 |
if (E->hasMemberDecorate(DecorationForcePow2DepthINTEL, 0, MemberNumber, |
| 4040 |
&Result)) |
| 4041 |
Out << "{force_pow2_depth:" << Result << '}'; |
| 4042 |
if (!AnnotStr.empty()) |
| 4043 |
AnnotStrVec.emplace_back(AnnotStr); |
| 4044 |
|
| 4045 |
if (E->hasMemberDecorate(DecorationUserSemantic, 0, MemberNumber)) { |
| 4046 |
auto Annotations = E->getAllMemberDecorationStringLiterals( |
| 4047 |
DecorationUserSemantic, MemberNumber); |
| 4048 |
for (size_t I = 0; I != Annotations.size(); ++I) { |
| 4049 |
// UserSemantic has a single literal string |
| 4050 |
llvm::SmallString<256> UserSemanticStr; |
| 4051 |
llvm::raw_svector_ostream UserSemanticOut(UserSemanticStr); |
| 4052 |
for (const auto &Str : Annotations[I]) |
| 4053 |
UserSemanticOut << Str; |
| 4054 |
AnnotStrVec.emplace_back(UserSemanticStr); |
| 4055 |
} |
| 4056 |
} |
| 4057 |
} |
| 4058 |
|
| 4059 |
void SPIRVToLLVM::transIntelFPGADecorations(SPIRVValue *BV, Value *V) { |
| 4060 |
if (!BV->isVariable() && !BV->isInst()) |
| 4061 |
return; |
| 4062 |
|
| 4063 |
if (auto *Inst = dyn_cast<Instruction>(V)) { |
| 4064 |
auto *AL = dyn_cast<AllocaInst>(Inst); |
| 4065 |
Type *AllocatedTy = AL ? AL->getAllocatedType() : Inst->getType(); |
| 4066 |
|
| 4067 |
IRBuilder<> Builder(Inst->getParent()); |
| 4068 |
|
| 4069 |
Type *Int8PtrTyPrivate = Type::getInt8PtrTy(*Context, SPIRAS_Private); |
| 4070 |
IntegerType *Int32Ty = IntegerType::get(*Context, 32); |
| 4071 |
|
| 4072 |
Value *UndefInt8Ptr = UndefValue::get(Int8PtrTyPrivate); |
| 4073 |
Value *UndefInt32 = UndefValue::get(Int32Ty); |
| 4074 |
|
| 4075 |
if (AL && BV->getType()->getPointerElementType()->isTypeStruct()) { |
| 4076 |
auto *ST = BV->getType()->getPointerElementType(); |
| 4077 |
SPIRVTypeStruct *STS = static_cast<SPIRVTypeStruct *>(ST); |
| 4078 |
|
| 4079 |
for (SPIRVWord I = 0; I < STS->getMemberCount(); ++I) { |
| 4080 |
std::vector<SmallString<256>> AnnotStrVec; |
| 4081 |
generateIntelFPGAAnnotationForStructMember(ST, I, AnnotStrVec); |
| 4082 |
for (const auto &AnnotStr : AnnotStrVec) { |
| 4083 |
auto *GS = Builder.CreateGlobalStringPtr(AnnotStr); |
| 4084 |
|
| 4085 |
Instruction *PtrAnnFirstArg = nullptr; |
| 4086 |
|
| 4087 |
if (GEPOrUseMap.count(AL)) { |
| 4088 |
auto IdxToInstMap = GEPOrUseMap[AL]; |
| 4089 |
if (IdxToInstMap.count(I)) { |
| 4090 |
PtrAnnFirstArg = IdxToInstMap[I]; |
| 4091 |
} |
| 4092 |
} |
| 4093 |
|
| 4094 |
Type *IntTy = nullptr; |
| 4095 |
|
| 4096 |
if (!PtrAnnFirstArg) { |
| 4097 |
GetElementPtrInst *GEP = cast<GetElementPtrInst>( |
| 4098 |
Builder.CreateConstInBoundsGEP2_32(AllocatedTy, AL, 0, I)); |
| 4099 |
|
| 4100 |
IntTy = GEP->getResultElementType()->isIntegerTy() |
| 4101 |
? GEP->getType() |
| 4102 |
: Int8PtrTyPrivate; |
| 4103 |
PtrAnnFirstArg = GEP; |
| 4104 |
} else { |
| 4105 |
IntTy = PtrAnnFirstArg->getType(); |
| 4106 |
} |
| 4107 |
|
| 4108 |
auto *AnnotationFn = llvm::Intrinsic::getDeclaration( |
| 4109 |
M, Intrinsic::ptr_annotation, {IntTy, Int8PtrTyPrivate}); |
| 4110 |
|
| 4111 |
llvm::Value *Args[] = { |
| 4112 |
Builder.CreateBitCast(PtrAnnFirstArg, IntTy, |
| 4113 |
PtrAnnFirstArg->getName()), |
| 4114 |
Builder.CreateBitCast(GS, Int8PtrTyPrivate), UndefInt8Ptr, |
| 4115 |
UndefInt32, UndefInt8Ptr}; |
| 4116 |
auto *PtrAnnotationCall = Builder.CreateCall(AnnotationFn, Args); |
| 4117 |
GEPOrUseMap[AL][I] = PtrAnnotationCall; |
| 4118 |
} |
| 4119 |
} |
| 4120 |
} |
| 4121 |
|
| 4122 |
std::vector<SmallString<256>> AnnotStrVec; |
| 4123 |
generateIntelFPGAAnnotation(BV, AnnotStrVec); |
| 4124 |
for (const auto &AnnotStr : AnnotStrVec) { |
| 4125 |
Constant *GS = nullptr; |
| 4126 |
const auto StringAnnotStr = static_cast<std::string>(AnnotStr); |
| 4127 |
auto AnnotItr = AnnotationsMap.find(StringAnnotStr); |
| 4128 |
if (AnnotItr != AnnotationsMap.end()) { |
| 4129 |
GS = AnnotItr->second; |
| 4130 |
} else { |
| 4131 |
GS = Builder.CreateGlobalStringPtr(AnnotStr); |
| 4132 |
AnnotationsMap.emplace(std::move(StringAnnotStr), GS); |
| 4133 |
} |
| 4134 |
|
| 4135 |
Value *BaseInst = |
| 4136 |
AL ? Builder.CreateBitCast(V, Int8PtrTyPrivate, V->getName()) : Inst; |
| 4137 |
|
| 4138 |
// Try to find alloca instruction for statically allocated variables. |
| 4139 |
// Alloca might be hidden by a couple of casts. |
| 4140 |
bool isStaticMemoryAttribute = AL ? true : false; |
| 4141 |
while (!isStaticMemoryAttribute && Inst && |
| 4142 |
(isa<BitCastInst>(Inst) || isa<AddrSpaceCastInst>(Inst))) { |
| 4143 |
Inst = dyn_cast<Instruction>(Inst->getOperand(0)); |
| 4144 |
isStaticMemoryAttribute = (Inst && isa<AllocaInst>(Inst)); |
| 4145 |
} |
| 4146 |
auto *AnnotationFn = llvm::Intrinsic::getDeclaration( |
| 4147 |
M, |
| 4148 |
isStaticMemoryAttribute ? Intrinsic::var_annotation |
| 4149 |
: Intrinsic::ptr_annotation, |
| 4150 |
{BaseInst->getType(), Int8PtrTyPrivate}); |
| 4151 |
|
| 4152 |
llvm::Value *Args[] = {BaseInst, |
| 4153 |
Builder.CreateBitCast(GS, Int8PtrTyPrivate), |
| 4154 |
UndefInt8Ptr, UndefInt32, UndefInt8Ptr}; |
| 4155 |
Builder.CreateCall(AnnotationFn, Args); |
| 4156 |
} |
| 4157 |
} else if (auto *GV = dyn_cast<GlobalVariable>(V)) { |
| 4158 |
// Do not add annotations for builtin variables. |
| 4159 |
SPIRVBuiltinVariableKind Kind; |
| 4160 |
if (isSPIRVBuiltinVariable(GV, &Kind)) |
| 4161 |
return; |
| 4162 |
|
| 4163 |
std::vector<SmallString<256>> AnnotStrVec; |
| 4164 |
generateIntelFPGAAnnotation(BV, AnnotStrVec); |
| 4165 |
|
| 4166 |
if (AnnotStrVec.empty()) { |
| 4167 |
// Check if IO pipe decoration is applied to the global |
| 4168 |
SPIRVWord ID; |
| 4169 |
if (BV->hasDecorate(DecorationIOPipeStorageINTEL, 0, &ID)) { |
| 4170 |
auto Literals = BV->getDecorationLiterals(DecorationIOPipeStorageINTEL); |
| 4171 |
assert(Literals.size() == 1 && |
| 4172 |
"IO PipeStorage decoration shall have 1 extra operand"); |
| 4173 |
GV->setMetadata("io_pipe_id", getMDNodeStringIntVec(Context, Literals)); |
| 4174 |
} |
| 4175 |
return; |
| 4176 |
} |
| 4177 |
|
| 4178 |
for (const auto &AnnotStr : AnnotStrVec) { |
| 4179 |
Constant *StrConstant = |
| 4180 |
ConstantDataArray::getString(*Context, StringRef(AnnotStr)); |
| 4181 |
|
| 4182 |
auto *GS = new GlobalVariable( |
| 4183 |
*GV->getParent(), StrConstant->getType(), |
| 4184 |
/*IsConstant*/ true, GlobalValue::PrivateLinkage, StrConstant, ""); |
| 4185 |
|
| 4186 |
GS->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
| 4187 |
GS->setSection("llvm.metadata"); |
| 4188 |
|
| 4189 |
Type *ResType = PointerType::getInt8PtrTy( |
| 4190 |
GV->getContext(), GV->getType()->getPointerAddressSpace()); |
| 4191 |
Constant *C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, ResType); |
| 4192 |
|
| 4193 |
Type *Int8PtrTyPrivate = Type::getInt8PtrTy(*Context, SPIRAS_Private); |
| 4194 |
IntegerType *Int32Ty = Type::getInt32Ty(*Context); |
| 4195 |
|
| 4196 |
llvm::Constant *Fields[5] = { |
| 4197 |
C, ConstantExpr::getBitCast(GS, Int8PtrTyPrivate), |
| 4198 |
UndefValue::get(Int8PtrTyPrivate), UndefValue::get(Int32Ty), |
| 4199 |
UndefValue::get(Int8PtrTyPrivate)}; |
| 4200 |
|
| 4201 |
GlobalAnnotations.push_back(ConstantStruct::getAnon(Fields)); |
| 4202 |
} |
| 4203 |
} |
| 4204 |
} |
| 4205 |
|
| 4206 |
// Translate aliasing decorations applied to instructions. These decorations |
| 4207 |
// are mapped on alias.scope and noalias metadata in LLVM. Translation of |
| 4208 |
// optional string operand isn't yet supported in the translator. |
| 4209 |
void SPIRVToLLVM::transMemAliasingINTELDecorations(SPIRVValue *BV, Value *V) { |
| 4210 |
if (!BV->isInst()) |
| 4211 |
return; |
| 4212 |
Instruction *Inst = dyn_cast<Instruction>(V); |
| 4213 |
if (!Inst) |
| 4214 |
return; |
| 4215 |
if (BV->hasDecorateId(DecorationAliasScopeINTEL)) { |
| 4216 |
std::vector<SPIRVId> AliasListIds; |
| 4217 |
AliasListIds = BV->getDecorationIdLiterals(DecorationAliasScopeINTEL); |
| 4218 |
assert(AliasListIds.size() == 1 && |
| 4219 |
"Memory aliasing decorations must have one argument"); |
| 4220 |
addMemAliasMetadata(Inst, AliasListIds[0], LLVMContext::MD_alias_scope); |
| 4221 |
} |
| 4222 |
if (BV->hasDecorateId(DecorationNoAliasINTEL)) { |
| 4223 |
std::vector<SPIRVId> AliasListIds; |
| 4224 |
AliasListIds = BV->getDecorationIdLiterals(DecorationNoAliasINTEL); |
| 4225 |
assert(AliasListIds.size() == 1 && |
| 4226 |
"Memory aliasing decorations must have one argument"); |
| 4227 |
addMemAliasMetadata(Inst, AliasListIds[0], LLVMContext::MD_noalias); |
| 4228 |
} |
| 4229 |
} |
| 4230 |
|
| 4231 |
// Having UserSemantic decoration on Function is against the spec, but we allow |
| 4232 |
// this for various purposes (like prototyping new features when we need to |
| 4233 |
// attach some information on function and propagate that through SPIR-V and |
| 4234 |
// ect.) |
| 4235 |
void SPIRVToLLVM::transUserSemantic(SPIRV::SPIRVFunction *Fun) { |
| 4236 |
auto TransFun = transFunction(Fun); |
| 4237 |
for (const auto &UsSem : |
| 4238 |
Fun->getDecorationStringLiteral(DecorationUserSemantic)) { |
| 4239 |
auto V = cast<Value>(TransFun); |
| 4240 |
Constant *StrConstant = |
| 4241 |
ConstantDataArray::getString(*Context, StringRef(UsSem)); |
| 4242 |
auto *GS = new GlobalVariable( |
| 4243 |
*TransFun->getParent(), StrConstant->getType(), |
| 4244 |
/*IsConstant*/ true, GlobalValue::PrivateLinkage, StrConstant, ""); |
| 4245 |
|
| 4246 |
GS->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
| 4247 |
GS->setSection("llvm.metadata"); |
| 4248 |
|
| 4249 |
Type *ResType = PointerType::getInt8PtrTy( |
| 4250 |
V->getContext(), V->getType()->getPointerAddressSpace()); |
| 4251 |
Constant *C = |
| 4252 |
ConstantExpr::getPointerBitCastOrAddrSpaceCast(TransFun, ResType); |
| 4253 |
|
| 4254 |
Type *Int8PtrTyPrivate = Type::getInt8PtrTy(*Context, SPIRAS_Private); |
| 4255 |
IntegerType *Int32Ty = Type::getInt32Ty(*Context); |
| 4256 |
|
| 4257 |
llvm::Constant *Fields[5] = { |
| 4258 |
C, ConstantExpr::getBitCast(GS, Int8PtrTyPrivate), |
| 4259 |
UndefValue::get(Int8PtrTyPrivate), UndefValue::get(Int32Ty), |
| 4260 |
UndefValue::get(Int8PtrTyPrivate)}; |
| 4261 |
GlobalAnnotations.push_back(ConstantStruct::getAnon(Fields)); |
| 4262 |
} |
| 4263 |
} |
| 4264 |
|
| 4265 |
void SPIRVToLLVM::transGlobalAnnotations() { |
| 4266 |
if (!GlobalAnnotations.empty()) { |
| 4267 |
Constant *Array = |
| 4268 |
ConstantArray::get(ArrayType::get(GlobalAnnotations[0]->getType(), |
| 4269 |
GlobalAnnotations.size()), |
| 4270 |
GlobalAnnotations); |
| 4271 |
auto *GV = new GlobalVariable(*M, Array->getType(), /*IsConstant*/ false, |
| 4272 |
GlobalValue::AppendingLinkage, Array, |
| 4273 |
"llvm.global.annotations"); |
| 4274 |
GV->setSection("llvm.metadata"); |
| 4275 |
} |
| 4276 |
} |
| 4277 |
|
| 4278 |
static llvm::MDNode * |
| 4279 |
transDecorationsToMetadataList(llvm::LLVMContext *Context, |
| 4280 |
std::vector<SPIRVDecorate const *> Decorates) { |
| 4281 |
SmallVector<Metadata *, 4> MDs; |
| 4282 |
MDs.reserve(Decorates.size()); |
| 4283 |
for (const auto *Deco : Decorates) { |
| 4284 |
std::vector<Metadata *> OPs; |
| 4285 |
auto *KindMD = ConstantAsMetadata::get( |
| 4286 |
ConstantInt::get(Type::getInt32Ty(*Context), Deco->getDecorateKind())); |
| 4287 |
OPs.push_back(KindMD); |
| 4288 |
switch (static_cast<size_t>(Deco->getDecorateKind())) { |
| 4289 |
case DecorationLinkageAttributes: { |
| 4290 |
const auto *const LinkAttrDeco = |
| 4291 |
static_cast<const SPIRVDecorateLinkageAttr *>(Deco); |
| 4292 |
auto *const LinkNameMD = |
| 4293 |
MDString::get(*Context, LinkAttrDeco->getLinkageName()); |
| 4294 |
auto *const LinkTypeMD = ConstantAsMetadata::get(ConstantInt::get( |
| 4295 |
Type::getInt32Ty(*Context), LinkAttrDeco->getLinkageType())); |
| 4296 |
OPs.push_back(LinkNameMD); |
| 4297 |
OPs.push_back(LinkTypeMD); |
| 4298 |
break; |
| 4299 |
} |
| 4300 |
case spv::internal::DecorationHostAccessINTEL: |
| 4301 |
case DecorationHostAccessINTEL: { |
| 4302 |
const auto *const HostAccDeco = |
| 4303 |
static_cast<const SPIRVDecorateHostAccessINTEL *>(Deco); |
| 4304 |
auto *const AccModeMD = ConstantAsMetadata::get(ConstantInt::get( |
| 4305 |
Type::getInt32Ty(*Context), HostAccDeco->getAccessMode())); |
| 4306 |
auto *const NameMD = MDString::get(*Context, HostAccDeco->getVarName()); |
| 4307 |
OPs.push_back(AccModeMD); |
| 4308 |
OPs.push_back(NameMD); |
| 4309 |
break; |
| 4310 |
} |
| 4311 |
case DecorationMergeINTEL: { |
| 4312 |
const auto MergeAttrLits = Deco->getVecLiteral(); |
| 4313 |
std::string FirstString = getString(MergeAttrLits); |
| 4314 |
std::string SecondString = |
| 4315 |
getString(MergeAttrLits.cbegin() + getVec(FirstString).size(), |
| 4316 |
MergeAttrLits.cend()); |
| 4317 |
OPs.push_back(MDString::get(*Context, FirstString)); |
| 4318 |
OPs.push_back(MDString::get(*Context, SecondString)); |
| 4319 |
break; |
| 4320 |
} |
| 4321 |
case DecorationMemoryINTEL: |
| 4322 |
case DecorationUserSemantic: { |
| 4323 |
auto *const StrMD = |
| 4324 |
MDString::get(*Context, getString(Deco->getVecLiteral())); |
| 4325 |
OPs.push_back(StrMD); |
| 4326 |
break; |
| 4327 |
} |
| 4328 |
default: { |
| 4329 |
for (const SPIRVWord Lit : Deco->getVecLiteral()) { |
| 4330 |
auto *const LitMD = ConstantAsMetadata::get( |
| 4331 |
ConstantInt::get(Type::getInt32Ty(*Context), Lit)); |
| 4332 |
OPs.push_back(LitMD); |
| 4333 |
} |
| 4334 |
break; |
| 4335 |
} |
| 4336 |
} |
| 4337 |
MDs.push_back(MDNode::get(*Context, OPs)); |
| 4338 |
} |
| 4339 |
return MDNode::get(*Context, MDs); |
| 4340 |
} |
| 4341 |
|
| 4342 |
void SPIRVToLLVM::transDecorationsToMetadata(SPIRVValue *BV, Value *V) { |
| 4343 |
if (!BV->isVariable() && !BV->isInst()) |
| 4344 |
return; |
| 4345 |
|
| 4346 |
auto SetDecorationsMetadata = [&](auto V) { |
| 4347 |
std::vector<SPIRVDecorate const *> Decorates = BV->getDecorations(); |
| 4348 |
if (!Decorates.empty()) { |
| 4349 |
MDNode *MDList = transDecorationsToMetadataList(Context, Decorates); |
| 4350 |
V->setMetadata(SPIRV_MD_DECORATIONS, MDList); |
| 4351 |
} |
| 4352 |
}; |
| 4353 |
|
| 4354 |
if (auto *GV = dyn_cast<GlobalVariable>(V)) |
| 4355 |
SetDecorationsMetadata(GV); |
| 4356 |
else if (auto *I = dyn_cast<Instruction>(V)) |
| 4357 |
SetDecorationsMetadata(I); |
| 4358 |
} |
| 4359 |
|
| 4360 |
namespace { |
| 4361 |
|
| 4362 |
static float convertSPIRVWordToFloat(SPIRVWord Spir) { |
| 4363 |
union { |
| 4364 |
float F; |
| 4365 |
SPIRVWord Spir; |
| 4366 |
} FPMaxError; |
| 4367 |
FPMaxError.Spir = Spir; |
| 4368 |
return FPMaxError.F; |
| 4369 |
} |
| 4370 |
|
| 4371 |
static bool transFPMaxErrorDecoration(SPIRVValue *BV, Value *V, |
| 4372 |
LLVMContext *Context) { |
| 4373 |
SPIRVWord ID; |
| 4374 |
if (Instruction *I = dyn_cast<Instruction>(V)) |
| 4375 |
if (BV->hasDecorate(DecorationFPMaxErrorDecorationINTEL, 0, &ID)) { |
| 4376 |
auto Literals = |
| 4377 |
BV->getDecorationLiterals(DecorationFPMaxErrorDecorationINTEL); |
| 4378 |
assert(Literals.size() == 1 && |
| 4379 |
"FP Max Error decoration shall have 1 operand"); |
| 4380 |
auto F = convertSPIRVWordToFloat(Literals[0]); |
| 4381 |
if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 4382 |
// Add attribute |
| 4383 |
auto A = llvm::Attribute::get(*Context, "fpbuiltin-max-error", |
| 4384 |
std::to_string(F)); |
| 4385 |
CI->addFnAttr(A); |
| 4386 |
} else { |
| 4387 |
// Add metadata |
| 4388 |
MDNode *N = |
| 4389 |
MDNode::get(*Context, MDString::get(*Context, std::to_string(F))); |
| 4390 |
I->setMetadata("fpbuiltin-max-error", N); |
| 4391 |
} |
| 4392 |
return true; |
| 4393 |
} |
| 4394 |
return false; |
| 4395 |
} |
| 4396 |
} // namespace |
| 4397 |
|
| 4398 |
bool SPIRVToLLVM::transDecoration(SPIRVValue *BV, Value *V) { |
| 4399 |
if (transFPMaxErrorDecoration(BV, V, Context)) |
| 4400 |
return true; |
| 4401 |
|
| 4402 |
if (!transAlign(BV, V)) |
| 4403 |
return false; |
| 4404 |
|
| 4405 |
transIntelFPGADecorations(BV, V); |
| 4406 |
transMemAliasingINTELDecorations(BV, V); |
| 4407 |
|
| 4408 |
// Decoration metadata is only enabled in SPIR-V friendly mode |
| 4409 |
if (BM->getDesiredBIsRepresentation() == BIsRepresentation::SPIRVFriendlyIR) |
| 4410 |
transDecorationsToMetadata(BV, V); |
| 4411 |
|
| 4412 |
DbgTran->transDbgInfo(BV, V); |
| 4413 |
return true; |
| 4414 |
} |
| 4415 |
|
| 4416 |
void SPIRVToLLVM::transGlobalCtorDtors(SPIRVVariable *BV) { |
| 4417 |
if (BV->getName() != "llvm.global_ctors" && |
| 4418 |
BV->getName() != "llvm.global_dtors") |
| 4419 |
return; |
| 4420 |
|
| 4421 |
Value *V = transValue(BV, nullptr, nullptr); |
| 4422 |
cast<GlobalValue>(V)->setLinkage(GlobalValue::AppendingLinkage); |
| 4423 |
} |
| 4424 |
|
| 4425 |
void SPIRVToLLVM::createCXXStructor(const char *ListName, |
| 4426 |
SmallVectorImpl<Function *> &Funcs) { |
| 4427 |
if (Funcs.empty()) |
| 4428 |
return; |
| 4429 |
|
| 4430 |
// If the SPIR-V input contained a variable for the structor list and it |
| 4431 |
// has already been translated, then don't interfere. |
| 4432 |
if (M->getGlobalVariable(ListName)) |
| 4433 |
return; |
| 4434 |
|
| 4435 |
// Type of a structor entry: { i32, void ()*, i8* } |
| 4436 |
Type *PriorityTy = Type::getInt32Ty(*Context); |
| 4437 |
PointerType *CtorTy = PointerType::getUnqual( |
| 4438 |
FunctionType::get(Type::getVoidTy(*Context), false)); |
| 4439 |
PointerType *ComdatTy = Type::getInt8PtrTy(*Context); |
| 4440 |
StructType *StructorTy = StructType::get(PriorityTy, CtorTy, ComdatTy); |
| 4441 |
|
| 4442 |
ArrayType *ArrTy = ArrayType::get(StructorTy, Funcs.size()); |
| 4443 |
|
| 4444 |
GlobalVariable *GV = |
| 4445 |
cast<GlobalVariable>(M->getOrInsertGlobal(ListName, ArrTy)); |
| 4446 |
GV->setLinkage(GlobalValue::AppendingLinkage); |
| 4447 |
|
| 4448 |
// Build the initializer. |
| 4449 |
SmallVector<Constant *, 2> ArrayElts; |
| 4450 |
for (auto *F : Funcs) { |
| 4451 |
SmallVector<Constant *, 3> Elts; |
| 4452 |
// SPIR-V does not specify an order between Initializers, so set default |
| 4453 |
// priority. |
| 4454 |
Elts.push_back(ConstantInt::get(PriorityTy, 65535)); |
| 4455 |
Elts.push_back(ConstantExpr::getBitCast(F, CtorTy)); |
| 4456 |
Elts.push_back(ConstantPointerNull::get(ComdatTy)); |
| 4457 |
ArrayElts.push_back(ConstantStruct::get(StructorTy, Elts)); |
| 4458 |
} |
| 4459 |
|
| 4460 |
Constant *NewArray = ConstantArray::get(ArrTy, ArrayElts); |
| 4461 |
GV->setInitializer(NewArray); |
| 4462 |
} |
| 4463 |
|
| 4464 |
bool SPIRVToLLVM::transFPContractMetadata() { |
| 4465 |
bool ContractOff = false; |
| 4466 |
for (unsigned I = 0, E = BM->getNumFunctions(); I != E; ++I) { |
| 4467 |
SPIRVFunction *BF = BM->getFunction(I); |
| 4468 |
if (!isKernel(BF)) |
| 4469 |
continue; |
| 4470 |
if (BF->getExecutionMode(ExecutionModeContractionOff)) { |
| 4471 |
ContractOff = true; |
| 4472 |
break; |
| 4473 |
} |
| 4474 |
} |
| 4475 |
if (!ContractOff) |
| 4476 |
M->getOrInsertNamedMetadata(kSPIR2MD::FPContract); |
| 4477 |
return true; |
| 4478 |
} |
| 4479 |
|
| 4480 |
std::string |
| 4481 |
SPIRVToLLVM::transOCLImageTypeAccessQualifier(SPIRV::SPIRVTypeImage *ST) { |
| 4482 |
return SPIRSPIRVAccessQualifierMap::rmap(ST->hasAccessQualifier() |
| 4483 |
? ST->getAccessQualifier() |
| 4484 |
: AccessQualifierReadOnly); |
| 4485 |
} |
| 4486 |
|
| 4487 |
bool SPIRVToLLVM::transNonTemporalMetadata(Instruction *I) { |
| 4488 |
Constant *One = ConstantInt::get(Type::getInt32Ty(*Context), 1); |
| 4489 |
MDNode *Node = MDNode::get(*Context, ConstantAsMetadata::get(One)); |
| 4490 |
I->setMetadata(M->getMDKindID("nontemporal"), Node); |
| 4491 |
return true; |
| 4492 |
} |
| 4493 |
|
| 4494 |
// Information of types of kernel arguments may be additionally stored in |
| 4495 |
// 'OpString "kernel_arg_type.%kernel_name%.type1,type2,type3,..' instruction. |
| 4496 |
// Try to find such instruction and generate metadata based on it. |
| 4497 |
// Return 'true' if 'OpString' was found and 'kernel_arg_type' metadata |
| 4498 |
// generated and 'false' otherwise. |
| 4499 |
static bool transKernelArgTypeMedataFromString(LLVMContext *Ctx, |
| 4500 |
SPIRVModule *BM, |
| 4501 |
Function *Kernel, |
| 4502 |
std::string MDName) { |
| 4503 |
// Run W/A translation only if the appropriate option is passed |
| 4504 |
if (!BM->shouldPreserveOCLKernelArgTypeMetadataThroughString()) |
| 4505 |
return false; |
| 4506 |
std::string ArgTypePrefix = |
| 4507 |
std::string(MDName) + "." + Kernel->getName().str() + "."; |
| 4508 |
auto ArgTypeStrIt = std::find_if( |
| 4509 |
BM->getStringVec().begin(), BM->getStringVec().end(), |
| 4510 |
[=](SPIRVString *S) { return S->getStr().find(ArgTypePrefix) == 0; }); |
| 4511 |
|
| 4512 |
if (ArgTypeStrIt == BM->getStringVec().end()) |
| 4513 |
return false; |
| 4514 |
|
| 4515 |
std::string ArgTypeStr = |
| 4516 |
(*ArgTypeStrIt)->getStr().substr(ArgTypePrefix.size()); |
| 4517 |
std::vector<Metadata *> TypeMDs; |
| 4518 |
|
| 4519 |
int CountBraces = 0; |
| 4520 |
std::string::size_type Start = 0; |
| 4521 |
|
| 4522 |
for (std::string::size_type I = 0; I < ArgTypeStr.length(); I++) { |
| 4523 |
switch (ArgTypeStr[I]) { |
| 4524 |
case '<': |
| 4525 |
CountBraces++; |
| 4526 |
break; |
| 4527 |
case '>': |
| 4528 |
CountBraces--; |
| 4529 |
break; |
| 4530 |
case ',': |
| 4531 |
if (CountBraces == 0) { |
| 4532 |
TypeMDs.push_back( |
| 4533 |
MDString::get(*Ctx, ArgTypeStr.substr(Start, I - Start))); |
| 4534 |
Start = I + 1; |
| 4535 |
} |
| 4536 |
} |
| 4537 |
} |
| 4538 |
|
| 4539 |
Kernel->setMetadata(MDName, MDNode::get(*Ctx, TypeMDs)); |
| 4540 |
return true; |
| 4541 |
} |
| 4542 |
|
| 4543 |
void SPIRVToLLVM::transFunctionDecorationsToMetadata(SPIRVFunction *BF, |
| 4544 |
Function *F) { |
| 4545 |
size_t TotalParameterDecorations = 0; |
| 4546 |
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) { |
| 4547 |
TotalParameterDecorations += Arg->getNumDecorations(); |
| 4548 |
}); |
| 4549 |
if (TotalParameterDecorations == 0) |
| 4550 |
return; |
| 4551 |
|
| 4552 |
// Generate metadata for spirv.ParameterDecorations |
| 4553 |
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F, |
| 4554 |
[=](SPIRVFunctionParameter *Arg) { |
| 4555 |
return transDecorationsToMetadataList( |
| 4556 |
Context, Arg->getDecorations()); |
| 4557 |
}); |
| 4558 |
} |
| 4559 |
|
| 4560 |
bool SPIRVToLLVM::transMetadata() { |
| 4561 |
SmallVector<Function *, 2> CtorKernels; |
| 4562 |
for (unsigned I = 0, E = BM->getNumFunctions(); I != E; ++I) { |
| 4563 |
SPIRVFunction *BF = BM->getFunction(I); |
| 4564 |
Function *F = static_cast<Function *>(getTranslatedValue(BF)); |
| 4565 |
assert(F && "Invalid translated function"); |
| 4566 |
|
| 4567 |
transOCLMetadata(BF); |
| 4568 |
transVectorComputeMetadata(BF); |
| 4569 |
transFPGAFunctionMetadata(BF, F); |
| 4570 |
|
| 4571 |
// Decoration metadata is only enabled in SPIR-V friendly mode |
| 4572 |
if (BM->getDesiredBIsRepresentation() == BIsRepresentation::SPIRVFriendlyIR) |
| 4573 |
transFunctionDecorationsToMetadata(BF, F); |
| 4574 |
|
| 4575 |
if (F->getCallingConv() != CallingConv::SPIR_KERNEL) |
| 4576 |
continue; |
| 4577 |
|
| 4578 |
// Generate metadata for reqd_work_group_size |
| 4579 |
if (auto EM = BF->getExecutionMode(ExecutionModeLocalSize)) { |
| 4580 |
F->setMetadata(kSPIR2MD::WGSize, |
| 4581 |
getMDNodeStringIntVec(Context, EM->getLiterals())); |
| 4582 |
} else if (auto *EM = BF->getExecutionModeId(ExecutionModeLocalSizeId)) { |
| 4583 |
std::vector<SPIRVWord> Values; |
| 4584 |
for (const auto Id : EM->getLiterals()) { |
| 4585 |
if (auto Val = transIdAsConstant(Id)) { |
| 4586 |
Values.emplace_back(static_cast<SPIRVWord>(*Val)); |
| 4587 |
} |
| 4588 |
} |
| 4589 |
F->setMetadata(kSPIR2MD::WGSize, getMDNodeStringIntVec(Context, Values)); |
| 4590 |
} |
| 4591 |
// Generate metadata for work_group_size_hint |
| 4592 |
if (auto EM = BF->getExecutionMode(ExecutionModeLocalSizeHint)) { |
| 4593 |
F->setMetadata(kSPIR2MD::WGSizeHint, |
| 4594 |
getMDNodeStringIntVec(Context, EM->getLiterals())); |
| 4595 |
} else if (auto *EM = |
| 4596 |
BF->getExecutionModeId(ExecutionModeLocalSizeHintId)) { |
| 4597 |
std::vector<SPIRVWord> Values; |
| 4598 |
for (const auto Id : EM->getLiterals()) { |
| 4599 |
if (auto Val = transIdAsConstant(Id)) { |
| 4600 |
Values.emplace_back(static_cast<SPIRVWord>(*Val)); |
| 4601 |
} |
| 4602 |
} |
| 4603 |
F->setMetadata(kSPIR2MD::WGSizeHint, |
| 4604 |
getMDNodeStringIntVec(Context, Values)); |
| 4605 |
} |
| 4606 |
// Generate metadata for vec_type_hint |
| 4607 |
if (auto EM = BF->getExecutionMode(ExecutionModeVecTypeHint)) { |
| 4608 |
std::vector<Metadata *> MetadataVec; |
| 4609 |
Type *VecHintTy = decodeVecTypeHint(*Context, EM->getLiterals()[0]); |
| 4610 |
assert(VecHintTy); |
| 4611 |
MetadataVec.push_back(ValueAsMetadata::get(UndefValue::get(VecHintTy))); |
| 4612 |
MetadataVec.push_back(ConstantAsMetadata::get( |
| 4613 |
ConstantInt::get(Type::getInt32Ty(*Context), 1))); |
| 4614 |
F->setMetadata(kSPIR2MD::VecTyHint, MDNode::get(*Context, MetadataVec)); |
| 4615 |
} |
| 4616 |
// Generate metadata for Initializer. |
| 4617 |
if (BF->getExecutionMode(ExecutionModeInitializer)) { |
| 4618 |
CtorKernels.push_back(F); |
| 4619 |
} |
| 4620 |
// Generate metadata for intel_reqd_sub_group_size |
| 4621 |
if (auto *EM = BF->getExecutionMode(ExecutionModeSubgroupSize)) { |
| 4622 |
auto SizeMD = ConstantAsMetadata::get(getUInt32(M, EM->getLiterals()[0])); |
| 4623 |
F->setMetadata(kSPIR2MD::SubgroupSize, MDNode::get(*Context, SizeMD)); |
| 4624 |
} |
| 4625 |
// Generate metadata for SubgroupsPerWorkgroup/SubgroupsPerWorkgroupId. |
| 4626 |
auto EmitSubgroupsPerWorkgroupMD = [this, F](SPIRVExecutionModeKind EMK, |
| 4627 |
uint64_t Value) { |
| 4628 |
NamedMDNode *ExecModeMD = |
| 4629 |
M->getOrInsertNamedMetadata(kSPIRVMD::ExecutionMode); |
| 4630 |
SmallVector<Metadata *, 2> OperandVec; |
| 4631 |
OperandVec.push_back(ConstantAsMetadata::get(F)); |
| 4632 |
OperandVec.push_back(ConstantAsMetadata::get(getUInt32(M, EMK))); |
| 4633 |
OperandVec.push_back(ConstantAsMetadata::get(getUInt32(M, Value))); |
| 4634 |
ExecModeMD->addOperand(MDNode::get(*Context, OperandVec)); |
| 4635 |
}; |
| 4636 |
if (auto *EM = BF->getExecutionMode(ExecutionModeSubgroupsPerWorkgroup)) { |
| 4637 |
EmitSubgroupsPerWorkgroupMD(EM->getExecutionMode(), EM->getLiterals()[0]); |
| 4638 |
} else if (auto *EM = BF->getExecutionModeId( |
| 4639 |
ExecutionModeSubgroupsPerWorkgroupId)) { |
| 4640 |
if (auto Val = transIdAsConstant(EM->getLiterals()[0])) { |
| 4641 |
EmitSubgroupsPerWorkgroupMD(EM->getExecutionMode(), *Val); |
| 4642 |
} |
| 4643 |
} |
| 4644 |
// Generate metadata for max_work_group_size |
| 4645 |
if (auto EM = BF->getExecutionMode(ExecutionModeMaxWorkgroupSizeINTEL)) { |
| 4646 |
F->setMetadata(kSPIR2MD::MaxWGSize, |
| 4647 |
getMDNodeStringIntVec(Context, EM->getLiterals())); |
| 4648 |
} |
| 4649 |
// Generate metadata for no_global_work_offset |
| 4650 |
if (BF->getExecutionMode(ExecutionModeNoGlobalOffsetINTEL)) { |
| 4651 |
F->setMetadata(kSPIR2MD::NoGlobalOffset, MDNode::get(*Context, {})); |
| 4652 |
} |
| 4653 |
// Generate metadata for max_global_work_dim |
| 4654 |
if (auto EM = BF->getExecutionMode(ExecutionModeMaxWorkDimINTEL)) { |
| 4655 |
F->setMetadata(kSPIR2MD::MaxWGDim, |
| 4656 |
getMDNodeStringIntVec(Context, EM->getLiterals())); |
| 4657 |
} |
| 4658 |
// Generate metadata for num_simd_work_items |
| 4659 |
if (auto EM = BF->getExecutionMode(ExecutionModeNumSIMDWorkitemsINTEL)) { |
| 4660 |
F->setMetadata(kSPIR2MD::NumSIMD, |
| 4661 |
getMDNodeStringIntVec(Context, EM->getLiterals())); |
| 4662 |
} |
| 4663 |
// Generate metadata for scheduler_target_fmax_mhz |
| 4664 |
if (auto EM = |
| 4665 |
BF->getExecutionMode(ExecutionModeSchedulerTargetFmaxMhzINTEL)) { |
| 4666 |
F->setMetadata(kSPIR2MD::FmaxMhz, |
| 4667 |
getMDNodeStringIntVec(Context, EM->getLiterals())); |
| 4668 |
} |
| 4669 |
// Generate metadata for Intel FPGA streaming interface |
| 4670 |
if (auto *EM = BF->getExecutionMode(ExecutionModeStreamingInterfaceINTEL)) { |
| 4671 |
std::vector<uint32_t> InterfaceVec = EM->getLiterals(); |
| 4672 |
assert(InterfaceVec.size() == 1 && |
| 4673 |
"Expected StreamingInterfaceINTEL to have exactly 1 literal"); |
| 4674 |
std::vector<Metadata *> InterfaceMDVec = |
| 4675 |
[&]() -> std::vector<Metadata *> { |
| 4676 |
switch (InterfaceVec[0]) { |
| 4677 |
case 0: |
| 4678 |
return {MDString::get(*Context, "streaming")}; |
| 4679 |
case 1: |
| 4680 |
return {MDString::get(*Context, "streaming"), |
| 4681 |
MDString::get(*Context, "stall_free_return")}; |
| 4682 |
default: |
| 4683 |
llvm_unreachable("Invalid streaming interface mode"); |
| 4684 |
} |
| 4685 |
}(); |
| 4686 |
F->setMetadata(kSPIR2MD::IntelFPGAIPInterface, |
| 4687 |
MDNode::get(*Context, InterfaceMDVec)); |
| 4688 |
} |
| 4689 |
if (auto *EM = BF->getExecutionMode(ExecutionModeMaximumRegistersINTEL)) { |
| 4690 |
NamedMDNode *ExecModeMD = |
| 4691 |
M->getOrInsertNamedMetadata(kSPIRVMD::ExecutionMode); |
| 4692 |
|
| 4693 |
SmallVector<Metadata *, 4> ValueVec; |
| 4694 |
ValueVec.push_back(ConstantAsMetadata::get(F)); |
| 4695 |
ValueVec.push_back( |
| 4696 |
ConstantAsMetadata::get(getUInt32(M, EM->getExecutionMode()))); |
| 4697 |
ValueVec.push_back( |
| 4698 |
ConstantAsMetadata::get(getUInt32(M, EM->getLiterals()[0]))); |
| 4699 |
ExecModeMD->addOperand(MDNode::get(*Context, ValueVec)); |
| 4700 |
} |
| 4701 |
if (auto *EM = BF->getExecutionMode(ExecutionModeMaximumRegistersIdINTEL)) { |
| 4702 |
NamedMDNode *ExecModeMD = |
| 4703 |
M->getOrInsertNamedMetadata(kSPIRVMD::ExecutionMode); |
| 4704 |
|
| 4705 |
SmallVector<Metadata *, 4> ValueVec; |
| 4706 |
ValueVec.push_back(ConstantAsMetadata::get(F)); |
| 4707 |
ValueVec.push_back( |
| 4708 |
ConstantAsMetadata::get(getUInt32(M, EM->getExecutionMode()))); |
| 4709 |
|
| 4710 |
auto *ExecOp = BF->getModule()->getValue(EM->getLiterals()[0]); |
| 4711 |
ValueVec.push_back( |
| 4712 |
MDNode::get(*Context, ConstantAsMetadata::get(cast<ConstantInt>( |
| 4713 |
transValue(ExecOp, nullptr, nullptr))))); |
| 4714 |
ExecModeMD->addOperand(MDNode::get(*Context, ValueVec)); |
| 4715 |
} |
| 4716 |
if (auto *EM = |
| 4717 |
BF->getExecutionMode(ExecutionModeNamedMaximumRegistersINTEL)) { |
| 4718 |
NamedMDNode *ExecModeMD = |
| 4719 |
M->getOrInsertNamedMetadata(kSPIRVMD::ExecutionMode); |
| 4720 |
|
| 4721 |
SmallVector<Metadata *, 4> ValueVec; |
| 4722 |
ValueVec.push_back(ConstantAsMetadata::get(F)); |
| 4723 |
ValueVec.push_back( |
| 4724 |
ConstantAsMetadata::get(getUInt32(M, EM->getExecutionMode()))); |
| 4725 |
|
| 4726 |
assert(EM->getLiterals()[0] == 0 && |
| 4727 |
"Invalid named maximum number of registers"); |
| 4728 |
ValueVec.push_back(MDString::get(*Context, "AutoINTEL")); |
| 4729 |
ExecModeMD->addOperand(MDNode::get(*Context, ValueVec)); |
| 4730 |
} |
| 4731 |
} |
| 4732 |
NamedMDNode *MemoryModelMD = |
| 4733 |
M->getOrInsertNamedMetadata(kSPIRVMD::MemoryModel); |
| 4734 |
MemoryModelMD->addOperand( |
| 4735 |
getMDTwoInt(Context, static_cast<unsigned>(BM->getAddressingModel()), |
| 4736 |
static_cast<unsigned>(BM->getMemoryModel()))); |
| 4737 |
createCXXStructor("llvm.global_ctors", CtorKernels); |
| 4738 |
return true; |
| 4739 |
} |
| 4740 |
|
| 4741 |
bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) { |
| 4742 |
Function *F = static_cast<Function *>(getTranslatedValue(BF)); |
| 4743 |
assert(F && "Invalid translated function"); |
| 4744 |
if (F->getCallingConv() != CallingConv::SPIR_KERNEL) |
| 4745 |
return true; |
| 4746 |
|
| 4747 |
if (BF->hasDecorate(DecorationVectorComputeFunctionINTEL)) |
| 4748 |
return true; |
| 4749 |
|
| 4750 |
// Generate metadata for kernel_arg_addr_space |
| 4751 |
addKernelArgumentMetadata( |
| 4752 |
Context, SPIR_MD_KERNEL_ARG_ADDR_SPACE, BF, F, |
| 4753 |
[=](SPIRVFunctionParameter *Arg) { |
| 4754 |
SPIRVType *ArgTy = Arg->getType(); |
| 4755 |
SPIRAddressSpace AS = SPIRAS_Private; |
| 4756 |
if (ArgTy->isTypePointer()) |
| 4757 |
AS = SPIRSPIRVAddrSpaceMap::rmap(ArgTy->getPointerStorageClass()); |
| 4758 |
else if (ArgTy->isTypeOCLImage() || ArgTy->isTypePipe()) |
| 4759 |
AS = SPIRAS_Global; |
| 4760 |
return ConstantAsMetadata::get( |
| 4761 |
ConstantInt::get(Type::getInt32Ty(*Context), AS)); |
| 4762 |
}); |
| 4763 |
// Generate metadata for kernel_arg_access_qual |
| 4764 |
addKernelArgumentMetadata(Context, SPIR_MD_KERNEL_ARG_ACCESS_QUAL, BF, F, |
| 4765 |
[=](SPIRVFunctionParameter *Arg) { |
| 4766 |
std::string Qual; |
| 4767 |
auto *T = Arg->getType(); |
| 4768 |
if (T->isTypeOCLImage()) { |
| 4769 |
auto *ST = static_cast<SPIRVTypeImage *>(T); |
| 4770 |
Qual = transOCLImageTypeAccessQualifier(ST); |
| 4771 |
} else if (T->isTypePipe()) { |
| 4772 |
auto *PT = static_cast<SPIRVTypePipe *>(T); |
| 4773 |
Qual = transOCLPipeTypeAccessQualifier(PT); |
| 4774 |
} else |
| 4775 |
Qual = "none"; |
| 4776 |
return MDString::get(*Context, Qual); |
| 4777 |
}); |
| 4778 |
// Generate metadata for kernel_arg_type |
| 4779 |
if (!transKernelArgTypeMedataFromString(Context, BM, F, |
| 4780 |
SPIR_MD_KERNEL_ARG_TYPE)) |
| 4781 |
addKernelArgumentMetadata(Context, SPIR_MD_KERNEL_ARG_TYPE, BF, F, |
| 4782 |
[=](SPIRVFunctionParameter *Arg) { |
| 4783 |
return transOCLKernelArgTypeName(Arg); |
| 4784 |
}); |
| 4785 |
// Generate metadata for kernel_arg_type_qual |
| 4786 |
if (!transKernelArgTypeMedataFromString(Context, BM, F, |
| 4787 |
SPIR_MD_KERNEL_ARG_TYPE_QUAL)) |
| 4788 |
addKernelArgumentMetadata( |
| 4789 |
Context, SPIR_MD_KERNEL_ARG_TYPE_QUAL, BF, F, |
| 4790 |
[=](SPIRVFunctionParameter *Arg) { |
| 4791 |
std::string Qual; |
| 4792 |
if (Arg->hasDecorate(DecorationVolatile)) |
| 4793 |
Qual = kOCLTypeQualifierName::Volatile; |
| 4794 |
Arg->foreachAttr([&](SPIRVFuncParamAttrKind Kind) { |
| 4795 |
Qual += Qual.empty() ? "" : " "; |
| 4796 |
if (Kind == FunctionParameterAttributeNoAlias) |
| 4797 |
Qual += kOCLTypeQualifierName::Restrict; |
| 4798 |
}); |
| 4799 |
if (Arg->getType()->isTypePipe()) { |
| 4800 |
Qual += Qual.empty() ? "" : " "; |
| 4801 |
Qual += kOCLTypeQualifierName::Pipe; |
| 4802 |
} |
| 4803 |
return MDString::get(*Context, Qual); |
| 4804 |
}); |
| 4805 |
// Generate metadata for kernel_arg_base_type |
| 4806 |
addKernelArgumentMetadata(Context, SPIR_MD_KERNEL_ARG_BASE_TYPE, BF, F, |
| 4807 |
[=](SPIRVFunctionParameter *Arg) { |
| 4808 |
return transOCLKernelArgTypeName(Arg); |
| 4809 |
}); |
| 4810 |
// Generate metadata for kernel_arg_name |
| 4811 |
if (BM->isGenArgNameMDEnabled()) { |
| 4812 |
addKernelArgumentMetadata(Context, SPIR_MD_KERNEL_ARG_NAME, BF, F, |
| 4813 |
[=](SPIRVFunctionParameter *Arg) { |
| 4814 |
return MDString::get(*Context, Arg->getName()); |
| 4815 |
}); |
| 4816 |
} |
| 4817 |
// Generate metadata for kernel_arg_buffer_location |
| 4818 |
addBufferLocationMetadata(Context, BF, F, [=](SPIRVFunctionParameter *Arg) { |
| 4819 |
auto Literals = Arg->getDecorationLiterals(DecorationBufferLocationINTEL); |
| 4820 |
assert(Literals.size() == 1 && |
| 4821 |
"BufferLocationINTEL decoration shall have 1 ID literal"); |
| 4822 |
|
| 4823 |
return ConstantAsMetadata::get( |
| 4824 |
ConstantInt::get(Type::getInt32Ty(*Context), Literals[0])); |
| 4825 |
}); |
| 4826 |
// Generate metadata for kernel_arg_runtime_aligned |
| 4827 |
addRuntimeAlignedMetadata(Context, BF, F, [=](SPIRVFunctionParameter *Arg) { |
| 4828 |
return ConstantAsMetadata::get( |
| 4829 |
ConstantInt::get(Type::getInt1Ty(*Context), 1)); |
| 4830 |
}); |
| 4831 |
// Generate metadata for spirv.ParameterDecorations |
| 4832 |
addKernelArgumentMetadata(Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F, |
| 4833 |
[=](SPIRVFunctionParameter *Arg) { |
| 4834 |
return transDecorationsToMetadataList( |
| 4835 |
Context, Arg->getDecorations()); |
| 4836 |
}); |
| 4837 |
return true; |
| 4838 |
} |
| 4839 |
|
| 4840 |
bool SPIRVToLLVM::transVectorComputeMetadata(SPIRVFunction *BF) { |
| 4841 |
using namespace VectorComputeUtil; |
| 4842 |
Function *F = static_cast<Function *>(getTranslatedValue(BF)); |
| 4843 |
assert(F && "Invalid translated function"); |
| 4844 |
|
| 4845 |
if (BF->hasDecorate(DecorationStackCallINTEL)) |
| 4846 |
F->addFnAttr(kVCMetadata::VCStackCall); |
| 4847 |
|
| 4848 |
if (BF->hasDecorate(DecorationVectorComputeFunctionINTEL)) |
| 4849 |
F->addFnAttr(kVCMetadata::VCFunction); |
| 4850 |
|
| 4851 |
SPIRVWord SIMTMode = 0; |
| 4852 |
if (BF->hasDecorate(DecorationSIMTCallINTEL, 0, &SIMTMode)) |
| 4853 |
F->addFnAttr(kVCMetadata::VCSIMTCall, std::to_string(SIMTMode)); |
| 4854 |
|
| 4855 |
auto SEVAttr = translateSEVMetadata(BF, F->getContext()); |
| 4856 |
|
| 4857 |
if (SEVAttr) |
| 4858 |
F->addAttributeAtIndex(AttributeList::ReturnIndex, SEVAttr.value()); |
| 4859 |
|
| 4860 |
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; |
| 4861 |
++I) { |
| 4862 |
auto ArgNo = I->getArgNo(); |
| 4863 |
SPIRVFunctionParameter *BA = BF->getArgument(ArgNo); |
| 4864 |
SPIRVWord Kind; |
| 4865 |
if (BA->hasDecorate(DecorationFuncParamIOKindINTEL, 0, &Kind)) { |
| 4866 |
Attribute Attr = Attribute::get(*Context, kVCMetadata::VCArgumentIOKind, |
| 4867 |
std::to_string(Kind)); |
| 4868 |
F->addParamAttr(ArgNo, Attr); |
| 4869 |
} |
| 4870 |
SEVAttr = translateSEVMetadata(BA, F->getContext()); |
| 4871 |
if (SEVAttr) |
| 4872 |
F->addParamAttr(ArgNo, SEVAttr.value()); |
| 4873 |
if (BA->hasDecorate(DecorationMediaBlockIOINTEL)) { |
| 4874 |
assert(BA->getType()->isTypeImage() && |
| 4875 |
"MediaBlockIOINTEL decoration is valid only on image parameters"); |
| 4876 |
F->addParamAttr(ArgNo, |
| 4877 |
Attribute::get(*Context, kVCMetadata::VCMediaBlockIO)); |
| 4878 |
} |
| 4879 |
} |
| 4880 |
|
| 4881 |
// Do not add float control if there is no any |
| 4882 |
bool IsVCFloatControl = false; |
| 4883 |
unsigned FloatControl = 0; |
| 4884 |
// RoundMode and FloatMode are always same for all types in Cm |
| 4885 |
// While Denorm could be different for double, float and half |
| 4886 |
if (isKernel(BF)) { |
| 4887 |
FPRoundingModeExecModeMap::foreach ( |
| 4888 |
[&](FPRoundingMode VCRM, ExecutionMode EM) { |
| 4889 |
if (BF->getExecutionMode(EM)) { |
| 4890 |
IsVCFloatControl = true; |
| 4891 |
FloatControl |= getVCFloatControl(VCRM); |
| 4892 |
} |
| 4893 |
}); |
| 4894 |
FPOperationModeExecModeMap::foreach ( |
| 4895 |
[&](FPOperationMode VCFM, ExecutionMode EM) { |
| 4896 |
if (BF->getExecutionMode(EM)) { |
| 4897 |
IsVCFloatControl = true; |
| 4898 |
FloatControl |= getVCFloatControl(VCFM); |
| 4899 |
} |
| 4900 |
}); |
| 4901 |
FPDenormModeExecModeMap::foreach ([&](FPDenormMode VCDM, ExecutionMode EM) { |
| 4902 |
auto ExecModes = BF->getExecutionModeRange(EM); |
| 4903 |
for (auto It = ExecModes.first; It != ExecModes.second; It++) { |
| 4904 |
IsVCFloatControl = true; |
| 4905 |
unsigned TargetWidth = (*It).second->getLiterals()[0]; |
| 4906 |
VCFloatType FloatType = VCFloatTypeSizeMap::rmap(TargetWidth); |
| 4907 |
FloatControl |= getVCFloatControl(VCDM, FloatType); |
| 4908 |
} |
| 4909 |
}); |
| 4910 |
} else { |
| 4911 |
if (BF->hasDecorate(DecorationFunctionRoundingModeINTEL)) { |
| 4912 |
std::vector<SPIRVDecorate const *> RoundModes = |
| 4913 |
BF->getDecorations(DecorationFunctionRoundingModeINTEL); |
| 4914 |
|
| 4915 |
assert(RoundModes.size() == 3 && "Function must have precisely 3 " |
| 4916 |
"FunctionRoundingModeINTEL decoration"); |
| 4917 |
|
| 4918 |
auto *DecRound = |
| 4919 |
static_cast<SPIRVDecorateFunctionRoundingModeINTEL const *>( |
| 4920 |
RoundModes.at(0)); |
| 4921 |
auto RoundingMode = DecRound->getRoundingMode(); |
| 4922 |
#ifndef NDEBUG |
| 4923 |
for (auto *DecPreCast : RoundModes) { |
| 4924 |
auto *Dec = static_cast<SPIRVDecorateFunctionRoundingModeINTEL const *>( |
| 4925 |
DecPreCast); |
| 4926 |
assert(Dec->getRoundingMode() == RoundingMode && |
| 4927 |
"Rounding Mode must be equal within all targets"); |
| 4928 |
} |
| 4929 |
#endif |
| 4930 |
IsVCFloatControl = true; |
| 4931 |
FloatControl |= getVCFloatControl(RoundingMode); |
| 4932 |
} |
| 4933 |
|
| 4934 |
if (BF->hasDecorate(DecorationFunctionDenormModeINTEL)) { |
| 4935 |
std::vector<SPIRVDecorate const *> DenormModes = |
| 4936 |
BF->getDecorations(DecorationFunctionDenormModeINTEL); |
| 4937 |
IsVCFloatControl = true; |
| 4938 |
|
| 4939 |
for (auto DecPtr : DenormModes) { |
| 4940 |
auto DecDenorm = |
| 4941 |
static_cast<SPIRVDecorateFunctionDenormModeINTEL const *>(DecPtr); |
| 4942 |
VCFloatType FType = |
| 4943 |
VCFloatTypeSizeMap::rmap(DecDenorm->getTargetWidth()); |
| 4944 |
FloatControl |= getVCFloatControl(DecDenorm->getDenormMode(), FType); |
| 4945 |
} |
| 4946 |
} |
| 4947 |
|
| 4948 |
if (BF->hasDecorate(DecorationFunctionFloatingPointModeINTEL)) { |
| 4949 |
std::vector<SPIRVDecorate const *> FloatModes = |
| 4950 |
BF->getDecorations(DecorationFunctionFloatingPointModeINTEL); |
| 4951 |
|
| 4952 |
assert(FloatModes.size() == 3 && |
| 4953 |
"Function must have precisely 3 FunctionFloatingPointModeINTEL " |
| 4954 |
"decoration"); |
| 4955 |
|
| 4956 |
auto *DecFlt = |
| 4957 |
static_cast<SPIRVDecorateFunctionFloatingPointModeINTEL const *>( |
| 4958 |
FloatModes.at(0)); |
| 4959 |
auto FloatingMode = DecFlt->getOperationMode(); |
| 4960 |
#ifndef NDEBUG |
| 4961 |
for (auto *DecPreCast : FloatModes) { |
| 4962 |
auto *Dec = |
| 4963 |
static_cast<SPIRVDecorateFunctionFloatingPointModeINTEL const *>( |
| 4964 |
DecPreCast); |
| 4965 |
assert(Dec->getOperationMode() == FloatingMode && |
| 4966 |
"Rounding Mode must be equal within all targets"); |
| 4967 |
} |
| 4968 |
#endif |
| 4969 |
|
| 4970 |
IsVCFloatControl = true; |
| 4971 |
FloatControl |= getVCFloatControl(FloatingMode); |
| 4972 |
} |
| 4973 |
} |
| 4974 |
|
| 4975 |
if (IsVCFloatControl) { |
| 4976 |
Attribute Attr = Attribute::get(*Context, kVCMetadata::VCFloatControl, |
| 4977 |
std::to_string(FloatControl)); |
| 4978 |
F->addFnAttr(Attr); |
| 4979 |
} |
| 4980 |
|
| 4981 |
if (auto EM = BF->getExecutionMode(ExecutionModeSharedLocalMemorySizeINTEL)) { |
| 4982 |
unsigned int SLMSize = EM->getLiterals()[0]; |
| 4983 |
Attribute Attr = Attribute::get(*Context, kVCMetadata::VCSLMSize, |
| 4984 |
std::to_string(SLMSize)); |
| 4985 |
F->addFnAttr(Attr); |
| 4986 |
} |
| 4987 |
|
| 4988 |
if (auto *EM = BF->getExecutionMode(ExecutionModeNamedBarrierCountINTEL)) { |
| 4989 |
unsigned int NBarrierCnt = EM->getLiterals()[0]; |
| 4990 |
Attribute Attr = Attribute::get(*Context, kVCMetadata::VCNamedBarrierCount, |
| 4991 |
std::to_string(NBarrierCnt)); |
| 4992 |
F->addFnAttr(Attr); |
| 4993 |
} |
| 4994 |
|
| 4995 |
return true; |
| 4996 |
} |
| 4997 |
|
| 4998 |
bool SPIRVToLLVM::transFPGAFunctionMetadata(SPIRVFunction *BF, Function *F) { |
| 4999 |
if (BF->hasDecorate(DecorationStallEnableINTEL)) { |
| 5000 |
std::vector<Metadata *> MetadataVec; |
| 5001 |
MetadataVec.push_back(ConstantAsMetadata::get(getInt32(M, 1))); |
| 5002 |
F->setMetadata(kSPIR2MD::StallEnable, MDNode::get(*Context, MetadataVec)); |
| 5003 |
} |
| 5004 |
if (BF->hasDecorate(DecorationFuseLoopsInFunctionINTEL)) { |
| 5005 |
std::vector<Metadata *> MetadataVec; |
| 5006 |
auto Literals = |
| 5007 |
BF->getDecorationLiterals(DecorationFuseLoopsInFunctionINTEL); |
| 5008 |
MetadataVec.push_back(ConstantAsMetadata::get(getUInt32(M, Literals[0]))); |
| 5009 |
MetadataVec.push_back(ConstantAsMetadata::get(getUInt32(M, Literals[1]))); |
| 5010 |
F->setMetadata(kSPIR2MD::LoopFuse, MDNode::get(*Context, MetadataVec)); |
| 5011 |
} |
| 5012 |
if (BF->hasDecorate(DecorationMathOpDSPModeINTEL)) { |
| 5013 |
std::vector<SPIRVWord> Literals = |
| 5014 |
BF->getDecorationLiterals(DecorationMathOpDSPModeINTEL); |
| 5015 |
assert(Literals.size() == 2 && |
| 5016 |
"MathOpDSPModeINTEL decoration shall have 2 literals"); |
| 5017 |
F->setMetadata(kSPIR2MD::PreferDSP, |
| 5018 |
MDNode::get(*Context, ConstantAsMetadata::get( |
| 5019 |
getUInt32(M, Literals[0])))); |
| 5020 |
if (Literals[1] != 0) { |
| 5021 |
F->setMetadata(kSPIR2MD::PropDSPPref, |
| 5022 |
MDNode::get(*Context, ConstantAsMetadata::get( |
| 5023 |
getUInt32(M, Literals[1])))); |
| 5024 |
} |
| 5025 |
} |
| 5026 |
if (BF->hasDecorate(DecorationInitiationIntervalINTEL)) { |
| 5027 |
std::vector<Metadata *> MetadataVec; |
| 5028 |
auto Literals = |
| 5029 |
BF->getDecorationLiterals(DecorationInitiationIntervalINTEL); |
| 5030 |
MetadataVec.push_back(ConstantAsMetadata::get(getUInt32(M, Literals[0]))); |
| 5031 |
F->setMetadata(kSPIR2MD::InitiationInterval, |
| 5032 |
MDNode::get(*Context, MetadataVec)); |
| 5033 |
} |
| 5034 |
if (BF->hasDecorate(DecorationMaxConcurrencyINTEL)) { |
| 5035 |
std::vector<Metadata *> MetadataVec; |
| 5036 |
auto Literals = BF->getDecorationLiterals(DecorationMaxConcurrencyINTEL); |
| 5037 |
MetadataVec.push_back(ConstantAsMetadata::get(getUInt32(M, Literals[0]))); |
| 5038 |
F->setMetadata(kSPIR2MD::MaxConcurrency, |
| 5039 |
MDNode::get(*Context, MetadataVec)); |
| 5040 |
} |
| 5041 |
if (BF->hasDecorate(DecorationPipelineEnableINTEL)) { |
| 5042 |
auto Literals = BF->getDecorationLiterals(DecorationPipelineEnableINTEL); |
| 5043 |
std::vector<Metadata *> MetadataVec; |
| 5044 |
MetadataVec.push_back(ConstantAsMetadata::get(getInt32(M, !Literals[0]))); |
| 5045 |
F->setMetadata(kSPIR2MD::DisableLoopPipelining, |
| 5046 |
MDNode::get(*Context, MetadataVec)); |
| 5047 |
} |
| 5048 |
return true; |
| 5049 |
} |
| 5050 |
|
| 5051 |
bool SPIRVToLLVM::transAlign(SPIRVValue *BV, Value *V) { |
| 5052 |
if (auto *AL = dyn_cast<AllocaInst>(V)) { |
| 5053 |
if (auto Align = getAlignment(BV)) |
| 5054 |
AL->setAlignment(llvm::Align(*Align)); |
| 5055 |
return true; |
| 5056 |
} |
| 5057 |
if (auto *GV = dyn_cast<GlobalVariable>(V)) { |
| 5058 |
if (auto Align = getAlignment(BV)) |
| 5059 |
GV->setAlignment(MaybeAlign(*Align)); |
| 5060 |
return true; |
| 5061 |
} |
| 5062 |
return true; |
| 5063 |
} |
| 5064 |
|
| 5065 |
Instruction *SPIRVToLLVM::transOCLBuiltinFromExtInst(SPIRVExtInst *BC, |
| 5066 |
BasicBlock *BB) { |
| 5067 |
assert(BB && "Invalid BB"); |
| 5068 |
auto ExtOp = static_cast<OCLExtOpKind>(BC->getExtOp()); |
| 5069 |
std::string UnmangledName = OCLExtOpMap::map(ExtOp); |
| 5070 |
|
| 5071 |
assert(BM->getBuiltinSet(BC->getExtSetId()) == SPIRVEIS_OpenCL && |
| 5072 |
"Not OpenCL extended instruction"); |
| 5073 |
|
| 5074 |
std::vector<Type *> ArgTypes = transTypeVector(BC->getArgTypes(), true); |
| 5075 |
Type *RetTy = transType(BC->getType()); |
| 5076 |
std::string MangledName = |
| 5077 |
getSPIRVFriendlyIRFunctionName(ExtOp, ArgTypes, RetTy); |
| 5078 |
opaquifyTypedPointers(ArgTypes); |
| 5079 |
|
| 5080 |
SPIRVDBG(spvdbgs() << "[transOCLBuiltinFromExtInst] UnmangledName: " |
| 5081 |
<< UnmangledName << " MangledName: " << MangledName |
| 5082 |
<< '\n'); |
| 5083 |
|
| 5084 |
FunctionType *FT = FunctionType::get(RetTy, ArgTypes, false); |
| 5085 |
Function *F = M->getFunction(MangledName); |
| 5086 |
if (!F) { |
| 5087 |
F = Function::Create(FT, GlobalValue::ExternalLinkage, MangledName, M); |
| 5088 |
F->setCallingConv(CallingConv::SPIR_FUNC); |
| 5089 |
if (isFuncNoUnwind()) |
| 5090 |
F->addFnAttr(Attribute::NoUnwind); |
| 5091 |
if (isFuncReadNone(UnmangledName)) |
| 5092 |
F->setDoesNotAccessMemory(); |
| 5093 |
} |
| 5094 |
auto Args = transValue(BC->getArgValues(), F, BB); |
| 5095 |
SPIRVDBG(dbgs() << "[transOCLBuiltinFromExtInst] Function: " << *F |
| 5096 |
<< ", Args: "; |
| 5097 |
for (auto &I |
| 5098 |
: Args) dbgs() |
| 5099 |
<< *I << ", "; |
| 5100 |
dbgs() << '\n'); |
| 5101 |
CallInst *CI = CallInst::Create(F, Args, BC->getName(), BB); |
| 5102 |
setCallingConv(CI); |
| 5103 |
addFnAttr(CI, Attribute::NoUnwind); |
| 5104 |
return CI; |
| 5105 |
} |
| 5106 |
|
| 5107 |
void SPIRVToLLVM::transAuxDataInst(SPIRVExtInst *BC) { |
| 5108 |
assert(BC->getExtSetKind() == SPIRV::SPIRVEIS_NonSemantic_AuxData); |
| 5109 |
if (!BC->getModule()->preserveAuxData()) |
| 5110 |
return; |
| 5111 |
auto Args = BC->getArguments(); |
| 5112 |
// Args 0 and 1 are common between attributes and metadata. |
| 5113 |
// 0 is the function, 1 is the name of the attribute/metadata as a string |
| 5114 |
auto *SpvFcn = BC->getModule()->getValue(Args[0]); |
| 5115 |
auto *F = static_cast<Function *>(getTranslatedValue(SpvFcn)); |
| 5116 |
assert(F && "Function should already have been translated!"); |
| 5117 |
auto AttrOrMDName = BC->getModule()->get<SPIRVString>(Args[1])->getStr(); |
| 5118 |
switch (BC->getExtOp()) { |
| 5119 |
case NonSemanticAuxData::FunctionAttribute: { |
| 5120 |
assert(Args.size() < 4 && "Unexpected FunctionAttribute Args"); |
| 5121 |
// If this attr was specially handled and added elsewhere, skip it. |
| 5122 |
const Attribute::AttrKind AsKind = |
| 5123 |
Attribute::getAttrKindFromName(AttrOrMDName); |
| 5124 |
if (AsKind != Attribute::None && F->hasFnAttribute(AsKind)) |
| 5125 |
return; |
| 5126 |
if (AsKind == Attribute::None && F->hasFnAttribute(AttrOrMDName)) |
| 5127 |
return; |
| 5128 |
// For attributes, arg 2 is the attribute value as a string, which may not |
| 5129 |
// exist. |
| 5130 |
if (Args.size() == 3) { |
| 5131 |
auto AttrValue = BC->getModule()->get<SPIRVString>(Args[2])->getStr(); |
| 5132 |
F->addFnAttr(AttrOrMDName, AttrValue); |
| 5133 |
} else { |
| 5134 |
if (AsKind != Attribute::None) |
| 5135 |
F->addFnAttr(AsKind); |
| 5136 |
else |
| 5137 |
F->addFnAttr(AttrOrMDName); |
| 5138 |
} |
| 5139 |
break; |
| 5140 |
} |
| 5141 |
case NonSemanticAuxData::FunctionMetadata: { |
| 5142 |
// If this metadata was specially handled and added elsewhere, skip it. |
| 5143 |
if (F->hasMetadata(AttrOrMDName)) |
| 5144 |
return; |
| 5145 |
SmallVector<Metadata *> MetadataArgs; |
| 5146 |
// Process the metadata values. |
| 5147 |
for (size_t CurArg = 2; CurArg < Args.size(); CurArg++) { |
| 5148 |
auto *Arg = BC->getModule()->get<SPIRVEntry>(Args[CurArg]); |
| 5149 |
// For metadata, the metadata values can be either values or strings. |
| 5150 |
if (Arg->getOpCode() == OpString) { |
| 5151 |
auto *ArgAsStr = static_cast<SPIRVString *>(Arg); |
| 5152 |
MetadataArgs.push_back( |
| 5153 |
MDString::get(F->getContext(), ArgAsStr->getStr())); |
| 5154 |
} else { |
| 5155 |
auto *ArgAsVal = static_cast<SPIRVValue *>(Arg); |
| 5156 |
auto *TranslatedMD = transValue(ArgAsVal, F, nullptr); |
| 5157 |
MetadataArgs.push_back(ValueAsMetadata::get(TranslatedMD)); |
| 5158 |
} |
| 5159 |
} |
| 5160 |
F->setMetadata(AttrOrMDName, MDNode::get(*Context, MetadataArgs)); |
| 5161 |
break; |
| 5162 |
} |
| 5163 |
default: |
| 5164 |
llvm_unreachable("Invalid op"); |
| 5165 |
} |
| 5166 |
} |
| 5167 |
|
| 5168 |
// SPIR-V only contains language version. Use OpenCL language version as |
| 5169 |
// SPIR version. |
| 5170 |
void SPIRVToLLVM::transSourceLanguage() { |
| 5171 |
SPIRVWord Ver = 0; |
| 5172 |
SourceLanguage Lang = BM->getSourceLanguage(&Ver); |
| 5173 |
if (Lang != SourceLanguageUnknown && // Allow unknown for debug info test |
| 5174 |
Lang != SourceLanguageOpenCL_C && Lang != SourceLanguageOpenCL_CPP) |
| 5175 |
return; |
| 5176 |
unsigned short Major = 0; |
| 5177 |
unsigned char Minor = 0; |
| 5178 |
unsigned char Rev = 0; |
| 5179 |
std::tie(Major, Minor, Rev) = decodeOCLVer(Ver); |
| 5180 |
SPIRVMDBuilder Builder(*M); |
| 5181 |
Builder.addNamedMD(kSPIRVMD::Source).addOp().add(Lang).add(Ver).done(); |
| 5182 |
// ToDo: Phasing out usage of old SPIR metadata |
| 5183 |
if (Ver <= kOCLVer::CL12) |
| 5184 |
addOCLVersionMetadata(Context, M, kSPIR2MD::SPIRVer, 1, 2); |
| 5185 |
else |
| 5186 |
addOCLVersionMetadata(Context, M, kSPIR2MD::SPIRVer, 2, 0); |
| 5187 |
|
| 5188 |
addOCLVersionMetadata(Context, M, kSPIR2MD::OCLVer, Major, Minor); |
| 5189 |
} |
| 5190 |
|
| 5191 |
bool SPIRVToLLVM::transSourceExtension() { |
| 5192 |
auto ExtSet = rmap<OclExt::Kind>(BM->getExtension()); |
| 5193 |
auto CapSet = rmap<OclExt::Kind>(BM->getCapability()); |
| 5194 |
ExtSet.insert(CapSet.begin(), CapSet.end()); |
| 5195 |
auto OCLExtensions = map<std::string>(ExtSet); |
| 5196 |
std::set<std::string> OCLOptionalCoreFeatures; |
| 5197 |
static const char *OCLOptCoreFeatureNames[] = { |
| 5198 |
"cl_images", |
| 5199 |
"cl_doubles", |
| 5200 |
}; |
| 5201 |
for (auto &I : OCLOptCoreFeatureNames) { |
| 5202 |
auto Loc = OCLExtensions.find(I); |
| 5203 |
if (Loc != OCLExtensions.end()) { |
| 5204 |
OCLExtensions.erase(Loc); |
| 5205 |
OCLOptionalCoreFeatures.insert(I); |
| 5206 |
} |
| 5207 |
} |
| 5208 |
addNamedMetadataStringSet(Context, M, kSPIR2MD::Extensions, OCLExtensions); |
| 5209 |
addNamedMetadataStringSet(Context, M, kSPIR2MD::OptFeatures, |
| 5210 |
OCLOptionalCoreFeatures); |
| 5211 |
return true; |
| 5212 |
} |
| 5213 |
|
| 5214 |
llvm::GlobalValue::LinkageTypes |
| 5215 |
SPIRVToLLVM::transLinkageType(const SPIRVValue *V) { |
| 5216 |
std::string ValueName = V->getName(); |
| 5217 |
if (ValueName == "llvm.used" || ValueName == "llvm.compiler.used") |
| 5218 |
return GlobalValue::AppendingLinkage; |
| 5219 |
int LT = V->getLinkageType(); |
| 5220 |
switch (LT) { |
| 5221 |
case internal::LinkageTypeInternal: |
| 5222 |
return GlobalValue::InternalLinkage; |
| 5223 |
case LinkageTypeImport: |
| 5224 |
// Function declaration |
| 5225 |
if (V->getOpCode() == OpFunction) { |
| 5226 |
if (static_cast<const SPIRVFunction *>(V)->getNumBasicBlock() == 0) |
| 5227 |
return GlobalValue::ExternalLinkage; |
| 5228 |
} |
| 5229 |
// Variable declaration |
| 5230 |
if (V->getOpCode() == OpVariable) { |
| 5231 |
if (static_cast<const SPIRVVariable *>(V)->getInitializer() == 0) |
| 5232 |
return GlobalValue::ExternalLinkage; |
| 5233 |
} |
| 5234 |
// Definition |
| 5235 |
return GlobalValue::AvailableExternallyLinkage; |
| 5236 |
case LinkageTypeExport: |
| 5237 |
if (V->getOpCode() == OpVariable) { |
| 5238 |
if (static_cast<const SPIRVVariable *>(V)->getInitializer() == 0) |
| 5239 |
// Tentative definition |
| 5240 |
return GlobalValue::CommonLinkage; |
| 5241 |
} |
| 5242 |
return GlobalValue::ExternalLinkage; |
| 5243 |
case LinkageTypeLinkOnceODR: |
| 5244 |
return GlobalValue::LinkOnceODRLinkage; |
| 5245 |
default: |
| 5246 |
llvm_unreachable("Invalid linkage type"); |
| 5247 |
} |
| 5248 |
} |
| 5249 |
|
| 5250 |
Instruction *SPIRVToLLVM::transAllAny(SPIRVInstruction *I, BasicBlock *BB) { |
| 5251 |
CallInst *CI = cast<CallInst>(transSPIRVBuiltinFromInst(I, BB)); |
| 5252 |
auto Mutator = mutateCallInst( |
| 5253 |
CI, getSPIRVFuncName(I->getOpCode(), getSPIRVFuncSuffix(I))); |
| 5254 |
Mutator.mapArg(0, [](IRBuilder<> &Builder, Value *OldArg) { |
| 5255 |
auto *NewArgTy = OldArg->getType()->getWithNewBitWidth(8); |
| 5256 |
return Builder.CreateSExtOrBitCast(OldArg, NewArgTy); |
| 5257 |
}); |
| 5258 |
return cast<Instruction>(Mutator.getMutated()); |
| 5259 |
} |
| 5260 |
|
| 5261 |
Instruction *SPIRVToLLVM::transRelational(SPIRVInstruction *I, BasicBlock *BB) { |
| 5262 |
CallInst *CI = cast<CallInst>(transSPIRVBuiltinFromInst(I, BB)); |
| 5263 |
auto Mutator = mutateCallInst( |
| 5264 |
CI, getSPIRVFuncName(I->getOpCode(), getSPIRVFuncSuffix(I))); |
| 5265 |
if (CI->getType()->isVectorTy()) { |
| 5266 |
Type *RetTy = CI->getType()->getWithNewBitWidth(8); |
| 5267 |
Mutator.changeReturnType(RetTy, [=](IRBuilder<> &Builder, CallInst *NewCI) { |
| 5268 |
return Builder.CreateTruncOrBitCast(NewCI, CI->getType()); |
| 5269 |
}); |
| 5270 |
} |
| 5271 |
return cast<Instruction>(Mutator.getMutated()); |
| 5272 |
} |
| 5273 |
|
| 5274 |
std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS) { |
| 5275 |
int IgnoreErrCode; |
| 5276 |
return getSpirvReport(IS, IgnoreErrCode); |
| 5277 |
} |
| 5278 |
|
| 5279 |
std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS, |
| 5280 |
int &ErrCode) { |
| 5281 |
SPIRVWord Word; |
| 5282 |
std::string Name; |
| 5283 |
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule()); |
| 5284 |
SPIRVDecoder D(IS, *BM); |
| 5285 |
D >> Word; |
| 5286 |
if (Word != MagicNumber) { |
| 5287 |
ErrCode = SPIRVEC_InvalidMagicNumber; |
| 5288 |
return {}; |
| 5289 |
} |
| 5290 |
D >> Word; |
| 5291 |
if (!isSPIRVVersionKnown(Word)) { |
| 5292 |
ErrCode = SPIRVEC_InvalidVersionNumber; |
| 5293 |
return {}; |
| 5294 |
} |
| 5295 |
SPIRVModuleReport Report; |
| 5296 |
Report.Version = static_cast<SPIRV::VersionNumber>(Word); |
| 5297 |
// Skip: Generator’s magic number, Bound and Reserved word |
| 5298 |
D.ignore(3); |
| 5299 |
|
| 5300 |
bool IsReportGenCompleted = false, IsMemoryModelDefined = false; |
| 5301 |
while (!IS.bad() && !IsReportGenCompleted && D.getWordCountAndOpCode()) { |
| 5302 |
switch (D.OpCode) { |
| 5303 |
case OpCapability: |
| 5304 |
D >> Word; |
| 5305 |
Report.Capabilities.push_back(Word); |
| 5306 |
break; |
| 5307 |
case OpExtension: |
| 5308 |
Name.clear(); |
| 5309 |
D >> Name; |
| 5310 |
Report.Extensions.push_back(Name); |
| 5311 |
break; |
| 5312 |
case OpExtInstImport: |
| 5313 |
Name.clear(); |
| 5314 |
D >> Word >> Name; |
| 5315 |
Report.ExtendedInstructionSets.push_back(Name); |
| 5316 |
break; |
| 5317 |
case OpMemoryModel: |
| 5318 |
if (IsMemoryModelDefined) { |
| 5319 |
ErrCode = SPIRVEC_RepeatedMemoryModel; |
| 5320 |
return {}; |
| 5321 |
} |
| 5322 |
SPIRVAddressingModelKind AddrModel; |
| 5323 |
SPIRVMemoryModelKind MemoryModel; |
| 5324 |
D >> AddrModel >> MemoryModel; |
| 5325 |
if (!isValid(AddrModel)) { |
| 5326 |
ErrCode = SPIRVEC_InvalidAddressingModel; |
| 5327 |
return {}; |
| 5328 |
} |
| 5329 |
if (!isValid(MemoryModel)) { |
| 5330 |
ErrCode = SPIRVEC_InvalidMemoryModel; |
| 5331 |
return {}; |
| 5332 |
} |
| 5333 |
Report.MemoryModel = MemoryModel; |
| 5334 |
Report.AddrModel = AddrModel; |
| 5335 |
IsMemoryModelDefined = true; |
| 5336 |
// In this report we don't analyze instructions after OpMemoryModel |
| 5337 |
IsReportGenCompleted = true; |
| 5338 |
break; |
| 5339 |
default: |
| 5340 |
// No more instructions to gather information about |
| 5341 |
IsReportGenCompleted = true; |
| 5342 |
} |
| 5343 |
} |
| 5344 |
if (IS.bad()) { |
| 5345 |
ErrCode = SPIRVEC_InvalidModule; |
| 5346 |
return {}; |
| 5347 |
} |
| 5348 |
if (!IsMemoryModelDefined) { |
| 5349 |
ErrCode = SPIRVEC_UnspecifiedMemoryModel; |
| 5350 |
return {}; |
| 5351 |
} |
| 5352 |
ErrCode = SPIRVEC_Success; |
| 5353 |
return std::make_optional(std::move(Report)); |
| 5354 |
} |
| 5355 |
|
| 5356 |
constexpr std::string_view formatAddressingModel(uint32_t AddrModel) { |
| 5357 |
switch (AddrModel) { |
| 5358 |
case AddressingModelLogical: |
| 5359 |
return "Logical"; |
| 5360 |
case AddressingModelPhysical32: |
| 5361 |
return "Physical32"; |
| 5362 |
case AddressingModelPhysical64: |
| 5363 |
return "Physical64"; |
| 5364 |
case AddressingModelPhysicalStorageBuffer64: |
| 5365 |
return "PhysicalStorageBuffer64"; |
| 5366 |
default: |
| 5367 |
return "Unknown"; |
| 5368 |
} |
| 5369 |
} |
| 5370 |
|
| 5371 |
constexpr std::string_view formatMemoryModel(uint32_t MemoryModel) { |
| 5372 |
switch (MemoryModel) { |
| 5373 |
case MemoryModelSimple: |
| 5374 |
return "Simple"; |
| 5375 |
case MemoryModelGLSL450: |
| 5376 |
return "GLSL450"; |
| 5377 |
case MemoryModelOpenCL: |
| 5378 |
return "OpenCL"; |
| 5379 |
case MemoryModelVulkan: |
| 5380 |
return "Vulkan"; |
| 5381 |
default: |
| 5382 |
return "Unknown"; |
| 5383 |
} |
| 5384 |
} |
| 5385 |
|
| 5386 |
SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report) { |
| 5387 |
SPIRVModuleTextReport TextReport; |
| 5388 |
TextReport.Version = |
| 5389 |
formatVersionNumber(static_cast<uint32_t>(Report.Version)); |
| 5390 |
TextReport.AddrModel = formatAddressingModel(Report.AddrModel); |
| 5391 |
TextReport.MemoryModel = formatMemoryModel(Report.MemoryModel); |
| 5392 |
// format capability codes as strings |
| 5393 |
std::string Name; |
| 5394 |
for (auto Capability : Report.Capabilities) { |
| 5395 |
bool Found = SPIRVCapabilityNameMap::find( |
| 5396 |
static_cast<SPIRVCapabilityKind>(Capability), &Name); |
| 5397 |
TextReport.Capabilities.push_back(Found ? Name : "Unknown"); |
| 5398 |
} |
| 5399 |
// other fields with string content can be copied as is |
| 5400 |
TextReport.Extensions = Report.Extensions; |
| 5401 |
TextReport.ExtendedInstructionSets = Report.ExtendedInstructionSets; |
| 5402 |
return TextReport; |
| 5403 |
} |
| 5404 |
|
| 5405 |
std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS, |
| 5406 |
const SPIRV::TranslatorOpts &Opts, |
| 5407 |
std::string &ErrMsg) { |
| 5408 |
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule(Opts)); |
| 5409 |
|
| 5410 |
IS >> *BM; |
| 5411 |
if (!BM->isModuleValid()) { |
| 5412 |
BM->getError(ErrMsg); |
| 5413 |
return nullptr; |
| 5414 |
} |
| 5415 |
return BM; |
| 5416 |
} |
| 5417 |
|
| 5418 |
std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS, |
| 5419 |
std::string &ErrMsg) { |
| 5420 |
SPIRV::TranslatorOpts DefaultOpts; |
| 5421 |
return readSpirvModule(IS, DefaultOpts, ErrMsg); |
| 5422 |
} |
| 5423 |
|
| 5424 |
} // namespace SPIRV |
| 5425 |
|
| 5426 |
std::unique_ptr<Module> |
| 5427 |
llvm::convertSpirvToLLVM(LLVMContext &C, SPIRVModule &BM, |
| 5428 |
const SPIRV::TranslatorOpts &Opts, |
| 5429 |
std::string &ErrMsg) { |
| 5430 |
std::unique_ptr<Module> M(new Module("", C)); |
| 5431 |
SPIRVToLLVM BTL(M.get(), &BM); |
| 5432 |
|
| 5433 |
if (!BTL.translate()) { |
| 5434 |
BM.getError(ErrMsg); |
| 5435 |
return nullptr; |
| 5436 |
} |
| 5437 |
|
| 5438 |
llvm::ModulePassManager PassMgr; |
| 5439 |
addSPIRVBIsLoweringPass(PassMgr, Opts.getDesiredBIsRepresentation()); |
| 5440 |
llvm::ModuleAnalysisManager MAM; |
| 5441 |
MAM.registerPass([&] { return PassInstrumentationAnalysis(); }); |
| 5442 |
PassMgr.run(*M, MAM); |
| 5443 |
|
| 5444 |
return M; |
| 5445 |
} |
| 5446 |
|
| 5447 |
std::unique_ptr<Module> |
| 5448 |
llvm::convertSpirvToLLVM(LLVMContext &C, SPIRVModule &BM, std::string &ErrMsg) { |
| 5449 |
SPIRV::TranslatorOpts DefaultOpts; |
| 5450 |
return llvm::convertSpirvToLLVM(C, BM, DefaultOpts, ErrMsg); |
| 5451 |
} |
| 5452 |
|
| 5453 |
bool llvm::readSpirv(LLVMContext &C, std::istream &IS, Module *&M, |
| 5454 |
std::string &ErrMsg) { |
| 5455 |
SPIRV::TranslatorOpts DefaultOpts; |
| 5456 |
// As it is stated in the documentation, the translator accepts all SPIR-V |
| 5457 |
// extensions by default |
| 5458 |
DefaultOpts.enableAllExtensions(); |
| 5459 |
return llvm::readSpirv(C, DefaultOpts, IS, M, ErrMsg); |
| 5460 |
} |
| 5461 |
|
| 5462 |
bool llvm::readSpirv(LLVMContext &C, const SPIRV::TranslatorOpts &Opts, |
| 5463 |
std::istream &IS, Module *&M, std::string &ErrMsg) { |
| 5464 |
std::unique_ptr<SPIRVModule> BM(readSpirvModule(IS, Opts, ErrMsg)); |
| 5465 |
|
| 5466 |
if (!BM) |
| 5467 |
return false; |
| 5468 |
|
| 5469 |
if (Opts.getFnVarSpecEnable()) { |
| 5470 |
if (!specializeFnVariants(BM.get(), ErrMsg)) { |
| 5471 |
return false; |
| 5472 |
} |
| 5473 |
|
| 5474 |
// Write out the specialized/targeted module |
| 5475 |
if (!BM->getFnVarSpvOut().empty()) { |
| 5476 |
auto SaveOpt = SPIRVUseTextFormat; |
| 5477 |
auto OFSSpv = std::ofstream(BM->getFnVarSpvOut(), std::ios::binary); |
| 5478 |
SPIRVUseTextFormat = false; |
| 5479 |
OFSSpv << *BM; |
| 5480 |
if (BM->getError(ErrMsg) != SPIRVEC_Success) { |
| 5481 |
return false; |
| 5482 |
} |
| 5483 |
SPIRVUseTextFormat = SaveOpt; |
| 5484 |
} |
| 5485 |
} |
| 5486 |
|
| 5487 |
if (BM->getExtension().find("SPV_INTEL_function_variants") != |
| 5488 |
BM->getExtension().end()) { |
| 5489 |
ErrMsg = "Instructions from SPV_INTEL_function_variants are not " |
| 5490 |
"convertible to LLVM IR."; |
| 5491 |
return false; |
| 5492 |
} |
| 5493 |
|
| 5494 |
M = convertSpirvToLLVM(C, *BM, Opts, ErrMsg).release(); |
| 5495 |
|
| 5496 |
if (!M) |
| 5497 |
return false; |
| 5498 |
|
| 5499 |
if (DbgSaveTmpLLVM) |
| 5500 |
dumpLLVM(M, DbgTmpLLVMFileName); |
| 5501 |
|
| 5502 |
return true; |
| 5503 |
} |
| 5504 |
|
| 5505 |
bool llvm::getSpecConstInfo(std::istream &IS, |
| 5506 |
std::vector<SpecConstInfoTy> &SpecConstInfo) { |
| 5507 |
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule()); |
| 5508 |
BM->setAutoAddExtensions(false); |
| 5509 |
SPIRVDecoder D(IS, *BM); |
| 5510 |
SPIRVWord Magic; |
| 5511 |
D >> Magic; |
| 5512 |
if (!BM->getErrorLog().checkError(Magic == MagicNumber, SPIRVEC_InvalidModule, |
| 5513 |
"invalid magic number")) { |
| 5514 |
return false; |
| 5515 |
} |
| 5516 |
// Skip the rest of the header |
| 5517 |
D.ignore(4); |
| 5518 |
|
| 5519 |
// According to the logical layout of SPIRV module (p2.4 of the spec), |
| 5520 |
// all constant instructions must appear before function declarations. |
| 5521 |
while (D.OpCode != OpFunction && D.getWordCountAndOpCode()) { |
| 5522 |
switch (D.OpCode) { |
| 5523 |
case OpDecorate: |
| 5524 |
// The decoration is added to the module in scope of SPIRVDecorate::decode |
| 5525 |
D.getEntry(); |
| 5526 |
break; |
| 5527 |
case OpTypeBool: |
| 5528 |
case OpTypeInt: |
| 5529 |
case OpTypeFloat: |
| 5530 |
BM->addEntry(D.getEntry()); |
| 5531 |
break; |
| 5532 |
case OpSpecConstant: |
| 5533 |
case OpSpecConstantTrue: |
| 5534 |
case OpSpecConstantFalse: { |
| 5535 |
auto *C = BM->addConstant(static_cast<SPIRVValue *>(D.getEntry())); |
| 5536 |
SPIRVWord SpecConstIdLiteral = 0; |
| 5537 |
if (C->hasDecorate(DecorationSpecId, 0, &SpecConstIdLiteral)) { |
| 5538 |
SPIRVType *Ty = C->getType(); |
| 5539 |
uint32_t SpecConstSize = Ty->isTypeBool() ? 1 : Ty->getBitWidth() / 8; |
| 5540 |
std::string TypeString = ""; |
| 5541 |
if (Ty->isTypeBool()) { |
| 5542 |
TypeString = "i1"; |
| 5543 |
} else if (Ty->isTypeInt()) { |
| 5544 |
switch (SpecConstSize) { |
| 5545 |
case 1: |
| 5546 |
TypeString = "i8"; |
| 5547 |
break; |
| 5548 |
case 2: |
| 5549 |
TypeString = "i16"; |
| 5550 |
break; |
| 5551 |
case 4: |
| 5552 |
TypeString = "i32"; |
| 5553 |
break; |
| 5554 |
case 8: |
| 5555 |
TypeString = "i64"; |
| 5556 |
break; |
| 5557 |
} |
| 5558 |
} else if (Ty->isTypeFloat()) { |
| 5559 |
switch (SpecConstSize) { |
| 5560 |
case 2: |
| 5561 |
TypeString = "f16"; |
| 5562 |
break; |
| 5563 |
case 4: |
| 5564 |
TypeString = "f32"; |
| 5565 |
break; |
| 5566 |
case 8: |
| 5567 |
TypeString = "f64"; |
| 5568 |
break; |
| 5569 |
} |
| 5570 |
} |
| 5571 |
if (TypeString == "") |
| 5572 |
return false; |
| 5573 |
|
| 5574 |
SpecConstInfo.emplace_back( |
| 5575 |
SpecConstInfoTy({SpecConstIdLiteral, SpecConstSize, TypeString})); |
| 5576 |
} |
| 5577 |
break; |
| 5578 |
} |
| 5579 |
default: |
| 5580 |
D.ignoreInstruction(); |
| 5581 |
} |
| 5582 |
} |
| 5583 |
return !IS.bad(); |
| 5584 |
} |
| 5585 |
|
| 5586 |
// clang-format off |
| 5587 |
const StringSet<> SPIRVToLLVM::BuiltInConstFunc { |
| 5588 |
"convert", "get_work_dim", "get_global_size", "sub_group_ballot_bit_count", |
| 5589 |
"get_global_id", "get_local_size", "get_local_id", "get_num_groups", |
| 5590 |
"get_group_id", "get_global_offset", "acos", "acosh", "acospi", |
| 5591 |
"asin", "asinh", "asinpi", "atan", "atan2", "atanh", "atanpi", |
| 5592 |
"atan2pi", "cbrt", "ceil", "copysign", "cos", "cosh", "cospi", |
| 5593 |
"erfc", "erf", "exp", "exp2", "exp10", "expm1", "fabs", "fdim", |
| 5594 |
"floor", "fma", "fmax", "fmin", "fmod", "ilogb", "ldexp", "lgamma", |
| 5595 |
"log", "log2", "log10", "log1p", "logb", "mad", "maxmag", "minmag", |
| 5596 |
"nan", "nextafter", "pow", "pown", "powr", "remainder", "rint", |
| 5597 |
"rootn", "round", "rsqrt", "sin", "sinh", "sinpi", "sqrt", "tan", |
| 5598 |
"tanh", "tanpi", "tgamma", "trunc", "half_cos", "half_divide", "half_exp", |
| 5599 |
"half_exp2", "half_exp10", "half_log", "half_log2", "half_log10", "half_powr", |
| 5600 |
"half_recip", "half_rsqrt", "half_sin", "half_sqrt", "half_tan", "native_cos", |
| 5601 |
"native_divide", "native_exp", "native_exp2", "native_exp10", "native_log", |
| 5602 |
"native_log2", "native_log10", "native_powr", "native_recip", "native_rsqrt", |
| 5603 |
"native_sin", "native_sqrt", "native_tan", "abs", "abs_diff", "add_sat", "hadd", |
| 5604 |
"rhadd", "clamp", "clz", "mad_hi", "mad_sat", "max", "min", "mul_hi", "rotate", |
| 5605 |
"sub_sat", "upsample", "popcount", "mad24", "mul24", "degrees", "mix", "radians", |
| 5606 |
"step", "smoothstep", "sign", "cross", "dot", "distance", "length", "normalize", |
| 5607 |
"fast_distance", "fast_length", "fast_normalize", "isequal", "isnotequal", |
| 5608 |
"isgreater", "isgreaterequal", "isless", "islessequal", "islessgreater", |
| 5609 |
"isfinite", "isinf", "isnan", "isnormal", "isordered", "isunordered", "signbit", |
| 5610 |
"any", "all", "bitselect", "select", "shuffle", "shuffle2", "get_image_width", |
| 5611 |
"get_image_height", "get_image_depth", "get_image_channel_data_type", |
| 5612 |
"get_image_channel_order", "get_image_dim", "get_image_array_size", |
| 5613 |
"get_image_array_size", "sub_group_inverse_ballot", "sub_group_ballot_bit_extract", |
| 5614 |
}; |
| 5615 |
// clang-format on |