1bd882c8aSJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2bd882c8aSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3bd882c8aSJames Wright // 4bd882c8aSJames Wright // SPDX-License-Identifier: BSD-2-Clause 5bd882c8aSJames Wright // 6bd882c8aSJames Wright // This file is part of CEED: http://github.com/ceed 7bd882c8aSJames Wright 8bd882c8aSJames Wright #include "ceed-sycl-compile.hpp" 9bd882c8aSJames Wright 10bd882c8aSJames Wright #include <ceed/backend.h> 11bd882c8aSJames Wright #include <ceed/ceed.h> 12bd882c8aSJames Wright #include <ceed/jit-tools.h> 13bd882c8aSJames Wright #include <level_zero/ze_api.h> 14bd882c8aSJames Wright 15bd882c8aSJames Wright #include <map> 16bd882c8aSJames Wright #include <sstream> 17bd882c8aSJames Wright #include <sycl/sycl.hpp> 18bd882c8aSJames Wright 19bd882c8aSJames Wright #include "./online_compiler.hpp" 20bd882c8aSJames Wright #include "ceed-sycl-common.hpp" 21bd882c8aSJames Wright 22bd882c8aSJames Wright using ByteVector_t = std::vector<unsigned char>; 23bd882c8aSJames Wright 24bd882c8aSJames Wright //------------------------------------------------------------------------------ 25bd882c8aSJames Wright // 26bd882c8aSJames Wright //------------------------------------------------------------------------------ 27bd882c8aSJames Wright static int CeedJitAddDefinitions_Sycl(Ceed ceed, const std::string &kernel_source, std::string &jit_source, 28bd882c8aSJames Wright const std::map<std::string, CeedInt> &constants = {}) { 29bd882c8aSJames Wright std::ostringstream oss; 30bd882c8aSJames Wright 31bd882c8aSJames Wright // Prepend defined constants 32bd882c8aSJames Wright for (const auto &[name, value] : constants) { 33bd882c8aSJames Wright oss << "#define " << name << " " << value << "\n"; 34bd882c8aSJames Wright } 35bd882c8aSJames Wright 36bd882c8aSJames Wright // libCeed definitions for Sycl Backends 37bd882c8aSJames Wright char *jit_defs_path, *jit_defs_source; 38bd882c8aSJames Wright const char *sycl_jith_path = "ceed/jit-source/sycl/sycl-jit.h"; 39bd882c8aSJames Wright CeedCallBackend(CeedGetJitAbsolutePath(ceed, sycl_jith_path, &jit_defs_path)); 40bd882c8aSJames Wright CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source)); 41bd882c8aSJames Wright 42bd882c8aSJames Wright oss << jit_defs_source << "\n"; 43bd882c8aSJames Wright 44bd882c8aSJames Wright CeedCallBackend(CeedFree(&jit_defs_path)); 45bd882c8aSJames Wright CeedCallBackend(CeedFree(&jit_defs_source)); 46bd882c8aSJames Wright 47bd882c8aSJames Wright // Append kernel_source 48bd882c8aSJames Wright oss << "\n" << kernel_source; 49bd882c8aSJames Wright 50bd882c8aSJames Wright jit_source = oss.str(); 51bd882c8aSJames Wright 52bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 53bd882c8aSJames Wright } 54bd882c8aSJames Wright 55bd882c8aSJames Wright //------------------------------------------------------------------------------ 56bd882c8aSJames Wright // TODO: Add architecture flags, optimization flags 57bd882c8aSJames Wright //------------------------------------------------------------------------------ 58bd882c8aSJames Wright static inline int CeedJitGetFlags_Sycl(std::vector<std::string> &flags) { 59bd882c8aSJames Wright flags = {std::string("-cl-std=CL3.0"), std::string("-Dint32_t=int")}; 60bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 61bd882c8aSJames Wright } 62bd882c8aSJames Wright 63bd882c8aSJames Wright //------------------------------------------------------------------------------ 64bd882c8aSJames Wright // Compile an OpenCL source to SPIR-V using Intel's online compiler extension 65bd882c8aSJames Wright //------------------------------------------------------------------------------ 66bd882c8aSJames Wright static inline int CeedJitCompileSource_Sycl(Ceed ceed, const sycl::device &sycl_device, const std::string &opencl_source, ByteVector_t &il_binary, 67bd882c8aSJames Wright const std::vector<std::string> &flags = {}) { 68bd882c8aSJames Wright sycl::ext::libceed::online_compiler<sycl::ext::libceed::source_language::opencl_c> compiler(sycl_device); 69bd882c8aSJames Wright 70bd882c8aSJames Wright try { 71bd882c8aSJames Wright il_binary = compiler.compile(opencl_source, flags); 72bd882c8aSJames Wright } catch (sycl::ext::libceed::online_compile_error &e) { 73bd882c8aSJames Wright return CeedError((ceed), CEED_ERROR_BACKEND, e.what()); 74bd882c8aSJames Wright } 75bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 76bd882c8aSJames Wright } 77bd882c8aSJames Wright 78bd882c8aSJames Wright // ------------------------------------------------------------------------------ 79bd882c8aSJames Wright // Load (compile) SPIR-V source and wrap in sycl kernel_bundle 80bd882c8aSJames Wright // TODO: determine appropriate flags 81bd882c8aSJames Wright // TODO: Error handle lz calls 82bd882c8aSJames Wright // ------------------------------------------------------------------------------ 83bd882c8aSJames Wright static int CeedJitLoadModule_Sycl(const sycl::context &sycl_context, const sycl::device &sycl_device, const ByteVector_t &il_binary, 84bd882c8aSJames Wright SyclModule_t **sycl_module) { 85bd882c8aSJames Wright auto lz_context = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(sycl_context); 86bd882c8aSJames Wright auto lz_device = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(sycl_device); 87bd882c8aSJames Wright 88bd882c8aSJames Wright ze_module_desc_t lz_mod_desc = {ZE_STRUCTURE_TYPE_MODULE_DESC, 89bd882c8aSJames Wright nullptr, 90bd882c8aSJames Wright ZE_MODULE_FORMAT_IL_SPIRV, 91bd882c8aSJames Wright il_binary.size(), 92bd882c8aSJames Wright il_binary.data(), 93bd882c8aSJames Wright " -ze-opt-large-register-file", // flags 94bd882c8aSJames Wright nullptr}; // build log 95bd882c8aSJames Wright 96bd882c8aSJames Wright ze_module_handle_t lz_module; 97bd882c8aSJames Wright zeModuleCreate(lz_context, lz_device, &lz_mod_desc, &lz_module, nullptr); 98bd882c8aSJames Wright 99bd882c8aSJames Wright // sycl make_<type> only throws errors for backend mismatch--assume we have vetted this already 100bd882c8aSJames Wright *sycl_module = new SyclModule_t(sycl::make_kernel_bundle<sycl::backend::ext_oneapi_level_zero, sycl::bundle_state::executable>( 101bd882c8aSJames Wright {lz_module, sycl::ext::oneapi::level_zero::ownership::transfer}, sycl_context)); 102bd882c8aSJames Wright 103bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 104bd882c8aSJames Wright } 105bd882c8aSJames Wright 106bd882c8aSJames Wright // ------------------------------------------------------------------------------ 107bd882c8aSJames Wright // Compile kernel source to an executable `sycl::kernel_bundle` 108bd882c8aSJames Wright // ------------------------------------------------------------------------------ 109*eb7e6cafSJeremy L Thompson int CeedBuildModule_Sycl(Ceed ceed, const std::string &kernel_source, SyclModule_t **sycl_module, const std::map<std::string, CeedInt> &constants) { 110bd882c8aSJames Wright Ceed_Sycl *data; 111bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 112bd882c8aSJames Wright 113bd882c8aSJames Wright std::string jit_source; 114bd882c8aSJames Wright CeedCallBackend(CeedJitAddDefinitions_Sycl(ceed, kernel_source, jit_source, constants)); 115bd882c8aSJames Wright 116bd882c8aSJames Wright std::vector<std::string> flags; 117bd882c8aSJames Wright CeedCallBackend(CeedJitGetFlags_Sycl(flags)); 118bd882c8aSJames Wright 119bd882c8aSJames Wright ByteVector_t il_binary; 120bd882c8aSJames Wright CeedCallBackend(CeedJitCompileSource_Sycl(ceed, data->sycl_device, jit_source, il_binary, flags)); 121bd882c8aSJames Wright 122bd882c8aSJames Wright CeedCallBackend(CeedJitLoadModule_Sycl(data->sycl_context, data->sycl_device, il_binary, sycl_module)); 123bd882c8aSJames Wright 124bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 125bd882c8aSJames Wright } 126bd882c8aSJames Wright 127bd882c8aSJames Wright // ------------------------------------------------------------------------------ 128bd882c8aSJames Wright // Get a sycl kernel from an existing kernel_bundle 129bd882c8aSJames Wright // 130bd882c8aSJames Wright // TODO: Error handle lz calls 131bd882c8aSJames Wright // ------------------------------------------------------------------------------ 132*eb7e6cafSJeremy L Thompson int CeedGetKernel_Sycl(Ceed ceed, const SyclModule_t *sycl_module, const std::string &kernel_name, sycl::kernel **sycl_kernel) { 133bd882c8aSJames Wright Ceed_Sycl *data; 134bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &data)); 135bd882c8aSJames Wright 136bd882c8aSJames Wright // sycl::get_native returns std::vector<ze_module_handle_t> for lz backend 137bd882c8aSJames Wright // https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/supported/sycl_ext_oneapi_backend_level_zero.md 138bd882c8aSJames Wright ze_module_handle_t lz_module = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(*sycl_module).front(); 139bd882c8aSJames Wright 140bd882c8aSJames Wright ze_kernel_desc_t lz_kernel_desc = {ZE_STRUCTURE_TYPE_KERNEL_DESC, nullptr, 0, kernel_name.c_str()}; 141bd882c8aSJames Wright ze_kernel_handle_t lz_kernel; 142bd882c8aSJames Wright zeKernelCreate(lz_module, &lz_kernel_desc, &lz_kernel); 143bd882c8aSJames Wright 144bd882c8aSJames Wright *sycl_kernel = new sycl::kernel(sycl::make_kernel<sycl::backend::ext_oneapi_level_zero>( 145bd882c8aSJames Wright {*sycl_module, lz_kernel, sycl::ext::oneapi::level_zero::ownership::transfer}, data->sycl_context)); 146bd882c8aSJames Wright 147bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 148bd882c8aSJames Wright } 149