Commit 041dbb5f authored by Diana Chen's avatar Diana Chen Committed by gbsbuild

vISA: Add APIs to get relocation table and get the function's binary offset

Change-Id: Ibe934600f9d28ad985bc0c9efceae9baa260523e
parent 8c4c4983
......@@ -5639,6 +5639,8 @@ void RelocationEntry::dump() const
inst->dump();
switch (relocType)
{
case RelocationType::R_NONE:
std::cerr << "R_NONE: symbol name = " << symName;
case RelocationType::R_SYM_ADDR:
std::cerr << "R_SYM_ADDR: symbol name = " << symName;
}
......
......@@ -41,6 +41,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "Gen4_IR.hpp"
#include "include/gtpin_IGC_interface.h"
#include "inc/common/RelocationInfo.h"
namespace vISA
{
......@@ -1187,15 +1188,11 @@ private:
gtpin::igc::igc_init_t* gtpin_init = nullptr;
};
enum class RelocationType
{
R_SYM_ADDR // patched value is the address of a symbol
};
class RelocationEntry
{
G4_INST* inst; // instruction to be relocated
int opndPos; // operand to be relocated. This should be a RelocImm
typedef IGC::GenRelocType RelocationType;
RelocationType relocType;
std::string symName; // the symbol name that it's address to be resolved
......@@ -1284,8 +1281,13 @@ class G4_Kernel
bool m_hasIndirectCall = false;
public:
typedef std::vector<RelocationEntry> RelocationTableTy;
private:
// stores all relocations to be performed after binary encoding
std::vector<RelocationEntry> relocationTable;
RelocationTableTy relocationTable;
// id -> function map for all functions (transitively) called by this kernel
// this differs from the "callees" in IR_Builder as the one in builder only contain
......@@ -1469,6 +1471,16 @@ public:
relocationTable.push_back(entry);
}
RelocationTableTy& getRelocationTable()
{
return relocationTable;
}
const RelocationTableTy& getRelocationTable() const
{
return relocationTable;
}
void doRelocation(void* binary, uint32_t binarySize);
void addCallee(uint32_t funcId, G4_Kernel* function) { allCallees.emplace(funcId, function); }
......
......@@ -665,6 +665,8 @@ public:
CM_BUILDER_API int GetErrorMessage(const char *&errorMsg);
CM_BUILDER_API virtual int GetGenxDebugInfo(void *&buffer, unsigned int &size, void*&, unsigned int&);
CM_BUILDER_API int GetGenReloc(BasicRelocEntry*& relocs, unsigned int& numRelocs);
/// GetGenRelocEntryBuffer -- allocate and return a buffer of all GenRelocEntry that are created by vISA
CM_BUILDER_API int GetGenRelocEntryBuffer(void *&buffer, unsigned int &byteSize, unsigned int &numEntries);
CM_BUILDER_API int GetGTPinBuffer(void*& buffer, unsigned int& size);
CM_BUILDER_API int SetGTPinInit(void* buffer);
CM_BUILDER_API int GetFreeGRFInfo(void*& buffer, unsigned int& size);
......@@ -695,6 +697,9 @@ public:
///Gets declaration id VISA_FileVar
CM_BUILDER_API int getDeclarationID(VISA_FileVar *decl);
///Gets gen binary offset
CM_BUILDER_API int64_t getGenOffset();
/********** MISC APIs END *************************/
int CreateVISAPredicateSrcOperand(VISA_VectorOpnd *& opnd, VISA_PredVar *decl, unsigned int size);
......
......@@ -8062,6 +8062,34 @@ int VISAKernelImpl::GetGenReloc(BasicRelocEntry*& relocs, unsigned int& numReloc
return CM_SUCCESS;
}
int VISAKernelImpl::GetGenRelocEntryBuffer(void *&buffer, unsigned int &byteSize, unsigned int &numEntries)
{
G4_Kernel::RelocationTableTy& reloc_table = m_kernel->getRelocationTable();
numEntries = reloc_table.size();
byteSize = sizeof(IGC::GenRelocEntry) * numEntries;
if (reloc_table.empty())
return CM_SUCCESS;
// allocate the buffer for relocation table
buffer = allocCodeBlock(byteSize);
if (buffer == NULL || buffer == nullptr)
return CM_FAILURE;
IGC::GenRelocEntry* buffer_p = (IGC::GenRelocEntry*)buffer;
for (auto reloc : reloc_table)
{
buffer_p->r_type = reloc.getType();
buffer_p->r_offset = (uint32_t)reloc.getInst()->getGenOffset();
assert(reloc.getSymbolName().size() <= IGC::MAX_SYMBOL_NAME_LENGTH);
std::strcpy(buffer_p->r_symbol, reloc.getSymbolName().c_str());
++buffer_p;
}
return CM_SUCCESS;
}
int VISAKernelImpl::GetGenxDebugInfo(void *&buffer, unsigned int &size, void*& mapGenISAOffsetToVISAIndex, unsigned int& mapNumElems)
{
unsigned int i = 0;
......@@ -8575,6 +8603,14 @@ int VISAKernelImpl::getDeclarationID(VISA_FileVar *decl)
return decl->index;
}
int64_t VISAKernelImpl::getGenOffset()
{
assert(m_kernel->fg.BBs.begin() != m_kernel->fg.BBs.end());
assert((*m_kernel->fg.BBs.begin())->begin() != (*m_kernel->fg.BBs.begin())->end());
// the offset of the first gen inst in this kernel/function
return (*(*m_kernel->fg.BBs.begin())->begin())->getGenOffset();
}
void VISAKernelImpl::computeAndEmitDebugInfo(std::list<VISAKernelImpl*>& functions)
{
std::list<VISAKernelImpl*> compilationUnitsForDebugInfo;
......
......@@ -766,6 +766,9 @@ public:
/// can patch required immediate offsets at indicated offsets.
CM_BUILDER_API virtual int GetGenReloc(BasicRelocEntry *&buffer, unsigned int &size) = 0;
/// GetGenRelocEntryBuffer -- allocate and return a buffer of all GenRelocEntry that are created by vISA
CM_BUILDER_API virtual int GetGenRelocEntryBuffer(void *&buffer, unsigned int &byteSize, unsigned int &numEntries) = 0;
/// SetGTPinInit -- pass igc_init_t struct instance
/// VISA decodes this struct and enables options accordingly
CM_BUILDER_API virtual int SetGTPinInit(void* buffer) = 0;
......@@ -820,6 +823,9 @@ public:
/// GetFunctionId -- Get function id for a stack call function VISAFunction instance.
/// This id is used by API client for invoking correct stack function using fcall.
CM_BUILDER_API virtual int GetFunctionId(unsigned int& id) = 0;
/// getGenOffset -- Get gen binary offset of this function
CM_BUILDER_API virtual int64_t getGenOffset() = 0;
};
typedef enum
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment