Skip to content

Commit

Permalink
[GR-13987] Split NFI backend implementation of Sulong into separate d…
Browse files Browse the repository at this point in the history
…istribution.

PullRequest: graal/3031
  • Loading branch information
rschatz committed Mar 25, 2019
2 parents 246ca9c + ed863fc commit 9cc877e
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 34 deletions.
2 changes: 1 addition & 1 deletion sulong/mx.sulong/mx_sulong.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def findGCCProgram(gccProgram, optional=False):

def getClasspathOptions(extra_dists=None):
"""gets the classpath of the Sulong distributions"""
return mx.get_runtime_jvm_args(['SULONG', 'SULONG_LAUNCHER'] + (extra_dists or []))
return mx.get_runtime_jvm_args(['SULONG', 'SULONG_LAUNCHER', 'TRUFFLE_NFI'] + (extra_dists or []))

def ensureLLVMBinariesExist():
"""downloads the LLVM binaries if they have not been downloaded yet"""
Expand Down
30 changes: 28 additions & 2 deletions sulong/mx.sulong/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,26 @@
"jacoco" : "include",
},

"com.oracle.truffle.llvm.runtime" : {
"com.oracle.truffle.llvm.nfi" : {
"subDir" : "projects",
"sourceDirs" : ["src"],
"dependencies" : [
"truffle:TRUFFLE_API",
"truffle:TRUFFLE_NFI",
],
"checkstyle" : "com.oracle.truffle.llvm.runtime",
"annotationProcessors" : ["truffle:TRUFFLE_DSL_PROCESSOR"],
"javaCompliance" : "1.8",
"workingSets" : "Truffle, LLVM",
"license" : "BSD-new",
"jacoco" : "include",
},

"com.oracle.truffle.llvm.runtime" : {
"subDir" : "projects",
"sourceDirs" : ["src"],
"dependencies" : [
"truffle:TRUFFLE_API",
"com.oracle.truffle.llvm.spi",
"com.oracle.truffle.llvm.instruments",
],
Expand Down Expand Up @@ -654,7 +668,6 @@
],
"distDependencies" : [
"truffle:TRUFFLE_API",
"truffle:TRUFFLE_NFI",
"truffle:ANTLR4",
"SULONG_LIBS",
],
Expand All @@ -664,6 +677,18 @@
"license" : "BSD-new",
},

"SULONG_NFI" : {
"subDir" : "projects",
"dependencies" : [
"com.oracle.truffle.llvm.nfi",
],
"distDependencies" : [
"truffle:TRUFFLE_NFI",
"SULONG",
],
"license" : "BSD-new",
},

"SULONG_LAUNCHER" : {
"subDir" : "projects",
"mainClass" : "com.oracle.truffle.llvm.launcher.LLVMLauncher",
Expand Down Expand Up @@ -721,6 +746,7 @@
"truffle:TRUFFLE_API",
"truffle:TRUFFLE_TCK",
"sulong:SULONG",
"sulong:SULONG_NFI",
"SULONG_TEST_NATIVE",
],
"javaProperties" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.oracle.truffle.llvm.nfi;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.nfi.types.NativeLibraryDescriptor;
import com.oracle.truffle.nfi.types.Parser;
import java.io.IOException;

@TruffleLanguage.Registration(id = "nfi/llvm", name = "nfi-llvm", version = "6.0.0", internal = true, interactive = false, characterMimeTypes = {"trufflenfi/llvm"})
public final class SulongNFI extends TruffleLanguage<Env> {

static class Context {

Env env;

Context(Env env) {
this.env = env;
}
}

@Override
protected Env createContext(Env env) {
return env;
}

@Override
protected CallTarget parse(ParsingRequest request) {
Env env = getContextReference().get();

NativeLibraryDescriptor descriptor = Parser.parseLibraryDescriptor(request.getSource().getCharacters());
TruffleFile file = env.getTruffleFile(descriptor.getFilename());
try {
Source source = Source.newBuilder("llvm", file).build();
return env.parse(source);
} catch (IOException ex) {
throw new SulongNFIException(ex.getMessage());
}
}

@Override
protected boolean isObjectOfLanguage(Object object) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.oracle.truffle.llvm.nfi;

import com.oracle.truffle.api.TruffleException;
import com.oracle.truffle.api.nodes.Node;

final class SulongNFIException extends RuntimeException implements TruffleException {

private static final long serialVersionUID = 1L;

SulongNFIException(String message) {
super(message);
}

@Override
public Node getLocation() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@

@TruffleLanguage.Registration(id = LLVMLanguage.ID, name = LLVMLanguage.NAME, version = "6.0.0", internal = false, interactive = false, defaultMimeType = LLVMLanguage.LLVM_BITCODE_MIME_TYPE, //
byteMimeTypes = {LLVMLanguage.LLVM_BITCODE_MIME_TYPE, LLVMLanguage.LLVM_ELF_SHARED_MIME_TYPE, LLVMLanguage.LLVM_ELF_EXEC_MIME_TYPE}, //
characterMimeTypes = {LLVMLanguage.LLVM_BITCODE_BASE64_MIME_TYPE, LLVMLanguage.LLVM_SULONG_TYPE}, fileTypeDetectors = LLVMFileDetector.class)
characterMimeTypes = {LLVMLanguage.LLVM_BITCODE_BASE64_MIME_TYPE}, fileTypeDetectors = LLVMFileDetector.class)
@ProvidedTags({StandardTags.StatementTag.class, StandardTags.CallTag.class, StandardTags.RootTag.class, DebuggerTags.AlwaysHalt.class})
public class LLVMLanguage extends TruffleLanguage<LLVMContext> {

public static final Assumption SINGLE_CONTEXT_ASSUMPTION = Truffle.getRuntime().createAssumption("Single Context");

public static final String LLVM_SULONG_TYPE = "application/x-sulong";

public static final String LLVM_BITCODE_MIME_TYPE = "application/x-llvm-ir-bitcode";
public static final String LLVM_BITCODE_EXTENSION = "bc";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ public class NFIAPITest {

@BeforeClass
public static void initialize() {
sulongObject = loadLibrary("basicTest", SULONG_FILENAME, "application/x-sulong");
sulongObject = loadLibrary("basicTest", SULONG_FILENAME);
lookupAndBind = lookupAndBind();
}

private static CallTarget lookupAndBind() {
return Truffle.getRuntime().createCallTarget(new LookupAndBindNode());
}

private static TruffleObject loadLibrary(String lib, String filename, String mimetype) {
private static TruffleObject loadLibrary(String lib, String filename) {
File file = new File(TEST_DIR.toFile(), lib + "/" + filename);
String loadLib = "load '" + file.getAbsolutePath() + "'";
Source source = Source.newBuilder("llvm", loadLib, "loadLibrary").mimeType(mimetype).build();
String loadLib = "with llvm load '" + file.getAbsolutePath() + "'";
Source source = Source.newBuilder("nfi", loadLib, "loadLibrary").build();
CallTarget target = runWithPolyglot.getTruffleTestEnv().parse(source);
return (TruffleObject) target.call();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@
import com.oracle.truffle.llvm.runtime.types.PrimitiveType;
import com.oracle.truffle.llvm.runtime.types.StructureType;
import com.oracle.truffle.llvm.runtime.types.Type;
import com.oracle.truffle.nfi.types.NativeLibraryDescriptor;
import com.oracle.truffle.nfi.types.Parser;

public final class Runner {

Expand Down Expand Up @@ -146,7 +144,7 @@ public CallTarget parse(Source source) {
return parse(source, input.bytes, input.library);
}

private ParserInput getParserData(Source source) {
private static ParserInput getParserData(Source source) {
ByteSequence bytes;
ExternalLibrary library;
if (source.hasBytes()) {
Expand All @@ -162,12 +160,6 @@ private ParserInput getParserData(Source source) {
bytes = ByteSequence.create(decodeBase64(source.getCharacters()));
library = new ExternalLibrary("<STREAM-" + UUID.randomUUID().toString() + ">", false, source.isInternal());
break;
case LLVMLanguage.LLVM_SULONG_TYPE:
NativeLibraryDescriptor descriptor = Parser.parseLibraryDescriptor(source.getCharacters());
String filename = descriptor.getFilename();
bytes = read(filename);
library = new ExternalLibrary(Paths.get(filename), false, source.isInternal());
break;
default:
throw new LLVMParserException("Character-based source with unexpected mime type: " + source.getMimeType());
}
Expand Down Expand Up @@ -980,15 +972,6 @@ private static byte[] decodeBase64(CharSequence charSequence) {
return Base64.getDecoder().decode(result);
}

private ByteSequence read(String filename) {
try {
TruffleFile truffleFile = context.getEnv().getTruffleFile(filename);
return ByteSequence.create(truffleFile.readAllBytes());
} catch (IOException | SecurityException | OutOfMemoryError ignore) {
return ByteSequence.create(new byte[0]);
}
}

private CallTarget createLibraryCallTarget(String name, List<LLVMParserResult> parserResults, InitializationOrder initializationOrder) {
RootCallTarget mainFunctionCallTarget = null;
LLVMFunctionDescriptor mainFunctionDescriptor = findMainMethod(parserResults);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -78,14 +78,20 @@ protected CallTarget parse(ParsingRequest request) throws Exception {
CharSequence nfiSource = request.getSource().getCharacters();
NativeSource source = Parser.parseNFISource(nfiSource);

String mimeType;
String backendId;
if (source.isDefaultBackend()) {
backendId = "native";
mimeType = "trufflenfi/native";
backendId = "nfi/native";
} else {
backendId = source.getNFIBackendId();
mimeType = "trufflenfi/" + source.getNFIBackendId();
backendId = Source.findLanguage(mimeType);
if (backendId == null) {
backendId = source.getNFIBackendId();
}
}

Source backendSource = Source.newBuilder(backendId, source.getLibraryDescriptor(), "<nfi-impl>").build();
Source backendSource = Source.newBuilder(backendId, source.getLibraryDescriptor(), "<nfi-impl>").mimeType(mimeType).build();
return Truffle.getRuntime().createCallTarget(new NFIRootNode(this, backendSource, source));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -51,7 +51,7 @@
import com.oracle.truffle.nfi.types.NativeLibraryDescriptor;
import com.oracle.truffle.nfi.types.Parser;

@TruffleLanguage.Registration(id = "native", name = "nfi-native", version = "0.1", characterMimeTypes = NFILanguageImpl.MIME_TYPE, internal = true)
@TruffleLanguage.Registration(id = "nfi/native", name = "nfi-native", version = "0.1", characterMimeTypes = NFILanguageImpl.MIME_TYPE, internal = true)
public class NFILanguageImpl extends TruffleLanguage<NFIContext> {

public static final String MIME_TYPE = "trufflenfi/native";
Expand Down

0 comments on commit 9cc877e

Please sign in to comment.