Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@
[submodule "third_party/hsa-runtime-headers"]
path = third_party/hsa-runtime-headers
url = https://github.com/iree-org/hsa-runtime-headers.git
[submodule "third_party/printf"]
path = third_party/printf
url = https://github.com/eyalroz/printf.git
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,9 @@ if(IREE_ENABLE_CPUINFO)
endif()
endif()

# Platform-independent printf implementation (eyalroz/printf).
add_subdirectory(build_tools/third_party/printf EXCLUDE_FROM_ALL)

# This defines the iree-flatcc-cli target, so we don't use EXCLUDE_FROM_ALL.
add_subdirectory(build_tools/third_party/flatcc)

Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ use_repo(
"iree_cuda",
"llvm-raw",
"nccl",
"printf_lib",
"rccl",
"spirv_cross",
"stablehlo",
Expand Down
7 changes: 7 additions & 0 deletions build_tools/bazel/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def _iree_extension_impl(module_ctx):
path = "third_party/rccl",
)

# eyalroz/printf (platform-independent printf implementation)
new_local_repository(
name = "printf_lib",
build_file = "@iree_core//:build_tools/third_party/printf/BUILD.overlay",
path = "third_party/printf",
)

# WebGPU headers
new_local_repository(
name = "webgpu_headers",
Expand Down
1 change: 1 addition & 0 deletions build_tools/bazel_to_cmake/bazel_to_cmake_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def __init__(self, repo_map: Dict[str, str]):
"@vulkan_headers": ["Vulkan::Headers"],
# Misc single targets
"@com_google_benchmark//:benchmark": ["benchmark"],
"@printf_lib//:printf": ["printf::printf"],
"@com_github_dvidelabs_flatcc//:flatcc": ["flatcc"],
"@com_github_dvidelabs_flatcc//:parsing": ["flatcc::parsing"],
"@com_github_dvidelabs_flatcc//:runtime": ["flatcc::runtime"],
Expand Down
16 changes: 16 additions & 0 deletions build_tools/cmake/linux_riscv64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ if(NOT "${RISCV_TOOLCHAIN_ROOT}" STREQUAL "")
set(CMAKE_RANLIB "${RISCV_TOOLCHAIN_ROOT}/bin/${RISCV_TOOLCHAIN_PREFIX}llvm-ranlib")
set(CMAKE_STRIP "${RISCV_TOOLCHAIN_ROOT}/bin/${RISCV_TOOLCHAIN_PREFIX}llvm-strip")
set(CMAKE_SYSROOT "${RISCV_TOOLCHAIN_ROOT}/sysroot")
# Strip debug info from libgcc.a to work around LLD incompatibility. The
# GCC 12.2.0-compiled libgcc.a contains DWARF debug info with relocations
# that LLD cannot parse (unknown relocation types 60/61 in .debug_rnglists
# and .debug_loclists sections). Code and data sections are unaffected;
# only debug metadata is removed. This is idempotent.
file(GLOB_RECURSE _LIBGCC_ARCHIVES "${RISCV_TOOLCHAIN_ROOT}/lib/gcc/*/libgcc.a")
foreach(_LIBGCC IN LISTS _LIBGCC_ARCHIVES)
execute_process(
COMMAND "${RISCV_TOOLCHAIN_ROOT}/bin/${RISCV_TOOLCHAIN_PREFIX}llvm-objcopy"
--strip-debug "${_LIBGCC}"
RESULT_VARIABLE _strip_result
)
if(NOT _strip_result EQUAL 0)
message(WARNING "Failed to strip debug from ${_LIBGCC}")
endif()
endforeach()
endif()

# Specify ISA spec for march=rv64gc. This is to resolve the mismatch between
Expand Down
1 change: 1 addition & 0 deletions build_tools/scripts/git/runtime_submodules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ third_party/googletest
third_party/hip-build-deps
third_party/hsa-runtime-headers
third_party/musl
third_party/printf
third_party/spirv_cross
third_party/tracy
third_party/vulkan_headers
Expand Down
23 changes: 23 additions & 0 deletions build_tools/third_party/printf/BUILD.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2026 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

package(default_visibility = ["//visibility:public"])

cc_library(
name = "printf",
srcs = ["src/printf/printf.c"],
hdrs = ["src/printf/printf.h"],
copts = select({
"@bazel_tools//src/conditions:windows": [],
"//conditions:default": [
"-Wno-sign-conversion",
],
}),
local_defines = [
"PRINTF_SUPPORT_WRITEBACK_SPECIFIER=0",
],
strip_include_prefix = "src",
)
32 changes: 32 additions & 0 deletions build_tools/third_party/printf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2026 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

set(PRINTF_ROOT "${IREE_ROOT_DIR}/third_party/printf/")

# We don't install any headers because their only use is via the
# iree/base/printf.h wrapper.
set(IREE_HDRS_ROOT_PATH OFF)
# Considered part of the runtime.
set(IREE_INSTALL_LIBRARY_TARGETS_DEFAULT_COMPONENT IREEBundledLibraries)
set(IREE_INSTALL_LIBRARY_TARGETS_DEFAULT_EXPORT_SET Runtime)

external_cc_library(
PACKAGE
printf
NAME
printf
ROOT
${PRINTF_ROOT}
INCLUDES
"${PRINTF_ROOT}/src"
SRCS
"src/printf/printf.c"
HDRS
"src/printf/printf.h"
COPTS
"-DPRINTF_SUPPORT_WRITEBACK_SPECIFIER=0"
PUBLIC
)
20 changes: 9 additions & 11 deletions runtime/bindings/python/status_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@ PyObject* ApiStatusToPyExcClass(iree_status_t status) {
} // namespace

std::string ApiStatusToString(iree_status_t status) {
iree_host_size_t buffer_length = 0;
if (IREE_UNLIKELY(!iree_status_format(status, /*buffer_capacity=*/0,
/*buffer=*/NULL, &buffer_length))) {
return "";
}
std::string result;
result.resize(buffer_length);
// NOTE: buffer capacity needs to be +1 for the NUL terminator in snprintf.
return iree_status_format(status, result.size() + 1,
const_cast<char*>(result.data()), &buffer_length)
? result
: "";
iree_status_format_to(
status,
[](iree_string_view_t chunk, void* user_data) -> bool {
auto* str = static_cast<std::string*>(user_data);
str->append(chunk.data, chunk.size);
return true;
},
&result);
return result;
}

nanobind::python_error ApiStatusToPyExc(iree_status_t status,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/iree/async/cts/socket/lifecycle_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ TEST_P(SocketTest, MultipleExchanges) {
for (int round = 0; round < 3; ++round) {
// Prepare send data with round number embedded.
char send_data[64];
snprintf(send_data, sizeof(send_data), "Message round %d", round);
iree_snprintf(send_data, sizeof(send_data), "Message round %d", round);
iree_host_size_t send_length = strlen(send_data);
iree_async_span_t send_span =
iree_async_span_from_ptr((void*)send_data, send_length);
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/iree/async/cts/socket/posix_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class SocketPosixTest : public SocketTestBase<> {
std::string UniqueAbstractName(const char* base_name) {
static std::atomic<int> counter{0};
char buffer[108]; // sun_path max
snprintf(buffer, sizeof(buffer), "@iree_cts_%s_%d_%d", base_name,
(int)getpid(), counter.fetch_add(1, std::memory_order_relaxed));
iree_snprintf(buffer, sizeof(buffer), "@iree_cts_%s_%d_%d", base_name,
(int)getpid(),
counter.fetch_add(1, std::memory_order_relaxed));
return std::string(buffer);
}
};
Expand Down
10 changes: 5 additions & 5 deletions runtime/src/iree/async/cts/socket/tcp_transfer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ TEST_P(ConcurrentConnectionsTest, ConcurrentConnections_Sequential) {
// Verify each connection is independent by sending unique data.
for (int i = 0; i < kNumConnections; ++i) {
char send_data[32];
snprintf(send_data, sizeof(send_data), "Connection %d", i);
iree_snprintf(send_data, sizeof(send_data), "Connection %d", i);
iree_host_size_t send_length = strlen(send_data);

iree_async_span_t send_span =
Expand Down Expand Up @@ -856,7 +856,7 @@ TEST_P(ConcurrentConnectionsTest, ConcurrentConnections_Parallel) {
CompletionTracker recv_trackers[kNumConnections];

for (int i = 0; i < kNumConnections; ++i) {
snprintf(send_data[i], sizeof(send_data[i]), "Parallel %d", i);
iree_snprintf(send_data[i], sizeof(send_data[i]), "Parallel %d", i);
send_spans[i] =
iree_async_span_from_ptr((void*)send_data[i], strlen(send_data[i]));

Expand Down Expand Up @@ -975,8 +975,8 @@ TEST_P(ConcurrentConnectionsTest, ConcurrentConnections_Interleaved) {

// Submit all sends and recvs for this round.
for (int i = 0; i < kNumConnections; ++i) {
snprintf(send_data[i], sizeof(send_data[i]), "Round %d Conn %d", round,
i);
iree_snprintf(send_data[i], sizeof(send_data[i]), "Round %d Conn %d",
round, i);
send_spans[i] =
iree_async_span_from_ptr((void*)send_data[i], strlen(send_data[i]));

Expand Down Expand Up @@ -1214,7 +1214,7 @@ TEST_P(ScatterGatherTest, ScatterGather_ManyBuffers) {
iree_host_size_t total_length = 0;

for (int i = 0; i < kNumBuffers; ++i) {
snprintf(buffers[i], sizeof(buffers[i]), "[Chunk %d]", i);
iree_snprintf(buffers[i], sizeof(buffers[i]), "[Chunk %d]", i);
iree_host_size_t len = strlen(buffers[i]);
spans[i] = iree_async_span_from_ptr(buffers[i], len);
total_length += len;
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/iree/async/platform/io_uring/proactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1605,8 +1605,9 @@ static iree_status_t iree_async_proactor_io_uring_import_file(
file->primitive = primitive;
file->fixed_file_index = -1; // Not using io_uring fixed files yet.

IREE_TRACE(
{ snprintf(file->debug_path, sizeof(file->debug_path), "fd:%d", fd); });
IREE_TRACE({
iree_snprintf(file->debug_path, sizeof(file->debug_path), "fd:%d", fd);
});

*out_file = file;
IREE_TRACE_ZONE_END(z0);
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/iree/async/platform/io_uring/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ static void iree_async_socket_initialize(
iree_atomic_store(&socket->failure_status, (intptr_t)iree_ok_status(),
iree_memory_order_release);
IREE_TRACE({
snprintf(socket->debug_label, sizeof(socket->debug_label), "socket:%d", fd);
iree_snprintf(socket->debug_label, sizeof(socket->debug_label), "socket:%d",
fd);
});
}

Expand Down
11 changes: 6 additions & 5 deletions runtime/src/iree/async/platform/iocp/proactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1742,9 +1742,10 @@ static iree_status_t iree_async_proactor_iocp_poll(
(intptr_t)iree_ok_status(),
iree_memory_order_release);
IREE_TRACE({
snprintf(accepted_socket->debug_label,
sizeof(accepted_socket->debug_label),
"accepted:%llu", (unsigned long long)accept_sock);
iree_snprintf(accepted_socket->debug_label,
sizeof(accepted_socket->debug_label),
"accepted:%llu",
(unsigned long long)accept_sock);
});
accept_op->accepted_socket = accepted_socket;
} else {
Expand Down Expand Up @@ -2317,8 +2318,8 @@ static iree_status_t iree_async_proactor_iocp_import_file(
file->fixed_file_index = -1;

IREE_TRACE({
snprintf(file->debug_path, sizeof(file->debug_path), "handle:%p",
(void*)handle);
iree_snprintf(file->debug_path, sizeof(file->debug_path), "handle:%p",
(void*)handle);
});

*out_file = file;
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/iree/async/platform/iocp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ static void iree_async_iocp_socket_initialize(
iree_atomic_store(&socket->failure_status, (intptr_t)iree_ok_status(),
iree_memory_order_release);
IREE_TRACE({
snprintf(socket->debug_label, sizeof(socket->debug_label), "socket:%llu",
(unsigned long long)sock);
iree_snprintf(socket->debug_label, sizeof(socket->debug_label),
"socket:%llu", (unsigned long long)sock);
});
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/iree/async/platform/posix/proactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -3124,7 +3124,7 @@ static iree_status_t iree_async_proactor_posix_import_file(
IREE_TRACE({
// For debugging, store a placeholder path since we don't know the actual
// path for an imported fd.
snprintf(file->debug_path, sizeof(file->debug_path), "fd:%d", fd);
iree_snprintf(file->debug_path, sizeof(file->debug_path), "fd:%d", fd);
});

*out_file = file;
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/iree/async/platform/posix/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ static void iree_async_socket_initialize(iree_async_socket_t* socket,
iree_atomic_store(&socket->failure_status, (intptr_t)iree_ok_status(),
iree_memory_order_release);
IREE_TRACE({
snprintf(socket->debug_label, sizeof(socket->debug_label), "socket:%d", fd);
iree_snprintf(socket->debug_label, sizeof(socket->debug_label), "socket:%d",
fd);
});
}

Expand Down
3 changes: 3 additions & 0 deletions runtime/src/iree/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ iree_runtime_cc_library(
"loop.h",
"loop_inline.c",
"loop_inline.h",
"printf.c",
"printf.h",
"status.c",
"status.h",
"status_cc.h",
Expand All @@ -57,6 +59,7 @@ iree_runtime_cc_library(
"//runtime/src/iree/base/internal",
"//runtime/src/iree/base/internal:time",
"//runtime/src/iree/base/tracing:provider",
"@printf_lib//:printf",
],
)

Expand Down
3 changes: 3 additions & 0 deletions runtime/src/iree/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ iree_cc_library(
"loop.h"
"loop_inline.c"
"loop_inline.h"
"printf.c"
"printf.h"
"status.c"
"status.h"
"status_cc.h"
Expand All @@ -88,6 +90,7 @@ iree_cc_library(
iree::base::internal
iree::base::internal::time
iree::base::tracing::provider
printf::printf
${IREE_LIBBACKTRACE_TARGET}
${_IREE_ALLOCATOR_SYSTEM_DEPS}
PUBLIC
Expand Down
1 change: 1 addition & 0 deletions runtime/src/iree/base/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#include "iree/base/config.h" // IWYU pragma: export
#include "iree/base/loop.h" // IWYU pragma: export
#include "iree/base/loop_inline.h" // IWYU pragma: export
#include "iree/base/printf.h" // IWYU pragma: export
#include "iree/base/status.h" // IWYU pragma: export
#include "iree/base/string_builder.h" // IWYU pragma: export
#include "iree/base/string_view.h" // IWYU pragma: export
Expand Down
10 changes: 5 additions & 5 deletions runtime/src/iree/base/internal/dynamic_library_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static iree_status_t iree_dynamic_library_make_temp_file_path(
const char* tmpdir, char** out_file_path) {
// Stamp in a unique file name (replacing XXXXXX in the string).
char temp_path[512];
if (snprintf(temp_path, sizeof(temp_path), "%s/iree_dylib_XXXXXX", tmpdir) >=
sizeof(temp_path)) {
if (iree_snprintf(temp_path, sizeof(temp_path), "%s/iree_dylib_XXXXXX",
tmpdir) >= sizeof(temp_path)) {
// NOTE: we could dynamically allocate things, but didn't seem worth it.
return iree_make_status(
IREE_STATUS_INVALID_ARGUMENT,
Expand All @@ -57,15 +57,15 @@ static iree_status_t iree_dynamic_library_make_temp_file_path(

// Allocate storage for the full file path and format it in.
int file_path_length =
snprintf(NULL, 0, "%s_%s.%s", temp_path, prefix, extension);
iree_snprintf(NULL, 0, "%s_%s.%s", temp_path, prefix, extension);
if (file_path_length < 0) {
return iree_make_status(IREE_STATUS_INVALID_ARGUMENT,
"unable to form temp path string");
}
IREE_RETURN_IF_ERROR(iree_allocator_malloc(
allocator, file_path_length + /*NUL=*/1, (void**)out_file_path));
snprintf(*out_file_path, file_path_length + /*NUL=*/1, "%s_%s.%s", temp_path,
prefix, extension);
iree_snprintf(*out_file_path, file_path_length + /*NUL=*/1, "%s_%s.%s",
temp_path, prefix, extension);

// Canonicalize away any double path separators.
iree_file_path_canonicalize(*out_file_path, file_path_length);
Expand Down
Loading
Loading