1/* 2 * Copyright (c) 2012-2015 Advanced Micro Devices, Inc. 3 * All rights reserved. 4 * 5 * For use for simulation and test purposes only 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the copyright holder nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Author: Steve Reinhardt, Anthony Gutierrez 34 */ 35 36#ifndef __BRIG_OBJECT_HH__ 37#define __BRIG_OBJECT_HH__ 38 39#include <cassert> 40#include <cstdint> 41#include <string> 42#include <vector> 43 44#include "arch/hsail/Brig.h" 45#include "gpu-compute/hsa_object.hh" 46#include "gpu-compute/hsail_code.hh" 47 48class LabelMap; 49class StorageMap; 50 51/* @class BrigObject 52 * this class implements the BRIG loader object, and 53 * is used when the simulator directly executes HSAIL. 54 * this class is responsible for extracting all 55 * information about kernels contained in BRIG format 56 * and converts them to HsailCode objects that are 57 * usable by the simulator and emulated runtime. 58 */ 59 60class BrigObject final : public HsaObject 61{ 62 public: 63 enum SectionIndex 64 { 65 DataSectionIndex, 66 CodeSectionIndex, 67 OperandsSectionIndex, 68 NumSectionIndices 69 }; 70 71 static const char *sectionNames[]; 72 73 struct SectionInfo 74 { 75 uint8_t *ptr; 76 int size; 77 }; 78 79 static HsaObject* tryFile(const std::string &fname, int len, 80 uint8_t *fileData); 81 82 SectionInfo sectionInfo[NumSectionIndices]; 83 const uint8_t *getSectionOffset(enum SectionIndex sec, int offs) const; 84 85 std::vector<HsailCode*> kernels; 86 std::vector<HsailCode*> functions; 87 std::string kern_block_name; 88 89 void processDirectives(const Brig::BrigBase *dirPtr, 90 const Brig::BrigBase *endPtr, 91 StorageMap *storageMap); 92 93 BrigObject(const std::string &fname, int len, uint8_t *fileData); 94 ~BrigObject(); 95 96 // eventually these will need to be per-kernel not per-object-file 97 StorageMap *storageMap; 98 LabelMap *labelMap; 99 100 const char* getString(int offs) const; 101 const Brig::BrigData* getBrigBaseData(int offs) const; 102 const uint8_t* getData(int offs) const; 103 const Brig::BrigBase* getCodeSectionEntry(int offs) const; 104 const Brig::BrigOperand* getOperand(int offs) const; 105 unsigned getOperandPtr(int offs, int index) const; 106 const Brig::BrigInstBase* getInst(int offs) const; 107 108 HsaCode* getKernel(const std::string &name) const override; 109 HsaCode* getFunction(const std::string &name) const override; 110 111 int numKernels() const override { return kernels.size(); } 112 113 HsaCode* getKernel(int i) const override { return kernels[i]; } 114 115 // pointer to the current kernel/function we're processing, so elements 116 // under construction can reference it. kinda ugly, but easier 117 // than passing it all over for the few places it's needed. 118 mutable HsailCode *currentCode; 119}; 120 121// Utility function to bump Brig item pointer to next element given 122// item size in bytes. Really just an add but with lots of casting. 123template<typename T> 124T* 125brigNext(T *ptr) 126{ 127 Brig::BrigBase *base_ptr = (Brig::BrigBase*)ptr; 128 int size = base_ptr->byteCount; 129 assert(size); 130 131 return (T*)((uint8_t*)ptr + size); 132} 133 134#endif // __BRIG_OBJECT_HH__ 135