Skip to content

Commit fac87b8

Browse files
[llvm-exegesis] Switch from intptr_t to uintptr_t in most cases (#102860)
This patch switches most of the uses of intptr_t to uintptr_t within llvm-exegesis for the subprocess memory support. In the vast majority of cases we do not want a signed component of the address, hence making intptr_t undesirable. intptr_t is left for error handling, for example when making syscalls and we need to see if the syscall returned -1.
1 parent c349ded commit fac87b8

File tree

12 files changed

+36
-34
lines changed

12 files changed

+36
-34
lines changed

llvm/tools/llvm-exegesis/lib/Assembler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ExecutableFunction {
115115

116116
// Executes the function.
117117
void operator()(char *Memory) const {
118-
((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
118+
((void (*)(char *))(uintptr_t)FunctionBytes.data())(Memory);
119119
}
120120

121121
StringRef FunctionBytes;

llvm/tools/llvm-exegesis/lib/BenchmarkResult.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct MemoryValue {
5353

5454
struct MemoryMapping {
5555
// The address to place the mapping at.
56-
intptr_t Address;
56+
uintptr_t Address;
5757
// The name of the value that should be mapped.
5858
std::string MemoryValueName;
5959
};
@@ -73,7 +73,7 @@ struct BenchmarkKey {
7373
std::string Config;
7474
// The address that the snippet should be loaded in at if the execution mode
7575
// being used supports it.
76-
intptr_t SnippetAddress = 0;
76+
uintptr_t SnippetAddress = 0;
7777
// The register that should be used to hold the loop counter.
7878
unsigned LoopRegister;
7979
};

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class SubProcessFunctionExecutorImpl
379379

380380
if (ChildSignalInfo.si_signo == SIGSEGV)
381381
return make_error<SnippetSegmentationFault>(
382-
reinterpret_cast<intptr_t>(ChildSignalInfo.si_addr));
382+
reinterpret_cast<uintptr_t>(ChildSignalInfo.si_addr));
383383

384384
return make_error<SnippetSignal>(ChildSignalInfo.si_signo);
385385
}
@@ -477,9 +477,10 @@ class SubProcessFunctionExecutorImpl
477477
if (__rseq_size < 32)
478478
RseqStructSize = 32;
479479

480-
long RseqDisableOutput =
481-
syscall(SYS_rseq, (intptr_t)__builtin_thread_pointer() + __rseq_offset,
482-
RseqStructSize, RSEQ_FLAG_UNREGISTER, RSEQ_SIG);
480+
long RseqDisableOutput = syscall(
481+
SYS_rseq,
482+
reinterpret_cast<uintptr_t>(__builtin_thread_pointer()) + __rseq_offset,
483+
RseqStructSize, RSEQ_FLAG_UNREGISTER, RSEQ_SIG);
483484
if (RseqDisableOutput != 0)
484485
exit(ChildProcessExitCodeE::RSeqDisableFailed);
485486
#endif // GLIBC_INITS_RSEQ
@@ -502,7 +503,7 @@ class SubProcessFunctionExecutorImpl
502503
char *FunctionDataCopy =
503504
(char *)mmap(MapAddress, FunctionDataCopySize, PROT_READ | PROT_WRITE,
504505
MapFlags, 0, 0);
505-
if ((intptr_t)FunctionDataCopy == -1)
506+
if (reinterpret_cast<intptr_t>(FunctionDataCopy) == -1)
506507
exit(ChildProcessExitCodeE::FunctionDataMappingFailed);
507508

508509
memcpy(FunctionDataCopy, this->Function.FunctionBytes.data(),
@@ -515,8 +516,8 @@ class SubProcessFunctionExecutorImpl
515516
if (!AuxMemFDOrError)
516517
exit(ChildProcessExitCodeE::AuxiliaryMemorySetupFailed);
517518

518-
((void (*)(size_t, int))(intptr_t)FunctionDataCopy)(FunctionDataCopySize,
519-
*AuxMemFDOrError);
519+
((void (*)(size_t, int))(uintptr_t)FunctionDataCopy)(FunctionDataCopySize,
520+
*AuxMemFDOrError);
520521

521522
exit(0);
522523
}

llvm/tools/llvm-exegesis/lib/Error.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ class SnippetExecutionFailure : public ErrorInfo<SnippetExecutionFailure> {
5252
class SnippetSegmentationFault : public SnippetExecutionFailure {
5353
public:
5454
static char ID;
55-
SnippetSegmentationFault(intptr_t SegFaultAddress)
56-
: Address(SegFaultAddress){};
55+
SnippetSegmentationFault(uintptr_t SegFaultAddress)
56+
: Address(SegFaultAddress) {};
5757

58-
intptr_t getAddress() { return Address; }
58+
uintptr_t getAddress() { return Address; }
5959

6060
void log(raw_ostream &OS) const override;
6161

6262
private:
63-
intptr_t Address;
63+
uintptr_t Address;
6464
};
6565

6666
// A class representing all other non-specific failures that happen during

llvm/tools/llvm-exegesis/lib/SnippetFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class BenchmarkCodeStreamer : public MCStreamer, public AsmCommentConsumer {
151151
}
152152
if (CommentText.consume_front("SNIPPET-ADDRESS")) {
153153
// LLVM-EXEGESIS-SNIPPET-ADDRESS <address>
154-
if (!to_integer<intptr_t>(CommentText.trim(), Result->Key.SnippetAddress,
155-
16)) {
154+
if (!to_integer<uintptr_t>(CommentText.trim(), Result->Key.SnippetAddress,
155+
16)) {
156156
errs() << "invalid comment 'LLVM-EXEGESIS-SNIPPET-ADDRESS "
157157
<< CommentText
158158
<< "', expected <ADDRESS> to contain a valid integer in "

llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Expected<int> SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
126126
int *AuxiliaryMemoryMapping =
127127
(int *)mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED,
128128
AuxiliaryMemoryFileDescriptor, 0);
129-
if ((intptr_t)AuxiliaryMemoryMapping == -1)
129+
if (reinterpret_cast<intptr_t>(AuxiliaryMemoryMapping) == -1)
130130
return make_error<Failure>("Mapping auxiliary memory failed");
131131
AuxiliaryMemoryMapping[0] = CounterFileDescriptor;
132132
for (auto &[Name, MemVal] : MemoryDefinitions) {

llvm/tools/llvm-exegesis/lib/Target.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ class ExegesisTarget {
118118
// Generates the code to mmap a region of code. The code generated by this
119119
// function may clobber registers.
120120
virtual std::vector<MCInst>
121-
generateMmap(intptr_t Address, size_t Length,
122-
intptr_t FileDescriptorAddress) const {
121+
generateMmap(uintptr_t Address, size_t Length,
122+
uintptr_t FileDescriptorAddress) const {
123123
report_fatal_error(
124124
"generateMmap is not implemented on the current architecture");
125125
}
@@ -161,7 +161,7 @@ class ExegesisTarget {
161161
"current architectures");
162162
}
163163

164-
virtual intptr_t getAuxiliaryMemoryStartAddress() const {
164+
virtual uintptr_t getAuxiliaryMemoryStartAddress() const {
165165
report_fatal_error("getAuxiliaryMemoryStartAddress is not implemented on "
166166
"the current architecture");
167167
}

llvm/tools/llvm-exegesis/lib/X86/Target.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,8 @@ class ExegesisX86Target : public ExegesisTarget {
747747
std::vector<MCInst> generateExitSyscall(unsigned ExitCode) const override;
748748

749749
std::vector<MCInst>
750-
generateMmap(intptr_t Address, size_t Length,
751-
intptr_t FileDescriptorAddress) const override;
750+
generateMmap(uintptr_t Address, size_t Length,
751+
uintptr_t FileDescriptorAddress) const override;
752752

753753
void generateMmapAuxMem(std::vector<MCInst> &GeneratedCode) const override;
754754

@@ -758,7 +758,7 @@ class ExegesisX86Target : public ExegesisTarget {
758758

759759
std::vector<MCInst> setStackRegisterToAuxMem() const override;
760760

761-
intptr_t getAuxiliaryMemoryStartAddress() const override;
761+
uintptr_t getAuxiliaryMemoryStartAddress() const override;
762762

763763
std::vector<MCInst> configurePerfCounter(long Request, bool SaveRegisters) const override;
764764

@@ -1091,9 +1091,9 @@ std::vector<MCInst> ExegesisX86Target::setRegTo(const MCSubtargetInfo &STI,
10911091
#ifdef __linux__
10921092

10931093
#ifdef __arm__
1094-
static constexpr const intptr_t VAddressSpaceCeiling = 0xC0000000;
1094+
static constexpr const uintptr_t VAddressSpaceCeiling = 0xC0000000;
10951095
#else
1096-
static constexpr const intptr_t VAddressSpaceCeiling = 0x0000800000000000;
1096+
static constexpr const uintptr_t VAddressSpaceCeiling = 0x0000800000000000;
10971097
#endif
10981098

10991099
void generateRoundToNearestPage(unsigned int Register,
@@ -1180,8 +1180,8 @@ ExegesisX86Target::generateExitSyscall(unsigned ExitCode) const {
11801180
}
11811181

11821182
std::vector<MCInst>
1183-
ExegesisX86Target::generateMmap(intptr_t Address, size_t Length,
1184-
intptr_t FileDescriptorAddress) const {
1183+
ExegesisX86Target::generateMmap(uintptr_t Address, size_t Length,
1184+
uintptr_t FileDescriptorAddress) const {
11851185
std::vector<MCInst> MmapCode;
11861186
MmapCode.push_back(loadImmediate(X86::RDI, 64, APInt(64, Address)));
11871187
MmapCode.push_back(loadImmediate(X86::RSI, 64, APInt(64, Length)));
@@ -1249,7 +1249,7 @@ std::vector<MCInst> ExegesisX86Target::setStackRegisterToAuxMem() const {
12491249
SubprocessMemory::AuxiliaryMemorySize)};
12501250
}
12511251

1252-
intptr_t ExegesisX86Target::getAuxiliaryMemoryStartAddress() const {
1252+
uintptr_t ExegesisX86Target::getAuxiliaryMemoryStartAddress() const {
12531253
// Return the second to last page in the virtual address space to try and
12541254
// prevent interference with memory annotations in the snippet
12551255
return VAddressSpaceCeiling - 2 * getpagesize();

llvm/unittests/tools/llvm-exegesis/BenchmarkRunnerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace {
1717

1818
TEST(ScratchSpaceTest, Works) {
1919
BenchmarkRunner::ScratchSpace Space;
20-
EXPECT_EQ(reinterpret_cast<intptr_t>(Space.ptr()) %
20+
EXPECT_EQ(reinterpret_cast<uintptr_t>(Space.ptr()) %
2121
BenchmarkRunner::ScratchSpace::kAlignment,
2222
0u);
2323
Space.ptr()[0] = 42;

llvm/unittests/tools/llvm-exegesis/X86/SnippetFileTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ MATCHER_P3(MemoryDefinitionIs, Name, Value, Size, "") {
8181
}
8282

8383
MATCHER_P2(MemoryMappingIs, Address, Name, "") {
84-
if (arg.Address == Address && arg.MemoryValueName == Name)
84+
if (arg.Address == static_cast<uintptr_t>(Address) &&
85+
arg.MemoryValueName == Name)
8586
return true;
8687
*result_listener << "expected: {" << Address << ", " << Name << "} ";
8788
*result_listener << "actual: {" << arg.Address << ", " << arg.MemoryValueName
@@ -216,7 +217,7 @@ TEST_F(X86SnippetFileTest, SnippetAddress) {
216217
ASSERT_TRUE(static_cast<bool>(Snippets));
217218
EXPECT_THAT(*Snippets, SizeIs(1));
218219
const auto &Snippet = (*Snippets)[0];
219-
EXPECT_EQ(Snippet.Key.SnippetAddress, 0x10000);
220+
EXPECT_EQ(Snippet.Key.SnippetAddress, static_cast<uintptr_t>(0x10000));
220221
}
221222

222223
TEST_F(X86SnippetFileTest, LoopRegister) {

llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class SubprocessMemoryTest : public X86TestBase {
6464
shm_open(DefinitionName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
6565
uint8_t *SharedMemoryMapping = (uint8_t *)mmap(
6666
NULL, DefinitionSize, PROT_READ, MAP_SHARED, SharedMemoryFD, 0);
67-
EXPECT_NE((intptr_t)SharedMemoryMapping, -1);
67+
EXPECT_NE(reinterpret_cast<intptr_t>(SharedMemoryMapping), -1);
6868
for (size_t I = 0; I < ExpectedValue.size(); ++I) {
6969
EXPECT_EQ(SharedMemoryMapping[I], ExpectedValue[I]);
7070
}

llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ TEST_F(X86Core2TargetTest, GenerateLowerMunmapTest) {
599599
}
600600

601601
#ifdef __arm__
602-
static constexpr const intptr_t VAddressSpaceCeiling = 0xC0000000;
602+
static constexpr const uintptr_t VAddressSpaceCeiling = 0xC0000000;
603603
#else
604-
static constexpr const intptr_t VAddressSpaceCeiling = 0x0000800000000000;
604+
static constexpr const uintptr_t VAddressSpaceCeiling = 0x0000800000000000;
605605
#endif
606606

607607
TEST_F(X86Core2TargetTest, GenerateUpperMunmapTest) {

0 commit comments

Comments
 (0)