@@ -562,41 +562,6 @@ bool SPIRVToLLVM::isSPIRVCmpInstTransToLLVMInst(SPIRVInstruction *BI) const {
562
562
return isCmpOpCode (OC) && !(OC >= OpLessOrGreater && OC <= OpUnordered);
563
563
}
564
564
565
- // TODO: Instead of direct translation to OCL we should always produce SPIR-V
566
- // friendly IR and apply lowering later if needed
567
- bool SPIRVToLLVM::isDirectlyTranslatedToOCL (Op OpCode) const {
568
- if (isSubgroupAvcINTELInstructionOpCode (OpCode))
569
- return false ;
570
- if (isIntelSubgroupOpCode (OpCode))
571
- return false ;
572
- if (OpCode == OpImageSampleExplicitLod || OpCode == OpSampledImage)
573
- return false ;
574
- if (OpCode == OpImageWrite || OpCode == OpImageRead ||
575
- OpCode == OpImageQueryOrder || OpCode == OpImageQueryFormat ||
576
- OpCode == OpImageQueryLevels)
577
- return false ;
578
- if (OpCode == OpGenericCastToPtrExplicit)
579
- return false ;
580
- if (isEventOpCode (OpCode))
581
- return false ;
582
- if (OpBitFieldInsert <= OpCode && OpCode <= OpBitReverse)
583
- return false ;
584
- if (OpCode == OpEnqueueMarker || OpCode == OpGetDefaultQueue)
585
- return false ;
586
- if (OCLSPIRVBuiltinMap::rfind (OpCode, nullptr )) {
587
- // Not every spirv opcode which is placed in OCLSPIRVBuiltinMap is
588
- // translated directly to OCL builtin. Some of them are translated
589
- // to LLVM representation without any modifications (SPIRV format of
590
- // instruction is represented in LLVM) and then its translated to
591
- // clang-consistent format in SPIRVToOCL pass.
592
- return !(isAtomicOpCode (OpCode) || isGroupOpCode (OpCode) ||
593
- isGroupNonUniformOpcode (OpCode) || isPipeOpCode (OpCode) ||
594
- isMediaBlockINTELOpcode (OpCode) || OpCode == OpGroupAsyncCopy ||
595
- OpCode == OpGroupWaitEvents);
596
- }
597
- return false ;
598
- }
599
-
600
565
void SPIRVToLLVM::setName (llvm::Value *V, SPIRVValue *BV) {
601
566
auto Name = BV->getName ();
602
567
if (!Name.empty () && (!V->hasName () || Name != V->getName ()))
@@ -1117,102 +1082,6 @@ Value *SPIRVToLLVM::transCmpInst(SPIRVValue *BV, BasicBlock *BB, Function *F) {
1117
1082
return Inst;
1118
1083
}
1119
1084
1120
- bool SPIRVToLLVM::postProcessOCL () {
1121
- StringRef DemangledName;
1122
- SPIRVWord SrcLangVer = 0 ;
1123
- BM->getSourceLanguage (&SrcLangVer);
1124
- bool IsCpp = SrcLangVer == kOCLVer ::CL21;
1125
- for (auto I = M->begin (), E = M->end (); I != E;) {
1126
- auto F = I++;
1127
- if (F->hasName () && F->isDeclaration ()) {
1128
- LLVM_DEBUG (dbgs () << " [postProcessOCL sret] " << *F << ' \n ' );
1129
- if (F->getReturnType ()->isStructTy () &&
1130
- oclIsBuiltin (F->getName (), DemangledName, IsCpp)) {
1131
- if (!postProcessOCLBuiltinReturnStruct (&(*F)))
1132
- return false ;
1133
- }
1134
- }
1135
- }
1136
- for (auto I = M->begin (), E = M->end (); I != E;) {
1137
- auto F = I++;
1138
- if (F->hasName () && F->isDeclaration ()) {
1139
- LLVM_DEBUG (dbgs () << " [postProcessOCL array arg] " << *F << ' \n ' );
1140
- if (hasArrayArg (&(*F)) &&
1141
- oclIsBuiltin (F->getName (), DemangledName, IsCpp))
1142
- if (!postProcessOCLBuiltinWithArrayArguments (&(*F), DemangledName))
1143
- return false ;
1144
- }
1145
- }
1146
- return true ;
1147
- }
1148
-
1149
- bool SPIRVToLLVM::postProcessOCLBuiltinReturnStruct (Function *F) {
1150
- std::string Name = F->getName ().str ();
1151
- F->setName (Name + " .old" );
1152
- for (auto I = F->user_begin (), E = F->user_end (); I != E;) {
1153
- if (auto CI = dyn_cast<CallInst>(*I++)) {
1154
- auto ST = dyn_cast<StoreInst>(*(CI->user_begin ()));
1155
- assert (ST);
1156
- std::vector<Type *> ArgTys;
1157
- getFunctionTypeParameterTypes (F->getFunctionType (), ArgTys);
1158
- ArgTys.insert (ArgTys.begin (),
1159
- PointerType::get (F->getReturnType (), SPIRAS_Private));
1160
- auto NewF =
1161
- getOrCreateFunction (M, Type::getVoidTy (*Context), ArgTys, Name);
1162
- NewF->setCallingConv (F->getCallingConv ());
1163
- auto Args = getArguments (CI);
1164
- Args.insert (Args.begin (), ST->getPointerOperand ());
1165
- auto NewCI = CallInst::Create (NewF, Args, CI->getName (), CI);
1166
- NewCI->setCallingConv (CI->getCallingConv ());
1167
- ST->eraseFromParent ();
1168
- CI->eraseFromParent ();
1169
- }
1170
- }
1171
- F->eraseFromParent ();
1172
- return true ;
1173
- }
1174
-
1175
- bool SPIRVToLLVM::postProcessOCLBuiltinWithArrayArguments (
1176
- Function *F, StringRef DemangledName) {
1177
- LLVM_DEBUG (dbgs () << " [postProcessOCLBuiltinWithArrayArguments] " << *F
1178
- << ' \n ' );
1179
- auto Attrs = F->getAttributes ();
1180
- auto Name = F->getName ();
1181
- mutateFunction (
1182
- F,
1183
- [=](CallInst *CI, std::vector<Value *> &Args) {
1184
- auto FBegin =
1185
- CI->getParent ()->getParent ()->begin ()->getFirstInsertionPt ();
1186
- for (auto &I : Args) {
1187
- auto T = I->getType ();
1188
- if (!T->isArrayTy ())
1189
- continue ;
1190
- auto Alloca = new AllocaInst (T, 0 , " " , &(*FBegin));
1191
- new StoreInst (I, Alloca, false , CI);
1192
- auto Zero =
1193
- ConstantInt::getNullValue (Type::getInt32Ty (T->getContext ()));
1194
- Value *Index[] = {Zero, Zero};
1195
- I = GetElementPtrInst::CreateInBounds (T, Alloca, Index, " " , CI);
1196
- }
1197
- return Name.str ();
1198
- },
1199
- nullptr , &Attrs);
1200
- return true ;
1201
- }
1202
-
1203
- CallInst *SPIRVToLLVM::postProcessOCLBuildNDRange (SPIRVInstruction *BI,
1204
- CallInst *CI,
1205
- const std::string &FuncName) {
1206
- assert (CI->getNumArgOperands () == 3 );
1207
- auto GWS = CI->getArgOperand (0 );
1208
- auto LWS = CI->getArgOperand (1 );
1209
- auto GWO = CI->getArgOperand (2 );
1210
- CI->setArgOperand (0 , GWO);
1211
- CI->setArgOperand (1 , GWS);
1212
- CI->setArgOperand (2 , LWS);
1213
- return CI;
1214
- }
1215
-
1216
1085
Type *SPIRVToLLVM::mapType (SPIRVType *BT, Type *T) {
1217
1086
SPIRVDBG (dbgs () << *T << ' \n ' ;)
1218
1087
TypeMap[BT] = T;
@@ -2569,14 +2438,17 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
2569
2438
2570
2439
default : {
2571
2440
auto OC = BV->getOpCode ();
2572
- if (isSPIRVCmpInstTransToLLVMInst (static_cast <SPIRVInstruction *>(BV))) {
2441
+ if (isSPIRVCmpInstTransToLLVMInst (static_cast <SPIRVInstruction *>(BV)))
2573
2442
return mapValue (BV, transCmpInst (BV, BB, F));
2574
- } else if (isDirectlyTranslatedToOCL (OC)) {
2575
- return mapValue (
2576
- BV, transOCLBuiltinFromInst (static_cast <SPIRVInstruction *>(BV), BB));
2577
- } else if (isBinaryShiftLogicalBitwiseOpCode (OC) || isLogicalOpCode (OC)) {
2443
+
2444
+ if (OCLSPIRVBuiltinMap::rfind (OC, nullptr ))
2445
+ return mapValue (BV, transSPIRVBuiltinFromInst (
2446
+ static_cast <SPIRVInstruction *>(BV), BB));
2447
+
2448
+ if (isBinaryShiftLogicalBitwiseOpCode (OC) || isLogicalOpCode (OC))
2578
2449
return mapValue (BV, transShiftLogicalBitwiseInst (BV, BB, F));
2579
- } else if (isCvtOpCode (OC) && OC != OpGenericCastToPtrExplicit) {
2450
+
2451
+ if (isCvtOpCode (OC) && OC != OpGenericCastToPtrExplicit) {
2580
2452
auto BI = static_cast <SPIRVInstruction *>(BV);
2581
2453
Value *Inst = nullptr ;
2582
2454
if (BI->hasFPRoundingMode () || BI->isSaturatedConversion ())
@@ -2929,8 +2801,6 @@ SPIRVToLLVM::transOCLBuiltinPostproc(SPIRVInstruction *BI, CallInst *CI,
2929
2801
}
2930
2802
if (OC == OpGenericPtrMemSemantics)
2931
2803
return BinaryOperator::CreateShl (CI, getInt32 (M, 8 ), " " , BB);
2932
- if (OC == OpBuildNDRange)
2933
- return postProcessOCLBuildNDRange (BI, CI, DemangledName);
2934
2804
if (SPIRVEnableStepExpansion &&
2935
2805
(DemangledName == " smoothstep" || DemangledName == " step" ))
2936
2806
return expandOCLBuiltinWithScalarArg (CI, DemangledName);
@@ -3161,30 +3031,6 @@ SPIRVToLLVM::SPIRVToLLVM(Module *LLVMModule, SPIRVModule *TheSPIRVModule)
3161
3031
DbgTran.reset (new SPIRVToLLVMDbgTran (TheSPIRVModule, LLVMModule, this ));
3162
3032
}
3163
3033
3164
- std::string SPIRVToLLVM::getOCLBuiltinName (SPIRVInstruction *BI) {
3165
- auto OC = BI->getOpCode ();
3166
- if (OC == OpBuildNDRange) {
3167
- auto NDRangeInst = static_cast <SPIRVBuildNDRange *>(BI);
3168
- auto EleTy = ((NDRangeInst->getOperands ())[0 ])->getType ();
3169
- int Dim = EleTy->isTypeArray () ? EleTy->getArrayLength () : 1 ;
3170
- // cygwin does not have std::to_string
3171
- ostringstream OS;
3172
- OS << Dim;
3173
- assert ((EleTy->isTypeInt () && Dim == 1 ) ||
3174
- (EleTy->isTypeArray () && Dim >= 2 && Dim <= 3 ));
3175
- return std::string (kOCLBuiltinName ::NDRangePrefix) + OS.str () + " D" ;
3176
- }
3177
-
3178
- return OCLSPIRVBuiltinMap::rmap (OC);
3179
- }
3180
-
3181
- Instruction *SPIRVToLLVM::transOCLBuiltinFromInst (SPIRVInstruction *BI,
3182
- BasicBlock *BB) {
3183
- assert (BB && " Invalid BB" );
3184
- auto FuncName = getOCLBuiltinName (BI);
3185
- return transBuiltinFromInst (FuncName, BI, BB);
3186
- }
3187
-
3188
3034
std::string getSPIRVFuncSuffix (SPIRVInstruction *BI) {
3189
3035
string Suffix = " " ;
3190
3036
if (BI->getOpCode () == OpCreatePipeFromPipeStorage) {
@@ -3231,6 +3077,17 @@ std::string getSPIRVFuncSuffix(SPIRVInstruction *BI) {
3231
3077
llvm_unreachable (" Invalid address space" );
3232
3078
}
3233
3079
}
3080
+ if (BI->getOpCode () == OpBuildNDRange) {
3081
+ Suffix += kSPIRVPostfix ::Divider;
3082
+ auto *NDRangeInst = static_cast <SPIRVBuildNDRange *>(BI);
3083
+ auto *EleTy = ((NDRangeInst->getOperands ())[0 ])->getType ();
3084
+ int Dim = EleTy->isTypeArray () ? EleTy->getArrayLength () : 1 ;
3085
+ assert ((EleTy->isTypeInt () && Dim == 1 ) ||
3086
+ (EleTy->isTypeArray () && Dim >= 2 && Dim <= 3 ));
3087
+ ostringstream OS;
3088
+ OS << Dim;
3089
+ Suffix += OS.str () + " D" ;
3090
+ }
3234
3091
return Suffix;
3235
3092
}
3236
3093
@@ -3319,8 +3176,13 @@ bool SPIRVToLLVM::translate() {
3319
3176
// as calls.
3320
3177
if (!lowerBuiltinVariablesToCalls (M))
3321
3178
return false ;
3322
- if (!postProcessOCL ())
3323
- return false ;
3179
+ if (BM->getDesiredBIsRepresentation () == BIsRepresentation::SPIRVFriendlyIR) {
3180
+ SPIRVWord SrcLangVer = 0 ;
3181
+ BM->getSourceLanguage (&SrcLangVer);
3182
+ bool IsCpp = SrcLangVer == kOCLVer ::CL21;
3183
+ if (!postProcessBuiltinsReturningStruct (M, IsCpp))
3184
+ return false ;
3185
+ }
3324
3186
eraseUselessFunctions (M);
3325
3187
3326
3188
DbgTran->addDbgInfoVersion ();
0 commit comments