From 97b255eefd2ddf930b8770e922f7c1116e07afa0 Mon Sep 17 00:00:00 2001 From: Dmitry Sidorov Date: Thu, 16 Jul 2020 15:51:28 +0300 Subject: [PATCH] [SYCL] Enable USM address spaces heneration upder opt This patch serves two purposes: 1. It reverts shift of accessors to global buffers from __global to __global_device address space, which was considered as not a good idea until proven otherwise. Still this shift is enabled under -fsycl_enable_usm_address_spaces option for testing; 2. It fixes a mangling issue of functions that accepts arguments of multi_ptr<> type for non-SPIR targets (see: https://github.com/intel/llvm/issues/2039) Signed-off-by: Dmitry Sidorov --- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/ToolChains/Clang.cpp | 7 ++++++ clang/test/Driver/sycl-offload.c | 3 ++- sycl/include/CL/sycl/access/access.hpp | 9 ++++++++ sycl/include/CL/sycl/atomic.hpp | 2 ++ sycl/include/CL/sycl/handler.hpp | 8 +++++++ sycl/include/CL/sycl/multi_ptr.hpp | 2 ++ .../check_device_code/kernel_arguments_as.cpp | 7 ++++-- sycl/test/check_device_code/usm_pointers.cpp | 22 +++++++++++++------ 9 files changed, 51 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 044a9e3f9a4a1..eadc77e0373e6 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1842,7 +1842,7 @@ def fintelfpga : Flag<["-"], "fintelfpga">, Group, Flags<[CC1Option, CoreOption]>, HelpText<"Perform ahead of time compilation for FPGA">; def fsycl_enable_usm_address_spaces : Flag<["-"], "fsycl-enable-usm-address-spaces">, Group, Flags<[CC1Option, CoreOption]>, - HelpText<"Enable SPV_INTEL_usm_storage_classes extension">; + HelpText<"Enable USM address spaces">; def fsycl_device_only : Flag<["-"], "fsycl-device-only">, Flags<[CoreOption]>, HelpText<"Compile SYCL kernels for device">; def fsycl_targets_EQ : CommaJoined<["-"], "fsycl-targets=">, Flags<[DriverOption, CC1Option, CoreOption]>, diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 5ff2dfecaed04..f1cac440453be 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6183,6 +6183,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } if (Args.hasArg(options::OPT_fsycl_unnamed_lambda)) CmdArgs.push_back("-fsycl-unnamed-lambda"); + + // Enable generation of USM address spaces as opt-in. + // __ENABLE_USM_ADDR_SPACE__ will be used during compilation of SYCL headers + if (getToolChain().getTriple().getSubArch() == + llvm::Triple::SPIRSubArch_fpga && + Args.hasArg(options::OPT_fsycl_enable_usm_address_spaces)) + CmdArgs.push_back("-D__ENABLE_USM_ADDR_SPACE__"); } if (IsHIP) diff --git a/clang/test/Driver/sycl-offload.c b/clang/test/Driver/sycl-offload.c index 74eaeb182a5b2..a297c7d71979b 100644 --- a/clang/test/Driver/sycl-offload.c +++ b/clang/test/Driver/sycl-offload.c @@ -668,7 +668,8 @@ // CHK-TOOLS-GEN: clang-offload-wrapper{{.*}} "-o=[[OUTPUT5:.+\.bc]]" "-host=x86_64-unknown-linux-gnu" "-target=spir64_gen{{.*}}" "-kind=sycl" "[[OUTPUT4]]" // CHK-TOOLS-CPU: clang-offload-wrapper{{.*}} "-o=[[OUTPUT5:.+\.bc]]" "-host=x86_64-unknown-linux-gnu" "-target=spir64_x86_64{{.*}}" "-kind=sycl" "[[OUTPUT4]]" // CHK-TOOLS-AOT: llc{{.*}} "-filetype=obj" "-o" "[[OUTPUT6:.+\.o]]" "[[OUTPUT5]]" -// CHK-TOOLS-FPGA: clang{{.*}} "-triple" "spir64_fpga-unknown-unknown-sycldevice" {{.*}} "-fsycl-int-header=[[INPUT1:.+\.h]]" "-faddrsig" +// CHK-TOOLS-FPGA-USM-DISABLE: clang{{.*}} "-triple" "spir64_fpga-unknown-unknown-sycldevice" {{.*}} "-fsycl-int-header=[[INPUT1:.+\.h]]" "-faddrsig" +// CHK-TOOLS-FPGA-USM-ENABLE: clang{{.*}} "-triple" "spir64_fpga-unknown-unknown-sycldevice" {{.*}} "-fsycl-int-header=[[INPUT1:.+\.h]]" "-D__ENABLE_USM_ADDR_SPACE__" "-faddrsig" // CHK-TOOLS-GEN: clang{{.*}} "-triple" "spir64_gen-unknown-unknown-sycldevice" {{.*}} "-fsycl-int-header=[[INPUT1:.+\.h]]" "-faddrsig" // CHK-TOOLS-CPU: clang{{.*}} "-triple" "spir64_x86_64-unknown-unknown-sycldevice" {{.*}} "-fsycl-int-header=[[INPUT1:.+\.h]]" "-faddrsig" // CHK-TOOLS-AOT: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-include" "[[INPUT1]]" {{.*}} "-o" "[[OUTPUT7:.+\.o]]" diff --git a/sycl/include/CL/sycl/access/access.hpp b/sycl/include/CL/sycl/access/access.hpp index 0aaf4ebd0e89e..0b357d97d09cb 100644 --- a/sycl/include/CL/sycl/access/access.hpp +++ b/sycl/include/CL/sycl/access/access.hpp @@ -105,8 +105,13 @@ constexpr bool modeWritesNewData(access::mode m) { #ifdef __SYCL_DEVICE_ONLY__ #define __OPENCL_GLOBAL_AS__ __attribute__((opencl_global)) +#ifdef __ENABLE_USM_ADDR_SPACE__ #define __OPENCL_GLOBAL_DEVICE_AS__ __attribute__((opencl_global_device)) #define __OPENCL_GLOBAL_HOST_AS__ __attribute__((opencl_global_host)) +#else +#define __OPENCL_GLOBAL_DEVICE_AS__ __attribute__((opencl_global)) +#define __OPENCL_GLOBAL_HOST_AS__ __attribute__((opencl_global)) +#endif // __ENABLE_USM_ADDR_SPACE__ #define __OPENCL_LOCAL_AS__ __attribute__((opencl_local)) #define __OPENCL_CONSTANT_AS__ __attribute__((opencl_constant)) #define __OPENCL_PRIVATE_AS__ __attribute__((opencl_private)) @@ -124,10 +129,12 @@ template struct TargetToAS { access::address_space::global_space; }; +#ifdef __ENABLE_USM_ADDR_SPACE__ template <> struct TargetToAS { constexpr static access::address_space AS = access::address_space::global_device_space; }; +#endif // __ENABLE_USM_ADDR_SPACE__ template <> struct TargetToAS { constexpr static access::address_space AS = @@ -192,6 +199,7 @@ struct remove_AS<__OPENCL_GLOBAL_AS__ T> { typedef T type; }; +#ifdef __ENABLE_USM_ADDR_SPACE__ template struct remove_AS<__OPENCL_GLOBAL_DEVICE_AS__ T> { typedef T type; }; @@ -199,6 +207,7 @@ template struct remove_AS<__OPENCL_GLOBAL_DEVICE_AS__ T> { template struct remove_AS<__OPENCL_GLOBAL_HOST_AS__ T> { typedef T type; }; +#endif // __ENABLE_USM_ADDR_SPACE__ template struct remove_AS<__OPENCL_PRIVATE_AS__ T> { diff --git a/sycl/include/CL/sycl/atomic.hpp b/sycl/include/CL/sycl/atomic.hpp index 3509f5541f99f..58ff72ddeab26 100644 --- a/sycl/include/CL/sycl/atomic.hpp +++ b/sycl/include/CL/sycl/atomic.hpp @@ -198,6 +198,7 @@ class atomic { "T and pointerT must be same size"); } +#ifdef __ENABLE_USM_ADDR_SPACE__ // Create atomic in global_space with one from global_device_space template &&RHS) { Ptr = RHS.Ptr; } +#endif // __ENABLE_USM_ADDR_SPACE__ void store(T Operand, memory_order Order = memory_order::relaxed) { __spirv_AtomicStore( diff --git a/sycl/include/CL/sycl/handler.hpp b/sycl/include/CL/sycl/handler.hpp index f9801782c54fa..0a40efb33bd2e 100644 --- a/sycl/include/CL/sycl/handler.hpp +++ b/sycl/include/CL/sycl/handler.hpp @@ -506,7 +506,11 @@ class __SYCL_EXPORT handler { access::placeholder IsPH> detail::enable_if_t readFromFirstAccElement(accessor Src) const { +#ifdef __ENABLE_USM_ADDR_SPACE__ atomic AtomicSrc = Src; +#else + atomic AtomicSrc = Src; +#endif // __ENABLE_USM_ADDR_SPACE__ return AtomicSrc.load(); } @@ -529,7 +533,11 @@ class __SYCL_EXPORT handler { access::placeholder IsPH> detail::enable_if_t writeToFirstAccElement(accessor Dst, T V) const { +#ifdef __ENABLE_USM_ADDR_SPACE__ atomic AtomicDst = Dst; +#else + atomic AtomicDst = Dst; +#endif // __ENABLE_USM_ADDR_SPACE__ AtomicDst.store(V); } diff --git a/sycl/include/CL/sycl/multi_ptr.hpp b/sycl/include/CL/sycl/multi_ptr.hpp index 489e34a277ff2..a3c6bc4768c38 100644 --- a/sycl/include/CL/sycl/multi_ptr.hpp +++ b/sycl/include/CL/sycl/multi_ptr.hpp @@ -275,6 +275,7 @@ template class multi_ptr { return multi_ptr(m_Pointer - r); } +#ifdef __ENABLE_USM_ADDR_SPACE__ // Explicit conversion to global_space // Only available if Space == address_space::global_device_space || // Space == address_space::global_host_space @@ -290,6 +291,7 @@ template class multi_ptr { return multi_ptr( reinterpret_cast(m_Pointer)); } +#endif // __ENABLE_USM_ADDR_SPACE__ // Only if Space == global_space template