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

Porting to Wasm #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ x64/
Release/
Debug/
build/
*.wasm
*.wat
.idea/*
cmake-build-debug/*
wasi-sdk/*
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ src/superscalar.cpp
src/vm_compiled.cpp
src/vm_interpreted_light.cpp
src/argon2_core.c
src/launcher.cpp
src/blake2_generator.cpp
src/instructions_portable.cpp
src/reciprocal.c
Expand Down Expand Up @@ -219,6 +220,13 @@ target_link_libraries(randomx-benchmark
PRIVATE randomx
PRIVATE ${CMAKE_THREAD_LIBS_INIT})

add_executable(runner
src/tests/api-example1.c
)
target_link_libraries(runner
PRIVATE randomx
PRIVATE ${CMAKE_THREAD_LIBS_INIT})

include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
#include <cstdint>
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM ubuntu:20.04

RUN apt update \
&& DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata \
&& apt install -y ca-certificates \
curl \
git \
make \
pkg-config \
libtinfo6

VOLUME /code
WORKDIR /code
ENTRYPOINT /code/entrypoint.sh
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
TARGET = randomx
CXX = ./wasi-sdk/bin/clang++
SYSROOT = ./wasi-sdk/share/wasi-sysroot
TARGET_TRIPLE = wasm32-wasi
CFLAGS = -fvisibility=hidden
LDFLAGS = -Wl,--demangle,--allow-undefined
EXPORT_FUNCS = \
--export=test_randomx

RANDOMX_SRC = \
src/launcher.cpp\
src/randomx.cpp\
src/vm_interpreted_light.cpp\
src/vm_interpreted.cpp\
src/virtual_machine.cpp\
src/dataset.cpp\
src/argon2_core.c\
src/aes_hash.cpp\
src/soft_aes.cpp\
src/reciprocal.c\
src/allocator.cpp\
src/superscalar.cpp\
src/instructions_portable.cpp\
src/bytecode_machine.cpp\
src/argon2_ref.c\
src/blake2/blake2b.c\
src/blake2_generator.cpp

RANDOMX_FLAGS = \
-fno-exceptions

.PHONY: default all clean

default: $(TARGET)
all: default

$(TARGET): $(RANDOMX_SRC)
$(CXX) -O3 --sysroot=$(SYSROOT) --target=$(TARGET_TRIPLE) $(RANDOMX_FLAGS) $(CFLAGS) $(LDFLAGS) -Wl,$(EXPORT_FUNCS) $^ -o $@.wasm

.PRECIOUS: $(TARGET)

clean:
-rm -f $(TARGET).wasm
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'
services:
randomx_wasm:
build:
context: .
container_name: randomx_wasm_builder1
volumes:
- ./:/code
5 changes: 5 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export PATH="${PATH}:/root/.cargo/bin:/root/.local/bin"
curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-linux.tar.gz | tar xz -C .
rm -rf wasi-sdk
mv -f wasi-sdk-* wasi-sdk
make
Binary file added randomx.wasm
Binary file not shown.
18 changes: 12 additions & 6 deletions src/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,29 @@ namespace randomx {

template<size_t alignment>
void* AlignedAllocator<alignment>::allocMemory(size_t count) {
void *mem = rx_aligned_alloc(count, alignment);
if (mem == nullptr)
throw std::bad_alloc();
//void *mem = rx_aligned_alloc(count, alignment);
void *mem = malloc(count);
if (mem == nullptr) {
abort();
//throw std::bad_alloc();
}
return mem;
}

template<size_t alignment>
void AlignedAllocator<alignment>::freeMemory(void* ptr, size_t count) {
rx_aligned_free(ptr);
//rx_aligned_free(ptr);
free(ptr);
}

template struct AlignedAllocator<CacheLineSize>;

void* LargePageAllocator::allocMemory(size_t count) {
void *mem = allocLargePagesMemory(count);
if (mem == nullptr)
throw std::bad_alloc();
if (mem == nullptr) {
abort();
//throw std::bad_alloc();
}
return mem;
}

Expand Down
86 changes: 43 additions & 43 deletions src/bytecode_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,48 +208,48 @@ namespace randomx {
*ibc.idst = temp;
}

static void exe_FSWAP_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_swap_vec_f128(*ibc.fdst);
}

static void exe_FADD_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_add_vec_f128(*ibc.fdst, *ibc.fsrc);
}

static void exe_FADD_M(RANDOMX_EXE_ARGS) {
rx_vec_f128 fsrc = rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad));
*ibc.fdst = rx_add_vec_f128(*ibc.fdst, fsrc);
}

static void exe_FSUB_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_sub_vec_f128(*ibc.fdst, *ibc.fsrc);
}

static void exe_FSUB_M(RANDOMX_EXE_ARGS) {
rx_vec_f128 fsrc = rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad));
*ibc.fdst = rx_sub_vec_f128(*ibc.fdst, fsrc);
}

static void exe_FSCAL_R(RANDOMX_EXE_ARGS) {
const rx_vec_f128 mask = rx_set1_vec_f128(0x80F0000000000000);
*ibc.fdst = rx_xor_vec_f128(*ibc.fdst, mask);
}

static void exe_FMUL_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_mul_vec_f128(*ibc.fdst, *ibc.fsrc);
}

static void exe_FDIV_M(RANDOMX_EXE_ARGS) {
rx_vec_f128 fsrc = maskRegisterExponentMantissa(
config,
rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad))
);
*ibc.fdst = rx_div_vec_f128(*ibc.fdst, fsrc);
}

static void exe_FSQRT_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_sqrt_vec_f128(*ibc.fdst);
}
static void exe_FSWAP_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_swap_vec_f128(*ibc.fdst);
}

static void exe_FADD_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_add_vec_f128(*ibc.fdst, *ibc.fsrc);
}

static void exe_FADD_M(RANDOMX_EXE_ARGS) {
rx_vec_f128 fsrc = rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad));
*ibc.fdst = rx_add_vec_f128(*ibc.fdst, fsrc);
}

static void exe_FSUB_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_sub_vec_f128(*ibc.fdst, *ibc.fsrc);
}

static void exe_FSUB_M(RANDOMX_EXE_ARGS) {
rx_vec_f128 fsrc = rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad));
*ibc.fdst = rx_sub_vec_f128(*ibc.fdst, fsrc);
}

static void exe_FSCAL_R(RANDOMX_EXE_ARGS) {
const rx_vec_f128 mask = rx_set1_vec_f128(0x80F0000000000000);
*ibc.fdst = rx_xor_vec_f128(*ibc.fdst, mask);
}

static void exe_FMUL_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_mul_vec_f128(*ibc.fdst, *ibc.fsrc);
}

static void exe_FDIV_M(RANDOMX_EXE_ARGS) {
rx_vec_f128 fsrc = maskRegisterExponentMantissa(
config,
rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad))
);
*ibc.fdst = rx_div_vec_f128(*ibc.fdst, fsrc);
}

static void exe_FSQRT_R(RANDOMX_EXE_ARGS) {
*ibc.fdst = rx_sqrt_vec_f128(*ibc.fdst);
}

static void exe_CBRANCH(RANDOMX_EXE_ARGS) {
*ibc.idst += ibc.imm;
Expand All @@ -259,7 +259,7 @@ namespace randomx {
}

static void exe_CFROUND(RANDOMX_EXE_ARGS) {
rx_set_rounding_mode(rotr(*ibc.isrc, ibc.imm) % 4);
//rx_set_rounding_mode(rotr(*ibc.isrc, ibc.imm) % 4);
}

static void exe_ISTORE(RANDOMX_EXE_ARGS) {
Expand Down
9 changes: 7 additions & 2 deletions src/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "blake2/endian.h"
#include "argon2.h"
#include "argon2_core.h"
#include "jit_compiler.hpp"
// #include "jit_compiler.hpp"
#include "intrin_portable.h"

static_assert(RANDOMX_ARGON_MEMORY % (RANDOMX_ARGON_LANES * ARGON2_SYNC_POINTS) == 0, "RANDOMX_ARGON_MEMORY - invalid value");
Expand All @@ -61,8 +61,10 @@ namespace randomx {
void deallocCache(randomx_cache* cache) {
if (cache->memory != nullptr)
Allocator::freeMemory(cache->memory, CacheSize);
/*
if (cache->jit != nullptr)
delete cache->jit;
*/
}

template void deallocCache<DefaultAllocator>(randomx_cache* cache);
Expand Down Expand Up @@ -128,7 +130,8 @@ namespace randomx {
randomx::Blake2Generator gen(key, keySize);
for (int i = 0; i < RANDOMX_CACHE_ACCESSES; ++i) {
randomx::generateSuperscalar(cache->programs[i], gen);
for (unsigned j = 0; j < cache->programs[i].getSize(); ++j) {

for (unsigned j = 0; j < cache->programs[i].getSize(); ++j) {
auto& instr = cache->programs[i](j);
if ((SuperscalarInstructionType)instr.opcode == SuperscalarInstructionType::IMUL_RCP) {
auto rcp = randomx_reciprocal(instr.getImm32());
Expand All @@ -141,10 +144,12 @@ namespace randomx {

void initCacheCompile(randomx_cache* cache, const void* key, size_t keySize) {
initCache(cache, key, keySize);
/*
cache->jit->enableWriting();
cache->jit->generateSuperscalarHash(cache->programs, cache->reciprocalCache);
cache->jit->generateDatasetInitCode();
cache->jit->enableExecution();
*/
}

constexpr uint64_t superscalarMul0 = 6364136223846793005ULL;
Expand Down
2 changes: 2 additions & 0 deletions src/dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ namespace randomx {
void initDataset(randomx_cache* cache, uint8_t* dataset, uint32_t startBlock, uint32_t endBlock);

inline randomx_argon2_impl* selectArgonImpl(randomx_flags flags) {
/*
if (flags & RANDOMX_FLAG_ARGON2_AVX2) {
return randomx_argon2_impl_avx2();
}
if (flags & RANDOMX_FLAG_ARGON2_SSSE3) {
return randomx_argon2_impl_ssse3();
}
*/
return &randomx_argon2_fill_segment_ref;
}
}
5 changes: 5 additions & 0 deletions src/instructions_portable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void rx_reset_float_state() {
}

void rx_set_rounding_mode(uint32_t mode) {
/*
switch (mode & 3) {
case RoundDown:
setRoundMode_(FE_DOWNWARD);
Expand All @@ -155,9 +156,11 @@ void rx_set_rounding_mode(uint32_t mode) {
default:
UNREACHABLE;
}
*/
}

uint32_t rx_get_rounding_mode() {
/*
switch (fegetround()) {
case FE_DOWNWARD:
return RoundDown;
Expand All @@ -170,6 +173,8 @@ uint32_t rx_get_rounding_mode() {
default:
UNREACHABLE;
}
*/
return 0;
}

#endif
Expand Down
6 changes: 4 additions & 2 deletions src/intrin_portable.h
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,13 @@ static const char* platformError = "Platform doesn't support hardware AES";
#include <stdexcept>

FORCE_INLINE rx_vec_i128 rx_aesenc_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
throw std::runtime_error(platformError);
abort();
// throw std::runtime_error(platformError);
}

FORCE_INLINE rx_vec_i128 rx_aesdec_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
throw std::runtime_error(platformError);
abort();
// throw std::runtime_error(platformError);
}

#define HAVE_AES 0
Expand Down
Loading