CIRCT 24.0.0git
Loading...
Searching...
No Matches
Common.cpp
Go to the documentation of this file.
1//===- Common.cpp ---------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DO NOT EDIT!
10// This file is distributed as part of an ESI package. The source for this file
11// should always be modified within CIRCT (lib/dialect/ESI/runtime/cpp/).
12//
13//===----------------------------------------------------------------------===//
14
15#include "esi/Common.h"
16#include "esi/Services.h"
17
18#include <iostream>
19#include <sstream>
20
21using namespace esi;
22
23std::string MessageData::toHex() const {
24 std::ostringstream ss;
25 ss << std::hex;
26 for (size_t i = 0, e = data.size(); i != e; ++i) {
27 // Add spaces every 8 bytes.
28 if (i % 8 == 0 && i != 0)
29 ss << ' ';
30 // Add an extra space every 64 bytes.
31 if (i % 64 == 0 && i != 0)
32 ss << ' ';
33 ss << static_cast<unsigned>(data[i]);
34 }
35 return ss.str();
36}
37
38std::string esi::toHex(void *val) {
39 return toHex(reinterpret_cast<uint64_t>(val));
40}
41
42std::string esi::toHex(uint64_t val) {
43 std::ostringstream ss;
44 ss << std::hex << val;
45 return ss.str();
46}
47
48//===----------------------------------------------------------------------===//
49// SegmentedMessageData
50//===----------------------------------------------------------------------===//
51
53 size_t total = 0;
54 for (size_t i = 0, e = numSegments(); i < e; ++i)
55 total += segment(i).size;
56 return total;
57}
58
59bool SegmentedMessageData::empty() const { return totalSize() == 0; }
60
62 size_t total = totalSize();
63 if (total == 0)
64 return MessageData();
65 std::vector<uint8_t> buf;
66 buf.reserve(total);
67 for (size_t i = 0, e = numSegments(); i < e; ++i) {
68 Segment seg = segment(i);
69 if (seg.size == 0)
70 continue;
71 buf.insert(buf.end(), seg.data, seg.data + seg.size);
72 }
73 return MessageData(std::move(buf));
74}
75
76//===----------------------------------------------------------------------===//
77// Segment
78//===----------------------------------------------------------------------===//
79
80std::optional<uint64_t> Segment::getDeviceAddress() const {
81 if (!region)
82 return std::nullopt;
83 return region->getDeviceAddress(data, size);
84}
85
86std::unique_ptr<services::HostMemRegion>
88 return nullptr;
89}
90
91//===----------------------------------------------------------------------===//
92// SegmentedMessageDataCursor
93//===----------------------------------------------------------------------===//
94
95std::span<const uint8_t> SegmentedMessageDataCursor::remaining() const {
96 return remainingSegment().span();
97}
98
100 // Scan forward past empty segments without mutating cursor state.
101 size_t idx = segIdx;
102 size_t off = offset;
103 while (idx < msg.numSegments()) {
104 Segment seg = msg.segment(idx);
105 if (seg.size > off)
106 return {seg.data + off, seg.size - off, seg.region};
107 ++idx;
108 off = 0;
109 }
110 return {nullptr, 0};
111}
112
114 while (n > 0 && !done()) {
115 Segment seg = msg.segment(segIdx);
116 size_t left = seg.size - offset;
117 if (left == 0) {
118 // Skip empty segments.
119 ++segIdx;
120 offset = 0;
121 continue;
122 }
123 if (n < left) {
124 offset += n;
125 return;
126 }
127 n -= left;
128 ++segIdx;
129 offset = 0;
130 }
131}
132
134 return segIdx >= msg.numSegments();
135}
A concrete flat message backed by a single vector of bytes.
Definition Common.h:197
std::string toHex() const
Convert the data to a hex string.
Definition Common.cpp:23
std::vector< uint8_t > data
Definition Common.h:258
Segment remainingSegment() const
The unconsumed part of the current segment, with its region.
Definition Common.cpp:99
const SegmentedMessageData & msg
Definition Common.h:291
void advance(size_t n)
Advance by n bytes, crossing segment boundaries as needed.
Definition Common.cpp:113
bool done() const
True when all segments have been consumed.
Definition Common.cpp:133
std::span< const uint8_t > remaining() const
Contiguous span from current position to end of current segment.
Definition Common.cpp:95
virtual Segment segment(size_t idx) const =0
Get a segment by index. Throws if the segment has been take()n.
virtual MessageData toMessageData() const
Flatten all segments into a standard MessageData.
Definition Common.cpp:61
bool empty() const
True if totalSize() == 0.
Definition Common.cpp:59
virtual std::unique_ptr< services::HostMemRegion > take(size_t segIdx)
Optionally called by a backend once it has transmitted segment segIdx, to obtain ownership of the Hos...
Definition Common.cpp:87
size_t totalSize() const
Total size across all segments.
Definition Common.cpp:52
virtual size_t numSegments() const =0
Number of segments in the message.
Definition esi.py:1
std::string toHex(void *val)
Definition Common.cpp:38
A contiguous, non-owning view of bytes within a SegmentedMessageData.
Definition Common.h:122
std::span< const uint8_t > span() const
Definition Common.h:131
std::optional< uint64_t > getDeviceAddress() const
Device address of this segment's bytes, if region is set and contains them; std::nullopt otherwise.
Definition Common.cpp:80
const uint8_t * data
Definition Common.h:123
services::HostMemRegion * region
The HostMem region (from services::HostMem::allocate()) which holds [data, data + size),...
Definition Common.h:129
size_t size
Definition Common.h:124