Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[clang] Fix nondeterminism in MemberPointerType #137910

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 4 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -3602,6 +3602,9 @@ class MemberPointerType : public Type, public llvm::FoldingSetNode {
}

NestedNameSpecifier *getQualifier() const { return Qualifier; }
/// Note: this can trigger extra deserialization when external AST sources are
/// used. Prefer `getCXXRecordDecl()` unless you really need the most recent
/// decl.
CXXRecordDecl *getMostRecentCXXRecordDecl() const;

bool isSugared() const;
Expand All @@ -3610,7 +3613,10 @@ class MemberPointerType : public Type, public llvm::FoldingSetNode {
}

void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getPointeeType(), getQualifier(), getMostRecentCXXRecordDecl());
// FIXME: `getMostRecentCXXRecordDecl()` should be possible to use here,
// however when external AST sources are used it causes nondeterminism
// issues (see https://github.com/llvm/llvm-project/pull/137910).
Profile(ID, getPointeeType(), getQualifier(), getCXXRecordDecl());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the problem that we run this profile in the 'middle' of a TU and the most recent decl changes by the end? If so, this makes sense.

That said, I'd expect this to be the above suggestion in case this member pointer type has a redeclaration that perhaps changes what the actual referenced decl is.

Suggested change
Profile(ID, getPointeeType(), getQualifier(), getCXXRecordDecl());
Profile(ID, getPointeeType(), getQualifier(), getCXXRecordDecl()->getCanonicalDecl());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but this suggestion seems not needed because this getCanonicalDecl() call is already made inside the Profile() method:

ID.AddPointer(Cls->getCanonicalDecl());

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm... then I'm surprised that this patch is needed at all for the purposes of non-determinism. getMostRecentCXXRecordDecl()->getCanonicalDecl() will get the same declaration as getCXXRecordDecl()->getCanonicalDecl().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is transient - the reproducibility rate on the reproducer (https://pastebin.com/6aL6rmBe) is only 1%. Also it's not the pointer we have here that's problematic, but rather the fact of extra AST node loading inside the FoldingSet hash calculation operation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... then I'm surprised that this patch is needed at all for the purposes of non-determinism

My theory is that non-determinism is caused by side-effects in the deserialization logic that runs behind getMostRecentCXXRecordDecl(). We probably just get unlucky with rehashing points at different times for the hash table (FoldingSet) that makes us either call or not call getMostRecentCXXRecordDecl.

It is not a new issue, just rare and now getting surfaced by the new place where getMostRecentCXXRecordDecl is called.

}

static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
Expand All @@ -3620,6 +3626,9 @@ class MemberPointerType : public Type, public llvm::FoldingSetNode {
static bool classof(const Type *T) {
return T->getTypeClass() == MemberPointer;
}

private:
CXXRecordDecl *getCXXRecordDecl() const;
};

/// Capture whether this is a normal array (e.g. int X[4])
Expand Down
10 changes: 7 additions & 3 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5275,10 +5275,14 @@ void MemberPointerType::Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
ID.AddPointer(Cls->getCanonicalDecl());
}

CXXRecordDecl *MemberPointerType::getCXXRecordDecl() const {
return dyn_cast<MemberPointerType>(getCanonicalTypeInternal())
->getQualifier()
->getAsRecordDecl();
}

CXXRecordDecl *MemberPointerType::getMostRecentCXXRecordDecl() const {
auto *RD = dyn_cast<MemberPointerType>(getCanonicalTypeInternal())
->getQualifier()
->getAsRecordDecl();
auto *RD = getCXXRecordDecl();
if (!RD)
return nullptr;
return RD->getMostRecentNonInjectedDecl();
Expand Down