1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 #include "shared.h" 26 27 class SpirvBackend : public Backend { 28 public: 29 30 class SpirvProgram : public Backend::CompilationUnit { 31 class SpirvKernel : public Backend::CompilationUnit::Kernel { 32 public: 33 SpirvKernel(Backend::CompilationUnit *compilationUnit, char *name) 34 : Backend::CompilationUnit::Kernel(compilationUnit, name) { 35 } 36 37 ~SpirvKernel() { 38 } 39 bool setArg(KernelArg *arg, Buffer *buffer) override{ 40 return false ; 41 } 42 bool setArg(KernelArg *arg) override{ 43 return false ; 44 } 45 }; 46 47 public: 48 SpirvProgram(Backend *backend, char *src, char *log, bool ok) 49 : Backend::CompilationUnit(backend,src,log,ok) { 50 } 51 52 ~SpirvProgram() { 53 } 54 55 Kernel *getKernel(int nameLen, char *name) { 56 return new SpirvKernel(this, name); 57 } 58 }; 59 class SpirvQueue: public Backend::Queue{ 60 public: 61 void wait()override{}; 62 void release()override{}; 63 void computeStart()override{}; 64 void computeEnd()override{}; 65 void dispatch(KernelContext *kernelContext, Backend::CompilationUnit::Kernel *kernel) override{ 66 std::cout << "spirv dispatch() " << std::endl; 67 size_t dims = 1; 68 if (backend->config->trace | backend->config->traceEnqueues){ 69 std::cout << "enqueued kernel dispatch \""<< kernel->name <<"\" globalSize=" << kernelContext->maxX << std::endl; 70 } 71 72 } 73 void copyToDevice(Buffer *buffer) override{} 74 void copyFromDevice(Buffer *buffer) override{}; 75 explicit SpirvQueue(Backend *backend):Queue(backend){} 76 ~SpirvQueue() override =default; 77 }; 78 public: 79 SpirvBackend(int mode): Backend(new Config(mode), new SpirvQueue(this)) { 80 } 81 82 ~SpirvBackend() { 83 } 84 85 Buffer * getOrCreateBuffer(BufferState *bufferState) override{ 86 Buffer *buffer = nullptr; 87 88 /* if (bufferState->vendorPtr == 0L || bufferState->state == BufferState::NEW_STATE){ 89 openclBuffer = new OpenCLBuffer(this, bufferState); 90 if (openclConfig.trace){ 91 std::cout << "We allocated arg buffer "<<std::endl; 92 } 93 }else{ 94 if (openclConfig.trace){ 95 std::cout << "Were reusing buffer buffer "<<std::endl; 96 } 97 openclBuffer= static_cast<OpenCLBuffer*>(bufferState->vendorPtr); 98 }*/ 99 return buffer; 100 } 101 bool getBufferFromDeviceIfDirty(void *memorySegment, long memorySegmentLength) override { 102 std::cout << "attempting to get buffer from SpirvBackend "<<std::endl; 103 return false; 104 } 105 void info() override{ 106 std::cout << "spirv info()" << std::endl; 107 } 108 void computeStart() override{ 109 std::cout << "spirv compute start()" << std::endl; 110 } 111 void computeEnd() override { 112 std::cout << "spirv compute start()" << std::endl; 113 } 114 115 CompilationUnit* compile(int len, char *source) override{ 116 std::cout << "spirv compile()" << std::endl; 117 size_t srcLen = ::strlen(source); 118 char *src = new char[srcLen + 1]; 119 ::strncpy(src, source, srcLen); 120 src[srcLen] = '\0'; 121 std::cout << "native compiling " << src << std::endl; 122 123 SpirvProgram *spirvProgram = new SpirvProgram(this, src, nullptr, false); 124 return dynamic_cast<CompilationUnit*>(spirvProgram); 125 } 126 }; 127 128 long getBackend(int mode) { 129 return reinterpret_cast<long>(new SpirvBackend(mode)); 130 }