Changeset 2534 in CLRX


Ignore:
Timestamp:
Nov 4, 2016, 8:26:00 PM (3 years ago)
Author:
matszpk
Message:

CLRadeonExtender: Add first code for ROCm binary generator.

Location:
CLRadeonExtender/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • CLRadeonExtender/trunk/CLRX/amdbin/ROCmBinaries.h

    r2533 r2534  
    3434#include <CLRX/utils/Containers.h>
    3535#include <CLRX/utils/Utilities.h>
     36#include <CLRX/utils/GPUId.h>
     37#include <CLRX/utils/InputOutput.h>
    3638
    3739/// main namespace
     
    139141extern bool isROCmBinary(size_t binarySize, const cxbyte* binary);
    140142
     143/*
     144 * ROCm Binary Generator
     145 */
     146
     147/// ROCm binary symbol input
     148struct ROCmSymbolInput
     149{
     150    CString symbolName; ///< symbol name
     151    size_t offset;  ///< offset in code
     152    size_t size;    ///< size of symbol
     153    bool isKernel;  ///< true if kernel
     154};
     155
     156struct ROCmInput
     157{
     158    GPUDeviceType deviceType;   ///< GPU device type
     159    uint32_t archMinor;         ///< GPU arch minor
     160    uint32_t archStepping;      ///< GPU arch stepping
     161    std::vector<ROCmSymbolInput> symbols;   ///< symbols
     162    size_t codeSize;        ///< code size
     163    const cxbyte* code;     ///< code
     164   
     165    std::vector<BinSection> extraSections;  ///< extra sections
     166    std::vector<BinSymbol> extraSymbols;    ///< extra symbols
     167};
     168
     169class ROCmBinGenerator: public NonCopyableAndNonMovable
     170{
     171private:
     172    private:
     173    bool manageable;
     174    const ROCmInput* input;
     175   
     176    void generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
     177             Array<cxbyte>* aPtr) const;
     178public:
     179    ROCmBinGenerator();
     180    /// constructor with ROCm input
     181    ROCmBinGenerator(const ROCmInput* rocmInput);
     182   
     183    ROCmBinGenerator(GPUDeviceType deviceType, uint32_t archMinor, uint32_t archStepping,
     184            size_t codeSize, const cxbyte* code,
     185            const std::vector<ROCmSymbolInput>& symbols);
     186    ROCmBinGenerator(GPUDeviceType deviceType, uint32_t archMinor, uint32_t archStepping,
     187            size_t codeSize, const cxbyte* code,
     188            std::vector<ROCmSymbolInput>&& symbols);
     189    ~ROCmBinGenerator();
     190   
     191    /// get input
     192    const ROCmInput* getInput() const
     193    { return input; }
     194   
     195    /// set input
     196    void setInput(const ROCmInput* input);
     197   
     198    /// generates binary to array of bytes
     199    void generate(Array<cxbyte>& array) const;
     200   
     201    /// generates binary to output stream
     202    void generate(std::ostream& os) const;
     203   
     204    /// generates binary to vector of char
     205    void generate(std::vector<char>& vector) const;
     206};
     207
    141208};
    142209
  • CLRadeonExtender/trunk/amdbin/ROCmBinaries.cpp

    r2533 r2534  
    2020#include <CLRX/Config.h>
    2121#include <cstdint>
     22#include <algorithm>
    2223#include <utility>
    23 #include <CLRX/amdbin/Elf.h>
     24#include <CLRX/amdbin/ElfBinaries.h>
    2425#include <CLRX/utils/Utilities.h>
    2526#include <CLRX/utils/MemAccess.h>
     27#include <CLRX/utils/InputOutput.h>
     28#include <CLRX/utils/Containers.h>
    2629#include <CLRX/amdbin/ROCmBinaries.h>
    2730
     
    132135    return true;
    133136}
     137
     138/*
     139 * ROCm Binary Generator
     140 */
     141
     142ROCmBinGenerator::ROCmBinGenerator() : manageable(false), input(nullptr)
     143{ }
     144
     145ROCmBinGenerator::ROCmBinGenerator(const ROCmInput* rocmInput)
     146        : manageable(false), input(rocmInput)
     147{ }
     148
     149ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType,
     150        uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code,
     151        const std::vector<ROCmSymbolInput>& symbols)
     152{
     153    input = new ROCmInput{ deviceType, archMinor, archStepping, symbols, codeSize, code };
     154}
     155
     156ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType,
     157        uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code,
     158        std::vector<ROCmSymbolInput>&& symbols)
     159{
     160    input = new ROCmInput{ deviceType, archMinor, archStepping, std::move(symbols),
     161                codeSize, code };
     162}
     163
     164ROCmBinGenerator::~ROCmBinGenerator()
     165{
     166    if (manageable)
     167        delete input;
     168}
     169
     170void ROCmBinGenerator::setInput(const ROCmInput* input)
     171{
     172    if (manageable)
     173        delete input;
     174    manageable = false;
     175    this->input = input;
     176}
     177
     178void ROCmBinGenerator::generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
     179             Array<cxbyte>* aPtr) const
     180{
     181    ElfBinaryGen64 elfBinGen64;
     182    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8,
     183                ".dynsym", SHT_DYNSYM, SHF_ALLOC, 3, 1));
     184   
     185    size_t binarySize;
     186    /****
     187     * prepare for write binary to output
     188     ****/
     189    std::unique_ptr<std::ostream> outStreamHolder;
     190    std::ostream* os = nullptr;
     191    if (aPtr != nullptr)
     192    {
     193        aPtr->resize(binarySize);
     194        outStreamHolder.reset(
     195                new ArrayOStream(binarySize, reinterpret_cast<char*>(aPtr->data())));
     196        os = outStreamHolder.get();
     197    }
     198    else if (vPtr != nullptr)
     199    {
     200        vPtr->resize(binarySize);
     201        outStreamHolder.reset(new VectorOStream(*vPtr));
     202        os = outStreamHolder.get();
     203    }
     204    else // from argument
     205        os = osPtr;
     206   
     207    const std::ios::iostate oldExceptions = os->exceptions();
     208    try
     209    {
     210    os->exceptions(std::ios::failbit | std::ios::badbit);
     211    /****
     212     * write binary to output
     213     ****/
     214    //elfBinGen64->generate(bos);
     215    //assert(bos.getWritten() == binarySize);
     216    }
     217    catch(...)
     218    {
     219        os->exceptions(oldExceptions);
     220        throw;
     221    }
     222    os->exceptions(oldExceptions);
     223}
     224
     225void ROCmBinGenerator::generate(Array<cxbyte>& array) const
     226{
     227    generateInternal(nullptr, nullptr, &array);
     228}
     229
     230void ROCmBinGenerator::generate(std::ostream& os) const
     231{
     232    generateInternal(&os, nullptr, nullptr);
     233}
     234
     235void ROCmBinGenerator::generate(std::vector<char>& v) const
     236{
     237    generateInternal(nullptr, &v, nullptr);
     238}
Note: See TracChangeset for help on using the changeset viewer.