Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Dialect/ESI/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ set(ESICppRuntimeSources
${CMAKE_CURRENT_SOURCE_DIR}/cpp/lib/Ports.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/lib/Types.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/lib/Utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/lib/Values.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/lib/backends/Trace.cpp
)
set(ESICppRuntimeHeaders
Expand All @@ -141,6 +142,7 @@ set(ESICppRuntimeHeaders
${CMAKE_CURRENT_SOURCE_DIR}/cpp/include/esi/Engines.h
${CMAKE_CURRENT_SOURCE_DIR}/cpp/include/esi/Logging.h
${CMAKE_CURRENT_SOURCE_DIR}/cpp/include/esi/Manifest.h
${CMAKE_CURRENT_SOURCE_DIR}/cpp/include/esi/Values.h
${CMAKE_CURRENT_SOURCE_DIR}/cpp/include/esi/Types.h
${CMAKE_CURRENT_SOURCE_DIR}/cpp/include/esi/Ports.h
${CMAKE_CURRENT_SOURCE_DIR}/cpp/include/esi/Services.h
Expand Down
6 changes: 5 additions & 1 deletion lib/Dialect/ESI/runtime/cpp/include/esi/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct AppID {
return name == other.name && idx == other.idx;
}
bool operator!=(const AppID &other) const { return !(*this == other); }
friend std::ostream &operator<<(std::ostream &os, const AppID &id);
};
bool operator<(const AppID &a, const AppID &b);

Expand All @@ -53,6 +54,7 @@ class AppIDPath : public std::vector<AppID> {
AppIDPath operator+(const AppIDPath &b) const;
AppIDPath parent() const;
std::string toStr() const;
friend std::ostream &operator<<(std::ostream &os, const AppIDPath &path);
};
bool operator<(const AppIDPath &a, const AppIDPath &b);

Expand Down Expand Up @@ -106,6 +108,8 @@ class MessageData {
public:
/// Adopts the data vector buffer.
MessageData() = default;
MessageData(std::span<const uint8_t> data)
: data(data.data(), data.data() + data.size()) {}
MessageData(std::vector<uint8_t> &data) : data(std::move(data)) {}
MessageData(std::vector<uint8_t> &&data) : data(std::move(data)) {}
MessageData(const uint8_t *data, size_t size) : data(data, data + size) {}
Expand All @@ -126,6 +130,7 @@ class MessageData {

/// Get the size of the data in bytes.
size_t getSize() const { return data.size(); }
size_t size() const { return getSize(); }

/// Returns true if this message contains no data.
bool empty() const { return data.empty(); }
Expand Down Expand Up @@ -158,7 +163,6 @@ class MessageData {
} // namespace esi

std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);
std::ostream &operator<<(std::ostream &, const esi::AppID &);

//===----------------------------------------------------------------------===//
// Functions which should be in the standard library.
Expand Down
2 changes: 0 additions & 2 deletions lib/Dialect/ESI/runtime/cpp/include/esi/Manifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class Manifest {

} // namespace esi

std::ostream &operator<<(std::ostream &os, const esi::AppID &id);
std::ostream &operator<<(std::ostream &, const esi::AppIDPath &);
std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);

#endif // ESI_MANIFEST_H
68 changes: 39 additions & 29 deletions lib/Dialect/ESI/runtime/cpp/include/esi/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <vector>

#include "esi/Common.h"
#include "esi/Values.h" // For BitVector / Int / UInt

namespace esi {

Expand All @@ -39,20 +40,29 @@ class Type {
ID getID() const { return id; }
virtual std::ptrdiff_t getBitWidth() const { return -1; }

/// Serialize an object to MessageData. The object should be passed as a
/// std::any to provide type erasure. Returns a MessageData containing the
/// serialized representation.
virtual MessageData serialize(const std::any &obj) const {
/// Serialize an object to a MutableBitVector (LSB-first stream). The object
/// should be passed via std::any. Implementations append fields in the order
/// they are iterated (the first serialized field occupies the
/// least-significant bits of the result).
virtual MutableBitVector serialize(const std::any &obj) const {
throw std::runtime_error("Serialization not implemented for type " + id);
}

/// Deserialize from a span of bytes to an object. Returns the deserialized
/// object as a std::any and a span to the remaining bytes.
virtual std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const {
/// Deserialize from a BitVector stream (LSB-first). Implementations consume
/// bits from 'data' in-place (via logical right shifts) and return the
/// reconstructed value. Remaining bits stay in 'data'.
virtual std::any deserialize(BitVector &data) const {
throw std::runtime_error("Deserialization not implemented for type " + id);
}

// Deserialize from a MessageData buffer. Maps the MessageData onto a
// MutableBitVector, and proceeds with regular MutableBitVector
// deserialization.
std::any deserialize(const MessageData &data) const {
auto bv = MutableBitVector(std::vector<uint8_t>(data.getData()));
return deserialize(bv);
}

/// Ensure that a std::any object is valid for this type. Throws
/// std::runtime_error if the object is not valid.
virtual void ensureValid(const std::any &obj) const {
Expand Down Expand Up @@ -101,14 +111,14 @@ class BundleType : public Type {
/// carry one values of one type.
class ChannelType : public Type {
public:
using Type::deserialize;
ChannelType(const ID &id, const Type *inner) : Type(id), inner(inner) {}
const Type *getInner() const { return inner; }
std::ptrdiff_t getBitWidth() const override { return inner->getBitWidth(); };

void ensureValid(const std::any &obj) const override;
MessageData serialize(const std::any &obj) const override;
std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const override;
MutableBitVector serialize(const std::any &obj) const override;
std::any deserialize(BitVector &data) const override;

private:
const Type *inner;
Expand All @@ -117,14 +127,14 @@ class ChannelType : public Type {
/// The "void" type is a special type which can be used to represent no type.
class VoidType : public Type {
public:
using Type::deserialize;
VoidType(const ID &id) : Type(id) {}
// 'void' is 1 bit by convention.
std::ptrdiff_t getBitWidth() const override { return 1; };

void ensureValid(const std::any &obj) const override;
MessageData serialize(const std::any &obj) const override;
std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const override;
MutableBitVector serialize(const std::any &obj) const override;
std::any deserialize(BitVector &data) const override;
};

/// The "any" type is a special type which can be used to represent any type, as
Expand Down Expand Up @@ -154,11 +164,11 @@ class BitVectorType : public Type {
class BitsType : public BitVectorType {
public:
using BitVectorType::BitVectorType;
using Type::deserialize;

void ensureValid(const std::any &obj) const override;
MessageData serialize(const std::any &obj) const override;
std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const override;
MutableBitVector serialize(const std::any &obj) const override;
std::any deserialize(BitVector &data) const override;
};

/// Integers are bit vectors which may be signed or unsigned and are interpreted
Expand All @@ -172,28 +182,29 @@ class IntegerType : public BitVectorType {
class SIntType : public IntegerType {
public:
using IntegerType::IntegerType;
using Type::deserialize;

void ensureValid(const std::any &obj) const override;
MessageData serialize(const std::any &obj) const override;
std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const override;
MutableBitVector serialize(const std::any &obj) const override;
std::any deserialize(BitVector &data) const override;
};

/// Unsigned integer.
class UIntType : public IntegerType {
public:
using IntegerType::IntegerType;
using Type::deserialize;

void ensureValid(const std::any &obj) const override;
MessageData serialize(const std::any &obj) const override;
std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const override;
MutableBitVector serialize(const std::any &obj) const override;
std::any deserialize(BitVector &data) const override;
};

/// Structs are an ordered collection of fields, each with a name and a type.
class StructType : public Type {
public:
using FieldVector = std::vector<std::pair<std::string, const Type *>>;
using Type::deserialize;

StructType(const ID &id, const FieldVector &fields, bool reverse = true)
: Type(id), fields(fields), reverse(reverse) {}
Expand All @@ -211,9 +222,8 @@ class StructType : public Type {
}

void ensureValid(const std::any &obj) const override;
MessageData serialize(const std::any &obj) const override;
std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const override;
MutableBitVector serialize(const std::any &obj) const override;
std::any deserialize(BitVector &data) const override;

// Returns whether this struct type should be reversed when
// serializing/deserializing.
Expand All @@ -233,6 +243,7 @@ class ArrayType : public Type {
ArrayType(const ID &id, const Type *elementType, uint64_t size,
bool reverse = true)
: Type(id), elementType(elementType), size(size), reverse(reverse) {}
using Type::deserialize;

const Type *getElementType() const { return elementType; }
uint64_t getSize() const { return size; }
Expand All @@ -245,9 +256,8 @@ class ArrayType : public Type {
}

void ensureValid(const std::any &obj) const override;
MessageData serialize(const std::any &obj) const override;
std::pair<std::any, std::span<const uint8_t>>
deserialize(std::span<const uint8_t> data) const override;
MutableBitVector serialize(const std::any &obj) const override;
std::any deserialize(BitVector &data) const override;

private:
const Type *elementType;
Expand Down
Loading