Skip to content

Commit

Permalink
test out using a custom toolchain in a docker image, add cmake emulat…
Browse files Browse the repository at this point in the history
…or for running sanity tests, add known good baseline sample using local_sync.c
  • Loading branch information
zero9178 committed May 9, 2024
1 parent 231bbf6 commit b61586a
Show file tree
Hide file tree
Showing 23 changed files with 731 additions and 31 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ jobs:
with:
submodules: recursive

- name: Download Pulp Toolchain
- name: Download Quidditch Toolchain
run: |
mkdir ./toolchain
wget -qO- https://github.com/pulp-platform/llvm-project/releases/download/0.12.0/riscv32-pulp-llvm-ubuntu2004-0.12.0.tar.gz \
| tar --strip-components=1 -xzv -C ./toolchain
# TODO: This should use main instead before landing.
docker run --rm ghcr.io/opencompl/Quidditch/toolchain:${{ github.ref_name }} tar -cC /opt/quidditch-toolchain . \
| tar -xC ./toolchain
- name: Set up Python
uses: actions/setup-python@v5
Expand Down Expand Up @@ -126,10 +127,14 @@ jobs:
run: |
cmake -GNinja -Bquidditch-runtime-build \
-DQUIDDITCH_CODEGEN_BUILD_DIR=${{github.workspace}}/quidditch-compiler-build \
-DPULP_TOOLCHAIN_ROOT=${{github.workspace}}/toolchain \
-DQUIDDITCH_TOOLCHAIN_ROOT=${{github.workspace}}/toolchain \
-DCMAKE_TOOLCHAIN_FILE=${{github.workspace}}/runtime/ToolchainFile.cmake
-S ${{github.workspace}}/runtime
- name: Build
run: cmake --build quidditch-runtime-build

# TODO: Test
- name: Test
working-directory: ${{github.workspace}}/quidditch-runtime-build
# TODO: This should run a proper test suite once we are no longer using verilator.
run: ctest --extra-verbose -j$(nproc) -R HelloWorld
42 changes: 42 additions & 0 deletions .github/workflows/toolchain.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: toolchain.yaml
on:
push:
paths:
- runtime/toolchain/**
workflow_dispatch:


jobs:
build-docker:
runs-on: ubuntu-22.04
steps:
# Free up disk space on Github-hosted runner
- name: Disk usage
run: df -h
- uses: jlumbroso/[email protected]
with:
android: true
dotnet: true
haskell: true
large-packages: true

- name: Disk usage after freeing up space
run: df -h
# Actually build the Docker container
- uses: actions/checkout@v2
- uses: docker/setup-buildx-action@v1

- name: GHCR Log-in
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v2
with:
context: ${{github.workspace}}/runtime/toolchain/
file: ${{github.workspace}}/runtime/toolchain/Dockerfile
push: true
tags: ghcr.io/opencompl/Quidditch/toolchain:${{ github.ref_name }}
3 changes: 3 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ add_subdirectory(iree-configuration)
add_subdirectory(snitch_cluster EXCLUDE_FROM_ALL SYSTEM)

add_subdirectory(samples)

enable_testing()
add_subdirectory(tests)
36 changes: 15 additions & 21 deletions runtime/ToolchainFile.cmake
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
# Many IDEs set this for configuring the toolchain. If present, use it as the
# default value inferred for the pulp toolchain.
set(normal_var "${CMAKE_C_COMPILER}")
cmake_path(GET normal_var PARENT_PATH pulp_toolchain_root_default)
cmake_path(GET pulp_toolchain_root_default PARENT_PATH pulp_toolchain_root_default)
cmake_path(GET normal_var PARENT_PATH quidditch_toolchain_root_default)
cmake_path(GET quidditch_toolchain_root_default PARENT_PATH quidditch_toolchain_root_default)

set(PULP_TOOLCHAIN_ROOT ${pulp_toolchain_root_default} CACHE PATH "")

set(CMAKE_SYSTEM_NAME Generic CACHE INTERNAL "")
set(CMAKE_SYSTEM_PROCESSOR riscv32 CACHE INTERNAL "")
set(QUIDDITCH_TOOLCHAIN_ROOT ${quidditch_toolchain_root_default} CACHE PATH "")

# Without that flag CMake is not able to pass test compilation check
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY CACHE INTERNAL "")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY CACHE INTERNAL "")

set(CMAKE_AR ${PULP_TOOLCHAIN_ROOT}/bin/llvm-ar${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_ASM_COMPILER ${PULP_TOOLCHAIN_ROOT}/bin/clang${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_C_COMPILER ${PULP_TOOLCHAIN_ROOT}/bin/clang${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER ${PULP_TOOLCHAIN_ROOT}/bin/clang++${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_OBJCOPY ${PULP_TOOLCHAIN_ROOT}/bin/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_RANLIB ${PULP_TOOLCHAIN_ROOT}/bin/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_SIZE ${PULP_TOOLCHAIN_ROOT}/bin/llvm-size${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_STRIP ${PULP_TOOLCHAIN_ROOT}/bin/llvm-strip${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_SYSTEM_NAME Generic CACHE INTERNAL "")
set(CMAKE_SYSTEM_PROCESSOR riscv32 CACHE INTERNAL "")

set(codegen_opts "-mcpu=snitch -menable-experimental-extensions")
set(CMAKE_ASM_FLAGS "${codegen_opts}" CACHE INTERNAL "")
set(CMAKE_C_FLAGS "${codegen_opts} -ffunction-sections -fdata-sections" CACHE INTERNAL "")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fno-exceptions -fno-rtti" CACHE INTERNAL "")
set(linker_flags "-Wl,--gc-sections -Wl,-z,norelro -fuse-ld=lld -nostartfiles")
set(CMAKE_EXE_LINKER_FLAGS ${linker_flags} CACHE INTERNAL "")
set(CMAKE_AR ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/llvm-ar${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_ASM_COMPILER ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/clang${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_C_COMPILER ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/clang${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/clang++${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_OBJCOPY ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_RANLIB ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_SIZE ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/llvm-size${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_STRIP ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/llvm-strip${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_CROSSCOMPILING_EMULATOR ${QUIDDITCH_TOOLCHAIN_ROOT}/bin/snitch_cluster.vlt CACHE INTERNAL "")

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
Expand Down
1 change: 1 addition & 0 deletions runtime/iree-configuration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(IREE_ENABLE_THREADING OFF)
set(IREE_BUILD_TESTS OFF)
set(IREE_HAL_EXECUTABLE_LOADER_DEFAULTS OFF CACHE BOOL "" FORCE)
set(IREE_HAL_DRIVER_DEFAULTS OFF CACHE BOOL "" FORCE)
set(IREE_HAL_DRIVER_LOCAL_SYNC ON CACHE BOOL "" FORCE)
set(IREE_ENABLE_WERROR_FLAG OFF)

# Include the IREE runtime as part of this build.
Expand Down
4 changes: 0 additions & 4 deletions runtime/iree-configuration/config.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@

#pragma once

// TODO: This only necessary to work with the pulp-toolchain which does not
// support 'aligned_alloc', a function called by in status.c.
#define IREE_STATUS_FEATURES 0

#define IREE_SYNCHRONIZATION_DISABLE_UNSAFE 1
#define IREE_FILE_IO_ENABLE 0
#define IREE_TIME_NOW_FN return 0;
Expand Down
14 changes: 14 additions & 0 deletions runtime/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ target_link_libraries(
simple_add_module
)

add_executable(IREE_LocalSyncBaseline local_sync.c)
target_link_libraries(
IREE_LocalSyncBaseline
PRIVATE
snRuntime
iree::base
iree::vm
iree::modules::hal
iree::modules::hal::types
iree::hal::drivers::local_sync::sync_driver
iree::hal::local::local
iree::hal::local::loaders::static_library_loader
simple_add_module
)
188 changes: 188 additions & 0 deletions runtime/samples/local_sync.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include <iree/base/allocator.h>
#include <iree/hal/allocator.h>
#include <iree/hal/drivers/local_sync/sync_device.h>
#include <iree/hal/local/loaders/static_library_loader.h>
#include <iree/modules/hal/module.h>
#include <iree/modules/hal/types.h>
#include <iree/vm/instance.h>

#include <simple_add.h>
#include <simple_add_module.h>
#include <team_decls.h>

static iree_status_t setup_instance_and_device(
iree_allocator_t host_allocator, iree_vm_instance_t** out_instance,
iree_hal_device_t** out_device) {
IREE_ASSERT_ARGUMENT(out_instance);
IREE_ASSERT_ARGUMENT(out_device);

IREE_RETURN_IF_ERROR(iree_vm_instance_create(IREE_VM_TYPE_CAPACITY_DEFAULT,
host_allocator, out_instance));

iree_status_t result = iree_hal_module_register_all_types(*out_instance);
if (!iree_status_is_ok(result)) goto error_release_vm;

const iree_hal_executable_library_query_fn_t libraries[] = {
add_dispatch_0_library_query};

iree_hal_executable_loader_t* loader;
result = iree_hal_static_library_loader_create(
IREE_ARRAYSIZE(libraries), libraries,
iree_hal_executable_import_provider_null(), host_allocator, &loader);
if (!iree_status_is_ok(result)) goto error_release_vm;

// TODO: Replace with more sophisticated allocator representing cluster
// memory.
iree_hal_allocator_t* device_allocator;
result = iree_hal_allocator_create_heap(iree_make_cstring_view("quidditch"),
host_allocator, host_allocator,
&device_allocator);
if (!iree_status_is_ok(result)) goto error_release_library_loader;

iree_hal_sync_device_params_t params;
iree_hal_sync_device_params_initialize(&params);

result =
iree_hal_sync_device_create(IREE_SV("quidditch-base-line"), &params,
/*loader_count=*/1, &loader, device_allocator,
host_allocator, out_device);
iree_hal_executable_loader_release(loader);
iree_hal_allocator_release(device_allocator);
return result;

error_release_library_loader:
iree_hal_executable_loader_release(loader);
error_release_vm:
iree_vm_instance_release(*out_instance);
return result;
}

static float data[128];

int main() {
// TODO: Remove/redirect compute cores once implemented.
if (!snrt_is_dm_core()) return 0;

for (int i = 0; i < 128; i++) {
data[i] = i;
}

iree_allocator_t host_allocator = iree_allocator_system();

iree_vm_instance_t* vmInstance;
iree_hal_device_t* device;
iree_status_t result =
setup_instance_and_device(host_allocator, &vmInstance, &device);
if (!iree_status_is_ok(result)) {
iree_status_fprint(stderr, result);
iree_status_free(result);
return -1;
}

iree_vm_module_t* hal_module = NULL;
result =
iree_hal_module_create(vmInstance, /*device_count=*/1,
/*devices=*/&device, IREE_HAL_MODULE_FLAG_NONE,
host_allocator, &hal_module);
if (!iree_status_is_ok(result)) goto error_release_instance_and_device;

iree_vm_module_t* mlir_module = NULL;
result = test_simple_add_create(vmInstance, host_allocator, &mlir_module);
if (!iree_status_is_ok(result)) goto error_release_hal_module;

iree_vm_module_t* modules[] = {hal_module, mlir_module};

iree_vm_context_t* context;
result = iree_vm_context_create_with_modules(
vmInstance, IREE_VM_CONTEXT_FLAG_NONE, IREE_ARRAYSIZE(modules), modules,
host_allocator, &context);
if (!iree_status_is_ok(result)) goto error_release_mlir_module;

iree_const_byte_span_t span = iree_make_const_byte_span(data, sizeof(data));

iree_hal_buffer_params_t params = {
.usage = IREE_HAL_BUFFER_USAGE_DISPATCH_STORAGE,
.access = IREE_HAL_MEMORY_ACCESS_NONE,
.type = IREE_HAL_MEMORY_TYPE_DEVICE_LOCAL,
};
iree_hal_buffer_params_canonicalize(&params);

iree_hal_buffer_view_t* buffer = NULL;
result = iree_hal_buffer_view_allocate_buffer_copy(
device, iree_hal_device_allocator(device), 1, (iree_hal_dim_t[]){128},
IREE_HAL_ELEMENT_TYPE_FLOAT_32, IREE_HAL_ENCODING_TYPE_DENSE_ROW_MAJOR,
params, span, &buffer);
if (!iree_status_is_ok(result)) goto error_release_context;

iree_vm_list_t* inputs = NULL;
result = iree_vm_list_create(
/*element_type=*/iree_vm_make_undefined_type_def(),
/*initial_capacity=*/2, iree_allocator_system(), &inputs);
if (!iree_status_is_ok(result)) goto error_release_context;

iree_vm_ref_t arg_buffer_view_ref;
arg_buffer_view_ref = iree_hal_buffer_view_move_ref(buffer);
result = iree_vm_list_push_ref_retain(inputs, &arg_buffer_view_ref);
if (!iree_status_is_ok(result)) goto error_release_input;

result = iree_vm_list_push_ref_move(inputs, &arg_buffer_view_ref);
if (!iree_status_is_ok(result)) goto error_release_input;

iree_vm_function_t main_function;
result = iree_vm_context_resolve_function(
context, iree_make_cstring_view("test_simple_add.add"), &main_function);

iree_vm_list_t* outputs = NULL;
result = iree_vm_list_create(
/*element_type=*/iree_vm_make_undefined_type_def(),
/*initial_capacity=*/1, iree_allocator_system(), &outputs);
if (!iree_status_is_ok(result)) goto error_release_output;

IREE_CHECK_OK(iree_vm_invoke(
context, main_function, IREE_VM_CONTEXT_FLAG_NONE,
/*policy=*/NULL, inputs, outputs, iree_allocator_system()));
if (!iree_status_is_ok(result)) goto error_release_output;

iree_hal_buffer_view_t* ret_buffer_view =
iree_vm_list_get_ref_deref(outputs, /*i=*/0, iree_hal_buffer_view_type());
if (ret_buffer_view == NULL) goto error_release_output;

iree_hal_buffer_mapping_t mapping;
result = iree_hal_buffer_map_range(
iree_hal_buffer_view_buffer(ret_buffer_view),
IREE_HAL_MAPPING_MODE_SCOPED, IREE_HAL_MEMORY_ACCESS_READ, 0,
IREE_WHOLE_BUFFER, &mapping);
if (!iree_status_is_ok(result)) goto error_release_output;

for (int i = 0; i < 128; i++) {
if (((float*)mapping.contents.data)[i] == i * 2) continue;

result = iree_make_status(IREE_STATUS_UNKNOWN, "output incorrect");
break;
}

iree_hal_buffer_unmap_range(&mapping);

error_release_output:
iree_vm_list_release(outputs);
error_release_input:
iree_vm_list_release(inputs);
error_release_context:
iree_vm_context_release(context);
error_release_mlir_module:
iree_vm_module_release(mlir_module);
error_release_hal_module:
iree_vm_module_release(hal_module);

error_release_instance_and_device:
iree_hal_device_release(device);
iree_vm_instance_release(vmInstance);

if (!iree_status_is_ok(result)) {
iree_status_fprint(stderr, result);
iree_status_free(result);
return -1;
}

return 0;
}
5 changes: 4 additions & 1 deletion runtime/snitch_cluster/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(SNITCH_RUNTIME_TARGET "rtl" CACHE STRING "Snitch runtime target to use (one
set(snRuntimeSrc ${SNITCH_CLUSTER_SOURCE_DIR})
set(header_dir ${CMAKE_CURRENT_BINARY_DIR}/cluster_gen)
set(config_file ${snRuntimeSrc}/target/snitch_cluster/cfg/default.hjson)
set(runtime_dir ${snRuntimeSrc}/target/snitch_cluster/sw/runtime/${SNITCH_RUNTIME_TARGET})
set(runtime_dir ${CMAKE_CURRENT_LIST_DIR}/${SNITCH_RUNTIME_TARGET})

execute_process(COMMAND ${bender_path} path register_interface
OUTPUT_VARIABLE reggen_base_path
Expand Down Expand Up @@ -45,6 +45,7 @@ run_cluster_gen(${snRuntimeSrc}/target/snitch_cluster/sw/runtime/common/snitch_c
add_library(snRuntime
${runtime_dir}/src/snitch_cluster_start.S
${runtime_dir}/src/snrt.c
${runtime_dir}/src/syscalls.c
)
target_include_directories(snRuntime PRIVATE
${snRuntimeSrc}/target/snitch_cluster/sw/runtime/common/
Expand All @@ -62,3 +63,5 @@ target_link_directories(snRuntime
${snRuntimeSrc}/sw/snRuntime
${runtime_dir}
)
# Required while snRuntime uses 'inline' qualifiers for declarations.
target_compile_options(snRuntime PUBLIC -Wno-undefined-inline)
8 changes: 8 additions & 0 deletions runtime/snitch_cluster/rtl/memory.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Copyright 2020 ETH Zurich and University of Bologna. */
/* Solderpad Hardware License, Version 0.51, see LICENSE for details. */
/* SPDX-License-Identifier: SHL-0.51 */

MEMORY
{
L3 (rwxa) : ORIGIN = 0x80000000, LENGTH = 0x80000000
}
Loading

0 comments on commit b61586a

Please sign in to comment.