Skip to content

[SYCL-PTX] Enable half for CUDA target. #2003

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

Merged
merged 3 commits into from
Jul 3, 2020
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
1 change: 1 addition & 0 deletions clang/lib/Basic/Targets/NVPTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
// FIXME: maybe we should have a way to control this ?
Opts.support("cl_khr_int64_base_atomics");
Opts.support("cl_khr_int64_extended_atomics");
Opts.support("cl_khr_fp16");
}

/// \returns If a target requires an address within a target specific address
Expand Down
19 changes: 18 additions & 1 deletion clang/lib/Sema/SPIRVBuiltins.td
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class FunctionExtension<string _Ext> : AbstractExtension<_Ext>;
def FuncExtNone : FunctionExtension<"">;

// Qualified Type. These map to ASTContext::QualType.
// TODO: Create a QualTypeFromASTContext.
// To fully make sense here, this class should represent
// the QualType only. How the QualType is accessed should be separated.
class QualType<string _Name, bit _IsAbstract=0, bit _IsSigned=0> {
// Name of the field or function in a clang::ASTContext
// E.g. Name="IntTy" for the int type, and "getIntPtrType()" for an intptr_t
Expand All @@ -64,6 +67,20 @@ class QualType<string _Name, bit _IsAbstract=0, bit _IsSigned=0> {
bit IsSigned = _IsSigned;
}

// Qualified Type. These map to a function taking an ASTContext
// and returning a QualType.
// Instead of direclty accessing ASTContext fields, the builtin lookup can
// call a function to extract the correct type for the call.
// The name will be interpreted as the function to call
// rather than the field to access.
class QualTypeFromFunction<string _Name, bit _IsAbstract=0, bit _IsSigned=0> :
QualType<_Name, _IsAbstract, _IsSigned> {
// TODO: At the moment the user is expected to write the function outside this file.
// Although they could be generated in the .inc file and
// the user would only have to provide the body here
// (like it can be done for attributes for instance).
}

// List of integers.
class IntList<string _Name, list<int> _List> {
string Name = _Name;
Expand Down Expand Up @@ -298,7 +315,7 @@ def Long : IntType<"long", QualType<"getIntTypeForBitwidth(64, true)",
def ULong : UIntType<"ulong", QualType<"getIntTypeForBitwidth(64, false)">, 64>;
def Float : FPType<"float", QualType<"FloatTy">, 32>;
def Double : FPType<"double", QualType<"DoubleTy">, 64>;
def Half : FPType<"half", QualType<"Float16Ty">, 16>;
def Half : FPType<"half", QualTypeFromFunction<"GetFloat16Type">, 16>;
def Void : Type<"void", QualType<"VoidTy">>;
// FIXME: ensure this is portable...
def Size : Type<"size_t", QualType<"getSizeType()">>;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include <utility>
#include <vector>

static inline clang::QualType GetFloat16Type(clang::ASTContext &Context);

#include "OpenCLBuiltins.inc"
#include "SPIRVBuiltins.inc"

Expand Down Expand Up @@ -678,6 +680,10 @@ LLVM_DUMP_METHOD void LookupResult::dump() {
D->dump();
}

static inline QualType GetFloat16Type(clang::ASTContext &Context) {
return Context.getLangOpts().OpenCL ? Context.HalfTy : Context.Float16Ty;
}

/// Get the QualType instances of the return type and arguments for a ProgModel
/// builtin function signature.
/// \param Context (in) The Context instance.
Expand Down
12 changes: 8 additions & 4 deletions clang/test/Misc/nvptx.languageOptsOpenCL.cl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
#endif
#pragma OPENCL EXTENSION cl_clang_storage_class_specifiers: enable

#ifdef cl_khr_fp16
#error "Incorrect cl_khr_fp16 define"
#endif
// TODO: Temporarily disabling the following test as a work around for the
// SYCL codepath until the cl_khr_fp16 is restricted to only the sycldevice triple.
// link to issue https://github.com/intel/llvm/issues/1814

// #ifdef cl_khr_fp16
// #error "Incorrect cl_khr_fp16 define"
// #endif
#pragma OPENCL EXTENSION cl_khr_fp16: enable
// expected-warning@-1{{unsupported OpenCL extension 'cl_khr_fp16' - ignoring}}
// expected warning@-1{{unsupported OpenCL extension 'cl_khr_fp16' - ignoring}}

// TODO: Temporarily disabling the following two tests as a work around for the
// SYCL codepath until the cl_khr_int64_base_atomics and
Expand Down
16 changes: 12 additions & 4 deletions clang/utils/TableGen/ClangProgModelBuiltinEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,12 @@ void BuiltinNameEmitter::EmitQualTypeFinder() {
I++) {
for (const auto *T :
GenType->getValueAsDef("TypeList")->getValueAsListOfDefs("List")) {
OS << "Context."
<< T->getValueAsDef("QTName")->getValueAsString("Name") << ", ";
if (T->getValueAsDef("QTName")->isSubClassOf("QualTypeFromFunction"))
OS << T->getValueAsDef("QTName")->getValueAsString("Name")
<< "(Context), ";
else
OS << "Context."
<< T->getValueAsDef("QTName")->getValueAsString("Name") << ", ";
}
}
OS << "});\n";
Expand Down Expand Up @@ -787,8 +791,12 @@ void BuiltinNameEmitter::EmitQualTypeFinder() {
continue;
// Emit the cases for non generic, non image types.
OS << " case TID_" << T->getValueAsString("Name") << ":\n";
OS << " QT.push_back(Context." << QT->getValueAsString("Name")
<< ");\n";
if (QT->isSubClassOf("QualTypeFromFunction"))
OS << " QT.push_back(" << QT->getValueAsString("Name")
<< "(Context));\n";
else
OS << " QT.push_back(Context." << QT->getValueAsString("Name")
<< ");\n";
OS << " break;\n";
}

Expand Down
7 changes: 3 additions & 4 deletions libclc/generic/include/clc/float/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@

#ifdef cl_khr_fp16

#if __OPENCL_VERSION__ >= 120

#define HALF_DIG 3
#define HALF_MANT_DIG 11
#define HALF_MAX_10_EXP +4
Expand All @@ -84,7 +82,8 @@
#define HALF_MAX 0x1.ffcp15h
#define HALF_MIN 0x1.0p-14h
#define HALF_EPSILON 0x1.0p-10h
#define HALF_MAX_SQRT 0x1.0p+8h
#define HALF_MIN_SQRT 0x1.0p-8h

#endif

#define M_PI_OVER_180_H 0x1.1ep-6h
#endif
1 change: 0 additions & 1 deletion sycl/test/basic_tests/vec_convert_half.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// XFAIL: cuda
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
Expand Down
3 changes: 1 addition & 2 deletions sycl/test/built-ins/nan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t_gpu.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
// XFAIL: cuda
// CUDA fails with: ptxas fatal : Unresolved extern function '_Z15__spirv_ocl_nanj'

#include <CL/sycl.hpp>

#include <cassert>
Expand Down