@@ -1236,20 +1236,67 @@ Value *SPIRVToLLVM::oclTransConstantPipeStorage(
1236
1236
GlobalValue::NotThreadLocal, SPIRAS_Global);
1237
1237
}
1238
1238
1239
+ namespace {
1240
+
1239
1241
// A pointer annotation may have been generated for the operand. If the operand
1240
1242
// is used further in IR, it should be replaced with the intrinsic call result.
1241
1243
// Otherwise, the generated pointer annotation call is left unused.
1242
- static void replaceOperandWithAnnotationIntrinsicCallResult (Value *&V) {
1243
- if (Use *SingleUse = V->getSingleUndroppableUse ()) {
1244
- if (auto *II = dyn_cast<IntrinsicInst>(SingleUse->getUser ())) {
1245
- if (II->getIntrinsicID () == Intrinsic::ptr_annotation &&
1246
- II->getType () == V->getType ())
1247
- // Overwrite the future operand with the intrinsic call result.
1248
- V = II;
1244
+ static void replaceOperandWithAnnotationIntrinsicCallResult (Function *F,
1245
+ Value *&V) {
1246
+
1247
+ SPIRVDBG (spvdbgs () << " \n "
1248
+ << " -------- REPLACE --------" << ' \n ' ;)
1249
+ SPIRVDBG (dbgs () << " value: " << *V << ' \n ' );
1250
+
1251
+ Value *BaseValue = nullptr ;
1252
+ IntrinsicInst *CallResult = nullptr ;
1253
+
1254
+ auto SearchPtrAnn = [=](Value *BV, IntrinsicInst *&CR) {
1255
+ CR = nullptr ;
1256
+ for (auto *Use : BV->users ()) {
1257
+ if (auto *II = dyn_cast<IntrinsicInst>(Use)) {
1258
+ if (II->getIntrinsicID () == Intrinsic::ptr_annotation &&
1259
+ II->getType () == BV->getType ()) {
1260
+ assert (CR == nullptr && " Multiple annotation created for same value" );
1261
+ CR = II;
1262
+ }
1263
+ }
1264
+ }
1265
+ return CR ? true : false ;
1266
+ };
1267
+
1268
+ if (SearchPtrAnn (V, CallResult)) {
1269
+ BaseValue = V;
1270
+ } else {
1271
+ // scan def-use chain, skip bitcast and addrspacecast
1272
+ // search for the closest floating ptr.annotation
1273
+ auto *Inst = dyn_cast<Instruction>(V);
1274
+ while (Inst && (isa<BitCastInst>(Inst) || isa<AddrSpaceCastInst>(Inst))) {
1275
+ if ((Inst = dyn_cast<Instruction>(Inst->getOperand (0 ))) &&
1276
+ SearchPtrAnn (Inst, CallResult)) {
1277
+ BaseValue = Inst;
1278
+ break ;
1279
+ }
1249
1280
}
1250
1281
}
1282
+
1283
+ // overwrite operand with intrinsic call result
1284
+ if (CallResult) {
1285
+ SPIRVDBG (dbgs () << " BaseValue: " << *BaseValue << ' \n '
1286
+ << " CallResult: " << *CallResult << ' \n ' );
1287
+ DominatorTree DT (*F);
1288
+ BaseValue->replaceUsesWithIf (CallResult, [&DT, &CallResult](Use &U) {
1289
+ return DT.dominates (CallResult, U);
1290
+ });
1291
+
1292
+ // overwrite V
1293
+ if (V == BaseValue)
1294
+ V = CallResult;
1295
+ }
1251
1296
}
1252
1297
1298
+ } // namespace
1299
+
1253
1300
// Translate aliasing memory access masks for SPIRVLoad and SPIRVStore
1254
1301
// instructions. These masks are mapped on alias.scope and noalias
1255
1302
// metadata in LLVM. Translation of optional string operand isn't yet supported
@@ -1727,9 +1774,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
1727
1774
auto *Src = transValue (BS->getSrc (), F, BB);
1728
1775
auto *Dst = transValue (BS->getDst (), F, BB);
1729
1776
// A ptr.annotation may have been generated for the source variable.
1730
- replaceOperandWithAnnotationIntrinsicCallResult (Src);
1777
+ replaceOperandWithAnnotationIntrinsicCallResult (F, Src);
1731
1778
// A ptr.annotation may have been generated for the destination variable.
1732
- replaceOperandWithAnnotationIntrinsicCallResult (Dst);
1779
+ replaceOperandWithAnnotationIntrinsicCallResult (F, Dst);
1733
1780
1734
1781
bool isVolatile = BS->SPIRVMemoryAccess ::isVolatile ();
1735
1782
uint64_t AlignValue = BS->SPIRVMemoryAccess ::getAlignment ();
@@ -1747,7 +1794,7 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
1747
1794
SPIRVLoad *BL = static_cast <SPIRVLoad *>(BV);
1748
1795
auto *V = transValue (BL->getSrc (), F, BB);
1749
1796
// A ptr.annotation may have been generated for the source variable.
1750
- replaceOperandWithAnnotationIntrinsicCallResult (V);
1797
+ replaceOperandWithAnnotationIntrinsicCallResult (F, V);
1751
1798
1752
1799
Type *Ty = transType (BL->getType ());
1753
1800
LoadInst *LI = nullptr ;
0 commit comments