Skip to content

Commit

Permalink
HAL rewrite branch rebased onto google branch.
Browse files Browse the repository at this point in the history
Removing unused HAL files/functions/types.

Removing last use of HAL C++ from the HAL VM module.

Swapping many C++ HAL types/enums for C ones.

Removing iree/base/bitfield.h.
The formatting functionality will reappear in iree_string_builder.

Removing iree/hal/cts/ for now until it can be ported to C.
Issue iree-org#3934 is tracking this.

Moving HAL C++ types to iree/hal/cc/.

Removing C++ command buffer validation as it'll be moved into C.

Porting the HAL C++ API to C.
Unfortunately the string handling code is still very dependent on abseil,
but everything else is now just plain C.

Fixing quoting on cmake variables that *must* be quoted. (iree-org#4404)

Removing unused iree/hal/testing/ mock types.

Adding a new arena type with a shared thread-safe block pool.
This will be used to provide storage for command buffers and other
builder types (submissions/etc) in the local HAL backend.

Fixing tracing macro errors with the latest VC++.

Got a new error emitted:
`error C4576: a parenthesized type followed by an initializer
list is a non-standard explicit type conversion syntax`

Unfortunately C99 supports paren-init and not non-paren-init and
C++ supports the opposite so this error is dumb. Oh well.

Porting a majority of Vulkan/Metal to C.

Removing Metal backend until it can be replaced.
This commit can be used to revive the configuration for the driver
and view a partially-ported version of it. The actual implementation
will need to be rebased onto the C type system and written in
Objective C. Note that the compiler backend has not been removed - we
can use that for verifying metal shader output.

Issue iree-org#4370 tracks reviving this.

Removing iree/base/time.h now that no C++ remains using it.

Windows ptr atomics fix (needs test coverage, obviously).

Ensure wait handles are zero-initialized.

Removing unused status results.

Adding support for cleaning up tasks when retired/discarded.

Making call and dispatch task closures independent types.

Adding batch task submission enqueuing.

Fixing executables with multiple pipelines in Vulkan.

Disabling executable linking until it can be fixed iree-org#4536.

Revive the HAL CTS using the C API. (iree-org#4498)

Adding back a Conformance Test Suite of tests for HAL drivers. These tests were forked from the prior [iree/hal/cts/ tests](https://github.com/google/iree/tree/6f1afa5ebf82d31dafc0c3e835c8baf3eea2ceda/iree/hal/cts), rebased on the new HAL C APIs, then expanded upon.

Tests pass with no validation layer warnings on the new Vulkan HAL on Windows with an NVIDIA GPU using latest drivers.

Fixes iree-org#3934, part of iree-org#4369.

Allowing iree_task_barrier_t to be initialized in two steps.
This is required when incrementally building the barrier: you want to
insert the barrier into the task DAG prior to knowing what the dependent
tasks are.

Improving `iree_task_post_batch_select_worker`.
With the way command buffers are issued today it tended to bias toward
only dispatching on the current worker and - even if worked around -
would bias towards idle workers even after the batch is queuing work on
them. This more evenly distributes work when many tasks are dispatched
within the same batch.

Working around crap _mktemp behavior.
It only generates 26 unique names per thread o_o

Adding a new iree/hal/local/ path as the local CPU driver.
This subsumes the existing iree/hal/host/ and will allow us to share the
same device for all local execution - including mixing and matching
executables in different forms (AOT, vmla, JIT, etc) within the same
device. It also adds the (initial, unoptimized) task-system based
command buffer and semaphore implementations.

Porting both the dylib and vmla drivers to use iree/hal/local.
Both entire drivers are now just loader implementations and then shims
that match the existing registration. In the future there will just be
--driver=local and dylib/vmla will just be compiler registrations like
HAL driver registrations currently are.

Removing iree/hal/host/ now that it is unused.

Moving VMLA VM module to iree/modules/vmla/.

Fixing temp file path cleanup. Can't wait to remove this code.

Making IREE_EXPECT_OK/IREE_ASSERT_OK print better messages.

Removing unused line to remove warning.

Tweaking iree_atomic_* macros to better check types and adding a test.

Fixing a type typo in the python bindings.

Fixing third_party/half bazel-to-cmake mapping.

Fixing a race where threads start prior to their handle being assigned.

Treating push constants offset/length as bytes in the API.
IR still needs a fixup, but is fine for now.

Making iree_status_t a non-void type to get better warnings on misuse.

Force mapping on hal.allocator.wrap.byte_buffer (today).

Cleanup of compile-time detection of sanitizers to enable TSAN/MSAN.
Also adds --config=tsan and --config=msan to iree.bazelrc.

Making threads join on shutdown (for sanity).
Doesn't seem to change anything but there's not really a reason to
detach and I'd rather never have to think about this code again.

Fixing push constants not being included in executable layout caching.
This would cause all kind of badness when multiple executables shared
the same hal.interface bindings but had differing push constants.

Adding iree_task_pool_t tests.

Adding iree_task_queue_t tests.

Adding iree_task_scope_t tests.

Adding iree_task_list_discard test.

Adding iree_task_topology_t tests and simplifying allocation.
The max size on the stack is 3KB, which is fine for something transient
like this that is only used for construction of the executor.

Removing HAL backend implementations of fill/read/write/copy.
They were all implemented using mapping anyway and this way we only have
that code once and make the restriction that buffers be mappable part of
their usage uniformly.

Better error status when drivers are not registered.

Split iree/tools/test/simple.mlir as it was a PITA all as one.

Fix Vulkan GUI sample after HAL C changes. (iree-org#4603)

More fixes after HAL C changes. (iree-org#4604)

* Use `IREE_RESTRICT` for older MSVC versions.
* Update iree-run-module-vulkan-gui-main.cc
* Include <atomic> for std::atomic use in GetTempFile.

Tested CMake build of `all` (vmla+vulkan only) on my Windows machine, with samples and Vulkan enabled.

Properly handling nop tasks in the task system.

Adding tests for the various iree_task_t types.

Temporarily disabling failing task system wait test.
This case is not yet exercised from our code but I'll be fixing it ASAP
anyway.

Fixing off-by-one in minilcg128.
  • Loading branch information
benvanik authored and ScottTodd committed Jan 27, 2021
1 parent b692b55 commit 6ee1d9a
Show file tree
Hide file tree
Showing 416 changed files with 22,323 additions and 26,605 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ endif()
# List of all HAL drivers to be built by default:
set(IREE_ALL_HAL_DRIVERS
DyLib
Metal
VMLA
Vulkan
)
Expand All @@ -126,9 +125,6 @@ if(IREE_HAL_DRIVERS_TO_BUILD STREQUAL "all")
# For Apple platforms we need to use Metal instead of Vulkan.
if(APPLE)
list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Vulkan)
else()
# And Metal isn't available on non-Apple platforms for sure.
list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Metal)
endif()
endif()
message(STATUS "Building HAL drivers: ${IREE_HAL_DRIVERS_TO_BUILD}")
Expand Down
12 changes: 4 additions & 8 deletions bindings/java/com/google/iree/native/context_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Status ContextWrapper::InvokeFunction(const FunctionWrapper& function_wrapper,
IREE_RETURN_IF_ERROR(iree_hal_buffer_view_create(
input_buffer, /*shape=*/&input_element_count,
/*shape_rank=*/1, IREE_HAL_ELEMENT_TYPE_FLOAT_32,
iree_allocator_system(), &input_buffer_view));
&input_buffer_view));
iree_hal_buffer_release(input_buffer);

// Marshal the input buffer views through the input VM variant list.
Expand All @@ -132,13 +132,9 @@ Status ContextWrapper::InvokeFunction(const FunctionWrapper& function_wrapper,
reinterpret_cast<iree_hal_buffer_view_t*>(iree_vm_list_get_ref_deref(
outputs.get(), 0, iree_hal_buffer_view_get_descriptor()));
auto* output_buffer = iree_hal_buffer_view_buffer(output_buffer_view);
iree_hal_mapped_memory_t mapped_memory;
IREE_RETURN_IF_ERROR(iree_hal_buffer_map(output_buffer,
IREE_HAL_MEMORY_ACCESS_READ, 0,
IREE_WHOLE_BUFFER, &mapped_memory));
memcpy(output, mapped_memory.contents.data,
mapped_memory.contents.data_length);
iree_hal_buffer_unmap(output_buffer, &mapped_memory);
// TODO(jennik): this is unsafe - we don't know the size of output ptr here!
IREE_RETURN_IF_ERROR(iree_hal_buffer_read_data(
output_buffer, 0, output, iree_hal_buffer_byte_length(output_buffer)));
return OkStatus();
}

Expand Down
19 changes: 10 additions & 9 deletions bindings/python/pyiree/rt/function_abi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ void FunctionAbi::RawUnpack(absl::Span<const Description> descs,
throw RaiseValueError(
"Could not deref result buffer view (wrong type?)");
}
iree_hal_buffer* raw_buffer = iree_hal_buffer_view_buffer(buffer_view);
iree_hal_buffer_t* raw_buffer =
iree_hal_buffer_view_buffer(buffer_view);
if (!raw_buffer) {
throw RaiseValueError("Could not deref result buffer (wrong type?)");
}
Expand Down Expand Up @@ -675,10 +676,10 @@ void FunctionAbi::AllocateResults(absl::Span<const Description> descs,
kScalarTypeToHalElementType[static_cast<unsigned>(
desc.scalar.type)]);
iree_hal_buffer_view_t* buffer_view;
CheckApiStatus(iree_hal_buffer_view_create(
raw_buffer, dims.data(), dims.size(), element_type,
iree_allocator_system(), &buffer_view),
"Error allocating buffer_view");
CheckApiStatus(
iree_hal_buffer_view_create(raw_buffer, dims.data(), dims.size(),
element_type, &buffer_view),
"Error allocating buffer_view");
iree_hal_buffer_release(raw_buffer);
iree_vm_ref_t buffer_view_ref =
iree_hal_buffer_view_move_ref(buffer_view);
Expand Down Expand Up @@ -763,10 +764,10 @@ void FunctionAbi::PackBuffer(const RawSignatureParser::Description& desc,
absl::InlinedVector<int, 5> dims(py_view.ndim);
std::copy(py_view.shape, py_view.shape + py_view.ndim, dims.begin());
iree_hal_buffer_view_t* buffer_view;
CheckApiStatus(iree_hal_buffer_view_create(
raw_buffer, dims.data(), dims.size(), element_type,
iree_allocator_system(), &buffer_view),
"Error allocating buffer_view");
CheckApiStatus(
iree_hal_buffer_view_create(raw_buffer, dims.data(), dims.size(),
element_type, &buffer_view),
"Error allocating buffer_view");
iree_hal_buffer_release(raw_buffer);
iree_vm_ref_t buffer_view_ref = iree_hal_buffer_view_move_ref(buffer_view);
CheckApiStatus(iree_vm_list_push_ref_move(f_args.raw_ptr(), &buffer_view_ref),
Expand Down
17 changes: 7 additions & 10 deletions bindings/python/pyiree/rt/hal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ namespace {

class HalMappedMemory {
public:
HalMappedMemory(iree_hal_mapped_memory_t mapped_memory,
HalMappedMemory(iree_hal_buffer_mapping_t mapped_memory,
iree_hal_buffer_view_t* bv)
: mapped_memory_(mapped_memory), bv_(bv) {
iree_hal_buffer_view_retain(bv_);
}
~HalMappedMemory() {
if (bv_) {
iree_hal_buffer_t* buffer = iree_hal_buffer_view_buffer(bv_);
IREE_CHECK_OK(iree_hal_buffer_unmap(buffer, &mapped_memory_));
iree_hal_buffer_unmap_range(&mapped_memory_);
iree_hal_buffer_view_release(bv_);
}
}
Expand All @@ -44,10 +44,10 @@ class HalMappedMemory {
static HalMappedMemory Create(HalBufferView& bv) {
iree_hal_buffer_t* buffer = iree_hal_buffer_view_buffer(bv.raw_ptr());
iree_device_size_t byte_length = iree_hal_buffer_byte_length(buffer);
iree_hal_mapped_memory_t mapped_memory;
CheckApiStatus(iree_hal_buffer_map(buffer, IREE_HAL_MEMORY_ACCESS_READ,
0 /* element_offset */, byte_length,
&mapped_memory),
iree_hal_buffer_mapping_t mapped_memory;
CheckApiStatus(iree_hal_buffer_map_range(
buffer, IREE_HAL_MEMORY_ACCESS_READ,
0 /* element_offset */, byte_length, &mapped_memory),
"Could not map memory");
return HalMappedMemory(mapped_memory, bv.raw_ptr());
}
Expand Down Expand Up @@ -81,7 +81,7 @@ class HalMappedMemory {
}

private:
iree_hal_mapped_memory_t mapped_memory_;
iree_hal_buffer_mapping_t mapped_memory_;
iree_hal_buffer_view_t* bv_;
};

Expand Down Expand Up @@ -167,9 +167,6 @@ void SetupHalBindings(pybind11::module m) {
py::class_<HalMappedMemory>(m, "MappedMemory", py::buffer_protocol())
.def_buffer(&HalMappedMemory::ToBufferInfo);
py::class_<HalBuffer>(m, "HalBuffer")
.def_static("allocate_heap", &HalBuffer::AllocateHeapBuffer,
py::arg("memory_type"), py::arg("usage"),
py::arg("allocation_size"))
.def("fill_zero", &HalBuffer::FillZero, py::arg("byte_offset"),
py::arg("byte_length"))
.def("create_view", &HalBuffer::CreateView, py::arg("shape"),
Expand Down
14 changes: 1 addition & 13 deletions bindings/python/pyiree/rt/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ class HalBufferView

class HalBuffer : public ApiRefCounted<HalBuffer, iree_hal_buffer_t> {
public:
static HalBuffer AllocateHeapBuffer(int32_t memory_type, int32_t usage,
iree_host_size_t allocation_size) {
iree_hal_buffer_t* buffer = nullptr;
CheckApiStatus(
iree_hal_heap_buffer_allocate(
static_cast<iree_hal_memory_type_t>(memory_type),
static_cast<iree_hal_buffer_usage_t>(usage), allocation_size,
iree_allocator_system(), iree_allocator_system(), &buffer),
"Error allocating heap buffer");
return HalBuffer::CreateRetained(buffer);
}

iree_device_size_t byte_length() const {
return iree_hal_buffer_byte_length(raw_ptr());
}
Expand All @@ -121,7 +109,7 @@ class HalBuffer : public ApiRefCounted<HalBuffer, iree_hal_buffer_t> {
IREE_HAL_ELEMENT_TYPE_NONE, element_size * 8);
CheckApiStatus(
iree_hal_buffer_view_create(raw_ptr(), shape.s.data(), shape.s.size(),
element_type, iree_allocator_system(), &bv),
element_type, &bv),
"Error creating buffer view");
return HalBufferView::CreateRetained(bv);
}
Expand Down
21 changes: 0 additions & 21 deletions bindings/python/pyiree/rt/hal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ def testEnums(self):
logging.info("MemoryType: %s", rt.MemoryType)
logging.info("HOST_VISIBLE: %s", int(rt.MemoryType.HOST_VISIBLE))

def testAllocateHeap(self):
b = rt.HalBuffer.allocate_heap(memory_type=int(rt.MemoryType.HOST_LOCAL),
usage=int(rt.BufferUsage.ALL),
allocation_size=4096)
self.assertIsNot(b, None)
b.fill_zero(0, 4096)
shape = rt.Shape([1, 1024])
unused_bv = b.create_view(shape, 4)

def testStrideCalculation(self):
b = rt.HalBuffer.allocate_heap(memory_type=int(rt.MemoryType.HOST_LOCAL),
usage=int(rt.BufferUsage.ALL),
allocation_size=4096)
self.assertIsNot(b, None)
b.fill_zero(0, 4096)
shape = rt.Shape([16, 1, 8, 4, 2])
bv = b.create_view(shape, 4)
self.assertEqual(
np.array(bv.map()).strides,
(1 * 8 * 4 * 2 * 4, 8 * 4 * 2 * 4, 4 * 2 * 4, 2 * 4, 4))


if __name__ == "__main__":
absltest.main()
11 changes: 5 additions & 6 deletions bindings/python/pyiree/rt/host_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,14 @@ class PyMappedMemory {
}
};

PyMappedMemory(Description desc, iree_hal_mapped_memory_t mapped_memory,
PyMappedMemory(Description desc, iree_hal_buffer_mapping_t mapped_memory,
HalBuffer buffer)
: desc_(std::move(desc)),
mapped_memory_(mapped_memory),
buf_(std::move(buffer)) {}
~PyMappedMemory() {
if (buf_) {
CheckApiStatus(iree_hal_buffer_unmap(buf_.raw_ptr(), &mapped_memory_),
"Error unmapping memory");
iree_hal_buffer_unmap_range(&mapped_memory_);
}
}
PyMappedMemory(PyMappedMemory&& other)
Expand All @@ -139,8 +138,8 @@ class PyMappedMemory {
HalBuffer buffer) {
iree_device_size_t byte_length =
iree_hal_buffer_byte_length(buffer.raw_ptr());
iree_hal_mapped_memory_t mapped_memory;
CheckApiStatus(iree_hal_buffer_map(
iree_hal_buffer_mapping_t mapped_memory;
CheckApiStatus(iree_hal_buffer_map_range(
buffer.raw_ptr(), IREE_HAL_MEMORY_ACCESS_READ,
0 /* element_offset */, byte_length, &mapped_memory),
"Could not map memory");
Expand All @@ -160,7 +159,7 @@ class PyMappedMemory {

private:
Description desc_;
iree_hal_mapped_memory_t mapped_memory_;
iree_hal_buffer_mapping_t mapped_memory_;
HalBuffer buf_;
};

Expand Down
41 changes: 30 additions & 11 deletions build_tools/bazel/iree.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,45 @@ build:macos_clang --per_file_copt=tensorflow,iree_tf_compiler@-Wno-range-loop-an
# https://github.com/google/sanitizers/wiki/AddressSanitizer
###############################################################################

# Turn on asan. Some toolchains make use of the asan feature and we'll directly
# set the appropriate opts.
# ASAN (address sanitizer)
# https://clang.llvm.org/docs/AddressSanitizer.html
build:asan --config=sanitizer
build:asan --features=asan
build:asan --copt=-fsanitize=address
build:asan --copt=-fsanitize-address-use-after-scope
build:asan --linkopt=-fsanitize=address
build:asan --cc_output_directory_tag=asan
build:asan --copt=-DADDRESS_SANITIZER

# MSAN (memory sanitizer)
# https://clang.llvm.org/docs/MemorySanitizer.html
build:msan --config=sanitizer
build:msan --features=msan
build:msan --copt=-fsanitize=memory
build:msan --copt=-fsanitize-memory-track-origins
build:msan --linkopt=-fsanitize=memory
build:msan --cc_output_directory_tag=msan
build:msan --copt=-DMEMORY_SANITIZER

# TSAN (thread sanitizer)
# https://clang.llvm.org/docs/ThreadSanitizer.html
build:tsan --config=sanitizer
build:tsan --features=tsan
build:tsan --copt=-fsanitize=thread
build:tsan --linkopt=-fsanitize=thread
build:tsan --cc_output_directory_tag=tsan
build:tsan --copt=-DTHREAD_SANITIZER

# Don't strip debug info
build:asan --strip=never
build:sanitizer --strip=never
# Ignore settings of `linkopts = ["-static"]` which can screw up the sanitizer.
# We don't use this in IREE (that's what linkstatic is for), but it could show
# up in dependencies.
build:asan --force_ignore_dash_static
# asan tests tend to take longer, so increase the timeouts
build:asan --test_timeout=120,600,1800,-1
# Make the outputs easy to find
build:asan --cc_output_directory_tag=asan
build:sanitizer --force_ignore_dash_static
# sanitizer tests tend to take longer, so increase the timeouts
build:sanitizer --test_timeout=120,600,1800,-1
# Get better stack traces
build:asan --copt=-fno-omit-frame-pointer
# This macro define is used by absl
build:asan --copt=-DADDRESS_SANITIZER
build:sanitizer --copt=-fno-omit-frame-pointer

###############################################################################
# Architecture specific options
Expand Down
2 changes: 1 addition & 1 deletion build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@renderdoc_api//:renderdoc_app": ["renderdoc_api::renderdoc_app"],
"@pffft": ["pffft"],
"@cpuinfo//:cpuinfo": ["cpuinfo"],
"@half//:half": ["half"],
"@half//:includes": [],
"@vulkan_memory_allocator//:impl_header_only": ["vulkan_memory_allocator"],
}

Expand Down
4 changes: 2 additions & 2 deletions build_tools/cmake/iree_macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ function(iree_add_test_environment_properties TEST_NAME)
#
# Tests which only depend on a compiler target backend or a runtime HAL
# driver, but not both, should generally use a different method of filtering.
if(NOT ${IREE_TARGET_BACKEND_VULKAN-SPIRV} OR NOT ${IREE_HAL_DRIVER_VULKAN})
if(NOT "${IREE_TARGET_BACKEND_VULKAN-SPIRV}" OR NOT "${IREE_HAL_DRIVER_VULKAN}")
set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "IREE_VULKAN_DISABLE=1")
endif()
if(NOT ${IREE_TARGET_BACKEND_DYLIB-LLVM-AOT} OR NOT ${IREE_HAL_DRIVER_DYLIB})
if(NOT "${IREE_TARGET_BACKEND_DYLIB-LLVM-AOT}" OR NOT "${IREE_HAL_DRIVER_DYLIB}")
set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "IREE_LLVMAOT_DISABLE=1")
endif()
endfunction()
Loading

0 comments on commit 6ee1d9a

Please sign in to comment.