Skip to content

Commit

Permalink
implement event in fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Dec 18, 2024
1 parent f5e2a99 commit 3c7c1d1
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 59 deletions.
1 change: 1 addition & 0 deletions src/backends/fallback/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (LLVM_FOUND AND embree_FOUND)
fallback_device.cpp
fallback_device_api.cpp
fallback_device_api_ir_module.cpp
fallback_event.cpp
fallback_command_queue.cpp
fallback_bindless_array.cpp
fallback_stream.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/backends/fallback/fallback_bindless_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class FallbackBindlessArray {
[[nodiscard]] auto &slot(unsigned int idx) const noexcept { return _slots[idx]; }
void update(luisa::unique_ptr<BindlessArrayUpdateCommand> cmd) noexcept;
[[nodiscard]] auto view() noexcept { return View{_slots.data(), _slots.size()}; }
[[nodiscard]] auto native_handle() noexcept { return _slots.data(); }
[[nodiscard]] auto native_handle() const noexcept { return _slots.data(); }
};

using FallbackBindlessArrayView = FallbackBindlessArray::View;
Expand Down
92 changes: 40 additions & 52 deletions src/backends/fallback/fallback_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <luisa/core/stl.h>
#include <luisa/core/logging.h>
#include <luisa/core/clock.h>

#include "fallback_stream.h"
#include "fallback_device.h"
#include "fallback_texture.h"
Expand All @@ -26,35 +27,11 @@
#include "fallback_bindless_array.h"
#include "fallback_shader.h"
#include "fallback_buffer.h"
#include "fallback_event.h"
#include "fallback_swapchain.h"

#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>

namespace luisa::compute::fallback {

static void loadLLVMModuleFromString(::llvm::orc::LLJIT &jit, const char *ir_string) {
// Create an LLVM context
auto llvm_ctx = std::make_unique<llvm::LLVMContext>();
// Wrap the string in a MemoryBuffer
auto buffer = ::llvm::MemoryBuffer::getMemBuffer(ir_string, "embedded_ir");
// Parse the IR
::llvm::SMDiagnostic err;
auto module = ::llvm::parseIR(buffer->getMemBufferRef(), err, *llvm_ctx);
if (!module) {
throw std::runtime_error("Failed to parse embedded LLVM IR: " + err.getMessage().str());
}
// Wrap the module in a ThreadSafeModule
auto tsm = ::llvm::orc::ThreadSafeModule(std::move(module), std::move(llvm_ctx));

// Add the module to the JIT
if (auto err = jit.addIRModule(std::move(tsm))) {
::llvm::handleAllErrors(std::move(err), [](const ::llvm::ErrorInfoBase &err) {
LUISA_WARNING_WITH_LOCATION("LLJIT::addIRModule(): {}", err.message());
});
}
}

FallbackDevice::FallbackDevice(Context &&ctx) noexcept
: DeviceInterface{std::move(ctx)} {

Expand Down Expand Up @@ -110,11 +87,11 @@ void FallbackDevice::synchronize_stream(uint64_t stream_handle) noexcept {
}

void FallbackDevice::destroy_shader(uint64_t handle) noexcept {
//luisa::delete_with_allocator(reinterpret_cast<LLVMShader *>(handle));
luisa::delete_with_allocator(reinterpret_cast<FallbackShader *>(handle));
}

void FallbackDevice::destroy_event(uint64_t handle) noexcept {
//luisa::delete_with_allocator(reinterpret_cast<LLVMEvent *>(handle));
luisa::delete_with_allocator(reinterpret_cast<FallbackEvent *>(handle));
}

void FallbackDevice::destroy_mesh(uint64_t handle) noexcept {
Expand All @@ -126,8 +103,7 @@ void FallbackDevice::destroy_accel(uint64_t handle) noexcept {
}

void FallbackDevice::destroy_swap_chain(uint64_t handle) noexcept {
auto b = reinterpret_cast<FallbackSwapchain *>(handle);
luisa::deallocate_with_allocator(b);
luisa::deallocate_with_allocator(reinterpret_cast<FallbackSwapchain *>(handle));
}

void FallbackDevice::present_display_in_stream(uint64_t stream_handle,
Expand All @@ -144,9 +120,7 @@ uint FallbackDevice::compute_warp_size() const noexcept {
}

BufferCreationInfo FallbackDevice::create_buffer(const Type *element, size_t elem_count, void *external_memory) noexcept {

BufferCreationInfo info{};

if (element == Type::of<void>()) {
info.element_stride = 1u;
} else {
Expand All @@ -164,17 +138,21 @@ BufferCreationInfo FallbackDevice::create_buffer(const ir::CArc<ir::Type> *eleme
}

ResourceCreationInfo FallbackDevice::create_texture(PixelFormat format, uint dimension, uint width, uint height, uint depth, uint mipmap_levels, bool simultaneous_access, bool allow_raster_target) noexcept {
ResourceCreationInfo info{};
auto texture = luisa::new_with_allocator<FallbackTexture>(
pixel_format_to_storage(format), dimension,
make_uint3(width, height, depth), mipmap_levels);
info.handle = reinterpret_cast<uint64_t>(texture);
return info;
return {
.handle = reinterpret_cast<uint64_t>(texture),
.native_handle = texture->native_handle(),
};
}

ResourceCreationInfo FallbackDevice::create_stream(StreamTag stream_tag) noexcept {
return ResourceCreationInfo{
.handle = reinterpret_cast<uint64_t>(luisa::new_with_allocator<FallbackStream>())};
auto stream = luisa::new_with_allocator<FallbackStream>();
return {
.handle = reinterpret_cast<uint64_t>(stream),
.native_handle = native_handle(),
};
}

void FallbackDevice::dispatch(uint64_t stream_handle, CommandList &&list) noexcept {
Expand All @@ -190,10 +168,9 @@ void FallbackDevice::set_stream_log_callback(uint64_t stream_handle, const Devic
SwapchainCreationInfo FallbackDevice::create_swapchain(const SwapchainOption &option, uint64_t stream_handle) noexcept {
auto stream = reinterpret_cast<FallbackStream *>(stream_handle);
auto sc = luisa::new_with_allocator<FallbackSwapchain>(stream, option);
return SwapchainCreationInfo{
ResourceCreationInfo{.handle = reinterpret_cast<uint64_t>(sc),
.native_handle = nullptr},
(option.wants_hdr ? PixelStorage::FLOAT4 : PixelStorage::BYTE4)};
return {ResourceCreationInfo{.handle = reinterpret_cast<uint64_t>(sc),
.native_handle = nullptr},
(option.wants_hdr ? PixelStorage::FLOAT4 : PixelStorage::BYTE4)};
}

ShaderCreationInfo FallbackDevice::create_shader(const ShaderOption &option, Function kernel) noexcept {
Expand Down Expand Up @@ -224,19 +201,29 @@ Usage FallbackDevice::shader_argument_usage(uint64_t handle, size_t index) noexc
}

void FallbackDevice::signal_event(uint64_t handle, uint64_t stream_handle, uint64_t fence_value) noexcept {
LUISA_NOT_IMPLEMENTED();
auto event = reinterpret_cast<FallbackEvent *>(handle);
auto stream = reinterpret_cast<FallbackStream *>(stream_handle);
stream->queue()->enqueue([event, fence_value] {
event->signal(fence_value);
});
}

void FallbackDevice::wait_event(uint64_t handle, uint64_t stream_handle, uint64_t fence_value) noexcept {
LUISA_NOT_IMPLEMENTED();
auto event = reinterpret_cast<FallbackEvent *>(handle);
auto stream = reinterpret_cast<FallbackStream *>(stream_handle);
stream->queue()->enqueue([event, fence_value] {
event->wait(fence_value);
});
}

bool FallbackDevice::is_event_completed(uint64_t handle, uint64_t fence_value) const noexcept {
return false;
auto event = reinterpret_cast<FallbackEvent *>(handle);
return event->is_completed(fence_value);
}

void FallbackDevice::synchronize_event(uint64_t handle, uint64_t fence_value) noexcept {
LUISA_NOT_IMPLEMENTED();
auto event = reinterpret_cast<FallbackEvent *>(handle);
event->wait(fence_value);
}

ResourceCreationInfo FallbackDevice::create_mesh(const AccelOption &option) noexcept {
Expand Down Expand Up @@ -322,18 +309,19 @@ void FallbackDevice::destroy_sparse_texture(uint64_t handle) noexcept {
}

ResourceCreationInfo FallbackDevice::create_bindless_array(size_t size) noexcept {
ResourceCreationInfo info{};
auto array = luisa::new_with_allocator<FallbackBindlessArray>(size);
info.handle = reinterpret_cast<uint64_t>(array);
return info;
return ResourceCreationInfo{
.handle = reinterpret_cast<uint64_t>(array),
.native_handle = array->native_handle(),
};
}

ResourceCreationInfo FallbackDevice::create_event() noexcept {
return ResourceCreationInfo{};
// return ResourceCreationInfo
// {
// .handle = reinterpret_cast<uint64_t>(luisa::new_with_allocator<LLVMEvent>())
// };
auto event = luisa::new_with_allocator<FallbackEvent>();
return ResourceCreationInfo{
.handle = reinterpret_cast<uint64_t>(event),
.native_handle = event->native_handle(),
};
}

}// namespace luisa::compute::fallback
Expand Down
22 changes: 22 additions & 0 deletions src/backends/fallback/fallback_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <thread>
#include <chrono>

#include "fallback_event.h"

namespace luisa::compute::fallback {

void FallbackEvent::signal(uint64_t fence_value) noexcept {
using A = _Atomic(uint64_t);
auto p = reinterpret_cast<A *>(&_fence_value);
__c11_atomic_fetch_max(p, fence_value, __ATOMIC_RELEASE);
}

void FallbackEvent::wait(uint64_t fence_value) const noexcept {
// spin until the fence value is reached
while (!is_completed(fence_value)) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(50us);
}
}

}// namespace luisa::compute::fallback
20 changes: 20 additions & 0 deletions src/backends/fallback/fallback_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <atomic>

namespace luisa::compute::fallback {

class FallbackEvent {

private:
std::atomic_uint64_t _fence_value{0u};

public:
void signal(uint64_t fence_value) noexcept;
void wait(uint64_t fence_value) const noexcept;
[[nodiscard]] auto is_completed(uint64_t fence_value) const noexcept { return _fence_value.load(std::memory_order_acquire) >= fence_value; }
[[nodiscard]] auto native_handle() noexcept { return &_fence_value; }
[[nodiscard]] auto native_handle() const noexcept { return &_fence_value; }
};

}// namespace luisa::compute::fallback
1 change: 1 addition & 0 deletions src/backends/fallback/fallback_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FallbackStream final {
explicit FallbackStream(size_t queue_size = 8u) noexcept;
~FallbackStream() noexcept = default;
[[nodiscard]] auto queue() noexcept { return &_queue; }
[[nodiscard]] auto native_handle() noexcept { return queue(); }
void synchronize() noexcept { _queue.synchronize(); }
void dispatch(CommandList &&cmd_list) noexcept;
void dispatch(luisa::move_only_function<void()> &&f) noexcept;
Expand Down
8 changes: 1 addition & 7 deletions src/backends/fallback/fallback_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,13 @@ class alignas(16u) FallbackTexture {

public:
FallbackTexture(PixelStorage storage, uint dim, uint3 size, uint levels) noexcept;

~FallbackTexture() noexcept;

FallbackTexture(FallbackTexture &&) noexcept = delete;

FallbackTexture(const FallbackTexture &) noexcept = delete;

FallbackTexture &operator=(FallbackTexture &&) noexcept = delete;

FallbackTexture &operator=(const FallbackTexture &) noexcept = delete;

[[nodiscard]] FallbackTextureView view(uint level) const noexcept;

[[nodiscard]] auto native_handle() noexcept { return _data; }
[[nodiscard]] auto storage() const noexcept { return _storage; }
[[nodiscard]] auto mip_levels() const noexcept { return _mip_levels; }
};
Expand Down

0 comments on commit 3c7c1d1

Please sign in to comment.