Skip to content

Commit 0bb987f

Browse files
committed
Revert "[CUDA][HIP] Fix CTAD for host/device constructors (#168711)"
This reverts commit e719e93. revert this since it caused regression in our internal CI. Deduction guide with host/device attrs have already been used in https://github.com/ROCm/rocm-libraries/blob/develop/projects/rocrand/library/src/rng/utils/cpp_utils.hpp#L249 ``` template<class V> __host__ __device__ vec_wrapper(V) -> vec_wrapper<V>; ```
1 parent ca3de05 commit 0bb987f

File tree

9 files changed

+2
-336
lines changed

9 files changed

+2
-336
lines changed

clang/docs/HIPSupport.rst

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -412,54 +412,6 @@ Example Usage
412412
__host__ __device__ int Four(void) __attribute__((weak, alias("_Z6__Fourv")));
413413
__host__ __device__ float Four(float f) __attribute__((weak, alias("_Z6__Fourf")));
414414

415-
C++17 Class Template Argument Deduction (CTAD) Support
416-
======================================================
417-
418-
Clang supports C++17 Class Template Argument Deduction (CTAD) in both host and
419-
device code for HIP. This allows you to omit template arguments when creating
420-
class template instances, letting the compiler deduce them from constructor
421-
arguments.
422-
423-
.. code-block:: c++
424-
425-
#include <tuple>
426-
427-
__host__ __device__ void func() {
428-
std::tuple<int, int> t = std::tuple(1, 1);
429-
}
430-
431-
In the above example, ``std::tuple(1, 1)`` automatically deduces the type to be
432-
``std::tuple<int, int>``.
433-
434-
Deduction Guides
435-
----------------
436-
437-
User-defined deduction guides are also supported. Since deduction guides are not
438-
executable code and only participate in type deduction, they semantically behave
439-
as ``__host__ __device__``. This ensures they are available for deduction in both
440-
host and device contexts, and CTAD continues to respect any constraints on the
441-
corresponding constructors in the usual C++ way.
442-
443-
.. code-block:: c++
444-
445-
template <typename T>
446-
struct MyType {
447-
T value;
448-
__device__ MyType(T v) : value(v) {}
449-
};
450-
451-
MyType(float) -> MyType<double>;
452-
453-
__device__ void deviceFunc() {
454-
MyType m(1.0f); // Deduces MyType<double>
455-
}
456-
457-
.. note::
458-
459-
Explicit HIP target attributes such as ``__host__`` or ``__device__``
460-
are not allowed on deduction guides. Clang treats all deduction guides
461-
as if they were ``__host__ __device__`` and diagnoses any explicit
462-
target attributes on them as errors.
463415

464416
Host and Device Attributes of Default Destructors
465417
===================================================

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -648,20 +648,6 @@ RISC-V Support
648648
CUDA/HIP Language Changes
649649
^^^^^^^^^^^^^^^^^^^^^^^^^
650650

651-
- Clang now supports C++17 Class Template Argument Deduction (CTAD) in CUDA/HIP
652-
device code by treating deduction guides as if they were ``__host__ __device__``.
653-
654-
- Clang avoids ambiguous CTAD in CUDA/HIP by not synthesizing duplicate implicit
655-
deduction guides when ``__host__`` and ``__device__`` constructors differ only
656-
in CUDA target attributes (same signature and constraints).
657-
658-
- Clang diagnoses CUDA/HIP target attributes written on deduction guides as errors,
659-
since deduction guides do not participate in code generation.
660-
661-
- Clang preserves distinct implicit deduction guides for constructors that differ
662-
by constraints, so constraint-based CTAD works in CUDA/HIP device code as in
663-
standard C++.
664-
665651
CUDA Support
666652
^^^^^^^^^^^^
667653

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,9 +2769,6 @@ def err_deduction_guide_name_not_class_template : Error<
27692769
"cannot specify deduction guide for "
27702770
"%select{<error>|function template|variable template|alias template|"
27712771
"template template parameter|concept|dependent template name}0 %1">;
2772-
def err_deduction_guide_target_attr : Error<
2773-
"in CUDA/HIP, target attributes are not allowed on deduction guides; "
2774-
"deduction guides are implicitly enabled for both host and device">;
27752772
def err_deduction_guide_wrong_scope : Error<
27762773
"deduction guide must be declared in the same scope as template %q0">;
27772774
def err_deduction_guide_defines_function : Error<

clang/lib/Sema/SemaCUDA.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,6 @@ CUDAFunctionTarget SemaCUDA::IdentifyTarget(const FunctionDecl *D,
215215
if (D == nullptr)
216216
return CurCUDATargetCtx.Target;
217217

218-
// C++ deduction guides are never codegen'ed and only participate in template
219-
// argument deduction. Treat them as if they were always host+device so that
220-
// CUDA/HIP target checking never rejects their use based solely on target.
221-
if (isa<CXXDeductionGuideDecl>(D))
222-
return CUDAFunctionTarget::HostDevice;
223-
224218
if (D->hasAttr<CUDAInvalidTargetAttr>())
225219
return CUDAFunctionTarget::InvalidTarget;
226220

@@ -992,12 +986,6 @@ bool SemaCUDA::CheckCall(SourceLocation Loc, FunctionDecl *Callee) {
992986
if (ExprEvalCtx.isUnevaluated() || ExprEvalCtx.isConstantEvaluated())
993987
return true;
994988

995-
// C++ deduction guides participate in overload resolution but are not
996-
// callable functions and are never codegen'ed. Treat them as always
997-
// allowed for CUDA/HIP compatibility checking.
998-
if (isa<CXXDeductionGuideDecl>(Callee))
999-
return true;
1000-
1001989
// FIXME: Is bailing out early correct here? Should we instead assume that
1002990
// the caller is a global initializer?
1003991
FunctionDecl *Caller = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7987,19 +7987,6 @@ void Sema::ProcessDeclAttributeList(
79877987
}
79887988
}
79897989

7990-
// CUDA/HIP: disallow explicit CUDA target attributes on deduction guides.
7991-
// Deduction guides are not callable functions and never participate in
7992-
// codegen; they are always treated as host+device for CUDA/HIP semantic
7993-
// checks, so explicit target attributes on them would be misleading.
7994-
if (getLangOpts().CUDA)
7995-
if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(D);
7996-
Guide &&
7997-
(Guide->hasAttr<CUDAHostAttr>() || Guide->hasAttr<CUDADeviceAttr>() ||
7998-
Guide->hasAttr<CUDAGlobalAttr>())) {
7999-
Diag(Guide->getLocation(), diag::err_deduction_guide_target_attr);
8000-
Guide->setInvalidDecl();
8001-
}
8002-
80037990
// Do not permit 'constructor' or 'destructor' attributes on __device__ code.
80047991
if (getLangOpts().CUDAIsDevice && D->hasAttr<CUDADeviceAttr>() &&
80057992
(D->hasAttr<ConstructorAttr>() || D->hasAttr<DestructorAttr>()) &&

clang/lib/Sema/SemaTemplateDeductionGuide.cpp

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,6 @@ using namespace clang;
5454
using namespace sema;
5555

5656
namespace {
57-
58-
/// Return true if two associated-constraint sets are semantically equal.
59-
static bool HaveSameAssociatedConstraints(
60-
Sema &SemaRef, const NamedDecl *Old, ArrayRef<AssociatedConstraint> OldACs,
61-
const NamedDecl *New, ArrayRef<AssociatedConstraint> NewACs) {
62-
if (OldACs.size() != NewACs.size())
63-
return false;
64-
if (OldACs.empty())
65-
return true;
66-
67-
// General case: pairwise compare each associated constraint expression.
68-
Sema::TemplateCompareNewDeclInfo NewInfo(New);
69-
for (size_t I = 0, E = OldACs.size(); I != E; ++I)
70-
if (!SemaRef.AreConstraintExpressionsEqual(
71-
Old, OldACs[I].ConstraintExpr, NewInfo, NewACs[I].ConstraintExpr))
72-
return false;
73-
74-
return true;
75-
}
76-
7757
/// Tree transform to "extract" a transformed type from a class template's
7858
/// constructor to a deduction guide.
7959
class ExtractTypeForDeductionGuide
@@ -238,51 +218,9 @@ buildDeductionGuide(Sema &SemaRef, TemplateDecl *OriginalTemplate,
238218
TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams();
239219

240220
// Build the implicit deduction guide template.
241-
QualType GuideType = TInfo->getType();
242-
243-
// In CUDA/HIP mode, avoid duplicate implicit guides that differ only in CUDA
244-
// target attributes (same constructor signature and constraints).
245-
if (IsImplicit && Ctor && SemaRef.getLangOpts().CUDA) {
246-
SmallVector<AssociatedConstraint, 4> NewACs;
247-
Ctor->getAssociatedConstraints(NewACs);
248-
249-
for (NamedDecl *Existing : DC->lookup(DeductionGuideName)) {
250-
auto *ExistingFT = dyn_cast<FunctionTemplateDecl>(Existing);
251-
auto *ExistingGuide =
252-
ExistingFT
253-
? dyn_cast<CXXDeductionGuideDecl>(ExistingFT->getTemplatedDecl())
254-
: dyn_cast<CXXDeductionGuideDecl>(Existing);
255-
if (!ExistingGuide)
256-
continue;
257-
258-
// Only consider guides that were also synthesized from a constructor.
259-
auto *ExistingCtor = ExistingGuide->getCorrespondingConstructor();
260-
if (!ExistingCtor)
261-
continue;
262-
263-
// If the underlying constructors are overloads (different signatures once
264-
// CUDA attributes are ignored), they should each get their own guides.
265-
if (SemaRef.IsOverload(Ctor, ExistingCtor,
266-
/*UseMemberUsingDeclRules=*/false,
267-
/*ConsiderCudaAttrs=*/false))
268-
continue;
269-
270-
// At this point, the constructors have the same signature ignoring CUDA
271-
// attributes. Decide whether their associated constraints are also the
272-
// same; only in that case do we treat one guide as a duplicate of the
273-
// other.
274-
SmallVector<AssociatedConstraint, 4> ExistingACs;
275-
ExistingCtor->getAssociatedConstraints(ExistingACs);
276-
277-
if (HaveSameAssociatedConstraints(SemaRef, ExistingCtor, ExistingACs,
278-
Ctor, NewACs))
279-
return Existing;
280-
}
281-
}
282-
283221
auto *Guide = CXXDeductionGuideDecl::Create(
284-
SemaRef.Context, DC, LocStart, ES, Name, GuideType, TInfo, LocEnd, Ctor,
285-
DeductionCandidate::Normal, FunctionTrailingRC);
222+
SemaRef.Context, DC, LocStart, ES, Name, TInfo->getType(), TInfo, LocEnd,
223+
Ctor, DeductionCandidate::Normal, FunctionTrailingRC);
286224
Guide->setImplicit(IsImplicit);
287225
Guide->setParams(Params);
288226

clang/test/SemaCUDA/deduction-guide-attrs.cu

Lines changed: 0 additions & 24 deletions
This file was deleted.

clang/test/SemaCUDA/deduction-guide-overload.cu

Lines changed: 0 additions & 111 deletions
This file was deleted.

clang/test/SemaCUDA/deduction-guide.cu

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)