Skip to content

Commit 6d1b078

Browse files
tomasstupkasteve-s
authored andcommitted
when in mixed mode, create DownCallNodeFactory-s depending on specified BackEndLLVM/Native list
1 parent 3b34843 commit 6d1b078

File tree

4 files changed

+94
-55
lines changed

4 files changed

+94
-55
lines changed

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/mixed/TruffleMixed_Context.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -52,11 +52,18 @@ public final class TruffleMixed_Context extends RFFIContext {
5252
private final TruffleNFI_Context nfiContext;
5353

5454
TruffleMixed_Context(RFFIContextState rffiContextState) {
55-
super(rffiContextState, new TruffleMixed_C(), new BaseRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE, TruffleNFI_DownCallNodeFactory.INSTANCE), new TruffleMixed_Call(), new TruffleMixed_DLL(),
55+
super(rffiContextState, new TruffleMixed_C(),
56+
createBaseDowncallNode(),
57+
new TruffleMixed_Call(),
58+
new TruffleMixed_DLL(),
5659
new TruffleLLVM_UserRng(),
57-
new ZipRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE), new PCRERFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE),
58-
new LapackRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE), new StatsRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE),
59-
new ToolsRFFI(), new REmbedRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE), new MiscRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE));
60+
new ZipRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE),
61+
new PCRERFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE),
62+
new LapackRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE),
63+
createStatsDowncallNode(),
64+
new ToolsRFFI(),
65+
new REmbedRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE),
66+
new MiscRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE));
6067
llvmContext = new TruffleLLVM_Context(rffiContextState) {
6168
@Override
6269
protected void addLibRToDLLContextState(RContext context, DLLInfo libR) {
@@ -75,6 +82,15 @@ protected void addLibRToDLLContextState(RContext context, DLLInfo libR) {
7582

7683
}
7784

85+
private static StatsRFFI createStatsDowncallNode() {
86+
return new StatsRFFI(RContext.getInstance().isLLVMPackage("stats") ? TruffleLLVM_DownCallNodeFactory.INSTANCE : TruffleNFI_DownCallNodeFactory.INSTANCE);
87+
}
88+
89+
private static BaseRFFI createBaseDowncallNode() {
90+
return RContext.getInstance().isLLVMPackage("base") ? new BaseRFFI(TruffleLLVM_DownCallNodeFactory.INSTANCE, TruffleNFI_DownCallNodeFactory.INSTANCE)
91+
: new BaseRFFI(TruffleNFI_DownCallNodeFactory.INSTANCE, TruffleNFI_DownCallNodeFactory.INSTANCE);
92+
}
93+
7894
@Override
7995
public Object getSulongArrayType(Object arrayElement) {
8096
return llvmContext.getSulongArrayType(arrayElement);

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/mixed/TruffleMixed_DLL.java

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,8 +24,6 @@
2424

2525
import com.oracle.truffle.api.TruffleFile;
2626
import com.oracle.truffle.api.TruffleLanguage;
27-
import java.util.HashSet;
28-
import java.util.Set;
2927

3028
import com.oracle.truffle.api.dsl.Cached;
3129
import com.oracle.truffle.api.dsl.CachedContext;
@@ -38,7 +36,6 @@
3836
import com.oracle.truffle.r.ffi.impl.mixed.TruffleMixed_DLLFactory.TruffleMixed_DLSymNodeGen;
3937
import com.oracle.truffle.r.ffi.impl.nfi.TruffleNFI_DLL;
4038
import com.oracle.truffle.r.ffi.impl.nfi.TruffleNFI_DLL.NFIHandle;
41-
import com.oracle.truffle.r.runtime.context.FastROptions;
4239
import com.oracle.truffle.r.runtime.context.RContext;
4340
import com.oracle.truffle.r.runtime.context.TruffleRLanguage;
4441
import com.oracle.truffle.r.runtime.data.RDataFactory;
@@ -51,47 +48,7 @@ public class TruffleMixed_DLL implements DLLRFFI {
5148
private final TruffleLLVM_DLL llvmDllRFFI = new TruffleLLVM_DLL();
5249
private final TruffleNFI_DLL nfiDllRFFI = new TruffleNFI_DLL();
5350

54-
private final Set<String> explicitPackages;
55-
private final boolean isLLVMDefault;
56-
5751
TruffleMixed_DLL() {
58-
if ("llvm".equals(System.getenv().get("FASTR_RFFI"))) {
59-
isLLVMDefault = true;
60-
explicitPackages = java.util.Collections.emptySet();
61-
} else {
62-
String backendOpt = RContext.getInstance().getOption(FastROptions.BackEnd);
63-
String explicitPkgsOpt;
64-
if ("native".equals(backendOpt)) {
65-
isLLVMDefault = false;
66-
explicitPkgsOpt = RContext.getInstance().getOption(FastROptions.BackEndLLVM);
67-
} else {
68-
// llvm
69-
isLLVMDefault = true;
70-
explicitPkgsOpt = RContext.getInstance().getOption(FastROptions.BackEndNative);
71-
}
72-
73-
String[] explicitPkgsOptSplit = explicitPkgsOpt == null ? null : explicitPkgsOpt.split(",");
74-
if (explicitPkgsOptSplit == null || explicitPkgsOptSplit.length == 0) {
75-
explicitPackages = java.util.Collections.emptySet();
76-
} else {
77-
explicitPackages = new HashSet<>();
78-
for (String pkg : explicitPkgsOptSplit) {
79-
explicitPackages.add(pkg);
80-
}
81-
}
82-
}
83-
}
84-
85-
boolean isLLVMPackage(TruffleFile libPath) {
86-
if (explicitPackages.isEmpty()) {
87-
return isLLVMDefault;
88-
}
89-
90-
assert libPath != null;
91-
String libName = libPath.getName();
92-
libName = libName.substring(0, libName.lastIndexOf('.'));
93-
boolean isExplicitPkg = explicitPackages.contains(libName);
94-
return isExplicitPkg ^ isLLVMDefault;
9552
}
9653

9754
@Override
@@ -113,23 +70,22 @@ abstract static class TruffleMixed_DLOpenNode extends Node implements DLOpenNode
11370

11471
private final DLOpenNode llvmDllOpenNode;
11572
private final DLOpenNode nfiDllOpenNode;
116-
private final TruffleMixed_DLL dllRffi;
11773

11874
TruffleMixed_DLOpenNode(TruffleMixed_DLL dllRffi) {
119-
this.dllRffi = dllRffi;
12075
this.llvmDllOpenNode = dllRffi.llvmDllRFFI.createDLOpenNode();
12176
this.nfiDllOpenNode = dllRffi.nfiDllRFFI.createDLOpenNode();
12277
}
12378

12479
@Specialization
12580
public LibHandle exec(String path, boolean local, boolean now,
12681
@CachedContext(TruffleRLanguage.class) TruffleLanguage.ContextReference<RContext> ctxRef) throws UnsatisfiedLinkError {
127-
TruffleFile libPath = ctxRef.get().getSafeTruffleFile(path);
82+
RContext context = ctxRef.get();
83+
TruffleFile libPath = context.getSafeTruffleFile(path);
12884
if (!libPath.exists()) {
12985
throw new UnsatisfiedLinkError(String.format("Shared library %s not found", path));
13086
}
13187

132-
boolean useLLVM = dllRffi.isLLVMPackage(libPath);
88+
boolean useLLVM = context.isLLVMPackage(libPath);
13389

13490
LibHandle nfiLibHandle = nfiDllOpenNode.execute(path, local, now);
13591
if (useLLVM) {

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/FastROptions.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
2626
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2727
import com.oracle.truffle.api.Option;
28+
import com.oracle.truffle.api.TruffleFile;
2829
import com.oracle.truffle.r.runtime.DSLConfig;
2930
import com.oracle.truffle.r.runtime.FastRConfig;
3031
import com.oracle.truffle.r.runtime.RError;
@@ -34,8 +35,10 @@
3435
import com.oracle.truffle.r.runtime.RRuntime;
3536
import com.oracle.truffle.r.runtime.data.NativeDataAccess.NativeDataInspector;
3637
import java.util.HashMap;
38+
import java.util.HashSet;
3739
import java.util.Iterator;
3840
import java.util.Map;
41+
import java.util.Set;
3942
import org.graalvm.options.OptionCategory;
4043
import org.graalvm.options.OptionDescriptor;
4144
import org.graalvm.options.OptionDescriptors;
@@ -291,4 +294,60 @@ public static String getName(OptionKey<?> key) {
291294
throw RInternalError.shouldNotReachHere();
292295
}
293296

297+
private Set<String> explicitPackages = null;
298+
private boolean isLLVMDefault;
299+
300+
boolean isLLVMPackage(TruffleFile libPath) {
301+
if (explicitPackages == null) {
302+
initLLVMPackages();
303+
}
304+
if (explicitPackages.isEmpty()) {
305+
return isLLVMDefault;
306+
}
307+
308+
assert libPath != null;
309+
String libName = libPath.getName();
310+
libName = libName.substring(0, libName.lastIndexOf('.'));
311+
return isLLVMPackage(libName);
312+
}
313+
314+
boolean isLLVMPackage(String libName) {
315+
if (explicitPackages == null) {
316+
initLLVMPackages();
317+
}
318+
if (explicitPackages.isEmpty()) {
319+
return isLLVMDefault;
320+
}
321+
322+
boolean isExplicitPkg = explicitPackages.contains(libName);
323+
return isExplicitPkg ^ isLLVMDefault;
324+
}
325+
326+
private void initLLVMPackages() {
327+
if ("llvm".equals(System.getenv().get("FASTR_RFFI"))) {
328+
isLLVMDefault = true;
329+
explicitPackages = java.util.Collections.emptySet();
330+
} else {
331+
String backendOpt = getValue(FastROptions.BackEnd);
332+
String explicitPkgsOpt;
333+
if ("native".equals(backendOpt)) {
334+
isLLVMDefault = false;
335+
explicitPkgsOpt = getValue(FastROptions.BackEndLLVM);
336+
} else {
337+
// llvm
338+
isLLVMDefault = true;
339+
explicitPkgsOpt = getValue(FastROptions.BackEndNative);
340+
}
341+
342+
String[] explicitPkgsOptSplit = explicitPkgsOpt == null ? null : explicitPkgsOpt.split(",");
343+
if (explicitPkgsOptSplit == null || explicitPkgsOptSplit.length == 0) {
344+
explicitPackages = java.util.Collections.emptySet();
345+
} else {
346+
explicitPackages = new HashSet<>();
347+
for (String pkg : explicitPkgsOptSplit) {
348+
explicitPackages.add(pkg);
349+
}
350+
}
351+
}
352+
}
294353
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -751,6 +751,14 @@ public void updateOption(OptionKey<String> key, String element) {
751751
setOption(key, s);
752752
}
753753

754+
public boolean isLLVMPackage(TruffleFile libPath) {
755+
return fastrOptions.isLLVMPackage(libPath);
756+
}
757+
758+
public boolean isLLVMPackage(String libName) {
759+
return fastrOptions.isLLVMPackage(libName);
760+
}
761+
754762
public InstrumentationState getInstrumentationState() {
755763
return stateInstrumentation;
756764
}

0 commit comments

Comments
 (0)