Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ class DataLayout {
PS.HasExternalState;
}

/// Returns if the null pointer for this address space has an all-zero bit
/// representation.
bool isNullPointerAllZeroes(unsigned AddrSpace) const {
return AddrSpace == 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion of getNullPointerValue() is probably better, could do something like the following?

Suggested change
bool isNullPointerAllZeroes(unsigned AddrSpace) const {
return AddrSpace == 0;
}
std::optional<APInt> getNullPointerValue(unsigned AS) const {
// Address space zero is currently defined to always have
// a all-zero null pointer representation, the others are
// target-specific and will require a data layout property.
// See https://discourse.llvm.org/t/rfc-introduce-sentinel-pointer-value-to-datalayout
if (AS == 0)
return 0;
return std::nullopt;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, it probably doesn't matter too much what we use since it will have to change again for #131557 , the main thing I would like to see is avoiding ==0 checks which this patch achieves as is so IMO no need to do something generic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, see commit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, race condition


/// Returns whether this address space has an "unstable" pointer
/// representation. The bitwise pattern of such pointers is allowed to change
/// in a target-specific way. For example, this could be used for copying
Expand Down
9 changes: 9 additions & 0 deletions llvm/unittests/IR/DataLayoutTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,15 @@ TEST(DataLayout, NonIntegralHelpers) {
}
}

TEST(DataLayoutTest, IsNullPointerAllZeroes) {
EXPECT_TRUE(DataLayout("").isNullPointerAllZeroes(0));
EXPECT_FALSE(DataLayout("").isNullPointerAllZeroes(1));
EXPECT_TRUE(DataLayout("p:32:32").isNullPointerAllZeroes(0));
EXPECT_FALSE(DataLayout("p:32:32").isNullPointerAllZeroes(1));
EXPECT_TRUE(DataLayout("p:64:64").isNullPointerAllZeroes(0));
EXPECT_FALSE(DataLayout("p:64:64").isNullPointerAllZeroes(1));
}

TEST(DataLayoutTest, CopyAssignmentInvalidatesStructLayout) {
DataLayout DL1 = cantFail(DataLayout::parse("p:32:32"));
DataLayout DL2 = cantFail(DataLayout::parse("p:64:64"));
Expand Down
Loading