Skip to content

Commit 897eb57

Browse files
committed
[ORC-RT] ExecutorAddrDiff ergonomic improvements; contains and overlaps methods
Renames StartAddress and EndAddress members to Start and End. Adds contains and overlap methods. Adds a constructor from an address and size. These changes are counterparts to LLVM commits ef391df, c0d8899, and 37f1b7a.
1 parent a892c0e commit 897eb57

File tree

5 files changed

+119
-26
lines changed

5 files changed

+119
-26
lines changed

compiler-rt/lib/orc/elfnix_platform.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Error validatePointerSectionExtent(const char *SectionName,
4141
if (SE.size().getValue() % sizeof(uintptr_t)) {
4242
std::ostringstream ErrMsg;
4343
ErrMsg << std::hex << "Size of " << SectionName << " 0x"
44-
<< SE.StartAddress.getValue() << " -- 0x" << SE.EndAddress.getValue()
44+
<< SE.Start.getValue() << " -- 0x" << SE.End.getValue()
4545
<< " is not a pointer multiple";
4646
return make_error<StringError>(ErrMsg.str());
4747
}
@@ -166,10 +166,10 @@ void ELFNixPlatformRuntimeState::destroy() {
166166

167167
Error ELFNixPlatformRuntimeState::registerObjectSections(
168168
ELFNixPerObjectSectionsToRegister POSR) {
169-
if (POSR.EHFrameSection.StartAddress)
170-
__register_frame(POSR.EHFrameSection.StartAddress.toPtr<const char *>());
169+
if (POSR.EHFrameSection.Start)
170+
__register_frame(POSR.EHFrameSection.Start.toPtr<const char *>());
171171

172-
if (POSR.ThreadDataSection.StartAddress) {
172+
if (POSR.ThreadDataSection.Start) {
173173
if (auto Err = registerThreadDataSection(
174174
POSR.ThreadDataSection.toSpan<const char>()))
175175
return Err;
@@ -180,8 +180,8 @@ Error ELFNixPlatformRuntimeState::registerObjectSections(
180180

181181
Error ELFNixPlatformRuntimeState::deregisterObjectSections(
182182
ELFNixPerObjectSectionsToRegister POSR) {
183-
if (POSR.EHFrameSection.StartAddress)
184-
__deregister_frame(POSR.EHFrameSection.StartAddress.toPtr<const char *>());
183+
if (POSR.EHFrameSection.Start)
184+
__deregister_frame(POSR.EHFrameSection.Start.toPtr<const char *>());
185185

186186
return Error::success();
187187
}

compiler-rt/lib/orc/executor_address.h

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,35 @@ inline ExecutorAddr operator+(const ExecutorAddrDiff &LHS,
135135
/// Represents an address range in the exceutor process.
136136
struct ExecutorAddrRange {
137137
ExecutorAddrRange() = default;
138-
ExecutorAddrRange(ExecutorAddr StartAddress, ExecutorAddr EndAddress)
139-
: StartAddress(StartAddress), EndAddress(EndAddress) {}
138+
ExecutorAddrRange(ExecutorAddr Start, ExecutorAddr End)
139+
: Start(Start), End(End) {}
140+
ExecutorAddrRange(ExecutorAddr Start, ExecutorAddrDiff Size)
141+
: Start(Start), End(Start + Size) {}
140142

141-
bool empty() const { return StartAddress == EndAddress; }
142-
ExecutorAddrDiff size() const { return EndAddress - StartAddress; }
143+
bool empty() const { return Start == End; }
144+
ExecutorAddrDiff size() const { return End - Start; }
145+
146+
friend bool operator==(const ExecutorAddrRange &LHS,
147+
const ExecutorAddrRange &RHS) {
148+
return LHS.Start == RHS.Start && LHS.End == RHS.End;
149+
}
150+
friend bool operator!=(const ExecutorAddrRange &LHS,
151+
const ExecutorAddrRange &RHS) {
152+
return !(LHS == RHS);
153+
}
154+
bool contains(ExecutorAddr Addr) const { return Start <= Addr && Addr < End; }
155+
bool overlaps(const ExecutorAddrRange &Other) {
156+
return !(Other.End <= Start || End <= Other.Start);
157+
}
143158

144159
template <typename T> span<T> toSpan() const {
145160
assert(size().getValue() % sizeof(T) == 0 &&
146161
"AddressRange is not a multiple of sizeof(T)");
147-
return span<T>(StartAddress.toPtr<T *>(), size().getValue() / sizeof(T));
162+
return span<T>(Start.toPtr<T *>(), size().getValue() / sizeof(T));
148163
}
149164

150-
ExecutorAddr StartAddress;
151-
ExecutorAddr EndAddress;
165+
ExecutorAddr Start;
166+
ExecutorAddr End;
152167
};
153168

154169
/// SPS serializatior for ExecutorAddr.
@@ -178,18 +193,18 @@ template <>
178193
class SPSSerializationTraits<SPSExecutorAddrRange, ExecutorAddrRange> {
179194
public:
180195
static size_t size(const ExecutorAddrRange &Value) {
181-
return SPSArgList<SPSExecutorAddr, SPSExecutorAddr>::size(
182-
Value.StartAddress, Value.EndAddress);
196+
return SPSArgList<SPSExecutorAddr, SPSExecutorAddr>::size(Value.Start,
197+
Value.End);
183198
}
184199

185200
static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddrRange &Value) {
186201
return SPSArgList<SPSExecutorAddr, SPSExecutorAddr>::serialize(
187-
BOB, Value.StartAddress, Value.EndAddress);
202+
BOB, Value.Start, Value.End);
188203
}
189204

190205
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddrRange &Value) {
191206
return SPSArgList<SPSExecutorAddr, SPSExecutorAddr>::deserialize(
192-
BIB, Value.StartAddress, Value.EndAddress);
207+
BIB, Value.Start, Value.End);
193208
}
194209
};
195210

compiler-rt/lib/orc/macho_platform.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Error validatePointerSectionExtent(const char *SectionName,
9393
if (SE.size().getValue() % sizeof(uintptr_t)) {
9494
std::ostringstream ErrMsg;
9595
ErrMsg << std::hex << "Size of " << SectionName << " 0x"
96-
<< SE.StartAddress.getValue() << " -- 0x" << SE.EndAddress.getValue()
96+
<< SE.Start.getValue() << " -- 0x" << SE.End.getValue()
9797
<< " is not a pointer multiple";
9898
return make_error<StringError>(ErrMsg.str());
9999
}
@@ -113,7 +113,7 @@ Error registerObjCSelectors(
113113
return Err;
114114

115115
fprintf(stderr, "Processing selrefs section at 0x%llx\n",
116-
ObjCSelRefs.StartAddress.getValue());
116+
ObjCSelRefs.Start.getValue());
117117
for (uintptr_t SelEntry : ObjCSelRefs.toSpan<uintptr_t>()) {
118118
const char *SelName = reinterpret_cast<const char *>(SelEntry);
119119
fprintf(stderr, "Registering selector \"%s\"\n", SelName);
@@ -179,8 +179,8 @@ Error registerSwift5Protocols(
179179

180180
for (const auto &Swift5Protocols : Swift5ProtocolSections)
181181
swift_registerProtocols(
182-
Swift5Protocols.StartAddress.toPtr<const ProtocolRecord *>(),
183-
Swift5Protocols.EndAddress.toPtr<const ProtocolRecord *>());
182+
Swift5Protocols.Start.toPtr<const ProtocolRecord *>(),
183+
Swift5Protocols.End.toPtr<const ProtocolRecord *>());
184184

185185
return Error::success();
186186
}
@@ -196,8 +196,8 @@ Error registerSwift5ProtocolConformances(
196196

197197
for (const auto &ProtoConfSec : Swift5ProtocolConformanceSections)
198198
swift_registerProtocolConformances(
199-
ProtoConfSec.StartAddress.toPtr<const ProtocolConformanceRecord *>(),
200-
ProtoConfSec.EndAddress.toPtr<const ProtocolConformanceRecord *>());
199+
ProtoConfSec.Start.toPtr<const ProtocolConformanceRecord *>(),
200+
ProtoConfSec.End.toPtr<const ProtocolConformanceRecord *>());
201201

202202
return Error::success();
203203
}
@@ -325,11 +325,11 @@ void MachOPlatformRuntimeState::destroy() {
325325

326326
Error MachOPlatformRuntimeState::registerObjectSections(
327327
MachOPerObjectSectionsToRegister POSR) {
328-
if (POSR.EHFrameSection.StartAddress)
328+
if (POSR.EHFrameSection.Start)
329329
walkEHFrameSection(POSR.EHFrameSection.toSpan<const char>(),
330330
__register_frame);
331331

332-
if (POSR.ThreadDataSection.StartAddress) {
332+
if (POSR.ThreadDataSection.Start) {
333333
if (auto Err = registerThreadDataSection(
334334
POSR.ThreadDataSection.toSpan<const char>()))
335335
return Err;
@@ -340,7 +340,7 @@ Error MachOPlatformRuntimeState::registerObjectSections(
340340

341341
Error MachOPlatformRuntimeState::deregisterObjectSections(
342342
MachOPerObjectSectionsToRegister POSR) {
343-
if (POSR.EHFrameSection.StartAddress)
343+
if (POSR.EHFrameSection.Start)
344344
walkEHFrameSection(POSR.EHFrameSection.toSpan<const char>(),
345345
__deregister_frame);
346346

compiler-rt/lib/orc/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(UNITTEST_SOURCES
8484
c_api_test.cpp
8585
endian_test.cpp
8686
error_test.cpp
87+
executor_address_test.cpp
8788
extensible_rtti_test.cpp
8889
orc_unit_test_main.cpp
8990
stl_extras_test.cpp
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===-- extensible_address_test.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+
// This file is a part of the ORC runtime.
10+
//
11+
// Note:
12+
// This unit test was adapted from
13+
// llvm/unittests/Support/ExecutorAddressTest.cpp
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "executor_address.h"
18+
#include "gtest/gtest.h"
19+
20+
using namespace __orc_rt;
21+
22+
TEST(ExecutorAddrTest, DefaultAndNull) {
23+
// Check that default constructed values and isNull behave as expected.
24+
25+
ExecutorAddr Default;
26+
ExecutorAddr Null(0);
27+
ExecutorAddr NonNull(1);
28+
29+
EXPECT_TRUE(Null.isNull());
30+
EXPECT_EQ(Default, Null);
31+
32+
EXPECT_FALSE(NonNull.isNull());
33+
EXPECT_NE(Default, NonNull);
34+
}
35+
36+
TEST(ExecutorAddrTest, Ordering) {
37+
// Check that ordering operations.
38+
ExecutorAddr A1(1), A2(2);
39+
40+
EXPECT_LE(A1, A1);
41+
EXPECT_LT(A1, A2);
42+
EXPECT_GT(A2, A1);
43+
EXPECT_GE(A2, A2);
44+
}
45+
46+
TEST(ExecutorAddrTest, PtrConversion) {
47+
// Test toPtr / fromPtr round-tripping.
48+
int X = 0;
49+
auto XAddr = ExecutorAddr::fromPtr(&X);
50+
int *XPtr = XAddr.toPtr<int *>();
51+
52+
EXPECT_EQ(XPtr, &X);
53+
}
54+
55+
TEST(ExecutorAddrTest, AddrRanges) {
56+
ExecutorAddr A0(0), A1(1), A2(2), A3(3);
57+
ExecutorAddrRange R0(A0, A1), R1(A1, A2), R2(A2, A3), R3(A0, A2), R4(A1, A3);
58+
// 012
59+
// R0: # -- Before R1
60+
// R1: # --
61+
// R2: # -- After R1
62+
// R3: ## -- Overlaps R1 start
63+
// R4: ## -- Overlaps R1 end
64+
65+
EXPECT_EQ(R1, ExecutorAddrRange(A1, A2));
66+
EXPECT_EQ(R1, ExecutorAddrRange(A1, ExecutorAddrDiff(1)));
67+
EXPECT_NE(R1, R2);
68+
69+
EXPECT_TRUE(R1.contains(A1));
70+
EXPECT_FALSE(R1.contains(A0));
71+
EXPECT_FALSE(R1.contains(A2));
72+
73+
EXPECT_FALSE(R1.overlaps(R0));
74+
EXPECT_FALSE(R1.overlaps(R2));
75+
EXPECT_TRUE(R1.overlaps(R3));
76+
EXPECT_TRUE(R1.overlaps(R4));
77+
}

0 commit comments

Comments
 (0)