Skip to content

[DataLayout] Refactor parsing of "ni" specification #104546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2024
Merged
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
57 changes: 41 additions & 16 deletions llvm/lib/IR/DataLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,27 @@ static Error reportError(const Twine &Message) {
return createStringError(inconvertibleErrorCode(), Message);
}

static Error createSpecFormatError(Twine Format) {
return createStringError("malformed specification, must be of the form \"" +
Format + "\"");
}

/// Attempts to parse an address space component of a specification,
/// commonly referred to as <n> or <address space>.
///
/// \p Name should be the name of the component as specified by LangRef,
/// it is used in diagnostic messages.
static Error parseAddrSpace(StringRef Str, unsigned &AddrSpace,
StringRef Name) {
if (Str.empty())
return createStringError(Name + " is required");

if (!to_integer(Str, AddrSpace, 10) || !isUInt<24>(AddrSpace))
return createStringError(Name + " must be a 24-bit integer");

return Error::success();
}

/// Checked version of split, to ensure mandatory subparts.
static Error split(StringRef Str, char Separator,
std::pair<StringRef, StringRef> &Split) {
Expand Down Expand Up @@ -313,6 +334,26 @@ static Error getAddrSpace(StringRef R, unsigned &AddrSpace) {
}

Error DataLayout::parseSpecification(StringRef Spec) {
// The "ni" specifier is the only two-character specifier. Handle it first.
if (Spec.starts_with("ni")) {
// ni:<address space>[:<address space>]...
StringRef Rest = Spec.drop_front(2);

// Drop the first ':', then split the rest of the string the usual way.
if (!Rest.consume_front(":"))
return createSpecFormatError("ni:<address space>[:<address space>]...");

for (StringRef Str : split(Rest, ':')) {
unsigned AddrSpace;
if (Error Err = parseAddrSpace(Str, AddrSpace, "<address space>"))
return Err;
if (AddrSpace == 0)
return createStringError("address space 0 cannot be non-integral");
NonIntegralAddressSpaces.push_back(AddrSpace);
}
return Error::success();
}

// Split at ':'.
std::pair<StringRef, StringRef> Split;
if (Error Err = ::split(Spec, ':', Split))
Expand All @@ -322,22 +363,6 @@ Error DataLayout::parseSpecification(StringRef Spec) {
StringRef &Tok = Split.first; // Current token.
StringRef &Rest = Split.second; // The rest of the string.

if (Tok == "ni") {
do {
if (Error Err = ::split(Rest, ':', Split))
return Err;
Rest = Split.second;
unsigned AS;
if (Error Err = getInt(Split.first, AS))
return Err;
if (AS == 0)
return reportError("Address space 0 can never be non-integral");
NonIntegralAddressSpaces.push_back(AS);
} while (!Rest.empty());

return Error::success();
}

char SpecifierChar = Tok.front();
Tok = Tok.substr(1);

Expand Down
40 changes: 40 additions & 0 deletions llvm/unittests/IR/DataLayoutTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,46 @@ TEST(DataLayout, LayoutStringFormat) {
FailedWithMessage("empty specification is not allowed"));
}

TEST(DataLayout, ParseNonIntegralAddrSpace) {
for (StringRef Str : {"ni:1", "ni:16777215", "ni:1:16777215"})
EXPECT_THAT_EXPECTED(DataLayout::parse(Str), Succeeded());

for (StringRef Str : {"ni", "ni42", "nix"})
EXPECT_THAT_EXPECTED(
DataLayout::parse(Str),
FailedWithMessage("malformed specification, must be of the form "
"\"ni:<address space>[:<address space>]...\""));

for (StringRef Str : {"ni:", "ni::42", "ni:42:"})
EXPECT_THAT_EXPECTED(DataLayout::parse(Str),
FailedWithMessage("<address space> is required"));

for (StringRef Str : {"ni:x", "ni:16777216", "ni:42:16777216"})
EXPECT_THAT_EXPECTED(
DataLayout::parse(Str),
FailedWithMessage("<address space> must be a 24-bit integer"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this rather than?

Suggested change
FailedWithMessage("<address space> must be a 24-bit integer"));
FailedWithMessage("address space must be a 24-bit integer"));

I get what you're going for, but I'm not sure trying to mimic the syntax used in LangRef is particularly helpful for error messages.

Copy link
Contributor Author

@s-barannikov s-barannikov Aug 16, 2024

Choose a reason for hiding this comment

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

It all started with S<size> specification, where <size> is in fact alignment, but it is neither ABI nor preferred.
I couldn't invent a name for it to pass to parseAlignment (see the draft PR) and I started passing "<size>" to the function so that the diagnostic becomes <size> must be ....
The rest of the changes were just for consistency.

I guess I can change this to plain English if it reads better and change <size> to stack natural alignment or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, and I'll update the draft accordingly.


for (StringRef Str : {"ni:0", "ni:42:0"})
EXPECT_THAT_EXPECTED(
DataLayout::parse(Str),
FailedWithMessage("address space 0 cannot be non-integral"));
}

TEST(DataLayout, IsNonIntegralAddressSpace) {
DataLayout Default;
EXPECT_THAT(Default.getNonIntegralAddressSpaces(), ::testing::SizeIs(0));
EXPECT_FALSE(Default.isNonIntegralAddressSpace(0));
EXPECT_FALSE(Default.isNonIntegralAddressSpace(1));

DataLayout Custom = cantFail(DataLayout::parse("ni:2:16777215"));
EXPECT_THAT(Custom.getNonIntegralAddressSpaces(),
::testing::ElementsAreArray({2U, 16777215U}));
EXPECT_FALSE(Custom.isNonIntegralAddressSpace(0));
EXPECT_FALSE(Custom.isNonIntegralAddressSpace(1));
EXPECT_TRUE(Custom.isNonIntegralAddressSpace(2));
EXPECT_TRUE(Custom.isNonIntegralAddressSpace(16777215));
}

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