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 package hat.backend.jextracted;
26 
27 
28 import hat.ComputeContext;
29 import hat.Config;
30 import hat.KernelContext;
31 //import hat.backend.ffi.C99FFIBackend;
32 import hat.backend.Backend;
33 import hat.callgraph.KernelCallGraph;
34 import optkl.codebuilders.ScopedCodeBuilderContext;
35 
36 import java.lang.foreign.Arena;
37 import java.lang.invoke.MethodHandle;
38 import java.lang.invoke.MethodHandles;
39 
40 public class OpenCLBackend extends C99JExtractedBackend {
41 
42 
43     final MethodHandle getBackend_MH;
44     public long getBackend(int mode, int platform, int device) {
45         try {
46          //   backendHandle = (long) getBackend_MH.invoke(mode, platform, device);
47         } catch (Throwable throwable) {
48             throw new IllegalStateException(throwable);
49         }
50         return 0l;//backendHandle;
51     }
52 
53     public OpenCLBackend(Config config) {
54         super(Arena.global(), MethodHandles.lookup(),config,"opencl_backend");
55         getBackend_MH  = null;// nativeLibrary.longFunc("getBackend",JAVA_INT,JAVA_INT, JAVA_INT);
56         getBackend(0,0,0);
57         info();
58     }
59     public OpenCLBackend() { // Ignore Intellij's no usages here, we load vai serviceloader!
60         this(Config.fromEnvOrProperty());
61     }
62 
63     @Override
64     public void computeContextHandoff(ComputeContext computeContext) {
65         //System.out.println("OpenCL backend received computeContext");
66         computeContext.computeEntrypoint().funcOp(injectBufferTracking(config(),lookup(),computeContext.computeEntrypoint().funcOp()));
67     }
68 
69     @Override
70     public void dispatchKernel(KernelCallGraph kernelCallGraph, KernelContext kernelContext, Object... args) {
71         //System.out.println("OpenCL backend dispatching kernel " + kernelCallGraph.entrypoint.method);
72         CompiledKernel compiledKernel = kernelCallGraphCompiledCodeMap.computeIfAbsent(kernelCallGraph, (_) -> {
73             String code = createCode(kernelCallGraph, new OpenCLJExtractedHATKernelBuilder(new ScopedCodeBuilderContext(kernelCallGraph.lookup(),kernelCallGraph.entrypoint.funcOp())), args);
74             System.out.println(code);
75             long programHandle = compileProgram(code);
76             if (programOK(programHandle)) {
77                 long kernelHandle = getKernel(programHandle, kernelCallGraph.entrypoint.name());
78                 return new CompiledKernel(this, kernelCallGraph, code, kernelHandle, args);
79             } else {
80                 throw new IllegalStateException("opencl failed to compile ");
81             }
82         });
83         compiledKernel.dispatch(kernelContext,args);
84 
85     }
86 }