Changeset 2537 in CLRX


Ignore:
Timestamp:
Nov 6, 2016, 10:23:25 AM (3 years ago)
Author:
matszpk
Message:

CLRadeonExtender: Add hash section to ROCm binaries. Add routines to calculate symbol's names hash codes and to optimize hash's buckets number.

Location:
CLRadeonExtender/trunk
Files:
3 edited

Legend:

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

    r2496 r2537  
    607607    /// get dynsym section
    608608    static ElfRegionTemplate dynsymSection()
    609     { return ElfRegionTemplate(0, (const cxbyte*)nullptr,  sizeof(typename Types::Word),
    610                 ".dynsym", SHT_DYNSYM, 0); }
     609    { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
     610                ".dynsym", SHT_DYNSYM, SHF_ALLOC); }
     611   
     612    /// get hash section
     613    static ElfRegionTemplate hashSection()
     614    { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
     615                ".hash", SHT_HASH, SHF_ALLOC); }
    611616};
    612617
  • CLRadeonExtender/trunk/amdbin/ElfBinaries.cpp

    r2496 r2537  
    507507    computeSize();
    508508    return size;
     509}
     510
     511template<typename Types>
     512static std::unique_ptr<uint32_t[]> calculateHashValuesForSymbols(bool addNullSymbol,
     513            const std::vector<ElfSymbolTemplate<Types> >& symbols)
     514{
     515    const size_t symsNum = symbols.size() + addNullSymbol;
     516    std::unique_ptr<uint32_t[]> hashCodes(new uint32_t[symsNum]);
     517    if (addNullSymbol)
     518        hashCodes[0] = 0;
     519    for (size_t i = 0; i < symbols.size(); i++)
     520    {
     521        uint32_t h = 0, g;
     522        const cxbyte* name = reinterpret_cast<const cxbyte*>(symbols[i].name);
     523        while(*name!=0)
     524        {
     525            h = (h<<4) + *name++;
     526            g = h & 0xf0000000U;
     527            if (g) h ^= g>>24;
     528            h &= ~g;
     529        }
     530        hashCodes[i+addNullSymbol] = h;
     531    }
     532    return hashCodes;
     533}
     534
     535/// return bucket number
     536static uint32_t optimizeHashBucketsNum(uint32_t hashNum, const uint32_t* hashCodes)
     537{
     538    uint32_t bestBucketNum = 0;
     539    uint64_t bestValue = UINT64_MAX;
     540    uint64_t maxSteps = (uint64_t(hashNum)<<1) - (hashNum>>2) + 1;
     541    const uint32_t steps = (maxSteps<=1000U) ? hashNum : hashNum<<((32-CLZ32(hashNum))>>1);
     542   
     543    std::unique_ptr<uint32_t[]> chainLengths(new uint32_t[maxSteps]);
     544    const uint32_t stepSize = maxSteps / steps;
     545    for (uint32_t buckets = hashNum>>2; buckets < (hashNum<<1); buckets += stepSize)
     546    {   //
     547        std::fill(chainLengths.get(), chainLengths.get() + buckets, 0U);
     548        // calculate chain lengths
     549        for (size_t i = 0; i < hashNum; i++)
     550            chainLengths[hashCodes[i] % buckets]++;
     551        /// value, smaller is better
     552        uint64_t value = uint64_t(buckets)*4;
     553        for (uint32_t i = 0; i < buckets; i++)
     554            value += chainLengths[i]*chainLengths[i];
     555        if (value < bestValue)
     556        {
     557            bestBucketNum = buckets;
     558            bestValue = value;
     559        }
     560    }
     561    return bestBucketNum;
    509562}
    510563
     
    793846                    }
    794847                }
     848                else if (region.section.type == SHT_HASH)
     849                {
     850                    if (::strcmp(region.section.name, ".hash") != 0)
     851                        continue;
     852                }
    795853                else if (region.section.type == SHT_STRTAB)
    796854                {
  • CLRadeonExtender/trunk/amdbin/ROCmBinaries.cpp

    r2536 r2537  
    195195    elfBinGen64.addRegion(ElfRegion64::programHeaderTable());
    196196    elfBinGen64.addRegion(ElfRegion64::dynsymSection());
     197    elfBinGen64.addRegion(ElfRegion64::hashSection());
    197198    elfBinGen64.addRegion(ElfRegion64::dynstrSection());
    198199    elfBinGen64.addRegion(ElfRegion64(input->codeSize, (const cxbyte*)input->code,
Note: See TracChangeset for help on using the changeset viewer.