Changeset 3665 in CLRX


Ignore:
Timestamp:
Jan 26, 2018, 11:09:35 PM (3 years ago)
Author:
matszpk
Message:

CLRadeonExtender: ROCm: Add tentatively target and metadata to the ROCm format.

Location:
CLRadeonExtender/trunk
Files:
8 edited

Legend:

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

    r3575 r3665  
    7070    ROCM_COMMENT = LAST_COMMON+1,        ///< ROCm comment section
    7171    ROCM_CONFIG_CTRL_DIRECTIVE,
     72    ROCM_METADATA,
    7273   
    7374    EXTRA_FIRST = 0xfc,
     
    508509    cxuint codeSection;
    509510    cxuint commentSection;
     511    cxuint metadataSection;
    510512    cxuint savedSection;
    511513    cxuint extraSectionCount;
  • CLRadeonExtender/trunk/CLRX/amdasm/Disassembler.h

    r3662 r3665  
    316316    uint32_t archMinor;     ///< GPU arch minor
    317317    uint32_t archStepping;     ///< GPU arch stepping
     318    uint32_t eflags;   ///< ELF header e_flags field
     319    bool newBinFormat;  ///< new binary format
    318320    std::vector<ROCmDisasmRegionInput> regions;  ///< regions
    319321    size_t codeSize;    ///< code size
    320322    const cxbyte* code; ///< code
    321     uint32_t eflags;   ///< ELF header e_flags field
     323    CString target;     ///< LLVM target triple
     324    size_t metadataSize;    ///< metadata size
     325    const char* metadata;   ///< metadata
    322326};
    323327
  • CLRadeonExtender/trunk/CLRX/amdbin/ROCmBinaries.h

    r3662 r3665  
    7979    size_t codeSize;
    8080    cxbyte* code;
     81    CString target;
     82    size_t metadataSize;
     83    char* metadata;
    8184public:
    8285    /// constructor
     
    107110    const cxbyte* getCode() const
    108111    { return code; }
     112   
     113    /// get metadata size
     114    size_t getMetadataSize() const
     115    { return metadataSize; }
     116    /// get metadata
     117    const char* getMetadata() const
     118    { return metadata; }
     119   
     120    const CString& getTarget() const
     121    { return target; }
    109122   
    110123    /// returns true if kernel map exists
     
    167180    uint32_t archMinor;         ///< GPU arch minor
    168181    uint32_t archStepping;      ///< GPU arch stepping
     182    uint32_t eflags;    ///< ELF headef e_flags field
     183    bool newBinFormat;       ///< use new binary format for ROCm
    169184    std::vector<ROCmSymbolInput> symbols;   ///< symbols
    170185    size_t codeSize;        ///< code size
     
    172187    size_t commentSize; ///< comment size (can be null)
    173188    const char* comment; ///< comment
     189    CString target;     ///< LLVM target triple
     190    size_t metadataSize;    ///< metadata size
     191    const char* metadata;   ///< metadata
    174192    std::vector<BinSection> extraSections;  ///< extra sections
    175193    std::vector<BinSymbol> extraSymbols;    ///< extra symbols
    176     uint32_t eflags;    ///< ELF headef e_flags field
    177194   
    178195    /// add empty kernel with default values
  • CLRadeonExtender/trunk/amdasm/AsmROCmFormat.cpp

    r3662 r3665  
    4747    "kernel_code_prefetch_offset", "kernel_code_prefetch_size",
    4848    "localsize", "machine", "max_scratch_backing_memory",
    49     "pgmrsrc1", "pgmrsrc2", "priority",
     49    "metadata", "pgmrsrc1", "pgmrsrc2", "priority",
    5050    "private_elem_size", "private_segment_align",
    5151    "privmode", "reserved_sgprs", "reserved_vgprs",
    5252    "runtime_loader_kernel_symbol",
    53     "scratchbuffer", "sgprsnum", "tgsize",
     53    "scratchbuffer", "sgprsnum", "target", "tgsize",
    5454    "use_debug_enabled", "use_dispatch_id",
    5555    "use_dispatch_ptr", "use_dynamic_call_stack",
     
    7979    ROCMOP_KERNEL_CODE_PREFETCH_OFFSET, ROCMOP_KERNEL_CODE_PREFETCH_SIZE,
    8080    ROCMOP_LOCALSIZE, ROCMOP_MACHINE, ROCMOP_MAX_SCRATCH_BACKING_MEMORY,
    81     ROCMOP_PGMRSRC1, ROCMOP_PGMRSRC2, ROCMOP_PRIORITY,
     81    ROCMOP_METADATA, ROCMOP_PGMRSRC1, ROCMOP_PGMRSRC2, ROCMOP_PRIORITY,
    8282    ROCMOP_PRIVATE_ELEM_SIZE, ROCMOP_PRIVATE_SEGMENT_ALIGN,
    8383    ROCMOP_PRIVMODE, ROCMOP_RESERVED_SGPRS, ROCMOP_RESERVED_VGPRS,
    8484    ROCMOP_RUNTIME_LOADER_KERNEL_SYMBOL,
    85     ROCMOP_SCRATCHBUFFER, ROCMOP_SGPRSNUM, ROCMOP_TGSIZE,
     85    ROCMOP_SCRATCHBUFFER, ROCMOP_SGPRSNUM, ROCMOP_TARGET, ROCMOP_TGSIZE,
    8686    ROCMOP_USE_DEBUG_ENABLED, ROCMOP_USE_DISPATCH_ID,
    8787    ROCMOP_USE_DISPATCH_PTR, ROCMOP_USE_DYNAMIC_CALL_STACK,
     
    102102AsmROCmHandler::AsmROCmHandler(Assembler& assembler): AsmFormatHandler(assembler),
    103103             output{}, codeSection(0), commentSection(ASMSECT_NONE),
    104              extraSectionCount(0)
     104             metadataSection(ASMSECT_NONE), extraSectionCount(0)
    105105{
    106106    output.archMinor = output.archStepping = UINT32_MAX;
     
    368368    handler.output.eflags = value;
    369369}
     370
     371void AsmROCmPseudoOps::setTarget(AsmROCmHandler& handler, const char* linePtr)
     372{
     373    Assembler& asmr = handler.assembler;
     374    const char* end = asmr.line + asmr.lineSize;
     375    skipSpacesToEnd(linePtr, end);
     376    std::string out;
     377    if (!asmr.parseString(out, linePtr))
     378        return;
     379    if (!checkGarbagesAtEnd(asmr, linePtr))
     380        return;
     381    handler.output.target = out;
     382}
    370383   
    371384void AsmROCmPseudoOps::doConfig(AsmROCmHandler& handler, const char* pseudoOpPlace,
     
    405418    asmr.goToSection(pseudoOpPlace, kernel.ctrlDirSection);
    406419    handler.kernelStates[asmr.currentKernel]->initializeKernelConfig();
     420}
     421
     422void AsmROCmPseudoOps::addMetadata(AsmROCmHandler& handler, const char* pseudoOpPlace,
     423                      const char* linePtr)
     424{
     425    Assembler& asmr = handler.assembler;
     426   
     427    if (!checkGarbagesAtEnd(asmr, linePtr))
     428        return;
     429   
     430    cxuint& metadataSection = handler.metadataSection;
     431    if (metadataSection == ASMSECT_NONE)
     432    {
     433        /* add this section */
     434        cxuint thisSection = handler.sections.size();
     435        handler.sections.push_back({ ASMKERN_GLOBAL, AsmSectionType::ROCM_METADATA,
     436            ELFSECTID_UNDEF, nullptr });
     437        metadataSection = thisSection;
     438    }
     439    asmr.goToSection(pseudoOpPlace, metadataSection);
    407440}
    408441
     
    12241257                             ROCMCVAL_MAX_SCRATCH_BACKING_MEMORY);
    12251258            break;
     1259        case ROCMOP_METADATA:
     1260            AsmROCmPseudoOps::addMetadata(*this, stmtPlace, linePtr);
     1261            break;
    12261262        case ROCMOP_PGMRSRC1:
    12271263            AsmROCmPseudoOps::setConfigValue(*this, stmtPlace, linePtr, ROCMCVAL_PGMRSRC1);
     
    12621298            AsmROCmPseudoOps::setConfigValue(*this, stmtPlace, linePtr,
    12631299                             ROCMCVAL_SGPRSNUM);
     1300            break;
     1301        case ROCMOP_TARGET:
     1302            AsmROCmPseudoOps::setTarget(*this, linePtr);
    12641303            break;
    12651304        case ROCMOP_TGSIZE:
     
    14201459                output.commentSize = sectionSize;
    14211460                output.comment = (const char*)sectionData;
     1461                break;
     1462            case AsmSectionType::ROCM_METADATA:
     1463                output.metadataSize = sectionSize;
     1464                output.metadata = (const char*)sectionData;
    14221465                break;
    14231466            default:
  • CLRadeonExtender/trunk/amdasm/AsmROCmInternals.h

    r3662 r3665  
    9292    // .arch_stepping
    9393    static void setArchStepping(AsmROCmHandler& handler, const char* linePtr);
    94     // set elf elfags
     94    // set elf eflags
    9595    static void setEFlags(AsmROCmHandler& handler, const char* linePtr);
     96   
     97    // .target
     98    static void setTarget(AsmROCmHandler& handler, const char* linePtr);
    9699   
    97100    /* user configuration pseudo-ops */
    98101    static void doConfig(AsmROCmHandler& handler, const char* pseudoOpPlace,
    99102                      const char* linePtr);
    100    
    101103    // .control_directive (open this seciton)
    102104    static void doControlDirective(AsmROCmHandler& handler, const char* pseudoOpPlace,
     105                      const char* linePtr);
     106    // .metadata
     107    static void addMetadata(AsmROCmHandler& handler, const char* pseudoOpPlace,
    103108                      const char* linePtr);
    104109   
  • CLRadeonExtender/trunk/amdasm/DisasmROCm.cpp

    r3662 r3665  
    5656    input->code = binary.getCode();
    5757    input->codeSize = binary.getCodeSize();
     58    input->metadata = binary.getMetadata();
     59    input->metadataSize = binary.getMetadataSize();
     60    input->target = binary.getTarget();
    5861    return input.release();
    5962}
     
    524527        output.write(buf, size);
    525528    }
     529    if (!rocmInput->target.empty())
     530    {
     531        output.write(".target \"", 9);
     532        const std::string escapedTarget = escapeStringCStyle(rocmInput->target);
     533        output.write(escapedTarget.c_str(), escapedTarget.size());
     534        output.write("\"\n", 2);
     535    }
     536   
     537    if (doMetadata && !doDumpConfig &&
     538        rocmInput->metadataSize != 0 && rocmInput->metadata != nullptr)
     539    {
     540        output.write(".metadata\n", 10);
     541        printDisasmLongString(rocmInput->metadataSize, rocmInput->metadata, output);
     542    }
    526543   
    527544    {
  • CLRadeonExtender/trunk/amdbin/ROCmBinaries.cpp

    r3662 r3665  
    3636ROCmBinary::ROCmBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags)
    3737        : ElfBinary64(binaryCodeSize, binaryCode, creationFlags),
    38           regionsNum(0), codeSize(0), code(nullptr)
     38          regionsNum(0), codeSize(0), code(nullptr), metadataSize(0), metadata(nullptr)
    3939{
    4040    cxuint textIndex = SHN_UNDEF;
     
    122122    }
    123123   
     124    // get metadata
     125    const size_t notesSize = getNotesSize();
     126    const cxbyte* noteContent = (const cxbyte*)getNotes();
     127   
     128    for (size_t offset = 0; offset < notesSize; )
     129    {
     130        const Elf64_Nhdr* nhdr = (const Elf64_Nhdr*)(noteContent + offset);
     131        size_t namesz = ULEV(nhdr->n_namesz);
     132        size_t descsz = ULEV(nhdr->n_descsz);
     133        if (usumGt(offset, namesz+descsz, notesSize))
     134            throw BinException("Note offset+size out of range");
     135       
     136        if (namesz==4 &&
     137            ::strcmp((const char*)noteContent+offset+ sizeof(Elf64_Nhdr), "AMD")==0)
     138        {
     139            const uint32_t noteType = ULEV(nhdr->n_type);
     140            if (noteType == 0xa)
     141            {
     142                metadata = (char*)(noteContent+offset+sizeof(Elf64_Nhdr) + 4);
     143                metadataSize = descsz;
     144            }
     145            else if (noteType == 0xb)
     146                target.assign((char*)(noteContent+offset+sizeof(Elf64_Nhdr) + 4), descsz);
     147        }
     148        size_t align = (((namesz+descsz)&3)!=0) ? 4-((namesz+descsz)&3) : 0;
     149        offset += sizeof(Elf64_Nhdr) + namesz + descsz + align;
     150    }
     151   
    124152    if (hasRegionMap())
    125153    {
     
    217245        const std::vector<ROCmSymbolInput>& symbols)
    218246{
    219     input = new ROCmInput{ deviceType, archMinor, archStepping, symbols, codeSize, code };
     247    input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false,
     248            symbols, codeSize, code };
    220249}
    221250
     
    224253        std::vector<ROCmSymbolInput>&& symbols)
    225254{
    226     input = new ROCmInput{ deviceType, archMinor, archStepping, std::move(symbols),
    227                 codeSize, code };
     255    input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false,
     256            std::move(symbols), codeSize, code };
    228257}
    229258
     
    351380    SULEV(*(uint32_t*)(noteBuf.get()+12), amdGpuArchValues.stepping);
    352381    elfBinGen64.addNote({"AMD", 0x1b, noteBuf.get(), 3U});
     382    if (!input->target.empty())
     383        elfBinGen64.addNote({"AMD", input->target.size(),
     384                (const cxbyte*)input->target.c_str(), 0xbU});
     385    if (input->metadataSize != 0)
     386        elfBinGen64.addNote({"AMD", input->metadataSize,
     387                (const cxbyte*)input->metadata, 0xaU});
    353388   
    354389    /// region and sections
  • CLRadeonExtender/trunk/tests/amdbin/ROCmBinGen.cpp

    r3662 r3665  
    5252    rocmInput.archMinor = 0;
    5353    rocmInput.archStepping = 0;
     54    rocmInput.metadataSize = 0;
     55    rocmInput.metadata = nullptr;
    5456   
    5557    {
Note: See TracChangeset for help on using the changeset viewer.