Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Initial SIMD support #528

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aot/src/main/java/com/dylibso/chicory/aot/AotMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ public Value[] call(int funcId, Value[] args) throws ChicoryException {
}
}

// TODO: implement SIMD support
@Override
public Map<OpCode, OpImpl> additionalOpCodes() {
return Map.of();
}

private MethodHandle[] compile(Class<?> clazz) {
var functions = module.functionSection();
var compiled = new MethodHandle[functionImports + functions.functionCount()];
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<module>log</module>
<module>runtime</module>
<module>runtime-tests</module>
<module>simd</module>
<module>test-gen-plugin</module>
<module>wabt</module>
<module>wasi</module>
Expand Down Expand Up @@ -162,6 +163,11 @@
<artifactId>runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>simd</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wabt</artifactId>
Expand Down
26 changes: 25 additions & 1 deletion runtime-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@
<name>Chicory - Runtime tests</name>
<description>Tests for the Chicory runtime using the WebAssembly testsuite</description>

<properties>
<maven.compiler.release>21</maven.compiler.release>
</properties>

<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>simd</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wabt</artifactId>
Expand Down Expand Up @@ -48,6 +57,7 @@

<build>
<plugins>

<plugin>
<groupId>com.dylibso.chicory</groupId>
<artifactId>test-gen-plugin</artifactId>
Expand Down Expand Up @@ -118,6 +128,7 @@
<wast>ref_null.wast</wast>
<wast>return.wast</wast>
<wast>select.wast</wast>
<wast>simd_load.wast</wast>
<wast>skip-stack-guard-page.wast</wast>
<wast>stack.wast</wast>
<wast>start.wast</wast>
Expand Down Expand Up @@ -194,7 +205,6 @@
<wast>simd_int_to_int_extend.wast</wast>
<wast>simd_lane.wast</wast>
<wast>simd_linking.wast</wast>
<wast>simd_load.wast</wast>
<wast>simd_load16_lane.wast</wast>
<wast>simd_load32_lane.wast</wast>
<wast>simd_load64_lane.wast</wast>
Expand All @@ -218,6 +228,20 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>--add-modules jdk.incubator.vector</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--add-modules jdk.incubator.vector</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.dylibso.chicory.runtime.HostImports;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.InterpreterMachine;
import com.dylibso.chicory.runtime.Store;
import com.dylibso.chicory.simd.Simd;
import com.dylibso.chicory.wabt.Wat2Wasm;
import com.dylibso.chicory.wasm.Module;
import com.dylibso.chicory.wasm.Parser;
Expand Down Expand Up @@ -62,6 +64,9 @@ public TestModule(Module module) {

public Instance instantiate(Store s) {
HostImports hostImports = s.toHostImports();
return Instance.builder(module).withHostImports(hostImports).build();
return Instance.builder(module)
.withMachineFactory((inst) -> new InterpreterMachine(inst, Simd.opcodesImpl))
.withHostImports(hostImports)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,35 @@
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
import java.util.Map;

/**
* This is responsible for holding and interpreting the Wasm code.
*/
class InterpreterMachine implements Machine {
public class InterpreterMachine implements Machine {

private final MStack stack;

private final Deque<StackFrame> callStack;

private final Instance instance;

private final Map<OpCode, OpImpl> additionalOpcodes;

public InterpreterMachine(Instance instance) {
this(instance, Map.of());
}

public InterpreterMachine(Instance instance, Map<OpCode, OpImpl> additionalOpcodes) {
this.instance = instance;
stack = new MStack();
this.callStack = new ArrayDeque<>();
this.additionalOpcodes = additionalOpcodes;
}

@Override
public Map<OpCode, OpImpl> additionalOpCodes() {
return additionalOpcodes;
}

@Override
Expand Down Expand Up @@ -57,7 +70,7 @@ public static Value[] call(
if (func != null) {
var stackFrame =
new StackFrame(func.instructions(), instance, funcId, args, func.localTypes());
stackFrame.pushCtrl(OpCode.CALL, 0, type.returns().size(), stack.size());
stackFrame.pushCtrl(OpCode.CALL, 0, sizeOf(type.returns()), stack.size());
callStack.push(stackFrame);

try {
Expand All @@ -67,7 +80,7 @@ public static Value[] call(
}
} else {
var stackFrame = new StackFrame(instance, funcId, args, List.of());
stackFrame.pushCtrl(OpCode.CALL, 0, type.returns().size(), stack.size());
stackFrame.pushCtrl(OpCode.CALL, 0, sizeOf(type.returns()), stack.size());
callStack.push(stackFrame);

var results = instance.callHostFunction(funcId, args);
Expand Down Expand Up @@ -95,7 +108,7 @@ public static Value[] call(
return null;
}

var totalResults = type.returns().size();
var totalResults = sizeOf(type.returns());
var results = new Value[totalResults];
for (var i = totalResults - 1; i >= 0; i--) {
results[i] = stack.pop();
Expand Down Expand Up @@ -745,8 +758,15 @@ static void eval(MStack stack, Instance instance, Deque<StackFrame> callStack)
ELEM_DROP(instance, operands);
break;
default:
throw new RuntimeException(
"Machine doesn't recognize Instruction " + instruction);
{
var additionalOpcodes = instance.getMachine().additionalOpCodes();
if (additionalOpcodes.containsKey(opcode)) {
additionalOpcodes.get(opcode).invoke(stack, instance, operands);
} else {
throw new RuntimeException(
"Machine doesn't recognize Instruction " + instruction);
}
}
}
}
}
Expand Down Expand Up @@ -1653,7 +1673,7 @@ private static void MEMORY_GROW(MStack stack, Instance instance) {
stack.push(Value.i32(nPages));
}

private static int readMemPtr(MStack stack, Operands operands) {
public static int readMemPtr(MStack stack, Operands operands) {
int offset = stack.pop().asInt();
if (operands.get(1) < 0 || operands.get(1) >= Integer.MAX_VALUE || offset < 0) {
throw new WASMRuntimeException("out of bounds memory access");
Expand Down Expand Up @@ -1861,7 +1881,7 @@ private static int numberOfParams(Instance instance, AnnotatedInstruction scope)
if (ValueType.isValid(typeId)) {
return 0;
}
return instance.type(typeId).params().size();
return sizeOf(instance.type(typeId).params());
}

private static int numberOfValuesToReturn(Instance instance, AnnotatedInstruction scope) {
Expand All @@ -1873,9 +1893,25 @@ private static int numberOfValuesToReturn(Instance instance, AnnotatedInstructio
return 0;
}
if (ValueType.isValid(typeId)) {
return 1;
if (ValueType.forId(typeId).isVec()) {
return 2;
} else {
return 1;
}
}
return sizeOf(instance.type(typeId).returns());
}

private static int sizeOf(List<ValueType> args) {
int total = 0;
for (var a : args) {
if (a.isVec()) {
total += 2;
} else {
total += 1;
}
}
return instance.type(typeId).returns().size();
return total;
}

private static void BLOCK(
Expand Down Expand Up @@ -1941,7 +1977,7 @@ static Value[] extractArgsForParams(MStack stack, List<ValueType> params) {
if (params == null) {
return Value.EMPTY_VALUES;
}
var args = new Value[params.size()];
var args = new Value[sizeOf(params)];
for (var i = params.size(); i > 0; i--) {
var p = stack.pop();
var t = params.get(i - 1);
Expand Down Expand Up @@ -1984,7 +2020,7 @@ private static void checkInterruption() {
}

@FunctionalInterface
private interface Operands {
public interface Operands {
long get(int index);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.wasm.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.types.OpCode;
import com.dylibso.chicory.wasm.types.Value;
import java.util.Map;

public interface Machine {

Value[] call(int funcId, Value[] args) throws ChicoryException;

Map<OpCode, OpImpl> additionalOpCodes();

@FunctionalInterface
public interface OpImpl {
void invoke(MStack stack, Instance instance, InterpreterMachine.Operands operands);
}
}
8 changes: 8 additions & 0 deletions runtime/src/main/java/com/dylibso/chicory/runtime/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ public Value readF64(int addr) {
}
}

public Value readV128(int addr) {
try {
return Value.v128(buffer.getLong(addr));
} catch (IndexOutOfBoundsException e) {
throw new WASMRuntimeException("out of bounds memory access");
}
}

public void zero() {
this.fill((byte) 0);
}
Expand Down
69 changes: 69 additions & 0 deletions simd/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.dylibso.chicory</groupId>
<artifactId>chicory</artifactId>
<version>999-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>simd</artifactId>
<packaging>jar</packaging>
<name>Chicory - SIMD</name>
<description>SIMD instructions support for Chicory</description>

<properties>
<maven.compiler.release>21</maven.compiler.release>

<!-- TODO: fixme working around "[WARNING] using incubating module(s): jdk.incubator.vector" -->
<maven.compiler.failOnWarning>false</maven.compiler.failOnWarning>
</properties>

<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasm</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-modules</arg>
<arg>jdk.incubator.vector</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<!-- Workaround: "MavenReportException: Error while generating Javadoc:" -->
<failOnError>false</failOnError>
<failOnWarnings>false</failOnWarnings>
<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
</plugin>
</plugins>
</build>

</project>
Loading
Loading