Skip to content

[SYCL][NVPTX][AMDGCN] Move devicelib cmath to header #18706

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

Draft
wants to merge 6 commits into
base: sycl
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,13 @@ def SYCLDevice : InheritableAttr {
let Documentation = [SYCLDeviceDocs];
}

def SYCLDeviceOnly : InheritableAttr {
let Spellings = [GNU<"sycl_device_only">];
let Subjects = SubjectList<[Function]>;
let LangOpts = [SYCLIsDevice, SilentlyIgnoreSYCLIsHost];
let Documentation = [Undocumented];
}

def SYCLGlobalVar : InheritableAttr {
let Spellings = [GNU<"sycl_global_var">];
let Subjects = SubjectList<[GlobalStorageNonLocalVar], ErrorDiag>;
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3729,6 +3729,11 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const {
!(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc))
return 0;

if (Context.getLangOpts().isSYCL() && hasAttr<SYCLDeviceOnlyAttr>() &&
BuiltinID != Builtin::BIprintf) {
return 0;
}

// As AMDGCN implementation of OpenMP does not have a device-side standard
// library, none of the predefined library functions except printf and malloc
// should be treated as a builtin i.e. 0 should be returned for them.
Expand Down
7 changes: 2 additions & 5 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2544,10 +2544,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
GenerateIntrinsics =
ConstWithoutErrnoOrExceptions && ErrnoOverridenToFalseWithOpt;
}
bool IsSYCLDeviceWithoutIntrinsics =
getLangOpts().SYCLIsDevice &&
(getTarget().getTriple().isNVPTX() || getTarget().getTriple().isAMDGCN());
if (GenerateIntrinsics && !IsSYCLDeviceWithoutIntrinsics) {
if (GenerateIntrinsics) {
switch (BuiltinIDIfNoAsmLabel) {
case Builtin::BIacos:
case Builtin::BIacosf:
Expand Down Expand Up @@ -3647,7 +3644,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_modf:
case Builtin::BI__builtin_modff:
case Builtin::BI__builtin_modfl:
if (Builder.getIsFPConstrained() || IsSYCLDeviceWithoutIntrinsics)
if (Builder.getIsFPConstrained())
break; // TODO: Emit constrained modf intrinsic once one exists.
return RValue::get(emitModfBuiltin(*this, E, Intrinsic::modf));
case Builtin::BI__builtin_isgreater:
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7199,6 +7199,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_SYCLDevice:
S.SYCL().handleSYCLDeviceAttr(D, AL);
break;
case ParsedAttr::AT_SYCLDeviceOnly:
handleSimpleAttribute<SYCLDeviceOnlyAttr>(S, D, AL);
break;
case ParsedAttr::AT_SYCLScope:
S.SYCL().handleSYCLScopeAttr(D, AL);
break;
Expand Down
27 changes: 27 additions & 0 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,14 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
}
}

// Allow overloads with SYCLDeviceOnlyAttr
if (SemaRef.getLangOpts().isSYCL()) {
if (Old->hasAttr<SYCLDeviceOnlyAttr>() !=
New->hasAttr<SYCLDeviceOnlyAttr>()) {
return true;
}
}

// The signatures match; this is not an overload.
return false;
}
Expand Down Expand Up @@ -10960,6 +10968,16 @@ bool clang::isBetterOverloadCandidate(
S.CUDA().IdentifyPreference(Caller, Cand2.Function);
}

// In SYCL device compilation mode prefer the overload with the
// SYCLDeviceOnly attribute.
if (S.getLangOpts().isSYCL() && S.getLangOpts().SYCLIsDevice &&
Cand1.Function && Cand2.Function) {
if (Cand1.Function->hasAttr<SYCLDeviceOnlyAttr>() !=
Cand2.Function->hasAttr<SYCLDeviceOnlyAttr>()) {
return Cand1.Function->hasAttr<SYCLDeviceOnlyAttr>();
}
}

// General member function overloading is handled above, so this only handles
// constructors with address spaces.
// This only handles address spaces since C++ has no other
Expand Down Expand Up @@ -11314,6 +11332,15 @@ OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
if (S.getLangOpts().CUDA)
CudaExcludeWrongSideCandidates(S, Candidates);

// In SYCL host compilation remove candidates marked SYCLDeviceOnly.
if (S.getLangOpts().isSYCL() && !S.getLangOpts().SYCLIsDevice) {
auto IsDeviceCand = [&](const OverloadCandidate *Cand) {
return Cand->Viable && Cand->Function &&
Cand->Function->hasAttr<SYCLDeviceOnlyAttr>();
};
llvm::erase_if(Candidates, IsDeviceCand);
}

Best = end();
for (auto *Cand : Candidates) {
Cand->Best = false;
Expand Down
144 changes: 0 additions & 144 deletions clang/test/CodeGenSYCL/sycl-libdevice-cmath.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
// CHECK-NEXT: SYCLDeviceGlobal (SubjectMatchRule_record)
// CHECK-NEXT: SYCLDeviceHas (SubjectMatchRule_function)
// CHECK-NEXT: SYCLDeviceIndirectlyCallable (SubjectMatchRule_function)
// CHECK-NEXT: SYCLDeviceOnly (SubjectMatchRule_function)
// CHECK-NEXT: SYCLGlobalVariableAllowed (SubjectMatchRule_record)
// CHECK-NEXT: SYCLIntelDisableLoopPipelining (SubjectMatchRule_function)
// CHECK-NEXT: SYCLIntelInitiationInterval (SubjectMatchRule_function)
Expand Down
5 changes: 2 additions & 3 deletions libdevice/cmath_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#include "device_math.h"

#if defined(__SPIR__) || defined(__SPIRV__) || defined(__NVPTX__) || \
defined(__AMDGCN__)
#if defined(__SPIR__) || defined(__SPIRV__)

DEVICE_EXTERN_C_INLINE
int abs(int x) { return __devicelib_abs(x); }
Expand Down Expand Up @@ -210,4 +209,4 @@ DEVICE_EXTERN_C_INLINE
float rintf(float x) { return __ocml_rint_f32(x); }
#endif // __AMDGCN__

#endif // __SPIR__ || __SPIRV__ || __NVPTX__ || __AMDGCN__
#endif // __SPIR__ || __SPIRV__
5 changes: 2 additions & 3 deletions libdevice/cmath_wrapper_fp64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

#include "device_math.h"

#if defined(__SPIR__) || defined(__SPIRV__) || defined(__NVPTX__) || \
defined(__AMDGCN__)
#if defined(__SPIR__) || defined(__SPIRV__)

// All exported functions in math and complex device libraries are weak
// reference. If users provide their own math or complex functions(with
Expand Down Expand Up @@ -507,4 +506,4 @@ double _Sinh(double x, double y) { // compute y * sinh(x), |y| <= 1
}
}
#endif // defined(_WIN32)
#endif // __SPIR__ || __SPIRV__ || __NVPTX__ || __AMDGCN__
#endif // __SPIR__ || __SPIRV__
5 changes: 2 additions & 3 deletions libdevice/fallback-cmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#include "device_math.h"

#if defined(__SPIR__) || defined(__SPIRV__) || defined(__NVPTX__) || \
defined(__AMDGCN__)
#if defined(__SPIR__) || defined(__SPIRV__)

// To support fallback device libraries on-demand loading, please update the
// DeviceLibFuncMap in llvm/tools/sycl-post-link/sycl-post-link.cpp if you add
Expand Down Expand Up @@ -215,4 +214,4 @@ float __devicelib_asinhf(float x) { return __spirv_ocl_asinh(x); }
DEVICE_EXTERN_C_INLINE
float __devicelib_atanhf(float x) { return __spirv_ocl_atanh(x); }

#endif // __SPIR__ || __SPIRV__ || __NVPTX__ || __AMDGCN__
#endif // __SPIR__ || __SPIRV__
4 changes: 4 additions & 0 deletions sycl/include/sycl/stl_wrappers/cmath
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

// *** <sycl/builtins.hpp> ***

#if defined(__NVPTX__) || defined(__AMDGCN__)
#include "cmath-fallback.h"
#endif

#include <sycl/detail/defines_elementary.hpp>

#ifdef __SYCL_DEVICE_ONLY__
Expand Down
Loading