Skip to content

Commit e5d17c5

Browse files
committedSep 20, 2019
[CUDA][HIP] Fix hostness of defaulted constructor
Clang does not respect the explicit device host attributes of defaulted special members. Also clang does not respect the hostness of special members determined by their first declarations. Clang also adds duplicate implicit device or host attributes in certain cases. This patch fixes that. Differential Revision: https://reviews.llvm.org/D67509 llvm-svn: 372394
1 parent 4896f72 commit e5d17c5

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed
 

‎clang/lib/Sema/SemaCUDA.cpp

+27-12
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ bool Sema::inferCUDATargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
267267
CXXMethodDecl *MemberDecl,
268268
bool ConstRHS,
269269
bool Diagnose) {
270+
// If the defaulted special member is defined lexically outside of its
271+
// owning class, or the special member already has explicit device or host
272+
// attributes, do not infer.
273+
bool InClass = MemberDecl->getLexicalParent() == MemberDecl->getParent();
274+
bool HasH = MemberDecl->hasAttr<CUDAHostAttr>();
275+
bool HasD = MemberDecl->hasAttr<CUDADeviceAttr>();
276+
bool HasExplicitAttr =
277+
(HasD && !MemberDecl->getAttr<CUDADeviceAttr>()->isImplicit()) ||
278+
(HasH && !MemberDecl->getAttr<CUDAHostAttr>()->isImplicit());
279+
if (!InClass || HasExplicitAttr)
280+
return false;
281+
270282
llvm::Optional<CUDAFunctionTarget> InferredTarget;
271283

272284
// We're going to invoke special member lookup; mark that these special
@@ -371,21 +383,24 @@ bool Sema::inferCUDATargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
371383
}
372384
}
373385

386+
387+
// If no target was inferred, mark this member as __host__ __device__;
388+
// it's the least restrictive option that can be invoked from any target.
389+
bool NeedsH = true, NeedsD = true;
374390
if (InferredTarget.hasValue()) {
375-
if (InferredTarget.getValue() == CFT_Device) {
376-
MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
377-
} else if (InferredTarget.getValue() == CFT_Host) {
378-
MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
379-
} else {
380-
MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
381-
MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
382-
}
383-
} else {
384-
// If no target was inferred, mark this member as __host__ __device__;
385-
// it's the least restrictive option that can be invoked from any target.
391+
if (InferredTarget.getValue() == CFT_Device)
392+
NeedsH = false;
393+
else if (InferredTarget.getValue() == CFT_Host)
394+
NeedsD = false;
395+
}
396+
397+
// We either setting attributes first time, or the inferred ones must match
398+
// previously set ones.
399+
assert(!(HasD || HasH) || (NeedsD == HasD && NeedsH == HasH));
400+
if (NeedsD && !HasD)
386401
MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
402+
if (NeedsH && !HasH)
387403
MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
388-
}
389404

390405
return false;
391406
}

‎clang/test/SemaCUDA/default-ctor.cu

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clang_cc1 -std=c++11 -triple nvptx64-nvidia-cuda -fsyntax-only \
2+
// RUN: -fcuda-is-device -verify -verify-ignore-unexpected=note %s
3+
// RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-linux-gnu -fsyntax-only \
4+
// RUN: -verify -verify-ignore-unexpected=note %s
5+
6+
#include "Inputs/cuda.h"
7+
8+
struct In { In() = default; };
9+
struct InD { __device__ InD() = default; };
10+
struct InH { __host__ InH() = default; };
11+
struct InHD { __host__ __device__ InHD() = default; };
12+
13+
struct Out { Out(); };
14+
struct OutD { __device__ OutD(); };
15+
struct OutH { __host__ OutH(); };
16+
struct OutHD { __host__ __device__ OutHD(); };
17+
18+
Out::Out() = default;
19+
__device__ OutD::OutD() = default;
20+
__host__ OutH::OutH() = default;
21+
__host__ __device__ OutHD::OutHD() = default;
22+
23+
__device__ void fd() {
24+
In in;
25+
InD ind;
26+
InH inh; // expected-error{{no matching constructor for initialization of 'InH'}}
27+
InHD inhd;
28+
Out out; // expected-error{{no matching constructor for initialization of 'Out'}}
29+
OutD outd;
30+
OutH outh; // expected-error{{no matching constructor for initialization of 'OutH'}}
31+
OutHD outhd;
32+
}
33+
34+
__host__ void fh() {
35+
In in;
36+
InD ind; // expected-error{{no matching constructor for initialization of 'InD'}}
37+
InH inh;
38+
InHD inhd;
39+
Out out;
40+
OutD outd; // expected-error{{no matching constructor for initialization of 'OutD'}}
41+
OutH outh;
42+
OutHD outhd;
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.