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
61const MessageData &
63 if (auto *flat = dynamic_cast<const MessageData *>(this))
64 return *flat;
65 scratch = toMessageData();
66 return scratch;
67}
68
70 size_t total = totalSize();
71 if (total == 0)
72 return MessageData();
73 std::vector<uint8_t> buf;
74 buf.reserve(total);
75 for (size_t i = 0, e = numSegments(); i < e; ++i) {
76 Segment seg = segment(i);
77 if (seg.size == 0)
78 continue;
79 buf.insert(buf.end(), seg.data, seg.data + seg.size);
80 }
81 return MessageData(std::move(buf));
82}
83
84//===----------------------------------------------------------------------===//
85// Segment
86//===----------------------------------------------------------------------===//
87
88std::optional<uint64_t> Segment::getDeviceAddress() const {
89 if (!region)
90 return std::nullopt;
91 return region->getDeviceAddress(data, size);
92}
93
94std::unique_ptr<services::HostMemRegion>
96 return nullptr;
97}
98
99//===----------------------------------------------------------------------===//
100// SegmentedMessageDataCursor
101//===----------------------------------------------------------------------===//
102
103std::span<const uint8_t> SegmentedMessageDataCursor::remaining() const {
104 return remainingSegment().span();
105}
106
108 // Scan forward past empty segments without mutating cursor state.
109 size_t idx = segIdx;
110 size_t off = offset;
111 while (idx < msg.numSegments()) {
112 Segment seg = msg.segment(idx);
113 if (seg.size > off)
114 return {seg.data + off, seg.size - off, seg.region};
115 ++idx;
116 off = 0;
117 }
118 return {nullptr, 0};
119}
120
122 while (n > 0 && !done()) {
123 Segment seg = msg.segment(segIdx);
124 size_t left = seg.size - offset;
125 if (left == 0) {
126 // Skip empty segments.
127 ++segIdx;
128 offset = 0;
129 continue;
130 }
131 if (n < left) {
132 offset += n;
133 return;
134 }
135 n -= left;
136 ++segIdx;
137 offset = 0;
138 }
139}
140
142 return segIdx >= msg.numSegments();
143}
A concrete flat message backed by a single vector of bytes.
Definition Common.h:201
std::string toHex() const
Convert the data to a hex string.
Definition Common.cpp:23
std::vector< uint8_t > data
Definition Common.h:266
Segment remainingSegment() const
The unconsumed part of the current segment, with its region.
Definition Common.cpp:107
const SegmentedMessageData & msg
Definition Common.h:299
void advance(size_t n)
Advance by n bytes, crossing segment boundaries as needed.
Definition Common.cpp:121
bool done() const
True when all segments have been consumed.
Definition Common.cpp:141
std::span< const uint8_t > remaining() const
Contiguous span from current position to end of current segment.
Definition Common.cpp:103
virtual Segment segment(size_t idx) const =0
Get a segment by index. Throws if the segment has been take()n.
const MessageData & asMessageData(MessageData &scratch) const
Return this message as a MessageData without copying if it already is one.
Definition Common.cpp:62
virtual MessageData toMessageData() const
Flatten all segments into a standard MessageData.
Definition Common.cpp:69
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:95
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:88
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