Skip to content

Commit 041dbb5

Browse files
DianaChengfxbot
authored andcommitted
vISA: Add APIs to get relocation table and get the function's binary offset
Change-Id: Ibe934600f9d28ad985bc0c9efceae9baa260523e
1 parent 8c4c498 commit 041dbb5

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

visa/FlowGraph.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5639,6 +5639,8 @@ void RelocationEntry::dump() const
56395639
inst->dump();
56405640
switch (relocType)
56415641
{
5642+
case RelocationType::R_NONE:
5643+
std::cerr << "R_NONE: symbol name = " << symName;
56425644
case RelocationType::R_SYM_ADDR:
56435645
std::cerr << "R_SYM_ADDR: symbol name = " << symName;
56445646
}

visa/FlowGraph.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4141
#include "Gen4_IR.hpp"
4242

4343
#include "include/gtpin_IGC_interface.h"
44+
#include "inc/common/RelocationInfo.h"
4445

4546
namespace vISA
4647
{
@@ -1187,15 +1188,11 @@ class gtPinData
11871188
gtpin::igc::igc_init_t* gtpin_init = nullptr;
11881189
};
11891190

1190-
enum class RelocationType
1191-
{
1192-
R_SYM_ADDR // patched value is the address of a symbol
1193-
};
1194-
11951191
class RelocationEntry
11961192
{
11971193
G4_INST* inst; // instruction to be relocated
11981194
int opndPos; // operand to be relocated. This should be a RelocImm
1195+
typedef IGC::GenRelocType RelocationType;
11991196
RelocationType relocType;
12001197
std::string symName; // the symbol name that it's address to be resolved
12011198

@@ -1284,8 +1281,13 @@ class G4_Kernel
12841281

12851282
bool m_hasIndirectCall = false;
12861283

1284+
public:
1285+
typedef std::vector<RelocationEntry> RelocationTableTy;
1286+
1287+
private:
12871288
// stores all relocations to be performed after binary encoding
1288-
std::vector<RelocationEntry> relocationTable;
1289+
RelocationTableTy relocationTable;
1290+
12891291

12901292
// id -> function map for all functions (transitively) called by this kernel
12911293
// this differs from the "callees" in IR_Builder as the one in builder only contain
@@ -1469,6 +1471,16 @@ class G4_Kernel
14691471
relocationTable.push_back(entry);
14701472
}
14711473

1474+
RelocationTableTy& getRelocationTable()
1475+
{
1476+
return relocationTable;
1477+
}
1478+
1479+
const RelocationTableTy& getRelocationTable() const
1480+
{
1481+
return relocationTable;
1482+
}
1483+
14721484
void doRelocation(void* binary, uint32_t binarySize);
14731485

14741486
void addCallee(uint32_t funcId, G4_Kernel* function) { allCallees.emplace(funcId, function); }

visa/VISAKernel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ class VISAKernelImpl : public VISAFunction
665665
CM_BUILDER_API int GetErrorMessage(const char *&errorMsg);
666666
CM_BUILDER_API virtual int GetGenxDebugInfo(void *&buffer, unsigned int &size, void*&, unsigned int&);
667667
CM_BUILDER_API int GetGenReloc(BasicRelocEntry*& relocs, unsigned int& numRelocs);
668+
/// GetGenRelocEntryBuffer -- allocate and return a buffer of all GenRelocEntry that are created by vISA
669+
CM_BUILDER_API int GetGenRelocEntryBuffer(void *&buffer, unsigned int &byteSize, unsigned int &numEntries);
668670
CM_BUILDER_API int GetGTPinBuffer(void*& buffer, unsigned int& size);
669671
CM_BUILDER_API int SetGTPinInit(void* buffer);
670672
CM_BUILDER_API int GetFreeGRFInfo(void*& buffer, unsigned int& size);
@@ -695,6 +697,9 @@ class VISAKernelImpl : public VISAFunction
695697
///Gets declaration id VISA_FileVar
696698
CM_BUILDER_API int getDeclarationID(VISA_FileVar *decl);
697699

700+
///Gets gen binary offset
701+
CM_BUILDER_API int64_t getGenOffset();
702+
698703
/********** MISC APIs END *************************/
699704
int CreateVISAPredicateSrcOperand(VISA_VectorOpnd *& opnd, VISA_PredVar *decl, unsigned int size);
700705

visa/VISAKernelImpl.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8062,6 +8062,34 @@ int VISAKernelImpl::GetGenReloc(BasicRelocEntry*& relocs, unsigned int& numReloc
80628062
return CM_SUCCESS;
80638063
}
80648064

8065+
int VISAKernelImpl::GetGenRelocEntryBuffer(void *&buffer, unsigned int &byteSize, unsigned int &numEntries)
8066+
{
8067+
G4_Kernel::RelocationTableTy& reloc_table = m_kernel->getRelocationTable();
8068+
numEntries = reloc_table.size();
8069+
byteSize = sizeof(IGC::GenRelocEntry) * numEntries;
8070+
8071+
if (reloc_table.empty())
8072+
return CM_SUCCESS;
8073+
8074+
// allocate the buffer for relocation table
8075+
buffer = allocCodeBlock(byteSize);
8076+
8077+
if (buffer == NULL || buffer == nullptr)
8078+
return CM_FAILURE;
8079+
8080+
IGC::GenRelocEntry* buffer_p = (IGC::GenRelocEntry*)buffer;
8081+
for (auto reloc : reloc_table)
8082+
{
8083+
buffer_p->r_type = reloc.getType();
8084+
buffer_p->r_offset = (uint32_t)reloc.getInst()->getGenOffset();
8085+
assert(reloc.getSymbolName().size() <= IGC::MAX_SYMBOL_NAME_LENGTH);
8086+
std::strcpy(buffer_p->r_symbol, reloc.getSymbolName().c_str());
8087+
++buffer_p;
8088+
}
8089+
8090+
return CM_SUCCESS;
8091+
}
8092+
80658093
int VISAKernelImpl::GetGenxDebugInfo(void *&buffer, unsigned int &size, void*& mapGenISAOffsetToVISAIndex, unsigned int& mapNumElems)
80668094
{
80678095
unsigned int i = 0;
@@ -8575,6 +8603,14 @@ int VISAKernelImpl::getDeclarationID(VISA_FileVar *decl)
85758603
return decl->index;
85768604
}
85778605

8606+
int64_t VISAKernelImpl::getGenOffset()
8607+
{
8608+
assert(m_kernel->fg.BBs.begin() != m_kernel->fg.BBs.end());
8609+
assert((*m_kernel->fg.BBs.begin())->begin() != (*m_kernel->fg.BBs.begin())->end());
8610+
// the offset of the first gen inst in this kernel/function
8611+
return (*(*m_kernel->fg.BBs.begin())->begin())->getGenOffset();
8612+
}
8613+
85788614
void VISAKernelImpl::computeAndEmitDebugInfo(std::list<VISAKernelImpl*>& functions)
85798615
{
85808616
std::list<VISAKernelImpl*> compilationUnitsForDebugInfo;

visa/include/VISABuilderAPIDefinition.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,9 @@ class VISAKernel
766766
/// can patch required immediate offsets at indicated offsets.
767767
CM_BUILDER_API virtual int GetGenReloc(BasicRelocEntry *&buffer, unsigned int &size) = 0;
768768

769+
/// GetGenRelocEntryBuffer -- allocate and return a buffer of all GenRelocEntry that are created by vISA
770+
CM_BUILDER_API virtual int GetGenRelocEntryBuffer(void *&buffer, unsigned int &byteSize, unsigned int &numEntries) = 0;
771+
769772
/// SetGTPinInit -- pass igc_init_t struct instance
770773
/// VISA decodes this struct and enables options accordingly
771774
CM_BUILDER_API virtual int SetGTPinInit(void* buffer) = 0;
@@ -820,6 +823,9 @@ class VISAFunction : public VISAKernel
820823
/// GetFunctionId -- Get function id for a stack call function VISAFunction instance.
821824
/// This id is used by API client for invoking correct stack function using fcall.
822825
CM_BUILDER_API virtual int GetFunctionId(unsigned int& id) = 0;
826+
827+
/// getGenOffset -- Get gen binary offset of this function
828+
CM_BUILDER_API virtual int64_t getGenOffset() = 0;
823829
};
824830

825831
typedef enum

0 commit comments

Comments
 (0)