Skip to content
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

Do not process template arguments when checking for recursive hyperobjects #224

Merged
merged 3 commits into from
Dec 15, 2023
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
5 changes: 5 additions & 0 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,11 @@ HyperobjectType::HyperobjectType(QualType Element, QualType CanonicalPtr,
: Type(Hyperobject, CanonicalPtr, Element->getDependence()),
ElementType(Element), Identity(i), Reduce(r),
IdentityID(ifn), ReduceID(rfn) {
if (Element->isIncompleteType()) // diagnosed in caller
addDependence(TypeDependence::Error);
addDependence(Element->getDependence());
addDependence(toTypeDependence(i->getDependence()));
addDependence(toTypeDependence(r->getDependence()));
}

bool HyperobjectType::hasCallbacks() const {
Expand Down
47 changes: 11 additions & 36 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1294,35 +1294,6 @@ static std::optional<unsigned> ContainsHyperobject(QualType Outer) {
break;
case Type::Record: {
const RecordDecl *Decl = cast<RecordType>(T)->getDecl();
// TODO: There must be a better way to do this.
// A hyperobject might sneak in without being explicitly
// declared in the template.
if (auto Spec = dyn_cast<ClassTemplateSpecializationDecl>(Decl)) {
if (ClassTemplateDecl *Inner = Spec->getSpecializedTemplate())
if (auto O = DeclContainsHyperobject(Inner->getTemplatedDecl()))
return O;
const TemplateArgumentList &Args = Spec->getTemplateArgs();
for (unsigned I = 0; I < Args.size(); ++I) {
const TemplateArgument &Arg = Args.get(I);
switch (Arg.getKind()) {
case TemplateArgument::Declaration:
if (auto O = ContainsHyperobject(Arg.getAsDecl()->getType()))
return O;
break;
case TemplateArgument::Type:
if (auto O = ContainsHyperobject(Arg.getAsType()))
return O;
break;
case TemplateArgument::Integral:
case TemplateArgument::NullPtr:
case TemplateArgument::Null:
break;
default:
return diag::confusing_hyperobject;
}
}
return std::optional<unsigned>();
}
if (const RecordDecl *Def = Decl->getDefinition())
return DeclContainsHyperobject(Def);
return diag::confusing_hyperobject;
Expand Down Expand Up @@ -1362,6 +1333,8 @@ static std::optional<unsigned> ContainsHyperobject(QualType Outer) {
}

static std::optional<unsigned> DeclContainsHyperobject(const RecordDecl *Decl) {
if (Decl->isInvalidDecl())
return std::optional<unsigned>();
for (const FieldDecl *FD : Decl->fields())
if (std::optional<unsigned> O = ContainsHyperobject(FD->getType()))
return O;
Expand Down Expand Up @@ -2469,20 +2442,22 @@ Expr *Sema::ValidateReducerCallback(Expr *E, unsigned NumArgs,

QualType Sema::BuildHyperobjectType(QualType Element, Expr *Identity,
Expr *Reduce, SourceLocation Loc) {
QualType Result = Element;
// This function must return a HyperobjectType with the given
// element type, otherwise the rest of the front end will get angry.
// Template instantiation is quite strict about preserving structure.
// The compiler will also get angry if the element type is incomplete
// and the HyperobjectType is not marked as containing an error.
if (!RequireCompleteType(Loc, Element, CompleteTypeKind::Normal,
diag::incomplete_hyperobject)) {
if (std::optional<unsigned> Code = ContainsHyperobject(Result))
Diag(Loc, *Code) << Result;
if (std::optional<unsigned> Code = ContainsHyperobject(Element))
Diag(Loc, *Code) << Element;
}

Identity = ValidateReducerCallback(Identity, 1, Loc);
Reduce = ValidateReducerCallback(Reduce, 2, Loc);

// The result of this function must be HyperobjectType if it is called
// from C++ template instantiation when rebuilding an existing hyperobject
// type.
return Context.getHyperobjectType(Result, Identity, Reduce);
// The result will be marked erroneous if Element is incomplete.
return Context.getHyperobjectType(Element, Identity, Reduce);
}

/// Build a Read-only Pipe type.
Expand Down
26 changes: 26 additions & 0 deletions clang/test/Cilk/222.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 %s -x c++ -fopencilk -emit-llvm -verify -mllvm -use-opencilk-runtime-bc=false -mllvm -debug-abi-calls -o /dev/null
#define DEPTH 3
template<int N> struct R {
static void identity(void *);
static void reduce(void *, void *);
int get(int depth) { return depth <= 0 ? i : field.get(depth - 1); }
public:
R<N - 1> field;
// expected-note@-1{{in instantiation}}
// expected-note@-2{{in instantiation}}
// expected-note@-3{{in instantiation}}
int _Hyperobject(identity, reduce) i;
// expected-warning@-1{{reducer callbacks not implemented for structure members}}
// expected-warning@-2{{reducer callbacks not implemented for structure members}}
// expected-warning@-3{{reducer callbacks not implemented for structure members}}
};

template<> struct R<0> { int field; int get(int) { return field; } };

extern R<DEPTH> r;

int f() { return r.get(DEPTH / 2); }
// expected-note@-1{{in instantiation}}
// expected-note@-2{{in instantiation}}
// expected-note@-3{{in instantiation}}

22 changes: 22 additions & 0 deletions clang/test/Cilk/223.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 %s -x c++ -O1 -fopencilk -verify -fsyntax-only
template <class T>
class A { };

class B {
A<B> a;
};

void identity(void* view) {}
void reduce(void* l, void* r) {}

B _Hyperobject(identity, reduce) b;

template <class T>
class C { T _Hyperobject *field; };
// expected-error@-1{{incomplete type 'D' may not be a hyperobject}}

class D { // expected-note{{}}}
C<D> a; // expected-note{{}}}
};

D _Hyperobject(identity, reduce) d;