Skip to content

Commit 81d4678

Browse files
dlei6ggfxbot
authored andcommitted
Create Patches for Function Pointer Symbol and Relocation Tables
Change-Id: I29ce6ed33d8f74caef9b818e452ff770419e9647
1 parent 6a6a81d commit 81d4678

File tree

9 files changed

+252
-28
lines changed

9 files changed

+252
-28
lines changed

IGC/AdaptorOCL/OCL/Patch/patch_parser.cpp

+53-20
Original file line numberDiff line numberDiff line change
@@ -1414,29 +1414,62 @@ void DebugPatchList(
14141414
break;
14151415

14161416
case iOpenCL::PATCH_TOKEN_GTPIN_FREE_GRF_INFO:
1417-
{
1418-
const iOpenCL::SPatchGtpinFreeGRFInfo* pPatchItem =
1419-
(const iOpenCL::SPatchGtpinFreeGRFInfo*)pHeader;
1417+
{
1418+
const iOpenCL::SPatchGtpinFreeGRFInfo* pPatchItem =
1419+
(const iOpenCL::SPatchGtpinFreeGRFInfo*)pHeader;
14201420

1421-
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1422-
"PATCH_TOKEN_GTPIN_FREE_GRF_INFO (%08X) (size = %d)\n",
1423-
pPatchItem->Token,
1424-
pPatchItem->Size);
1421+
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1422+
"PATCH_TOKEN_GTPIN_FREE_GRF_INFO (%08X) (size = %d)\n",
1423+
pPatchItem->Token,
1424+
pPatchItem->Size);
14251425

1426-
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1427-
"\tBufferSize = %d\n",
1428-
pPatchItem->BufferSize);
1429-
}
1426+
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1427+
"\tBufferSize = %d\n",
1428+
pPatchItem->BufferSize);
1429+
}
1430+
break;
14301431
case iOpenCL::PATCH_TOKEN_GTPIN_INFO:
1431-
{
1432-
const iOpenCL::SPatchItemHeader* pPatchItem = pHeader;
1433-
1434-
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1435-
"PATCH_TOKEN_GTPIN_INFO (%08X) (size = %d)\n",
1436-
pPatchItem->Token,
1437-
pPatchItem->Size);
1438-
}
1439-
break;
1432+
{
1433+
const iOpenCL::SPatchItemHeader* pPatchItem = pHeader;
1434+
1435+
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1436+
"PATCH_TOKEN_GTPIN_INFO (%08X) (size = %d)\n",
1437+
pPatchItem->Token,
1438+
pPatchItem->Size);
1439+
}
1440+
break;
1441+
case iOpenCL::PATCH_TOKEN_FUNCTION_SYMBOL_TABLE:
1442+
{
1443+
const iOpenCL::SPatchFunctionTableInfo* pPatchItem =
1444+
(const iOpenCL::SPatchFunctionTableInfo*)pHeader;
1445+
1446+
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1447+
"PATCH_TOKEN_FUNCTION_SYMBOL_TABLE (%08X) (size = %d)\n",
1448+
pPatchItem->Token,
1449+
pPatchItem->Size);
1450+
1451+
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1452+
"\tNumEntries = %d\n",
1453+
pPatchItem->NumEntries);
1454+
1455+
}
1456+
break;
1457+
case iOpenCL::PATCH_TOKEN_FUNCTION_RELOCATION_TABLE:
1458+
{
1459+
const iOpenCL::SPatchFunctionTableInfo* pPatchItem =
1460+
(const iOpenCL::SPatchFunctionTableInfo*)pHeader;
1461+
1462+
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1463+
"PATCH_TOKEN_FUNCTION_RELOCATION_TABLE (%08X) (size = %d)\n",
1464+
pPatchItem->Token,
1465+
pPatchItem->Size);
1466+
1467+
ICBE_DPF_STR(output, GFXDBG_HARDWARE,
1468+
"\tNumEntries = %d\n",
1469+
pPatchItem->NumEntries);
1470+
1471+
}
1472+
break;
14401473

14411474
default:
14421475
{

IGC/AdaptorOCL/OCL/sp/sp_g8.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,98 @@ RETVAL CGen8OpenCLStateProcessor::CreatePatchList(
21042104
}
21052105
}
21062106

2107+
// Patch for function symbol table
2108+
if (retValue.Success)
2109+
{
2110+
iOpenCL::SPatchFunctionTableInfo patch;
2111+
memset(&patch, 0, sizeof(patch));
2112+
2113+
patch.Token = PATCH_TOKEN_FUNCTION_SYMBOL_TABLE;
2114+
uint32_t size = 0;
2115+
uint32_t entries = 0;
2116+
void* buffer = nullptr;
2117+
const IGC::SKernelProgram* program = &(annotations.m_kernelProgram);
2118+
if (annotations.m_executionEnivronment.CompiledSIMDSize == 8)
2119+
{
2120+
buffer = program->simd8.m_funcSymbolTable;
2121+
size = program->simd8.m_funcSymbolTableSize;
2122+
entries = program->simd8.m_funcSymbolTableEntries;
2123+
}
2124+
else if (annotations.m_executionEnivronment.CompiledSIMDSize == 16)
2125+
{
2126+
buffer = program->simd16.m_funcSymbolTable;
2127+
size = program->simd16.m_funcSymbolTableSize;
2128+
entries = program->simd16.m_funcSymbolTableEntries;
2129+
}
2130+
else if (annotations.m_executionEnivronment.CompiledSIMDSize == 32)
2131+
{
2132+
buffer = program->simd32.m_funcSymbolTable;
2133+
size = program->simd32.m_funcSymbolTableSize;
2134+
entries = program->simd32.m_funcSymbolTableEntries;
2135+
}
2136+
2137+
if (size > 0)
2138+
{
2139+
patch.Size = sizeof(patch) + size;
2140+
patch.NumEntries = entries;
2141+
2142+
retValue = AddPatchItem(patch, membuf);
2143+
2144+
if (!membuf.Write((const char*)buffer, size))
2145+
{
2146+
retValue.Success = false;
2147+
return retValue;
2148+
}
2149+
free(buffer);
2150+
}
2151+
}
2152+
2153+
// Patch for function relocation table
2154+
if (retValue.Success)
2155+
{
2156+
iOpenCL::SPatchFunctionTableInfo patch;
2157+
memset(&patch, 0, sizeof(patch));
2158+
2159+
patch.Token = PATCH_TOKEN_FUNCTION_RELOCATION_TABLE;
2160+
uint32_t size = 0;
2161+
uint32_t entries = 0;
2162+
void* buffer = nullptr;
2163+
const IGC::SKernelProgram* program = &(annotations.m_kernelProgram);
2164+
if (annotations.m_executionEnivronment.CompiledSIMDSize == 8)
2165+
{
2166+
buffer = program->simd8.m_funcRelocationTable;
2167+
size = program->simd8.m_funcRelocationTableSize;
2168+
entries = program->simd8.m_funcRelocationTableEntries;
2169+
}
2170+
else if (annotations.m_executionEnivronment.CompiledSIMDSize == 16)
2171+
{
2172+
buffer = program->simd16.m_funcRelocationTable;
2173+
size = program->simd16.m_funcRelocationTableSize;
2174+
entries = program->simd16.m_funcRelocationTableEntries;
2175+
}
2176+
else if (annotations.m_executionEnivronment.CompiledSIMDSize == 32)
2177+
{
2178+
buffer = program->simd32.m_funcRelocationTable;
2179+
size = program->simd32.m_funcRelocationTableSize;
2180+
entries = program->simd32.m_funcRelocationTableEntries;
2181+
}
2182+
2183+
if (size > 0)
2184+
{
2185+
patch.Size = sizeof(patch) + size;
2186+
patch.NumEntries = entries;
2187+
2188+
retValue = AddPatchItem(patch, membuf);
2189+
2190+
if (!membuf.Write((const char*)buffer, size))
2191+
{
2192+
retValue.Success = false;
2193+
return retValue;
2194+
}
2195+
freeBlock(buffer);
2196+
}
2197+
}
2198+
21072199
return retValue;
21082200
}
21092201

IGC/AdaptorOCL/ocl_igc_shared/executable_format/patch_list.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Abstract: Contains common patch structure definitions
3434
namespace iOpenCL
3535
{
3636

37-
const uint32_t CURRENT_ICBE_VERSION = 1057;
37+
const uint32_t CURRENT_ICBE_VERSION = 1058;
3838

3939
const uint32_t MAGIC_CL = 0x494E5443; // 'I', 'N', 'T', 'C'
4040
const uint32_t INVALID_INDEX = 0xFFFFFFFF;
@@ -147,13 +147,15 @@ enum PATCH_TOKEN
147147
PATCH_TOKEN_CONSTRUCTOR_DESTRUCTOR_KERNEL_PROGRAM_BINARY_INFO, // 49 - (Unused)
148148
PATCH_TOKEN_INLINE_VME_SAMPLER_INFO, // 50 - (Unused)
149149
PATCH_TOKEN_GTPIN_FREE_GRF_INFO, // 51 @SPatchGtpinFreeGRFInfo@
150-
PATCH_TOKEN_GTPIN_INFO,
150+
PATCH_TOKEN_GTPIN_INFO, // 52 @SPatchItemHeader@
151+
PATCH_TOKEN_FUNCTION_SYMBOL_TABLE, // 53 @SPatchFunctionTableInfo@
152+
PATCH_TOKEN_FUNCTION_RELOCATION_TABLE, // 54 @SPatchFunctionTableInfo@
151153

152154
NUM_PATCH_TOKENS
153155
};
154156

155157
// Update CURRENT_ICBE_VERSION when modifying the patch list
156-
static_assert( NUM_PATCH_TOKENS == 53, "NUM_PATCH_TOKENS has invalid value");
158+
static_assert( NUM_PATCH_TOKENS == 55, "NUM_PATCH_TOKENS has invalid value");
157159

158160
/*****************************************************************************\
159161
ENUM: IMAGE_MEMORY_OBJECT_TYPE

IGC/AdaptorOCL/ocl_igc_shared/executable_format/patch_shared.h

+12
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ struct SPatchGtpinFreeGRFInfo :
540540
// Update CURRENT_ICBE_VERSION when modifying the patch list
541541
static_assert(sizeof(SPatchGtpinFreeGRFInfo) == (4 + sizeof(SPatchItemHeader)), "The size of SPatchGtpinFreeGRFInfo is not what is expected");
542542

543+
/*****************************************************************************\
544+
STRUCT: SPatchFunctionTableInfo
545+
\*****************************************************************************/
546+
struct SPatchFunctionTableInfo :
547+
SPatchItemHeader
548+
{
549+
uint32_t NumEntries;
550+
};
551+
552+
// Update CURRENT_ICBE_VERSION when modifying the patch list
553+
static_assert(sizeof(SPatchFunctionTableInfo) == (4 + sizeof(SPatchItemHeader)), "The size of SPatchFunctionTableInfo is not what is expected");
554+
543555

544556
} // namespace
545557
#pragma pack( pop )

IGC/Compiler/CISACodeGen/CISABuilder.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3939
#include "common/shaderOverride.hpp"
4040
#include "common/CompilerStatsUtils.hpp"
4141
#include "inc/common/sku_wa.h"
42+
#include "inc/common/RelocationInfo.h"
4243
#include <iStdLib/utility.h>
4344

4445
#if !defined(_WIN32)
@@ -4214,6 +4215,75 @@ bool CEncoder::AvoidRetryOnSmallSpill() const
42144215
context->m_retryManager.IsFirstTry();
42154216
}
42164217

4218+
void CEncoder::CreateFunctionSymbolTable(void*& buffer, unsigned& bufferSize, unsigned& tableEntries)
4219+
{
4220+
buffer = nullptr;
4221+
bufferSize = 0;
4222+
tableEntries = 0;
4223+
4224+
if (IGC_IS_FLAG_ENABLED(EnableFunctionPointer))
4225+
{
4226+
Module* pModule = m_program->GetContext()->getModule();
4227+
std::vector<Function*> funcsToExport;
4228+
for (auto &F : pModule->getFunctionList())
4229+
{
4230+
// Find all functions in the module we need to export as symbols
4231+
if (F.hasFnAttribute("AsFunctionPointer"))
4232+
{
4233+
if (!F.isDeclaration() || F.getNumUses() > 0)
4234+
funcsToExport.push_back(&F);
4235+
}
4236+
}
4237+
4238+
if (funcsToExport.empty())
4239+
return;
4240+
4241+
// Allocate buffer to store symbol table entries
4242+
tableEntries = funcsToExport.size();
4243+
bufferSize = sizeof(IGC::GenSymEntry) * tableEntries;
4244+
buffer = (void*) malloc(bufferSize);
4245+
assert(buffer && "Function Symbol Table not allocated");
4246+
IGC::GenSymEntry* entry_ptr = (IGC::GenSymEntry*) buffer;
4247+
4248+
for (auto pFunc : funcsToExport)
4249+
{
4250+
assert(pFunc->getName().size() <= IGC::MAX_SYMBOL_NAME_LENGTH);
4251+
strcpy(entry_ptr->s_name, pFunc->getName().str().c_str());
4252+
4253+
if (pFunc->isDeclaration())
4254+
{
4255+
// If the function is only declared, set as undefined type
4256+
entry_ptr->s_type = IGC::GenSymType::S_UNDEF;
4257+
entry_ptr->s_offset = 0;
4258+
}
4259+
else
4260+
{
4261+
auto Iter = stackFuncMap.find(pFunc);
4262+
assert(Iter != stackFuncMap.end() && "vISA function not found");
4263+
4264+
// Query vISA for the function's byte offset within the compiled module
4265+
VISAFunction* visaFunc = Iter->second;
4266+
entry_ptr->s_type = IGC::GenSymType::S_FUNC;
4267+
entry_ptr->s_offset = (uint32_t) visaFunc->getGenOffset();
4268+
}
4269+
entry_ptr++;
4270+
}
4271+
}
4272+
}
4273+
void CEncoder::CreateFunctionRelocationTable(void*& buffer, unsigned& bufferSize, unsigned& tableEntries)
4274+
{
4275+
buffer = nullptr;
4276+
bufferSize = 0;
4277+
tableEntries = 0;
4278+
4279+
if (IGC_IS_FLAG_ENABLED(EnableFunctionPointer))
4280+
{
4281+
// vISA will directly return the buffer with GenRelocEntry layout
4282+
V(vMainKernel->GetGenRelocEntryBuffer(buffer, bufferSize, tableEntries));
4283+
assert(sizeof(IGC::GenRelocEntry) * tableEntries == bufferSize);
4284+
}
4285+
}
4286+
42174287
void CEncoder::Compile()
42184288
{
42194289
COMPILER_TIME_START(m_program->GetContext(), TIME_CG_vISAEmitPass);
@@ -4450,6 +4520,13 @@ void CEncoder::Compile()
44504520

44514521
vMainKernel->GetGTPinBuffer(pOutput->m_gtpinBuffer, pOutput->m_gtpinBufferSize);
44524522

4523+
CreateFunctionSymbolTable(pOutput->m_funcSymbolTable,
4524+
pOutput->m_funcSymbolTableSize,
4525+
pOutput->m_funcSymbolTableEntries);
4526+
CreateFunctionRelocationTable(pOutput->m_funcRelocationTable,
4527+
pOutput->m_funcRelocationTableSize,
4528+
pOutput->m_funcRelocationTableEntries);
4529+
44534530
if (jitInfo->isSpill == true)
44544531
{
44554532
pOutput->m_scratchSpaceUsedBySpills = jitInfo->spillMemUsed;

IGC/Compiler/CISACodeGen/CISABuilder.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ class CEncoder
489489
// save compile time by avoiding retry if the amount of spill is (very) small
490490
bool AvoidRetryOnSmallSpill() const;
491491

492+
void CreateFunctionSymbolTable(void*& buffer, unsigned& bufferSize, unsigned& tableEntries);
493+
void CreateFunctionRelocationTable(void*& buffer, unsigned& bufferSize, unsigned& tableEntries);
494+
492495
protected:
493496
// encoder states
494497
SEncoderState m_encoderState;

IGC/Compiler/CISACodeGen/CShader.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,8 @@ void CShader::CreateConstantBufferOutput(SKernelProgram *pKernelProgram)
533533

534534
void CShader::CreateFuncSymbolToRegisterMap(llvm::Function* pFunc)
535535
{
536+
// Functions with uses in this module requires relocation
536537
CVariable* funcAddr = GetSymbol(pFunc);
537-
//CVariable* funcAddr32bit = GetNewVariable(1, ISA_TYPE_UD, EALIGN_GRF, true);
538-
//encoder.Cast(funcAddr32bit, funcAddr);
539-
//encoder.Push();
540538
encoder.AddFunctionSymbol(pFunc, funcAddr);
541539
encoder.Push();
542540
}
@@ -2232,7 +2230,8 @@ CVariable* CShader::GetSymbol(llvm::Value *value, bool fromConstantPool)
22322230
if (Constant *C = llvm::dyn_cast<llvm::Constant>(value))
22332231
{
22342232
// Function Pointer
2235-
if (isa<GlobalValue>(value) &&
2233+
if (IGC_IS_FLAG_ENABLED(EnableFunctionPointer) &&
2234+
isa<GlobalValue>(value) &&
22362235
value->getType()->isPointerTy() &&
22372236
value->getType()->getPointerElementType()->isFunctionTy())
22382237
{

IGC/Compiler/CISACodeGen/EmitVISAPass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ bool EmitPass::runOnFunction(llvm::Function &F)
414414
// Creates a mapping of the function symbol to a register.
415415
// Any user function used by the kernel, including function declarations,
416416
// should have a register allocated to store it's physical address
417-
if (F.getNumUses() > 0 && F.hasFnAttribute("AsFunctionPointer"))
417+
if (F.hasFnAttribute("AsFunctionPointer") && F.getNumUses() > 0)
418418
{
419419
m_currShader->CreateFuncSymbolToRegisterMap(&F);
420420
}

IGC/Compiler/CodeGenPublic.h

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ namespace IGC
102102
unsigned int m_InstructionCount;
103103
void* m_gtpinBuffer; // Will be populated by VISA only when special switch is passed by gtpin
104104
unsigned int m_gtpinBufferSize;
105+
void* m_funcSymbolTable;
106+
unsigned int m_funcSymbolTableSize;
107+
unsigned int m_funcSymbolTableEntries;
108+
void* m_funcRelocationTable;
109+
unsigned int m_funcRelocationTableSize;
110+
unsigned int m_funcRelocationTableEntries;
105111

106112
void Destroy()
107113
{

0 commit comments

Comments
 (0)