@@ -8,8 +8,12 @@ SPDX-License-Identifier: MIT
88
99#include " Compiler/Optimizer/OpenCLPasses/ImageFuncs/ResolveSampledImageBuiltins.hpp"
1010#include " Compiler/IGCPassSupport.h"
11+ #include " Compiler/CodeGenPublic.h"
12+ #include " RelocationInfo.h"
13+ #include " GenISAIntrinsics/GenIntrinsics.h"
1114#include " common/MDFrameWork.h"
1215#include " common/LLVMWarningsPush.hpp"
16+ #include < llvm/IR/Constants.h>
1317#include < llvm/IR/Function.h>
1418#include < llvm/IR/Instructions.h>
1519#include " common/LLVMWarningsPop.hpp"
@@ -25,7 +29,7 @@ using namespace IGC;
2529#define PASS_CFG_ONLY false
2630#define PASS_ANALYSIS false
2731IGC_INITIALIZE_PASS_BEGIN (ResolveSampledImageBuiltins, PASS_FLAG , PASS_DESCRIPTION , PASS_CFG_ONLY , PASS_ANALYSIS )
28- IGC_INITIALIZE_PASS_DEPENDENCY (MetaDataUtilsWrapper )
32+ IGC_INITIALIZE_PASS_DEPENDENCY (CodeGenContextWrapper )
2933IGC_INITIALIZE_PASS_END (ResolveSampledImageBuiltins, PASS_FLAG , PASS_DESCRIPTION , PASS_CFG_ONLY , PASS_ANALYSIS )
3034
3135char ResolveSampledImageBuiltins::ID = 0 ;
@@ -39,7 +43,7 @@ ResolveSampledImageBuiltins::ResolveSampledImageBuiltins() : ModulePass(ID) {
3943
4044bool ResolveSampledImageBuiltins::runOnModule (Module &M) {
4145 m_changed = false ;
42- modMD = getAnalysis<MetaDataUtilsWrapper >().getModuleMetaData ();
46+ modMD = getAnalysis<CodeGenContextWrapper >().getCodeGenContext ()-> getModuleMetaData ();
4347 visit (M);
4448
4549 for (auto builtin : m_builtinsToRemove) {
@@ -138,6 +142,13 @@ Value *ResolveSampledImageBuiltins::lowerGetSampler(CallInst &CI) {
138142 // %image_offset = ptrtoint %spirv.SampledImage._void_1_0_0_0_0_0_0 addrspace(1)* %image to i64
139143 // %sampler_offset = add i64 %image_offset, 128
140144 // %queried_sampler = or i64 %sampler_offset, 1
145+ // Or:
146+ // %image_offset = ptrtoint %spirv.SampledImage._void_1_0_0_0_0_0_0 addrspace(1)* %image to i64
147+ // %surfaceStateSize = call i32 @__static_constant_patch_value
148+ // %surfaceStateSize64 = zext i32 %surfaceStateSize to i64
149+ // %twiceSurfaceStatesSize = shl i64 %surfaceStateSize64, 1
150+ // %sampler_offset = add i64 %image_offset, %twiceSurfaceStatesSize
151+ // %queried_sampler = or i64 %sampler_offset, 1
141152 if (!samplerArg) {
142153 IGC_ASSERT (modMD->UseBindlessImage );
143154 IGC_ASSERT (image->getType ()->isPointerTy ());
@@ -147,12 +158,25 @@ Value *ResolveSampledImageBuiltins::lowerGetSampler(CallInst &CI) {
147158 // | image state | image implicit args state | sampler state | redescribed image state | ...
148159 // Sampler state offset is addition of image state offset, size of
149160 // image state and size of image implicit args state.
150- // Both size of image state and image implicit args state are 64 bytes.
151- constexpr uint64_t surfaceStateSize = 64 ;
152- auto *stateSizeValue = ConstantInt::get (Int64Ty, surfaceStateSize * 2 );
153- auto *samplerOffset = BinaryOperator::CreateAdd (imageOffset, stateSizeValue, " sampler_offset" , &CI );
161+ Module *M = CI .getParent ()->getParent ()->getParent ();
162+ Value *TwoStatesOffset;
163+ if (IGC_GET_FLAG_VALUE (ForceEnableSurfaceStateSizeReloc)) {
164+ // SurfaceStateSize is a relocation patched by the runtime.
165+ auto *PatchNameArg =
166+ ConstantDataArray::getString (CI .getContext (), vISA::SURFACE_STATE_SIZE_RELOCATION_NAME , false );
167+ Type *PatchTys[] = {Type::getInt32Ty (CI .getContext ()), PatchNameArg->getType ()};
168+ auto *PatchFunc = GenISAIntrinsic::getDeclaration (M, GenISAIntrinsic::GenISA_staticConstantPatchValue, PatchTys);
169+ auto *SurfaceStateSizeI32 = CallInst::Create (PatchFunc, PatchNameArg, " surfaceStateSize" , &CI );
170+ auto *SurfaceStateSizeI64 = CastInst::Create (Instruction::ZExt, SurfaceStateSizeI32, Int64Ty, " " , &CI );
171+ // Sampler offset = image state offset + 2 * surfaceStateSize
172+ TwoStatesOffset =
173+ BinaryOperator::Create (Instruction::Shl, SurfaceStateSizeI64, ConstantInt::get (Int64Ty, 1 ), " " , &CI );
174+ } else {
175+ TwoStatesOffset = ConstantInt::get (Int64Ty, 2 * 64 );
176+ }
177+ auto *SamplerOffset = BinaryOperator::CreateAdd (imageOffset, TwoStatesOffset, " sampler_offset" , &CI );
154178 // Set bit-field 0 to 1 to select Bindless Sampler State Base Address.
155- return BinaryOperator::CreateOr (samplerOffset , ConstantInt::get (Int64Ty, 1 ), " " , &CI );
179+ return BinaryOperator::CreateOr (SamplerOffset , ConstantInt::get (Int64Ty, 1 ), " " , &CI );
156180 }
157181
158182 // Transforms the following sequence:
0 commit comments