Changeset 3666 in CLRX


Ignore:
Timestamp:
Jan 27, 2018, 1:27:51 PM (2 years ago)
Author:
matszpk
Message:

CLRadeonExtender: DisasmROCm: print globalData (rodata) and newbinfmt (if new binary format).
ROCmBinGen: rewrite the creation of mainBuiltinSectionTable: create dynamically. Set eflags by default (if not given). Add globalData (if supplied) to binary.

Location:
CLRadeonExtender/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • CLRadeonExtender/trunk/CLRX/amdasm/Disassembler.h

    r3665 r3666  
    321321    size_t codeSize;    ///< code size
    322322    const cxbyte* code; ///< code
     323    size_t globalDataSize;    ///< global data size
     324    const cxbyte* globalData; ///< global data
    323325    CString target;     ///< LLVM target triple
    324326    size_t metadataSize;    ///< metadata size
  • CLRadeonExtender/trunk/CLRX/amdbin/ROCmBinaries.h

    r3665 r3666  
    7979    size_t codeSize;
    8080    cxbyte* code;
     81    size_t globalDataSize;
     82    cxbyte* globalData;
    8183    CString target;
    8284    size_t metadataSize;
    8385    char* metadata;
     86    bool newBinFormat;
    8487public:
    8588    /// constructor
     
    110113    const cxbyte* getCode() const
    111114    { return code; }
     115    /// get code
     116    cxbyte* getCode()
     117    { return code; }
     118   
     119    /// get global data size
     120    size_t getGlobalDataSize() const
     121    { return globalDataSize; }
     122   
     123    /// get global data
     124    const cxbyte* getGlobalData() const
     125    { return globalData; }
     126    /// get global data
     127    cxbyte* getGlobalData()
     128    { return globalData; }
    112129   
    113130    /// get metadata size
     
    117134    const char* getMetadata() const
    118135    { return metadata; }
     136    /// get metadata
     137    char* getMetadata()
     138    { return metadata; }
    119139   
    120140    const CString& getTarget() const
    121141    { return target; }
     142   
     143    /// return true is new binary format
     144    bool isNewBinaryFormat() const
     145    { return newBinFormat; }
    122146   
    123147    /// returns true if kernel map exists
     
    182206    uint32_t eflags;    ///< ELF headef e_flags field
    183207    bool newBinFormat;       ///< use new binary format for ROCm
     208    size_t globalDataSize;  ///< global data size
     209    const cxbyte* globalData;   ///< global data
    184210    std::vector<ROCmSymbolInput> symbols;   ///< symbols
    185211    size_t codeSize;        ///< code size
     
    220246     * \param codeSize size of code
    221247     * \param code code pointer
     248     * \param globalDataSize size of global data
     249     * \param globalData global data pointer
    222250     * \param symbols symbols (kernels, datas,...)
    223251     */
    224252    ROCmBinGenerator(GPUDeviceType deviceType, uint32_t archMinor, uint32_t archStepping,
    225253            size_t codeSize, const cxbyte* code,
     254            size_t globalDataSize, const cxbyte* globalData,
    226255            const std::vector<ROCmSymbolInput>& symbols);
    227256    /// constructor
    228257    ROCmBinGenerator(GPUDeviceType deviceType, uint32_t archMinor, uint32_t archStepping,
    229258            size_t codeSize, const cxbyte* code,
     259            size_t globalDataSize, const cxbyte* globalData,
    230260            std::vector<ROCmSymbolInput>&& symbols);
    231261    /// destructor
  • CLRadeonExtender/trunk/amdasm/AsmROCmFormat.cpp

    r3665 r3666  
    105105{
    106106    output.archMinor = output.archStepping = UINT32_MAX;
     107    output.eflags = BINGEN_DEFAULT;
    107108    assembler.currentKernel = ASMKERN_GLOBAL;
    108109    assembler.currentSection = 0;
  • CLRadeonExtender/trunk/amdasm/DisasmROCm.cpp

    r3665 r3666  
    5858    input->metadata = binary.getMetadata();
    5959    input->metadataSize = binary.getMetadataSize();
     60    input->globalData = binary.getGlobalData();
     61    input->globalDataSize = binary.getGlobalDataSize();
    6062    input->target = binary.getTarget();
     63    input->newBinFormat = binary.isNewBinaryFormat();
    6164    return input.release();
    6265}
     
    516519    const bool doMetadata = ((flags & (DISASM_METADATA|DISASM_CONFIG)) != 0);
    517520    const bool doDumpConfig = ((flags & DISASM_CONFIG) != 0);
     521    const bool doDumpData = ((flags & DISASM_DUMPDATA) != 0);
    518522   
    519523    const GPUArchitecture arch = getGPUArchitectureFromDeviceType(rocmInput->deviceType);
     
    527531        output.write(buf, size);
    528532    }
     533   
     534    if (rocmInput->newBinFormat)
     535        output.write(".newbinfmt\n", 11);
     536   
    529537    if (!rocmInput->target.empty())
    530538    {
     
    533541        output.write(escapedTarget.c_str(), escapedTarget.size());
    534542        output.write("\"\n", 2);
     543    }
     544   
     545    if (doDumpData && rocmInput->globalData != nullptr &&
     546        rocmInput->globalDataSize != 0)
     547    {
     548        output.write(".globaldata\n", 12);
     549        output.write(".gdata:\n", 8); /// symbol used by text relocations
     550        printDisasmData(rocmInput->globalDataSize, rocmInput->globalData, output);
    535551    }
    536552   
  • CLRadeonExtender/trunk/amdbin/ROCmBinaries.cpp

    r3665 r3666  
    3636ROCmBinary::ROCmBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags)
    3737        : ElfBinary64(binaryCodeSize, binaryCode, creationFlags),
    38           regionsNum(0), codeSize(0), code(nullptr), metadataSize(0), metadata(nullptr)
     38          regionsNum(0), codeSize(0), code(nullptr),
     39          globalDataSize(0), globalData(nullptr), metadataSize(0), metadata(nullptr),
     40          newBinFormat(false)
    3941{
    4042    cxuint textIndex = SHN_UNDEF;
     
    5254        codeOffset = ULEV(textShdr.sh_offset);
    5355    }
     56   
     57    cxuint rodataIndex = SHN_UNDEF;
     58    try
     59    { rodataIndex = getSectionIndex(".rodata"); }
     60    catch(const Exception& ex)
     61    { } // ignore failed
     62    // find '.text' section
     63    if (rodataIndex!=SHN_UNDEF)
     64    {
     65        globalData = getSectionContent(rodataIndex);
     66        const Elf64_Shdr& rodataShdr = getSectionHeader(rodataIndex);
     67        globalDataSize = ULEV(rodataShdr.sh_size);
     68    }
     69   
     70    cxuint gpuConfigIndex = SHN_UNDEF;
     71    try
     72    { gpuConfigIndex = getSectionIndex(".AMDGPU.config"); }
     73    catch(const Exception& ex)
     74    { } // ignore failed
     75    newBinFormat = (gpuConfigIndex == SHN_UNDEF);
    5476   
    5577    // counts regions (symbol or kernel)
     
    230252    symbols.push_back({ kernelName, 0, 0, ROCmRegionType::KERNEL });
    231253}
     254
    232255/*
    233256 * ROCm Binary Generator
     
    243266ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType,
    244267        uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code,
     268        size_t globalDataSize, const cxbyte* globalData,
    245269        const std::vector<ROCmSymbolInput>& symbols)
    246270{
    247271    input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false,
    248             symbols, codeSize, code };
     272            globalDataSize, globalData, symbols, codeSize, code };
    249273}
    250274
    251275ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType,
    252276        uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code,
     277        size_t globalDataSize, const cxbyte* globalData,
    253278        std::vector<ROCmSymbolInput>&& symbols)
    254279{
    255280    input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false,
    256             std::move(symbols), codeSize, code };
     281            globalDataSize, globalData, std::move(symbols), codeSize, code };
    257282}
    258283
     
    279304  'A', 'M', 'D', 0, 'A', 'M', 'D', 'G', 'P', 'U', 0 };
    280305
    281 // section index for symbol binding
    282 static const uint16_t mainBuiltinSectionTable[] =
    283 {
    284     10, // ELFSECTID_SHSTRTAB
    285     11, // ELFSECTID_STRTAB
    286     9, // ELFSECTID_SYMTAB
    287     3, // ELFSECTID_DYNSTR
    288     1, // ELFSECTID_DYNSYM
    289     4, // ELFSECTID_TEXT
    290     SHN_UNDEF, // ELFSECTID_RODATA
    291     SHN_UNDEF, // ELFSECTID_DATA
    292     SHN_UNDEF, // ELFSECTID_BSS
    293     8, // ELFSECTID_COMMENT
    294     2, // ROCMSECTID_HASH
    295     5, // ROCMSECTID_DYNAMIC
    296     6, // ROCMSECTID_NOTE
    297     7 // ROCMSECTID_GPUCONFIG
    298 };
     306static inline void addMainSectionToTable(cxuint& sectionsNum, uint16_t* builtinTable,
     307                cxuint elfSectId)
     308{ builtinTable[elfSectId - ELFSECTID_START] = sectionsNum++; }
    299309
    300310void ROCmBinGenerator::generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
     
    319329    }
    320330   
     331    uint32_t eflags = input->newBinFormat ? 2 : 0;
     332    if (input->eflags != BINGEN_DEFAULT)
     333        eflags = input->eflags;
     334   
    321335    ElfBinaryGen64 elfBinGen64({ 0U, 0U, 0x40, 0, ET_DYN,
    322             0xe0, EV_CURRENT, UINT_MAX, 0, input->eflags },
     336            0xe0, EV_CURRENT, UINT_MAX, 0, eflags },
    323337            true, true, true, PHREGION_FILESTART);
    324338    // add symbols (kernels, function kernels and data symbols)
     
    356370        DT_SYMTAB, DT_SYMENT, DT_STRTAB, DT_STRSZ, DT_HASH };
    357371    elfBinGen64.addDynamics(sizeof(dynTags)/sizeof(int32_t), dynTags);
     372   
     373    uint16_t mainBuiltinSectTable[ROCMSECTID_MAX-ELFSECTID_START+1];
     374    std::fill(mainBuiltinSectTable,
     375              mainBuiltinSectTable + ROCMSECTID_MAX-ELFSECTID_START+1, SHN_UNDEF);
     376    cxuint mainSectionsNum = 1;
     377   
     378    // generate main builtin section table (for section id translation)
     379    if (input->newBinFormat)
     380        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE);
     381    if (input->globalData != nullptr)
     382        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_RODATA);
     383    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSYM);
     384    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_HASH);
     385    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSTR);
     386    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_TEXT);
     387    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_DYNAMIC);
     388    if (!input->newBinFormat)
     389    {
     390        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE);
     391        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_GPUCONFIG);
     392    }
     393    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_COMMENT);
     394    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SYMTAB);
     395    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SHSTRTAB);
     396    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_STRTAB);
     397   
    358398    // elf program headers
    359399    elfBinGen64.addProgramHeader({ PT_PHDR, PF_R, 0, 1,
     
    389429    /// region and sections
    390430    elfBinGen64.addRegion(ElfRegion64::programHeaderTable());
     431    if (input->newBinFormat)
     432        elfBinGen64.addRegion(ElfRegion64::noteSection());
     433    if (input->globalData != nullptr)
     434        elfBinGen64.addRegion(ElfRegion64(input->globalDataSize, input->globalData, 4,
     435                ".rodata", SHT_PROGBITS, SHF_ALLOC, 0, 0, Elf64Types::nobase));
     436   
    391437    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8,
    392438                ".dynsym", SHT_DYNSYM, SHF_ALLOC, 0, 1, Elf64Types::nobase));
    393439    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 4,
    394                 ".hash", SHT_HASH, SHF_ALLOC, 1, 0, Elf64Types::nobase));
     440                ".hash", SHT_HASH, SHF_ALLOC,
     441                mainBuiltinSectTable[ELFSECTID_DYNSYM-ELFSECTID_START], 0,
     442                Elf64Types::nobase));
    395443    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB,
    396444                SHF_ALLOC, 0, 0, Elf64Types::nobase));
     
    400448              Elf64Types::nobase, 0, false, 256));
    401449    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 0x1000,
    402                 ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE, 3, 0,
     450                ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE,
     451                mainBuiltinSectTable[ELFSECTID_DYNSTR-ELFSECTID_START], 0,
    403452                Elf64Types::nobase, 0, false, 8));
    404     elfBinGen64.addRegion(ElfRegion64::noteSection());
    405     elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1,
    406                 ".AMDGPU.config", SHT_PROGBITS, 0));
     453    if (!input->newBinFormat)
     454    {
     455        elfBinGen64.addRegion(ElfRegion64::noteSection());
     456        elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1,
     457                    ".AMDGPU.config", SHT_PROGBITS, 0));
     458    }
    407459    elfBinGen64.addRegion(ElfRegion64(commentSize, (const cxbyte*)comment, 1, ".comment",
    408460              SHT_PROGBITS, SHF_MERGE|SHF_STRINGS, 0, 0, 0, 1));
     
    415467    /* extra sections */
    416468    for (const BinSection& section: input->extraSections)
    417         elfBinGen64.addRegion(ElfRegion64(section, mainBuiltinSectionTable,
    418                          ROCMSECTID_MAX, 12));
     469        elfBinGen64.addRegion(ElfRegion64(section, mainBuiltinSectTable,
     470                         ROCMSECTID_MAX, mainSectionsNum));
    419471    /* extra symbols */
    420472    for (const BinSymbol& symbol: input->extraSymbols)
    421         elfBinGen64.addSymbol(ElfSymbol64(symbol, mainBuiltinSectionTable,
    422                          ROCMSECTID_MAX, 12));
     473        elfBinGen64.addSymbol(ElfSymbol64(symbol, mainBuiltinSectTable,
     474                         ROCMSECTID_MAX, mainSectionsNum));
    423475   
    424476    size_t binarySize = elfBinGen64.countSize();
  • CLRadeonExtender/trunk/tests/amdasm/AsmROCmFormat.cpp

    r3662 r3666  
    126126    os << "  Code:\n";
    127127    printHexData(os, 1, output->codeSize, output->code);
    128     if (output->eflags != 0)
     128    if (output->eflags != BINGEN_DEFAULT)
    129129        os << "  EFlags=" << output->eflags << std::endl;
    130130   
  • CLRadeonExtender/trunk/tests/amdbin/ROCmBinGen.cpp

    r3665 r3666  
    5252    rocmInput.archMinor = 0;
    5353    rocmInput.archStepping = 0;
    54     rocmInput.metadataSize = 0;
    55     rocmInput.metadata = nullptr;
     54    rocmInput.metadataSize = binary.getMetadataSize();
     55    rocmInput.metadata = binary.getMetadata();
     56    rocmInput.newBinFormat = binary.isNewBinaryFormat();
     57    rocmInput.globalDataSize = binary.getGlobalDataSize();
     58    rocmInput.globalData = binary.getGlobalData();
    5659   
    5760    {
Note: See TracChangeset for help on using the changeset viewer.