From 27e6e4a4d0e3296cebad8db577ec0469a286795e Mon Sep 17 00:00:00 2001 From: "Yaxun (Sam) Liu" Date: Thu, 16 Nov 2023 08:42:54 -0500 Subject: [CUDA][HIP] make trivial ctor/dtor host device (#72394) Make trivial ctor/dtor implicitly host device functions so that they can be used to initialize file-scope device variables to match nvcc behavior. Fixes: https://github.com/llvm/llvm-project/issues/72261 Fixes: SWDEV-432412 --- clang/lib/Sema/SemaCUDA.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'clang/lib/Sema/SemaCUDA.cpp') diff --git a/clang/lib/Sema/SemaCUDA.cpp b/clang/lib/Sema/SemaCUDA.cpp index 318174f..b94f448 100644 --- a/clang/lib/Sema/SemaCUDA.cpp +++ b/clang/lib/Sema/SemaCUDA.cpp @@ -772,6 +772,22 @@ void Sema::maybeAddCUDAHostDeviceAttrs(FunctionDecl *NewD, NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context)); } +// If a trivial ctor/dtor has no host/device +// attributes, make it implicitly host device function. +void Sema::maybeAddCUDAHostDeviceAttrsToTrivialCtorDtor(FunctionDecl *FD) { + bool IsTrivialCtor = false; + if (auto *CD = dyn_cast(FD)) + IsTrivialCtor = isEmptyCudaConstructor(SourceLocation(), CD); + bool IsTrivialDtor = false; + if (auto *DD = dyn_cast(FD)) + IsTrivialDtor = isEmptyCudaDestructor(SourceLocation(), DD); + if ((IsTrivialCtor || IsTrivialDtor) && !FD->hasAttr() && + !FD->hasAttr()) { + FD->addAttr(CUDAHostAttr::CreateImplicit(Context)); + FD->addAttr(CUDADeviceAttr::CreateImplicit(Context)); + } +} + // TODO: `__constant__` memory may be a limited resource for certain targets. // A safeguard may be needed at the end of compilation pipeline if // `__constant__` memory usage goes beyond limit. -- cgit v1.1