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 final: public Backend {
 28 public:
 29 
 30     class SpirvProgram final : public CompilationUnit {
 31         class SpirvKernel final : public Kernel {
 32         public:
 33             SpirvKernel(CompilationUnit *compilationUnit, char *name)
 34                     : Kernel(compilationUnit, name) {
 35             }
 36 
 37             ~SpirvKernel() override = default;
 38             bool setArg(KernelArg *arg, Buffer *buffer) override{
 39                 return false ;
 40             }
 41             bool setArg(KernelArg *arg) override{
 42                 return false ;
 43             }
 44         };
 45 
 46     public:
 47         SpirvProgram(Backend *backend, char *src, char *log, bool ok)
 48                 : CompilationUnit(backend,src,log,ok) {
 49         }
 50 
 51         ~SpirvProgram() override = default;
 52 
 53         Kernel *getKernel(int nameLen, char *name) override {
 54             return new SpirvKernel(this, name);
 55         }
 56     };
 57     class SpirvQueue final : public Queue{
 58     public:
 59         void wait()override{};
 60         void release()override{};
 61         void computeStart()override{};
 62         void computeEnd()override{};
 63         void dispatch(KernelContext *kernelContext, CompilationUnit::Kernel *kernel) override{
 64             std::cout << "spirv dispatch() " << std::endl;
 65             if (backend->config->trace | backend->config->traceEnqueues){
 66                 std::cout << "enqueued kernel dispatch \""<< kernel->name <<"\" globalSize=" << kernelContext->maxX << std::endl;
 67             }
 68 
 69         }
 70         void copyToDevice(Buffer *buffer) override{}
 71         void copyFromDevice(Buffer *buffer) override{};
 72         explicit SpirvQueue(Backend *backend):Queue(backend){}
 73         ~SpirvQueue() override =default;
 74     };
 75 public:
 76     explicit SpirvBackend(const int mode): Backend(new Config(mode), new SpirvQueue(this)) {
 77     }
 78 
 79     ~SpirvBackend() override = default;
 80 
 81     Buffer * getOrCreateBuffer(BufferState *bufferState) override{
 82         Buffer *buffer = nullptr;
 83 
 84       /* if (bufferState->vendorPtr == 0L || bufferState->state == BufferState::NEW_STATE){
 85             openclBuffer = new OpenCLBuffer(this,  bufferState);
 86             if (openclConfig.trace){
 87                 std::cout << "We allocated arg buffer "<<std::endl;
 88             }
 89         }else{
 90             if (openclConfig.trace){
 91                 std::cout << "Were reusing  buffer  buffer "<<std::endl;
 92             }
 93             openclBuffer=  static_cast<OpenCLBuffer*>(bufferState->vendorPtr);
 94         }*/
 95         return buffer;
 96     }
 97     bool getBufferFromDeviceIfDirty(void *memorySegment, long memorySegmentLength) override {
 98         std::cout << "attempting  to get buffer from SpirvBackend "<<std::endl;
 99         return false;
100     }
101     void info() override{
102         std::cout << "spirv info()" << std::endl;
103     }
104      void computeStart() override{
105        std::cout << "spirv compute start()" << std::endl;
106      }
107         void computeEnd() override {
108           std::cout << "spirv compute start()" << std::endl;
109         }
110 
111     CompilationUnit* compile(int len, char *source) override{
112         std::cout << "spirv compile()" << std::endl;
113         const size_t srcLen = ::strlen(source);
114         char *src = new char[srcLen + 1];
115         strncpy(src, source, srcLen);
116         src[srcLen] = '\0';
117         std::cout << "native compiling " << src << std::endl;
118 
119         const auto spirvProgram = new SpirvProgram(this,  src, nullptr, false);
120         return spirvProgram;
121     }
122 };
123 
124 long getBackend(const int mode) {
125     return reinterpret_cast<long>(new SpirvBackend(mode));
126 }