Skip to content

Commit b05b1c9

Browse files
smilczekigcbot
authored andcommitted
Introduce __INTEL_SURFACE_STATE_SIZE relocation
This commit introduces a mechanism to allow for resolving of surface state size by UMD.
1 parent 96d0990 commit b05b1c9

14 files changed

Lines changed: 107 additions & 23 deletions

File tree

IGC/AdaptorOCL/OCL/sp/zebin_builder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ void ZEBinaryBuilder::addRuntimeSymbols(const IGC::SOpenCLProgramInfo &annotatio
365365
if (annotations.m_hasPerThreadOffsetRelocations)
366366
mBuilder.addSymbol(vISA::PER_THREAD_OFF_RELOCATION_NAME, /*addr*/ 0, /*size*/ 0, llvm::ELF::STB_GLOBAL,
367367
llvm::ELF::STT_NOTYPE, /*sectionId*/ -1);
368+
if (annotations.m_hasSurfaceStateSizeRelocations)
369+
mBuilder.addSymbol(vISA::SURFACE_STATE_SIZE_RELOCATION_NAME, /*addr*/ 0, /*size*/ 0, llvm::ELF::STB_GLOBAL,
370+
llvm::ELF::STT_OBJECT, /*sectionId*/ -1);
368371
}
369372

370373
void ZEBinaryBuilder::addProgramSymbols(const IGC::SOpenCLProgramInfo &annotations) {

IGC/Compiler/CISACodeGen/CISABuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6125,6 +6125,9 @@ void CEncoder::createRelocationTables(VISAKernel &pMainKernel) {
61256125
} else if (reloc.r_symbol == vISA::PER_THREAD_OFF_RELOCATION_NAME) {
61266126
auto cl_context = static_cast<OpenCLProgramContext *>(context);
61276127
cl_context->m_programInfo.m_hasPerThreadOffsetRelocations = true;
6128+
} else if (reloc.r_symbol == vISA::SURFACE_STATE_SIZE_RELOCATION_NAME) {
6129+
auto cl_context = static_cast<OpenCLProgramContext *>(context);
6130+
cl_context->m_programInfo.m_hasSurfaceStateSizeRelocations = true;
61286131
}
61296132
}
61306133
}

IGC/Compiler/CISACodeGen/messageEncoding.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ SPDX-License-Identifier: MIT
77
============================= end_copyright_notice ===========================*/
88

99
#pragma once
10-
#include "Compiler/CodeGenPublic.h"
1110
#include "Compiler/CISACodeGen/helper.h"
1211
#include "visa_igc_common_header.h"
1312

IGC/Compiler/CodeGenPublic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ struct SOpenCLProgramInfo {
636636
ZEBinGlobalHostAccessTable m_zebinGlobalHostAccessTable;
637637
bool m_hasCrossThreadOffsetRelocations = false;
638638
bool m_hasPerThreadOffsetRelocations = false;
639+
bool m_hasSurfaceStateSizeRelocations = false;
639640
};
640641

641642
class CBTILayout {

IGC/Compiler/Optimizer/OpenCLPasses/BindlessImage/ResolveImageImplicitArgsForBindless.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ SPDX-License-Identifier: MIT
88

99
#include "ResolveImageImplicitArgsForBindless.hpp"
1010

11+
#include "Compiler/CodeGenPublic.h"
1112
#include "Compiler/IGCPassSupport.h"
13+
#include "Compiler/CodeGenContextWrapper.hpp"
14+
#include "RelocationInfo.h"
1215
#include "Compiler/CISACodeGen/messageEncoding.hpp"
1316
#include "LLVMWarningsPush.hpp"
1417
#include <llvm/ADT/StringRef.h>
@@ -83,10 +86,10 @@ std::map<StringRef, ImageImplicitArgInfo> BuiltinToArgMap = {
8386
bool ResolveImageImplicitArgsForBindless::runOnModule(Module &M) {
8487
mChanged = false;
8588

86-
CodeGenContext *Ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
87-
if (!Ctx->getModuleMetaData()->extensions.spvINTELBindlessImages)
89+
auto *Ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
90+
auto *modMD = Ctx->getModuleMetaData();
91+
if (!modMD->extensions.spvINTELBindlessImages)
8892
return false; // Bindless exclusive pass.
89-
mDriverInfo = &Ctx->m_DriverInfo;
9093

9194
visit(M);
9295

@@ -119,18 +122,31 @@ void ResolveImageImplicitArgsForBindless::visitCallInst(CallInst &CI) {
119122
Value *Img = CI.getOperand(0);
120123
Value *ImgToInt = isa<IntegerType>(Img->getType()) ? Builder.CreateZExtOrTrunc(Img, Builder.getInt64Ty())
121124
: Builder.CreatePtrToInt(Img, Builder.getInt64Ty());
122-
uint64_t SurfaceStateSize = mDriverInfo->getSurfaceStateSize();
123-
auto *StateSizeValue = Builder.getInt64(SurfaceStateSize);
125+
126+
Value *StateSizeValue;
127+
if (IGC_GET_FLAG_VALUE(ForceEnableSurfaceStateSizeReloc)) {
128+
// When SurfaceStateSize is a relocation patched by the runtime, UMD can choose the surface state size without the
129+
// need to recompile the module.
130+
auto *PatchNameArg = ConstantDataArray::getString(CI.getContext(), vISA::SURFACE_STATE_SIZE_RELOCATION_NAME, false);
131+
Type *PatchTys[] = {Builder.getInt32Ty(), PatchNameArg->getType()};
132+
auto *PatchFunc = GenISAIntrinsic::getDeclaration(M, GenISAIntrinsic::GenISA_staticConstantPatchValue, PatchTys);
133+
Value *SurfaceStateSizeI32 = Builder.CreateCall(PatchFunc, PatchNameArg, "surfaceStateSize");
134+
StateSizeValue = Builder.CreateZExt(SurfaceStateSizeI32, Builder.getInt64Ty());
135+
} else {
136+
StateSizeValue = Builder.getInt64(64);
137+
}
138+
139+
// Offset Img arg into ImageImplicitArgs bindless slot:
124140
auto *ImageImplicitArgsOffset = Builder.CreateAdd(ImgToInt, StateSizeValue);
125-
Value *ImageImplicitArgs =
141+
Value *ImplicitArgsPtr =
126142
Builder.CreateBitOrPointerCast(ImageImplicitArgsOffset, BindlessOffsetPtrTy, "imageImplicitArgs");
127143

128144
// Create ldraw decl:
129-
Type *const Tys[] = {CI.getType(), ImageImplicitArgs->getType()};
145+
Type *const Tys[] = {CI.getType(), ImplicitArgsPtr->getType()};
130146
auto *Callee = GenISAIntrinsic::getDeclaration(M, GenISAIntrinsic::GenISA_ldraw_indexed, Tys);
131147

132148
// Create ldraw call:
133-
Value *const Args[] = {ImageImplicitArgs, Builder.getInt32(ArgOffsetInStruct), Builder.getInt32(AlignmentInBytes),
149+
Value *const Args[] = {ImplicitArgsPtr, Builder.getInt32(ArgOffsetInStruct), Builder.getInt32(AlignmentInBytes),
134150
Builder.getInt1(false)};
135151
CallInst *Result = Builder.CreateCall(Callee, Args);
136152
Result->setDebugLoc(CI.getDebugLoc());

IGC/Compiler/Optimizer/OpenCLPasses/BindlessImage/ResolveImageImplicitArgsForBindless.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SPDX-License-Identifier: MIT
99
///
1010
/// \file
1111
/// This file contains the declaration of ResolveImageImplicitArgsForBindless
12-
/// llvm pass.
12+
/// LLVM pass.
1313
///
1414
/// This pass searches for built-in calls querying for image properties and
1515
/// replaces them with calls to GenISA_ldraw_indexed intrinsic to fetch the
@@ -19,18 +19,14 @@ SPDX-License-Identifier: MIT
1919

2020
#pragma once
2121

22-
#include "Compiler/MetaDataUtilsWrapper.h"
2322
#include "Compiler/CodeGenContextWrapper.hpp"
24-
2523
#include <common/LLVMWarningsPush.hpp>
2624
#include <llvm/ADT/SetVector.h>
2725
#include <llvm/Pass.h>
2826
#include <llvm/IR/InstVisitor.h>
2927
#include <common/LLVMWarningsPop.hpp>
3028

3129
namespace IGC {
32-
class CPlatform;
33-
3430
class ResolveImageImplicitArgsForBindless : public llvm::ModulePass,
3531
public llvm::InstVisitor<ResolveImageImplicitArgsForBindless> {
3632
public:
@@ -51,7 +47,6 @@ class ResolveImageImplicitArgsForBindless : public llvm::ModulePass,
5147
bool runOnModule(llvm::Module &M) override;
5248

5349
private:
54-
CDriverInfo const *mDriverInfo = nullptr;
5550
llvm::SmallSetVector<llvm::Instruction *, 16> mInstsToRemove;
5651

5752
bool mChanged = false;

IGC/Compiler/Optimizer/OpenCLPasses/ImageFuncs/ResolveSampledImageBuiltins.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2731
IGC_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)
2933
IGC_INITIALIZE_PASS_END(ResolveSampledImageBuiltins, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
3034

3135
char ResolveSampledImageBuiltins::ID = 0;
@@ -39,7 +43,7 @@ ResolveSampledImageBuiltins::ResolveSampledImageBuiltins() : ModulePass(ID) {
3943

4044
bool 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:

IGC/Compiler/Optimizer/OpenCLPasses/ImageFuncs/ResolveSampledImageBuiltins.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SPDX-License-Identifier: MIT
99
#pragma once
1010

1111
#include "Compiler/MetaDataUtilsWrapper.h"
12+
#include "Compiler/CodeGenContextWrapper.hpp"
1213
#include "common/LLVMWarningsPush.hpp"
1314
#include <llvm/Pass.h>
1415
#include <llvm/IR/InstVisitor.h>
@@ -32,7 +33,7 @@ class ResolveSampledImageBuiltins : public llvm::ModulePass, public llvm::InstVi
3233

3334
virtual llvm::StringRef getPassName() const override { return "ResolveSampledImageBuiltins"; }
3435

35-
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { AU.addRequired<MetaDataUtilsWrapper>(); }
36+
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { AU.addRequired<CodeGenContextWrapper>(); }
3637

3738
virtual bool runOnModule(llvm::Module &M) override;
3839
void visitCallInst(llvm::CallInst &CI);

IGC/Compiler/tests/ResolveImageImplicitARgsForBindless/basic.ll renamed to IGC/Compiler/tests/ResolveImageImplicitArgsForBindless/basic.ll

File renamed without changes.

IGC/Compiler/tests/ResolveImageImplicitARgsForBindless/exit-for-bindful.ll renamed to IGC/Compiler/tests/ResolveImageImplicitArgsForBindless/exit-for-bindful.ll

File renamed without changes.

0 commit comments

Comments
 (0)