Skip to content

Commit c0d8899

Browse files
committed
[ORC] Add 'contains' and 'overlaps' operations to ExecutorAddrRange.
Also includes unit tests for not-yet tested operations like comparison and to/from pointer conversion.
1 parent a9ae243 commit c0d8899

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class ExecutorAddrDiff {
3737
class ExecutorAddr {
3838
public:
3939
ExecutorAddr() = default;
40+
41+
/// Create an ExecutorAddr from the given value.
4042
explicit ExecutorAddr(uint64_t Addr) : Addr(Addr) {}
4143

4244
/// Create an ExecutorAddr from the given pointer.
@@ -137,6 +139,19 @@ struct ExecutorAddrRange {
137139
bool empty() const { return Start == End; }
138140
ExecutorAddrDiff size() const { return End - Start; }
139141

142+
friend bool operator==(const ExecutorAddrRange &LHS,
143+
const ExecutorAddrRange &RHS) {
144+
return LHS.Start == RHS.Start && LHS.End == RHS.End;
145+
}
146+
friend bool operator!=(const ExecutorAddrRange &LHS,
147+
const ExecutorAddrRange &RHS) {
148+
return !(LHS == RHS);
149+
}
150+
bool contains(ExecutorAddr Addr) const { return Start <= Addr && Addr < End; }
151+
bool overlaps(const ExecutorAddrRange &Other) {
152+
return !(Other.End <= Start || End <= Other.Start);
153+
}
154+
140155
ExecutorAddr Start;
141156
ExecutorAddr End;
142157
};

llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
1616

1717
add_llvm_unittest(OrcJITTests
1818
CoreAPIsTest.cpp
19+
ExecutorAddressTest.cpp
1920
ExecutionSessionWrapperFunctionCallsTest.cpp
2021
EPCGenericJITLinkMemoryManagerTest.cpp
2122
EPCGenericMemoryAccessTest.cpp
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===--------- ExecutorAddrTest.cpp - Unit tests for ExecutorAddr ---------===//
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+
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
10+
#include "OrcTestCommon.h"
11+
12+
using namespace llvm;
13+
using namespace llvm::orc;
14+
15+
namespace {
16+
17+
TEST(ExecutorAddrTest, DefaultAndNull) {
18+
// Check that default constructed values and isNull behave as expected.
19+
20+
ExecutorAddr Default;
21+
ExecutorAddr Null(0);
22+
ExecutorAddr NonNull(1);
23+
24+
EXPECT_TRUE(Null.isNull());
25+
EXPECT_EQ(Default, Null);
26+
27+
EXPECT_FALSE(NonNull.isNull());
28+
EXPECT_NE(Default, NonNull);
29+
}
30+
31+
TEST(ExecutorAddrTest, Ordering) {
32+
// Check that ordering operations.
33+
ExecutorAddr A1(1), A2(2);
34+
35+
EXPECT_LE(A1, A1);
36+
EXPECT_LT(A1, A2);
37+
EXPECT_GT(A2, A1);
38+
EXPECT_GE(A2, A2);
39+
}
40+
41+
TEST(ExecutorAddrTest, PtrConversion) {
42+
// Test toPtr / fromPtr round-tripping.
43+
int X = 0;
44+
auto XAddr = ExecutorAddr::fromPtr(&X);
45+
int *XPtr = XAddr.toPtr<int *>();
46+
47+
EXPECT_EQ(XPtr, &X);
48+
}
49+
50+
TEST(ExecutorAddrTest, AddrRanges) {
51+
ExecutorAddr A0(0), A1(1), A2(2), A3(3);
52+
ExecutorAddrRange R0(A0, A1), R1(A1, A2), R2(A2, A3), R3(A0, A2), R4(A1, A3);
53+
// 012
54+
// R0: # -- Before R1
55+
// R1: # --
56+
// R2: # -- After R1
57+
// R3: ## -- Overlaps R1 start
58+
// R4: ## -- Overlaps R1 end
59+
60+
EXPECT_EQ(R1, ExecutorAddrRange(A1, A2));
61+
EXPECT_NE(R1, R2);
62+
63+
EXPECT_TRUE(R1.contains(A1));
64+
EXPECT_FALSE(R1.contains(A0));
65+
EXPECT_FALSE(R1.contains(A2));
66+
67+
EXPECT_FALSE(R1.overlaps(R0));
68+
EXPECT_FALSE(R1.overlaps(R2));
69+
EXPECT_TRUE(R1.overlaps(R3));
70+
EXPECT_TRUE(R1.overlaps(R4));
71+
}
72+
73+
} // namespace

0 commit comments

Comments
 (0)