diff --git a/bin/.gitattributes b/bin/.gitattributes new file mode 100644 index 0000000..c32f4d2 --- /dev/null +++ b/bin/.gitattributes @@ -0,0 +1 @@ +lld filter=lfs diff=lfs merge=lfs -text diff --git a/bin/iga64 b/bin/iga64 new file mode 100755 index 0000000..1f5f6e6 Binary files /dev/null and b/bin/iga64 differ diff --git a/bin/lld b/bin/lld new file mode 100755 index 0000000..7d43d61 --- /dev/null +++ b/bin/lld @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ee11aee3e56440cf6c462a5d7b70d76f396c9d677849c090c62002e08593711a +size 130394592 diff --git a/include/iga/iga.h b/include/iga/iga.h index 242ee29..6c0a979 100644 --- a/include/iga/iga.h +++ b/include/iga/iga.h @@ -439,6 +439,7 @@ static_assert(sizeof(iga_disassemble_options_t) == 6 * 4, /* just the default decoding opts */ #define IGA_DECODING_OPTS_DEFAULT (0u) + /* * Disassembles kernel bits into a string. * diff --git a/include/iga/igaEncoderWrapper.hpp b/include/iga/igaEncoderWrapper.hpp index 3e84f4d..0890798 100644 --- a/include/iga/igaEncoderWrapper.hpp +++ b/include/iga/igaEncoderWrapper.hpp @@ -59,6 +59,7 @@ class KernelEncoder { // enable IGA swsb set. When enabled, the original swsb in the input // instructions will be obsoleted void enableIGAAutoDeps(bool enable = true) { m_enableAutoDeps = enable; } + }; #endif // _IGA_ENCODER_WRAPPER_HPP diff --git a/include/iga/igad.h b/include/iga/igad.h index 5b94122..1a85021 100644 --- a/include/iga/igad.h +++ b/include/iga/igad.h @@ -148,12 +148,19 @@ typedef iga_status_t(CDECLATTRIBUTE *pIGAOpspecOp)(iga_opspec_t op, typedef kv_t *(CDECLATTRIBUTE *pIGAKVCreate)( iga_gen_t plat, const void *bytes, size_t bytes_len, iga_status_t *status, char *errbuf, size_t errbuf_cap, - /* iga::SWSB_ENCODE_MODE */ uint32_t swsb_mode); + /* iga::SWSB_ENCODE_MODE */ uint32_t swsb_mode + ); #define IGA_KV_DELETE_STR "kv_delete" typedef void(CDECLATTRIBUTE *pIGAKVDelete)(kv_t *); #define IGA_KV_GET_INST_SIZE_STR "kv_get_inst_size" typedef int32_t(CDECLATTRIBUTE *pIGAKVGetInstSize)(const kv_t *kv, int32_t pc); +#define IGA_KV_GET_INST_MSG_INFO_STR "kv_get_inst_msg_info" +typedef int32_t(CDECLATTRIBUTE *pIGAKVGetInstMsgInfo)(const kv_t *kv, + int32_t pc, + bool *isAtomic, + bool *isSlm, + bool *isScratch); #define IGA_KV_GET_INST_TARGETS_STR "kv_get_inst_targets" typedef uint32_t(CDECLATTRIBUTE *pIGAKVGetInstTargets)(const kv_t *kv, int32_t pc, @@ -379,6 +386,7 @@ typedef struct { pIGAKVCreate kv_create; pIGAKVDelete kv_delete; pIGAKVGetInstSize kv_get_inst_size; + pIGAKVGetInstMsgInfo kv_get_inst_msg_info; pIGAKVGetInstTargets kv_get_inst_targets; pIGAKVIsInstTarget kv_is_inst_target; pIGAKVGetDefaultLabelName kv_get_default_label_name; diff --git a/include/iga/kv.h b/include/iga/kv.h index 5eb330c..abd7d7f 100644 --- a/include/iga/kv.h +++ b/include/iga/kv.h @@ -89,7 +89,8 @@ typedef enum { */ IGA_API struct kv_t *kv_create(iga_gen_t plat, const void *bytes, size_t bytes_len, iga_status_t *status, char *errbuf, size_t errbuf_cap, - uint32_t swsb_enc_mode /* iga::SWSB_ENCODE_MODE */); + uint32_t swsb_enc_mode /* iga::SWSB_ENCODE_MODE */ + ); /* * Destroys a kernel view deallocating all resources sequestered by the kv_t @@ -112,6 +113,18 @@ IGA_API void kv_delete(struct kv_t *); */ IGA_API int32_t kv_get_inst_size(const struct kv_t *kv, int32_t pc); +/* + * Returns some attributes of a message: if it is atomic, slm, scratch + * operation. + * Returns: + * KV_SUCCESS on success; output is stored to isAtomic and isSlm + * KV_ERROR if kv is invalid + * KV_INVALID_PC if instruction cannot be found + * KV_DECODE_ERROR if an instruction cannot be docoded + */ +IGA_API kv_status_t kv_get_inst_msg_info(const kv_t* kv, int32_t pc, + bool* isAtomic, bool* isSlm, bool* isScratch); + /* * Returns true if the instruction has the opt */ @@ -307,6 +320,11 @@ IGA_API kv_status_t kv_get_sendg_ind_desc1(const kv_t *kv, int32_t pc, */ #define KV_INVALID_SEND_DESC ((uint32_t)0xFFFFFFFFF) + /* + * A symbol to indicate an invalid sendg descriptor value. + */ +#define KV_INVALID_SEND_DESC64B ((uint64_t)0xFFFFFFFFFFFFFFFFL) + /* TODO: review necessity of this macro. * A symbol to indicate an invalid message length value. */ diff --git a/include/iga/kv.hpp b/include/iga/kv.hpp index d229318..3cab9eb 100644 --- a/include/iga/kv.hpp +++ b/include/iga/kv.hpp @@ -50,10 +50,12 @@ class KernelView { KernelView( iga_gen_t platf, const void *bytes, size_t bytesLength, iga::SWSB_ENCODE_MODE swsb_mode = iga::SWSB_ENCODE_MODE::SWSBInvalidMode, - char *decodeLog = nullptr, size_t decodeLogLen = 0) + char *decodeLog = nullptr, size_t decodeLogLen = 0 + ) : m_kv(nullptr), m_disasm_status(IGA_SUCCESS), m_gen(platf) { m_kv = kv_create(platf, bytes, bytesLength, &m_disasm_status, decodeLog, - decodeLogLen, static_cast(swsb_mode)); + decodeLogLen, static_cast(swsb_mode) + ); } // The destructor automatically deallocates the underlying kernel view diff --git a/include/igc/OCLAPI/oclapi.h b/include/igc/OCLAPI/oclapi.h index 06be0cd..08e53d7 100644 --- a/include/igc/OCLAPI/oclapi.h +++ b/include/igc/OCLAPI/oclapi.h @@ -10,9 +10,9 @@ SPDX-License-Identifier: MIT #define OCL_API_CALLS_H #if defined(_WIN32) - #define OCL_API_CALL +#define OCL_API_CALL #else - #define OCL_API_CALL __attribute__ ((visibility("default"))) +#define OCL_API_CALL __attribute__((visibility("default"))) #endif #endif // OCL_API_CALLS_H diff --git a/include/igc/cif/cif/builtins/builtins_registry.cpp b/include/igc/cif/cif/builtins/builtins_registry.cpp index 64ca625..672c220 100644 --- a/include/igc/cif/cif/builtins/builtins_registry.cpp +++ b/include/igc/cif/cif/builtins/builtins_registry.cpp @@ -17,24 +17,25 @@ namespace CIF { namespace Builtins { // List of all supported builtins -using AllBuiltinsListT = InterfacesList; +using AllBuiltinsListT = InterfacesList; -bool IsBuiltin(InterfaceId_t intId){ - return AllBuiltinsListT::ContainsInterface(intId); -} +bool IsBuiltin(InterfaceId_t intId) { return AllBuiltinsListT::ContainsInterface(intId); } -ICIF *Create(InterfaceId_t entryPointInterface, Version_t version, ICIF *parentInterface){ - return AllBuiltinsListT::template forwardToOne(entryPointInterface, nullptr, version, version, parentInterface); +ICIF *Create(InterfaceId_t entryPointInterface, Version_t version, ICIF *parentInterface) { + return AllBuiltinsListT::template forwardToOne( + entryPointInterface, nullptr, version, version, parentInterface); } -bool GetSupportedVersions(InterfaceId_t entryPointInterface, Version_t &verMin, Version_t &verMax){ - return AllBuiltinsListT::template forwardToOne(entryPointInterface, false, verMin, verMax); +bool GetSupportedVersions(InterfaceId_t entryPointInterface, Version_t &verMin, Version_t &verMax) { + return AllBuiltinsListT::template forwardToOne(entryPointInterface, false, + verMin, verMax); } -InterfaceId_t FindIncompatible(InterfaceId_t entryPointInterface, CIF::CompatibilityDataHandle handle){ - return AllBuiltinsListT::template forwardToOne(entryPointInterface, entryPointInterface, handle); +InterfaceId_t FindIncompatible(InterfaceId_t entryPointInterface, CIF::CompatibilityDataHandle handle) { + return AllBuiltinsListT::template forwardToOne( + entryPointInterface, entryPointInterface, handle); } -} +} // namespace Builtins -} +} // namespace CIF diff --git a/include/igc/cif/cif/builtins/builtins_registry.h b/include/igc/cif/cif/builtins/builtins_registry.h index ee3fb99..44eca2e 100644 --- a/include/igc/cif/cif/builtins/builtins_registry.h +++ b/include/igc/cif/cif/builtins/builtins_registry.h @@ -24,6 +24,6 @@ bool GetSupportedVersions(InterfaceId_t entryPointInterface, Version_t &verMin, InterfaceId_t FindIncompatible(InterfaceId_t entryPointInterface, CIF::CompatibilityDataHandle handle); -} +} // namespace Builtins -} +} // namespace CIF diff --git a/include/igc/cif/cif/builtins/memory/buffer/buffer.h b/include/igc/cif/cif/builtins/memory/buffer/buffer.h index 9b59450..6b2cc4b 100644 --- a/include/igc/cif/cif/builtins/memory/buffer/buffer.h +++ b/include/igc/cif/cif/builtins/memory/buffer/buffer.h @@ -26,54 +26,53 @@ using AllocatorT = void *(CIF_CALLING_CONV *)(size_t size); using ReallocatorT = void *(CIF_CALLING_CONV *)(void *oldMemory, size_t oldSize, size_t newSize); /// Custom deallocator function -using DeallocatorT = void (CIF_CALLING_CONV *)(void *memory); +using DeallocatorT = void(CIF_CALLING_CONV *)(void *memory); /// Builtin interface that can be used as generic buffer CIF_DECLARE_COMMON_INTERFACE(Buffer, "CIF_BUFFER"); /// v #1 -CIF_DEFINE_INTERFACE_VER(Buffer, 1){ +CIF_DEFINE_INTERFACE_VER(Buffer, 1) { CIF_INHERIT_CONSTRUCTOR(); /// Get address to underlying buffer with ElementT as pointer element type - template - ElementT *GetMemoryWriteable(){ - return reinterpret_cast(GetMemoryRawWriteable()); + template ElementT *GetMemoryWriteable() { + return reinterpret_cast(GetMemoryRawWriteable()); } /// Get address to underlying const buffer with ElementT as pointer element type - template - const ElementT *GetMemory() const{ - return reinterpret_cast(GetMemoryRaw()); + template const ElementT *GetMemory() const { + return reinterpret_cast(GetMemoryRaw()); } /// Get size in units of ElementT (ElementT == void version) - template - const typename std::enable_if::value, size_t>::type GetSize() const{ - return GetSizeRaw(); + template + const typename std::enable_if::value, size_t>::type GetSize() const { + return GetSizeRaw(); } /// Get size in units of ElementT (ElementT != void version) - template - const typename std::enable_if::value, size_t>::type GetSize() const{ - return GetSizeRaw() / sizeof(ElementT); + template + const typename std::enable_if::value, size_t>::type GetSize() const { + return GetSizeRaw() / sizeof(ElementT); } /// Copies given element to the end of the buffer /// Note : If (packed == false), then this function will automatically align current underlying buffer /// pointer to alignof(ElementT) - template - bool PushBackRawCopy(const ElementT &newEl, bool packed = true){ - static_assert(std::is_standard_layout_v && std::is_trivial_v && std::is_trivially_copyable_v, "Supporting only POD types"); - if(packed == false){ - size_t alignment = alignof(ElementT); - bool success = AlignUp(static_cast(alignment)); - if(success == false){ - return false; - } + template bool PushBackRawCopy(const ElementT &newEl, bool packed = true) { + static_assert(std::is_standard_layout_v && std::is_trivial_v && + std::is_trivially_copyable_v, + "Supporting only POD types"); + if (packed == false) { + size_t alignment = alignof(ElementT); + bool success = AlignUp(static_cast(alignment)); + if (success == false) { + return false; } + } - return PushBackRawBytes(&newEl, sizeof(ElementT)); + return PushBackRawBytes(&newEl, sizeof(ElementT)); } /// Sets custom allocator and deallocator functions to be used by this buffer interface @@ -135,49 +134,51 @@ CIF_GENERATE_VERSIONS_LIST(Buffer); CIF_MARK_LATEST_VERSION(BufferLatest, Buffer); using BufferSimple = Buffer<1>; /// tag the most common version -template -CIF::RAII::UPtr_t CreateConstBuffer(CIF::CIFMain *provider, const void *data, size_t size){ - if(provider == nullptr){ - return CIF::RAII::UPtr(nullptr); - } - auto buff = provider->CreateBuiltin(); - if(buff == nullptr){ - return CIF::RAII::UPtr(nullptr); - } - if((data != nullptr) && (size != 0)){ - buff->SetUnderlyingStorage(data, size); - } - return buff; +template +CIF::RAII::UPtr_t CreateConstBuffer(CIF::CIFMain *provider, const void *data, size_t size) { + if (provider == nullptr) { + return CIF::RAII::UPtr(nullptr); + } + auto buff = provider->CreateBuiltin(); + if (buff == nullptr) { + return CIF::RAII::UPtr(nullptr); + } + if ((data != nullptr) && (size != 0)) { + buff->SetUnderlyingStorage(data, size); + } + return buff; } -template -CIF::RAII::UPtr_t CreateWriteableBuffer(CIF::CIFMain *provider, const void *initialData, size_t initialSize){ - auto buff = CreateConstBuffer(provider, initialData, initialSize); - if(buff == nullptr){ - return CIF::RAII::UPtr(nullptr); - } - auto writeableMem = buff->GetMemoryRawWriteable(); - if(writeableMem == nullptr && (initialData != nullptr && initialSize != 0)){ - // failed to allocate new memory in writeable memory - return CIF::RAII::UPtr(nullptr); - } - return buff; +template +CIF::RAII::UPtr_t CreateWriteableBuffer(CIF::CIFMain *provider, const void *initialData, + size_t initialSize) { + auto buff = CreateConstBuffer(provider, initialData, initialSize); + if (buff == nullptr) { + return CIF::RAII::UPtr(nullptr); + } + auto writeableMem = buff->GetMemoryRawWriteable(); + if (writeableMem == nullptr && (initialData != nullptr && initialSize != 0)) { + // failed to allocate new memory in writeable memory + return CIF::RAII::UPtr(nullptr); + } + return buff; } -template -CIF::RAII::UPtr_t CreateBufferFromPtr(CIF::CIFMain *provider, void *ptr, size_t size, DeallocatorT ptrDeallocator){ - auto buff = CreateConstBuffer(provider, nullptr, 0); - if(buff == nullptr){ - return CIF::RAII::UPtr(nullptr); - } +template +CIF::RAII::UPtr_t CreateBufferFromPtr(CIF::CIFMain *provider, void *ptr, size_t size, + DeallocatorT ptrDeallocator) { + auto buff = CreateConstBuffer(provider, nullptr, 0); + if (buff == nullptr) { + return CIF::RAII::UPtr(nullptr); + } - buff->SetUnderlyingStorage(ptr, size, ptrDeallocator); + buff->SetUnderlyingStorage(ptr, size, ptrDeallocator); - return buff; + return buff; } -} +} // namespace Builtins -} +} // namespace CIF #include "cif/macros/disable.h" diff --git a/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.cpp b/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.cpp index 326ddfc..81bc89c 100644 --- a/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.cpp +++ b/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.cpp @@ -11,72 +11,51 @@ SPDX-License-Identifier: MIT #include "cif/macros/enable.h" -namespace CIF{ +namespace CIF { -namespace Builtins{ +namespace Builtins { -void CIF_GET_INTERFACE_CLASS(Buffer, 1)::SetAllocator(AllocatorT allocator, DeallocatorT deallocator, ReallocatorT reallocator){ - CIF_GET_PIMPL()->SetAllocator(allocator, deallocator, reallocator); +void CIF_GET_INTERFACE_CLASS(Buffer, 1)::SetAllocator(AllocatorT allocator, DeallocatorT deallocator, + ReallocatorT reallocator) { + CIF_GET_PIMPL()->SetAllocator(allocator, deallocator, reallocator); } -void CIF_GET_INTERFACE_CLASS(Buffer, 1)::SetUnderlyingStorage(void *memory, size_t size, DeallocatorT deallocator){ - CIF_GET_PIMPL()->SetUnderlyingStorage(memory, size, deallocator); +void CIF_GET_INTERFACE_CLASS(Buffer, 1)::SetUnderlyingStorage(void *memory, size_t size, DeallocatorT deallocator) { + CIF_GET_PIMPL()->SetUnderlyingStorage(memory, size, deallocator); } -void CIF_GET_INTERFACE_CLASS(Buffer, 1)::SetUnderlyingStorage(const void *memory, size_t size){ - CIF_GET_PIMPL()->SetUnderlyingStorage(memory, size); +void CIF_GET_INTERFACE_CLASS(Buffer, 1)::SetUnderlyingStorage(const void *memory, size_t size) { + CIF_GET_PIMPL()->SetUnderlyingStorage(memory, size); } -void *CIF_GET_INTERFACE_CLASS(Buffer, 1)::DetachAllocation(){ - return CIF_GET_PIMPL()->DetachAllocation(); -} +void *CIF_GET_INTERFACE_CLASS(Buffer, 1)::DetachAllocation() { return CIF_GET_PIMPL()->DetachAllocation(); } -const void *CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetMemoryRaw() const{ - return CIF_GET_PIMPL()->GetMemoryRaw(); -} +const void *CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetMemoryRaw() const { return CIF_GET_PIMPL()->GetMemoryRaw(); } -void *CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetMemoryRawWriteable(){ - return CIF_GET_PIMPL()->GetMemoryRawWriteable(); -} +void *CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetMemoryRawWriteable() { return CIF_GET_PIMPL()->GetMemoryRawWriteable(); } -size_t CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetSizeRaw() const{ - return CIF_GET_PIMPL()->GetSizeRaw(); -} +size_t CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetSizeRaw() const { return CIF_GET_PIMPL()->GetSizeRaw(); } -size_t CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetCapacityRaw() const{ - return CIF_GET_PIMPL()->GetCapacityRaw(); -} +size_t CIF_GET_INTERFACE_CLASS(Buffer, 1)::GetCapacityRaw() const { return CIF_GET_PIMPL()->GetCapacityRaw(); } -bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::Resize(size_t newSize){ - return CIF_GET_PIMPL()->Resize(newSize); -} +bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::Resize(size_t newSize) { return CIF_GET_PIMPL()->Resize(newSize); } -bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::Reserve(size_t newCapacity){ - return CIF_GET_PIMPL()->Reserve(newCapacity); -} +bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::Reserve(size_t newCapacity) { return CIF_GET_PIMPL()->Reserve(newCapacity); } -void CIF_GET_INTERFACE_CLASS(Buffer, 1)::Clear(){ - CIF_GET_PIMPL()->Clear(); -} +void CIF_GET_INTERFACE_CLASS(Buffer, 1)::Clear() { CIF_GET_PIMPL()->Clear(); } -void CIF_GET_INTERFACE_CLASS(Buffer, 1)::Deallocate(){ - CIF_GET_PIMPL()->Deallocate(); -} +void CIF_GET_INTERFACE_CLASS(Buffer, 1)::Deallocate() { CIF_GET_PIMPL()->Deallocate(); } -bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::AlignUp(uint32_t alignment){ - return CIF_GET_PIMPL()->AlignUp(alignment); -} +bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::AlignUp(uint32_t alignment) { return CIF_GET_PIMPL()->AlignUp(alignment); } -bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::PushBackRawBytes(const void *newData, size_t size){ - return CIF_GET_PIMPL()->PushBackRawBytes(newData, size); +bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::PushBackRawBytes(const void *newData, size_t size) { + return CIF_GET_PIMPL()->PushBackRawBytes(newData, size); } -bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::IsConst()const{ - return CIF_GET_PIMPL()->IsConst(); -} +bool CIF_GET_INTERFACE_CLASS(Buffer, 1)::IsConst() const { return CIF_GET_PIMPL()->IsConst(); } -} +} // namespace Builtins -} +} // namespace CIF #include "cif/macros/disable.h" diff --git a/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.h b/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.h index 329e76c..cec680d 100644 --- a/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.h +++ b/include/igc/cif/cif/builtins/memory/buffer/impl/buffer_impl.h @@ -21,284 +21,260 @@ namespace CIF { namespace Builtins { CIF_DECLARE_INTERFACE_PIMPL(Buffer) : CIF::PimplBase { - CIF_PIMPL_DECLARE_CONSTRUCTOR(Version_t version, CIF::ICIF *parentInterface) { - } + CIF_PIMPL_DECLARE_CONSTRUCTOR(Version_t version, CIF::ICIF * parentInterface) {} - CIF_PIMPL_DECLARE_CONSTRUCTOR(Version_t version) { - } + CIF_PIMPL_DECLARE_CONSTRUCTOR(Version_t version) {} - CIF_PIMPL_DECLARE_CONSTRUCTOR() { - } + CIF_PIMPL_DECLARE_CONSTRUCTOR() {} - CIF_PIMPL_DECLARE_DESTRUCTOR(){ - Deallocate(); - } + CIF_PIMPL_DECLARE_DESTRUCTOR() { Deallocate(); } /// Copies given element to the end of the buffer /// Note : If (packed == false), then this function will automatically align current underlying buffer /// pointer to alignof(ElementT) - template - bool PushBackRawCopy(const ElementT &newEl, bool packed = true){ - static_assert(std::is_pod::value, "Supporting only POD types"); - if(packed == false){ - size_t alignment = alignof(ElementT); - bool success = AlignUp(alignment); - if(success == false){ - return false; - } + template bool PushBackRawCopy(const ElementT &newEl, bool packed = true) { + static_assert(std::is_standard_layout_v && std::is_trivial_v, "Supporting only POD types"); + if (packed == false) { + size_t alignment = alignof(ElementT); + bool success = AlignUp(alignment); + if (success == false) { + return false; } + } - return PushBackRawBytes(&newEl, sizeof(ElementT)); + return PushBackRawBytes(&newEl, sizeof(ElementT)); } - /// Sets custom allocator and deallocator functions to be used by this buffer interface - /// Note : will reallocate using new allocators only when additional space will be needed - /// Note : reallocator is optional and can be nullptr - void SetAllocator(AllocatorT allocator, DeallocatorT deallocator, ReallocatorT reallocator){ - if(allocator == nullptr){ - allocator = EmptyAllocator; - } - - if((this->memory) && (this->tempDeallocator == nullptr)){ - this->tempDeallocator = this->deallocator; - } - - this->allocator = allocator; - this->deallocator = deallocator; - this->reallocator = reallocator; + /// Sets custom allocator and deallocator functions to be used by this buffer interface + /// Note : will reallocate using new allocators only when additional space will be needed + /// Note : reallocator is optional and can be nullptr + void SetAllocator(AllocatorT allocator, DeallocatorT deallocator, ReallocatorT reallocator) { + if (allocator == nullptr) { + allocator = EmptyAllocator; } - /// Sets underlying buffer storage and its deallocator - /// Note : will destroy current underlying buffer (if any) - void SetUnderlyingStorage(void *memory, size_t size, DeallocatorT deallocator){ - Deallocate(); - - this->memory = memory; - this->size = size; - this->capacity = size; - this->tempDeallocator = deallocator; + if ((this->memory) && (this->tempDeallocator == nullptr)) { + this->tempDeallocator = this->deallocator; } - /// Sets underlying constant buffer storage and its deallocator - /// Note : will destroy current underlying buffer (if any) - /// Note : will allocate new memory if this const memory will be accessed in non-const manners - void SetUnderlyingStorage(const void *memory, size_t size){ - Deallocate(); + this->allocator = allocator; + this->deallocator = deallocator; + this->reallocator = reallocator; + } - // casting-away constness, but marking as const - this->memory = const_cast(memory); - this->isConst = true; + /// Sets underlying buffer storage and its deallocator + /// Note : will destroy current underlying buffer (if any) + void SetUnderlyingStorage(void *memory, size_t size, DeallocatorT deallocator) { + Deallocate(); - this->size = size; - this->capacity = size; - this->tempDeallocator = nullptr; - } + this->memory = memory; + this->size = size; + this->capacity = size; + this->tempDeallocator = deallocator; + } - /// Detaches and returns current allocation leaving this buffer's underlying buffer empty - void *DetachAllocation(){ - void *mem = memory; - this->memory = nullptr; - this->size = 0; - this->capacity = 0; - this->isConst = false; - this->tempDeallocator = nullptr; - return mem; - } + /// Sets underlying constant buffer storage and its deallocator + /// Note : will destroy current underlying buffer (if any) + /// Note : will allocate new memory if this const memory will be accessed in non-const manners + void SetUnderlyingStorage(const void *memory, size_t size) { + Deallocate(); - /// Returns const raw access to underlying buffer - const void *GetMemoryRaw() const{ - return memory; - } + // casting-away constness, but marking as const + this->memory = const_cast(memory); + this->isConst = true; - /// Returns writeable access to underlyng buffer - /// Note : for constant buffer this will cause reallocation (copy!) to writeable memory - void *GetMemoryRawWriteable(){ - if(this->isConst && this->capacity != 0){ - if(false == Reallocate(this->size, this->capacity)){ - return nullptr; - } - } - return memory; - } + this->size = size; + this->capacity = size; + this->tempDeallocator = nullptr; + } - /// Returns size in bytes of underlying buffer - size_t GetSizeRaw() const{ - return size; - } + /// Detaches and returns current allocation leaving this buffer's underlying buffer empty + void *DetachAllocation() { + void *mem = memory; + this->memory = nullptr; + this->size = 0; + this->capacity = 0; + this->isConst = false; + this->tempDeallocator = nullptr; + return mem; + } - /// Returns capacity in bytes of underlying buffer - size_t GetCapacityRaw() const{ - return capacity; - } + /// Returns const raw access to underlying buffer + const void *GetMemoryRaw() const { return memory; } - /// Resizes (may reallocate) underlying buffer's size - bool Resize(size_t newSize) { - return this->Reallocate(newSize, std::max(this->capacity, newSize)); + /// Returns writeable access to underlyng buffer + /// Note : for constant buffer this will cause reallocation (copy!) to writeable memory + void *GetMemoryRawWriteable() { + if (this->isConst && this->capacity != 0) { + if (false == Reallocate(this->size, this->capacity)) { + return nullptr; + } } + return memory; + } - /// Resizes (may reallocate) the underlying buffer's capacity - bool Reserve(size_t newCapacity){ - return this->Reallocate(this->size, newCapacity); + /// Returns size in bytes of underlying buffer + size_t GetSizeRaw() const { return size; } + + /// Returns capacity in bytes of underlying buffer + size_t GetCapacityRaw() const { return capacity; } + + /// Resizes (may reallocate) underlying buffer's size + bool Resize(size_t newSize) { return this->Reallocate(newSize, std::max(this->capacity, newSize)); } + + /// Resizes (may reallocate) the underlying buffer's capacity + bool Reserve(size_t newCapacity) { return this->Reallocate(this->size, newCapacity); } + + /// Sets the size to 0 + void Clear() { this->size = 0; } + + /// Sets the size to 0 and deallocates underlying buffer + void Deallocate() { + if (isConst) { + this->memory = nullptr; + this->size = 0; + this->capacity = 0; + this->isConst = false; + this->tempDeallocator = nullptr; + } else if (tempDeallocator) { + tempDeallocator(this->memory); + this->memory = nullptr; + this->size = 0; + this->capacity = 0; + this->tempDeallocator = nullptr; + } else { + if (deallocator != nullptr) { + deallocator(this->memory); + } + this->memory = nullptr; + this->size = 0; + this->capacity = 0; } + } - /// Sets the size to 0 - void Clear(){ - this->size = 0; - } + /// Aligns up current size to meet required alignment + bool AlignUp(uint32_t alignment) { + size_t misalligned = alignment - (this->size % alignment); + size_t newSize = this->size + misalligned; + return Reallocate(newSize, newSize); + } - /// Sets the size to 0 and deallocates underlying buffer - void Deallocate(){ - if(isConst){ - this->memory = nullptr; - this->size = 0; - this->capacity = 0; - this->isConst = false; - this->tempDeallocator = nullptr; - }else if(tempDeallocator){ - tempDeallocator(this->memory); - this->memory = nullptr; - this->size = 0; - this->capacity = 0; - this->tempDeallocator = nullptr; - }else{ - if(deallocator != nullptr){ - deallocator(this->memory); - } - this->memory = nullptr; - this->size = 0; - this->capacity = 0; - } + /// Pushes new raw element to buffer + bool PushBackRawBytes(const void *newData, size_t size) { + if (size == 0) { + // nothing to do + return true; } - /// Aligns up current size to meet required alignment - bool AlignUp(uint32_t alignment){ - size_t misalligned = alignment - (this->size % alignment); - size_t newSize = this->size + misalligned; - return Reallocate(newSize, newSize); + size_t oldSize = this->size; + if (false == Reallocate(this->size + size, this->size + size)) { + return false; } - /// Pushes new raw element to buffer - bool PushBackRawBytes(const void *newData, size_t size){ - if(size == 0){ - // nothing to do - return true; - } + CIF::SafeCopy(CIF::OffsetedPtr(GetMemoryRawWriteable(), oldSize), this->capacity - oldSize, newData, size); + return true; + } - size_t oldSize = this->size; - if(false == Reallocate(this->size + size, this->size + size)){ - return false; - } + /// Can be true if SetUnderlyingStorage(const void *, size_t) was used, please refer to SetUnderlyingStorage + bool IsConst() const { return isConst; } - CIF::SafeCopy(CIF::OffsetedPtr(GetMemoryRawWriteable(), oldSize), this->capacity - oldSize, newData, size); - return true; +protected: + static void *CIF_CALLING_CONV EmptyAllocator(size_t size) { return nullptr; } + + static void *CIF_CALLING_CONV DefaultAllocator(size_t size) { return new (std::nothrow) char[size]; } + + static void CIF_CALLING_CONV DefaultDeallocator(void *memory) { + if (memory != nullptr) { + delete[] reinterpret_cast(memory); } + } - /// Can be true if SetUnderlyingStorage(const void *, size_t) was used, please refer to SetUnderlyingStorage - bool IsConst() const{ - return isConst; + bool Reallocate(size_t newSize, size_t newCapacity) { + if (isConst) { + void *newMem = this->allocator(newCapacity); + if (newMem == nullptr) { + return false; + } + + size_t bytesToCopy = std::min(newSize, this->size); + CIF::SafeCopy(newMem, newCapacity, this->memory, bytesToCopy); + this->memory = newMem; + this->isConst = false; + this->tempDeallocator = nullptr; + this->capacity = newCapacity; + this->size = newSize; + return true; } -protected: - static void * CIF_CALLING_CONV EmptyAllocator(size_t size){ - return nullptr; + if (tempDeallocator != nullptr) { + if (newCapacity <= this->capacity) { + this->size = newSize; + return true; + } + + void *newMem = this->allocator(newCapacity); + if (newMem == nullptr) { + return false; + } + size_t bytesToCopy = std::min(newSize, this->size); + CIF::SafeCopy(newMem, newCapacity, this->memory, bytesToCopy); + void *oldMemory = this->memory; + this->memory = newMem; + this->capacity = newCapacity; + this->size = newSize; + this->tempDeallocator(oldMemory); + this->tempDeallocator = nullptr; + return true; } - static void * CIF_CALLING_CONV DefaultAllocator(size_t size){ - return new(std::nothrow) char[size]; + if (newCapacity <= this->capacity) { + this->size = newSize; + return true; } - static void CIF_CALLING_CONV DefaultDeallocator(void * memory){ - if(memory != nullptr){ - delete []reinterpret_cast(memory); - } + if (reallocator != nullptr) { + void *newMem = reallocator(this->memory, this->capacity, newCapacity); + if (newMem == nullptr) { + return false; + } + + this->memory = newMem; + this->capacity = newCapacity; + this->size = newSize; + return true; } - bool Reallocate(size_t newSize, size_t newCapacity){ - if(isConst){ - void * newMem = this->allocator(newCapacity); - if(newMem == nullptr){ - return false; - } - - size_t bytesToCopy = std::min(newSize, this->size); - CIF::SafeCopy(newMem, newCapacity, this->memory, bytesToCopy); - this->memory = newMem; - this->isConst = false; - this->tempDeallocator = nullptr; - this->capacity = newCapacity; - this->size = newSize; - return true; - } - - if(tempDeallocator != nullptr){ - if(newCapacity <= this->capacity){ - this->size = newSize; - return true; - } - - void * newMem = this->allocator(newCapacity); - if(newMem == nullptr){ - return false; - } - size_t bytesToCopy = std::min(newSize, this->size); - CIF::SafeCopy(newMem, newCapacity, this->memory, bytesToCopy); - void *oldMemory = this->memory; - this->memory = newMem; - this->capacity = newCapacity; - this->size = newSize; - this->tempDeallocator(oldMemory); - this->tempDeallocator = nullptr; - return true; - } - - if(newCapacity <= this->capacity){ - this->size = newSize; - return true; - } - - if(reallocator != nullptr){ - void *newMem = reallocator(this->memory, this->capacity, newCapacity); - if(newMem == nullptr){ - return false; - } - - this->memory = newMem; - this->capacity = newCapacity; - this->size = newSize; - return true; - } - - void *newMem = allocator(newCapacity); - if(newMem == nullptr){ - return false; - } - - size_t bytesToCopy = std::min(newSize, this->size); - CIF::SafeCopy(newMem, newCapacity, this->memory, bytesToCopy); - if(deallocator != nullptr){ - deallocator(this->memory); - } - this->memory = newMem; - this->capacity = newCapacity; - this->size = newSize; + void *newMem = allocator(newCapacity); + if (newMem == nullptr) { + return false; + } - return true; + size_t bytesToCopy = std::min(newSize, this->size); + CIF::SafeCopy(newMem, newCapacity, this->memory, bytesToCopy); + if (deallocator != nullptr) { + deallocator(this->memory); } + this->memory = newMem; + this->capacity = newCapacity; + this->size = newSize; + + return true; + } - void *memory = nullptr; - size_t size = 0; - size_t capacity = 0; - bool isConst = false; - DeallocatorT tempDeallocator = nullptr; - AllocatorT allocator = DefaultAllocator; - DeallocatorT deallocator = DefaultDeallocator; - ReallocatorT reallocator = nullptr; + void *memory = nullptr; + size_t size = 0; + size_t capacity = 0; + bool isConst = false; + DeallocatorT tempDeallocator = nullptr; + AllocatorT allocator = DefaultAllocator; + DeallocatorT deallocator = DefaultDeallocator; + ReallocatorT reallocator = nullptr; }; CIF_DEFINE_INTERFACE_TO_PIMPL_FORWARDING_CTOR_DTOR(Buffer); -} +} // namespace Builtins -} +} // namespace CIF #include "cif/macros/disable.h" diff --git a/include/igc/cif/cif/common/cif.h b/include/igc/cif/cif/common/cif.h index ba7c029..d34967b 100644 --- a/include/igc/cif/cif/common/cif.h +++ b/include/igc/cif/cif/common/cif.h @@ -23,41 +23,35 @@ namespace CIF { constexpr Version_t CifFrameworkVersion = 2; namespace RAII { -template -struct ReleaseHelper { - void operator() (T *handle) const { - assert(handle != nullptr); - handle->Release(); - } +template struct ReleaseHelper { + void operator()(T *handle) const { + assert(handle != nullptr); + handle->Release(); + } }; -template -using UPtr_t = std::unique_ptr>; +template using UPtr_t = std::unique_ptr>; -template static UPtr_t UPtr(T *handle) { - return UPtr_t(handle); -} +template static UPtr_t UPtr(T *handle) { return UPtr_t(handle); } -template -UPtr_t RetainAndPack(SrcType * ptr){ - if(ptr == nullptr){ - return UPtr(nullptr); - } +template UPtr_t RetainAndPack(SrcType *ptr) { + if (ptr == nullptr) { + return UPtr(nullptr); + } - ptr->Retain(); - return UPtr(static_cast(ptr)); + ptr->Retain(); + return UPtr(static_cast(ptr)); } -template -UPtr_t Pack(SrcType * ptr){ - if(ptr == nullptr){ - return UPtr(nullptr); - } +template UPtr_t Pack(SrcType *ptr) { + if (ptr == nullptr) { + return UPtr(nullptr); + } - return UPtr(static_cast(ptr)); + return UPtr(static_cast(ptr)); } -} +} // namespace RAII // generic interface for all components struct ICIF { @@ -72,46 +66,37 @@ struct ICIF { virtual Version_t GetEnabledVersion() const = 0; virtual Version_t GetUnderlyingVersion() const { - return GetEnabledVersion(); // by default : redirect to enabled version + return GetEnabledVersion(); // by default : redirect to enabled version } - virtual bool GetSupportedVersions(InterfaceId_t intId, Version_t &verMin, - Version_t &verMax) const { - return false; // by default : no sub-interface are supported + virtual bool GetSupportedVersions(InterfaceId_t intId, Version_t &verMin, Version_t &verMax) const { + return false; // by default : no sub-interface are supported } // get specific version of interface - template - RAII::UPtr_t CreateInterface() { - static_assert((version >= InterfaceT::GetBackwardsCompatibilityVersion()) && - (version <= InterfaceT::GetVersion()), + template RAII::UPtr_t CreateInterface() { + static_assert((version >= InterfaceT::GetBackwardsCompatibilityVersion()) && (version <= InterfaceT::GetVersion()), "Invalid version requested"); - return RAII::UPtr(reinterpret_cast( - CreateInterfaceImpl(InterfaceT::GetInterfaceId(), version))); + return RAII::UPtr(reinterpret_cast(CreateInterfaceImpl(InterfaceT::GetInterfaceId(), version))); } // get latest version of interface that matches interface class - template - RAII::UPtr_t CreateInterface() { + template RAII::UPtr_t CreateInterface() { uint64_t minVerSupported = 0; uint64_t maxVerSupported = 0; - if (false == GetSupportedVersions(InterfaceT::GetInterfaceId(), - minVerSupported, maxVerSupported)) { + if (false == GetSupportedVersions(InterfaceT::GetInterfaceId(), minVerSupported, maxVerSupported)) { // interface not supported return RAII::UPtr(nullptr); } - if ((InterfaceT::GetVersion() < minVerSupported) || - (InterfaceT::GetVersion() > maxVerSupported)) { + if ((InterfaceT::GetVersion() < minVerSupported) || (InterfaceT::GetVersion() > maxVerSupported)) { // interface version not supported return RAII::UPtr(nullptr); } // get latest compatible - uint64_t chosenVersion = - std::min(maxVerSupported, InterfaceT::GetVersion()); + uint64_t chosenVersion = std::min(maxVerSupported, InterfaceT::GetVersion()); - return RAII::UPtr(reinterpret_cast( - CreateInterfaceImpl(InterfaceT::GetInterfaceId(), chosenVersion))); + return RAII::UPtr(reinterpret_cast(CreateInterfaceImpl(InterfaceT::GetInterfaceId(), chosenVersion))); } protected: @@ -119,97 +104,86 @@ struct ICIF { virtual ~ICIF() = default; virtual ICIF *CreateInterfaceImpl(InterfaceId_t intId, Version_t version) { - return nullptr; // by default : no sub-interface are supported + return nullptr; // by default : no sub-interface are supported } }; -template -struct NamedCIF : public BaseClass { +template struct NamedCIF : public BaseClass { static constexpr InterfaceId_t GetInterfaceId() { return id; }; }; -template