|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 3 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 3 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 3 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package com.oracle.truffle.r.ffi.impl.altrep; |
| 24 | + |
| 25 | +import com.oracle.truffle.api.CompilerAsserts; |
| 26 | +import com.oracle.truffle.api.TruffleLanguage.ContextReference; |
| 27 | +import com.oracle.truffle.api.dsl.Cached; |
| 28 | +import com.oracle.truffle.api.dsl.CachedContext; |
| 29 | +import com.oracle.truffle.api.dsl.GenerateUncached; |
| 30 | +import com.oracle.truffle.api.dsl.Specialization; |
| 31 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 32 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 33 | +import com.oracle.truffle.api.nodes.ExplodeLoop; |
| 34 | +import com.oracle.truffle.api.profiles.BranchProfile; |
| 35 | +import com.oracle.truffle.api.profiles.ConditionProfile; |
| 36 | +import com.oracle.truffle.api.profiles.ValueProfile; |
| 37 | +import com.oracle.truffle.r.ffi.impl.llvm.TruffleLLVM_Context; |
| 38 | +import com.oracle.truffle.r.ffi.impl.nfi.TruffleNFI_Context; |
| 39 | +import com.oracle.truffle.r.runtime.RInternalError; |
| 40 | +import com.oracle.truffle.r.runtime.context.RContext; |
| 41 | +import com.oracle.truffle.r.runtime.context.TruffleRLanguage; |
| 42 | +import com.oracle.truffle.r.runtime.data.altrep.AltrepMethodDescriptor; |
| 43 | +import com.oracle.truffle.r.runtime.ffi.AfterDownCallProfiles; |
| 44 | +import com.oracle.truffle.r.runtime.ffi.FFIMaterializeNode; |
| 45 | +import com.oracle.truffle.r.runtime.ffi.FFIToNativeMirrorNode; |
| 46 | +import com.oracle.truffle.r.runtime.ffi.FFIUnwrapNode; |
| 47 | +import com.oracle.truffle.r.runtime.ffi.FFIUnwrapNodeGen; |
| 48 | +import com.oracle.truffle.r.runtime.ffi.FFIWrap.FFIDownCallWrap; |
| 49 | +import com.oracle.truffle.r.runtime.ffi.RFFIFactory.Type; |
| 50 | +import com.oracle.truffle.r.runtime.nodes.altrep.AltrepDownCallNode; |
| 51 | + |
| 52 | +@GenerateUncached |
| 53 | +public abstract class AltrepDownCallNodeImpl extends AltrepDownCallNode { |
| 54 | + @Override |
| 55 | + public abstract Object execute(AltrepMethodDescriptor altrepDowncall, boolean unwrapResult, boolean[] wrapArguments, Object[] args); |
| 56 | + |
| 57 | + public static AltrepDownCallNodeImpl create() { |
| 58 | + return AltrepDownCallNodeImplNodeGen.create(); |
| 59 | + } |
| 60 | + |
| 61 | + public static AltrepDownCallNodeImpl getUncached() { |
| 62 | + return AltrepDownCallNodeImplNodeGen.getUncached(); |
| 63 | + } |
| 64 | + |
| 65 | + @Specialization(limit = "3") |
| 66 | + public Object doIt(AltrepMethodDescriptor altrepDowncallIn, boolean unwrapResult, boolean[] wrapArguments, Object[] args, |
| 67 | + @CachedLibrary("altrepDowncallIn.method") InteropLibrary methodInterop, |
| 68 | + @Cached(value = "createUnwrapNode(unwrapResult)", uncached = "createUncachedUnwrapNode()") FFIUnwrapNode unwrapNode, |
| 69 | + @CachedContext(TruffleRLanguage.class) ContextReference<RContext> ctxRef, |
| 70 | + @Cached(value = "createMaterialized(wrapArguments)", allowUncached = true) FFIMaterializeNode[] materializeNodes, |
| 71 | + @Cached(value = "createToNatives(wrapArguments)", allowUncached = true) FFIToNativeMirrorNode[] toNativeNodes, |
| 72 | + @Cached("createBinaryProfile()") ConditionProfile isLLVMProfile, |
| 73 | + @Cached BranchProfile unwrapResultProfile, |
| 74 | + @Cached("createIdentityProfile()") ValueProfile identityProfile, |
| 75 | + @Cached AfterDownCallProfiles afterDownCallProfiles) { |
| 76 | + CompilerAsserts.partialEvaluationConstant(unwrapResult); |
| 77 | + CompilerAsserts.partialEvaluationConstant(args.length); |
| 78 | + AltrepMethodDescriptor altrepMethodDescriptor = identityProfile.profile(altrepDowncallIn); |
| 79 | + |
| 80 | + assert methodInterop.isExecutable(altrepMethodDescriptor.method); |
| 81 | + RContext ctx = ctxRef.get(); |
| 82 | + |
| 83 | + Object before = null; |
| 84 | + if (isLLVMProfile.profile(altrepMethodDescriptor.rffiType == Type.LLVM)) { |
| 85 | + before = ctx.getRFFI(TruffleLLVM_Context.class).beforeDowncall(null, altrepMethodDescriptor.rffiType); |
| 86 | + } else { |
| 87 | + before = ctx.getRFFI(TruffleNFI_Context.class).beforeDowncall(null, altrepMethodDescriptor.rffiType); |
| 88 | + } |
| 89 | + |
| 90 | + Object ret; |
| 91 | + try (FFIDownCallWrap ffiWrap = new FFIDownCallWrap(args.length)) { |
| 92 | + Object[] wrappedArgs = ffiWrap.wrapSome(args, materializeNodes, toNativeNodes, wrapArguments); |
| 93 | + ret = methodInterop.execute(altrepMethodDescriptor.method, wrappedArgs); |
| 94 | + if (unwrapResult) { |
| 95 | + unwrapResultProfile.enter(); |
| 96 | + ret = unwrapNode.execute(ret); |
| 97 | + } |
| 98 | + } catch (Exception ex) { |
| 99 | + throw RInternalError.shouldNotReachHere(ex); |
| 100 | + } |
| 101 | + |
| 102 | + if (isLLVMProfile.profile(altrepMethodDescriptor.rffiType == Type.LLVM)) { |
| 103 | + ctx.getRFFI(TruffleLLVM_Context.class).afterDowncall(before, altrepMethodDescriptor.rffiType, afterDownCallProfiles); |
| 104 | + } else { |
| 105 | + ctx.getRFFI(TruffleNFI_Context.class).afterDowncall(before, altrepMethodDescriptor.rffiType, afterDownCallProfiles); |
| 106 | + } |
| 107 | + |
| 108 | + return ret; |
| 109 | + } |
| 110 | + |
| 111 | + protected static FFIUnwrapNode createUnwrapNode(boolean unwrapFlag) { |
| 112 | + if (unwrapFlag) { |
| 113 | + return FFIUnwrapNode.create(); |
| 114 | + } else { |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + protected static FFIUnwrapNode createUncachedUnwrapNode() { |
| 120 | + return FFIUnwrapNodeGen.getUncached(); |
| 121 | + } |
| 122 | + |
| 123 | + @ExplodeLoop |
| 124 | + protected static FFIMaterializeNode[] createMaterialized(boolean[] wrapArguments) { |
| 125 | + return FFIMaterializeNode.create(wrapArguments.length); |
| 126 | + } |
| 127 | + |
| 128 | + @ExplodeLoop |
| 129 | + protected static FFIToNativeMirrorNode[] createToNatives(boolean[] wrapArguments) { |
| 130 | + return FFIToNativeMirrorNode.create(wrapArguments.length); |
| 131 | + } |
| 132 | +} |
0 commit comments