@@ -15,9 +15,11 @@ SPDX-License-Identifier: MIT
15
15
#include " llvmWrapper/IR/IRBuilder.h"
16
16
#include " llvmWrapper/IR/Instructions.h"
17
17
#include " common/LLVMWarningsPop.hpp"
18
+ #include " AdaptorCommon/ImplicitArgs.hpp"
18
19
#include " LLVM3DBuilder/BuiltinsFrontend.hpp"
19
20
#include " Probe/Assertion.h"
20
21
#include " IGC/common/StringMacros.hpp"
22
+ #include < llvm/Support/Casting.h>
21
23
22
24
using namespace llvm ;
23
25
using namespace IGC ;
@@ -249,7 +251,7 @@ Argument* CImagesBI::CImagesUtils::findImageFromBufferPtr(const MetaDataUtils& M
249
251
return nullptr ;
250
252
}
251
253
252
- static bool isBindlessImageLoad (Value *v)
254
+ static bool isSYCLBindlessImageLoad (Value *v)
253
255
{
254
256
auto *load = dyn_cast<LoadInst>(v);
255
257
if (!load)
@@ -270,7 +272,7 @@ ConstantInt* CImagesBI::CImagesUtils::getImageIndex(
270
272
{
271
273
ConstantInt* imageIndex = nullptr ;
272
274
273
- imageParam = ValueTracker::track (pCallInst, paramIndex, nullptr , nullptr , isBindlessImageLoad );
275
+ imageParam = ValueTracker::track (pCallInst, paramIndex, nullptr , nullptr , isSYCLBindlessImageLoad );
274
276
IGC_ASSERT (imageParam);
275
277
IGC_ASSERT (isa<Argument>(imageParam) || isa<LoadInst>(imageParam));
276
278
int i = (*pParamMap)[imageParam].index ;
@@ -280,7 +282,7 @@ ConstantInt* CImagesBI::CImagesUtils::getImageIndex(
280
282
281
283
BufferType CImagesBI::CImagesUtils::getImageType (ParamMap* pParamMap, CallInst* pCallInst, unsigned int paramIndex)
282
284
{
283
- Value *imageParam = ValueTracker::track (pCallInst, paramIndex, nullptr , nullptr , isBindlessImageLoad );
285
+ Value *imageParam = ValueTracker::track (pCallInst, paramIndex, nullptr , nullptr , isSYCLBindlessImageLoad );
284
286
IGC_ASSERT (imageParam);
285
287
IGC_ASSERT (isa<Argument>(imageParam) || isa<LoadInst>(imageParam));
286
288
return isa<LoadInst>(imageParam) ? BufferType::BINDLESS : (*pParamMap)[imageParam].type ;
@@ -439,62 +441,104 @@ class COCL_sample : public CImagesBI
439
441
}
440
442
return false ;
441
443
};
442
- Value* samplerParam = ValueTracker::track (m_pCallInst, 1 , m_pMdUtils, m_modMD, isBindlessSampler);
443
- if (!samplerParam ) {
444
+ Value* samplerValue = ValueTracker::track (m_pCallInst, 1 , m_pMdUtils, m_modMD, isBindlessSampler);
445
+ if (!samplerValue ) {
444
446
emitError (" There are instructions that use a sampler, but no sampler found in the kernel!" , m_pCallInst);
445
447
return nullptr ;
446
448
}
447
449
448
450
auto modMD = m_pCodeGenContext->getModuleMetaData ();
449
451
450
- // If bindless image is preferred, map the bindless pointer
452
+ auto addToInlineSamplersMD = [&](int samplerConstantVal)
453
+ {
454
+ // Is this sampler already allocated?
455
+ auto iter = m_pInlineMap->find (samplerConstantVal);
456
+ if (iter != m_pInlineMap->end ())
457
+ {
458
+ return ConstantInt::get (m_pIntType, iter->second );
459
+ }
460
+
461
+ // No, allocate it.
462
+ int currSamplerIdx = (*m_pNextSampler)++;
463
+ (*m_pInlineMap)[samplerConstantVal] = currSamplerIdx;
464
+ samplerIndex = ConstantInt::get (m_pIntType, currSamplerIdx);
465
+
466
+ // Push this information into the metadata, for the state processor's benefit
467
+ FunctionMetaData& funcMD = m_modMD->FuncMD [m_pFunc];
468
+ ResourceAllocMD& resAllocMD = funcMD.resAllocMD ;
469
+ InlineSamplersMD inlineSamplerMD;
470
+ CreateInlineSamplerAnnotations (inlineSamplerMD, samplerConstantVal);
471
+ inlineSamplerMD.index = currSamplerIdx;
472
+ resAllocMD.inlineSamplersMD .push_back (inlineSamplerMD);
473
+ m_pMdUtils->save (*m_pCtx);
474
+ return samplerIndex;
475
+ };
476
+
451
477
if (modMD->UseBindlessImage )
452
478
{
453
- // If sampler is argument, look up index in the parameter map.
454
- int i = isa<Argument>(samplerParam) ? (*m_pParamMap)[samplerParam].index : 0 ;
455
- samplerIndex = ConstantInt::get (m_pIntType, i);
456
- unsigned int addressSpace = IGC::EncodeAS4GFXResource (*samplerIndex, BufferType::BINDLESS_SAMPLER);
457
- Type* ptrTy = llvm::PointerType::get (m_pFloatType, addressSpace);
458
- Value* bindlessSampler = isa<IntegerType>(samplerParam->getType ()) ?
459
- BitCastInst::CreateBitOrPointerCast (samplerParam, ptrTy, " bindless_sampler" , m_pCallInst) :
460
- BitCastInst::CreatePointerCast (samplerParam, ptrTy, " bindless_sampler" , m_pCallInst);
479
+ // In bindless mode, samplerValue is BinaryOperator::Or computed in ResolveSampledImageBuiltins pass.
480
+ // Continue to track back the sampler's origin value, which could be one of following values:
481
+ // * a constant, e.g. inline sampler.
482
+ // * kernel argument, e.g. sampler that is kernel argument.
483
+ // * load inst, e.g. sampler in SYCL bindless sampled image.
484
+ Value *samplerValueOrigin = ValueTracker::track (samplerValue, m_pFunc, m_pMdUtils, m_modMD, isSYCLBindlessImageLoad);
485
+ if (!samplerValueOrigin) {
486
+ emitError (" There are instructions that use a sampler, but no sampler found in the kernel!" , m_pCallInst);
487
+ return nullptr ;
488
+ }
489
+ // Map the bindless pointer.
490
+ Value *bindlessSampler = nullptr ;
491
+ if (auto *samplerConstant = dyn_cast<ConstantInt>(samplerValueOrigin))
492
+ {
493
+ // Sampler is constant, e.g. inline sampler.
494
+ int samplerConstantVal = int_cast<int >(samplerConstant->getZExtValue ());
495
+ samplerIndex = addToInlineSamplersMD (samplerConstantVal);
496
+
497
+ // Bindless inline sampler is passed via implicit kernel argument.
498
+ ImplicitArgs implicitArgs (*m_pFunc, m_pMdUtils);
499
+
500
+ Argument *arg = implicitArgs.getNumberedImplicitArg (*m_pFunc, ImplicitArg::INLINE_SAMPLER, samplerConstantVal);
501
+ if (!arg) {
502
+ emitError (" Implicit arg isn't found for inline sampler!" , m_pCallInst);
503
+ return nullptr ;
504
+ }
505
+ auto *binOp = cast<BinaryOperator>(samplerValue);
506
+ auto *argExt = new ZExtInst (arg, Type::getInt64Ty (*m_pCtx), " " , binOp);
507
+ binOp->setOperand (0 , argExt);
508
+
509
+ unsigned addressSpace = IGC::EncodeAS4GFXResource (*samplerIndex, BufferType::BINDLESS_SAMPLER);
510
+ Type *ptrTy = PointerType::get (m_pFloatType, addressSpace);
511
+ bindlessSampler = BitCastInst::CreateBitOrPointerCast (m_pCallInst->getOperand (1 ), ptrTy, " bindless_sampler" , m_pCallInst);
512
+ }
513
+ else
514
+ {
515
+ // Sampler is either sampler that is kernel argument, or sampler in SYCL bindless sampled image.
516
+ // If sampler is argument, look up index in the parameter map.
517
+ int i = isa<Argument>(samplerValueOrigin) ? (*m_pParamMap)[samplerValueOrigin].index : 0 ;
518
+ samplerIndex = ConstantInt::get (m_pIntType, i);
519
+ unsigned int addressSpace = IGC::EncodeAS4GFXResource (*samplerIndex, BufferType::BINDLESS_SAMPLER);
520
+ Type* ptrTy = llvm::PointerType::get (m_pFloatType, addressSpace);
521
+ bindlessSampler = isa<IntegerType>(samplerValue->getType ()) ?
522
+ BitCastInst::CreateBitOrPointerCast (samplerValue, ptrTy, " bindless_sampler" , m_pCallInst) :
523
+ BitCastInst::CreatePointerCast (samplerValue, ptrTy, " bindless_sampler" , m_pCallInst);
524
+ }
461
525
return bindlessSampler;
462
526
}
463
527
464
528
// Argument samplers are looked up in the parameter map
465
- if (isa<Argument>(samplerParam ))
529
+ if (isa<Argument>(samplerValue ))
466
530
{
467
- int i = (*m_pParamMap)[samplerParam ].index ;
531
+ int i = (*m_pParamMap)[samplerValue ].index ;
468
532
samplerIndex = ConstantInt::get (m_pIntType, i);
469
533
return samplerIndex;
470
534
}
471
535
472
536
// The sampler is not an argument, make sure it's a constant
473
- IGC_ASSERT_MESSAGE (isa<ConstantInt>(samplerParam), " Sampler must be a global variable or a constant" );
474
- ConstantInt* constSampler = cast<ConstantInt>(samplerParam);
475
- int samplerValue = int_cast<int >(constSampler->getZExtValue ());
476
-
477
- // Is this sampler already allocated?
478
- auto iter = m_pInlineMap->find (samplerValue);
479
- if (iter != m_pInlineMap->end ())
480
- {
481
- return ConstantInt::get (m_pIntType, iter->second );
482
- }
537
+ IGC_ASSERT_MESSAGE (isa<ConstantInt>(samplerValue), " Sampler must be a global variable or a constant" );
538
+ auto *samplerConstant = cast<ConstantInt>(samplerValue);
539
+ int samplerConstantVal = int_cast<int >(samplerConstant->getZExtValue ());
483
540
484
- // No, allocate it.
485
- int currSamplerIdx = (*m_pNextSampler)++;
486
- (*m_pInlineMap)[samplerValue] = currSamplerIdx;
487
- samplerIndex = ConstantInt::get (m_pIntType, currSamplerIdx);
488
-
489
- // Push this information into the metadata, for the state processor's benefit
490
- FunctionMetaData& funcMD = m_modMD->FuncMD [m_pFunc];
491
- ResourceAllocMD& resAllocMD = funcMD.resAllocMD ;
492
- InlineSamplersMD inlineSamplerMD;
493
- CreateInlineSamplerAnnotations (inlineSamplerMD, samplerValue);
494
- inlineSamplerMD.index = currSamplerIdx;
495
- resAllocMD.inlineSamplersMD .push_back (inlineSamplerMD);
496
- m_pMdUtils->save (*m_pCtx);
497
- return samplerIndex;
541
+ return addToInlineSamplersMD (samplerConstantVal);
498
542
}
499
543
500
544
void CreateInlineSamplerAnnotations (InlineSamplersMD& inlineSamplerMD, int samplerValue)
0 commit comments