[Runtime][Build] Enable freestanding builds for IREE_PLATFORM_GENERIC (#24820)
Avoid assuming POSIX facilities are available on Generic platforms,
allowing the runtime to build for freestanding/bare-metal targets.
This disables POSIX-dependent functionality such as sockets and singal
handling by default, avoids selecting a POSIX async proactor, and
provides the small platform fallbacks needed by the Generic runtime
build.
This commit also adds Bazel support for the Generic riscv64
configuration, which is currently being brought up. It fixes additional
build-system issues encountered during Generic riscv bring up.
Assisted-by: Claude code
Signed-off-by: Pooja Hemashekar <hemashekar@roofline.ai>
diff --git a/build_tools/bazel/BUILD.bazel b/build_tools/bazel/BUILD.bazel
index 7d870c7..dcf0f30 100644
--- a/build_tools/bazel/BUILD.bazel
+++ b/build_tools/bazel/BUILD.bazel
@@ -45,6 +45,16 @@
constraint_values = ["@platforms//os:macos"],
)
+# Generic RISC-V 64-bit targets. Include the CPU constraint because
+# @platforms//os:none alone would also match freestanding wasm32 targets.
+config_setting(
+ name = "iree_is_generic_riscv64",
+ constraint_values = [
+ "@platforms//cpu:riscv64",
+ "@platforms//os:none",
+ ],
+)
+
config_setting(
name = "iree_is_windows",
constraint_values = ["@platforms//os:windows"],
diff --git a/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py b/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
index fdd76a7..c50ecbb 100644
--- a/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
+++ b/build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
@@ -31,6 +31,7 @@
_PLATFORM_CMAKE_SYSTEM_NAME = {
# select() condition labels (config_setting in build_tools/bazel/).
"//build_tools/bazel:iree_is_android": "Android",
+ "//build_tools/bazel:iree_is_generic_riscv64": "Generic",
"//build_tools/bazel:iree_is_linux": "Linux",
"//build_tools/bazel:iree_is_macos": "Darwin",
"//build_tools/bazel:iree_is_wasm": "wasm_32",
diff --git a/runtime/src/iree/async/BUILD.bazel b/runtime/src/iree/async/BUILD.bazel
index ad5c77d..b36805f 100644
--- a/runtime/src/iree/async/BUILD.bazel
+++ b/runtime/src/iree/async/BUILD.bazel
@@ -127,6 +127,8 @@
"//build_tools/bazel:iree_is_wasm": [
"//runtime/src/iree/async/platform/js:proactor",
],
+ # Generic targets have no platform proactor backend.
+ "//build_tools/bazel:iree_is_generic_riscv64": [],
"//conditions:default": [
"//runtime/src/iree/async/platform/posix",
],
diff --git a/runtime/src/iree/async/CMakeLists.txt b/runtime/src/iree/async/CMakeLists.txt
index 1211e7c..309440e 100644
--- a/runtime/src/iree/async/CMakeLists.txt
+++ b/runtime/src/iree/async/CMakeLists.txt
@@ -102,6 +102,7 @@
list(APPEND _platform_platform_deps iree::async::platform::iocp)
elseif(IREE_ARCH STREQUAL "wasm_32")
list(APPEND _platform_platform_deps iree::async::platform::js::proactor)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "Generic")
else()
list(APPEND _platform_platform_deps iree::async::platform::posix)
endif()
diff --git a/runtime/src/iree/async/proactor_platform.c b/runtime/src/iree/async/proactor_platform.c
index 6d7a670..70a49e9 100644
--- a/runtime/src/iree/async/proactor_platform.c
+++ b/runtime/src/iree/async/proactor_platform.c
@@ -10,9 +10,11 @@
#include "iree/async/platform/io_uring/api.h"
#endif // IREE_PLATFORM_LINUX && !IREE_PLATFORM_ANDROID
-#if !defined(IREE_PLATFORM_WINDOWS) && !defined(IREE_PLATFORM_WASM)
+#if !defined(IREE_PLATFORM_WINDOWS) && !defined(IREE_PLATFORM_WASM) && \
+ !defined(IREE_PLATFORM_GENERIC)
#include "iree/async/platform/posix/api.h"
-#endif // !IREE_PLATFORM_WINDOWS && !IREE_PLATFORM_WASM
+#endif // !IREE_PLATFORM_WINDOWS && !IREE_PLATFORM_WASM &&
+ // !IREE_PLATFORM_GENERIC
#if defined(IREE_PLATFORM_WINDOWS)
#include "iree/async/platform/iocp/api.h"
@@ -51,6 +53,10 @@
status = iree_async_proactor_create_js(options, allocator, out_proactor);
+#elif defined(IREE_PLATFORM_GENERIC)
+
+ // Generic targets have no platform proactor; status remains UNAVAILABLE.
+
#else // macOS, BSD, Android, etc.
status = iree_async_proactor_create_posix(options, allocator, out_proactor);
diff --git a/runtime/src/iree/async/util/signal.c b/runtime/src/iree/async/util/signal.c
index 8c7b671..89a0be2 100644
--- a/runtime/src/iree/async/util/signal.c
+++ b/runtime/src/iree/async/util/signal.c
@@ -8,9 +8,9 @@
#include "iree/base/internal/atomics.h"
-#if !defined(IREE_PLATFORM_WINDOWS)
+#if IREE_POSIX_SIGNALS_ENABLE
#include <signal.h>
-#endif
+#endif // IREE_POSIX_SIGNALS_ENABLE
//===----------------------------------------------------------------------===//
// iree_async_signal_name
@@ -42,25 +42,9 @@
// iree_async_signal_block_default / iree_async_signal_ignore_broken_pipe
//===----------------------------------------------------------------------===//
-#if defined(IREE_PLATFORM_WINDOWS)
+#if !IREE_POSIX_SIGNALS_ENABLE
-// Windows doesn't have POSIX signals. These are no-ops.
-
-IREE_API_EXPORT iree_status_t iree_async_signal_block_default(void) {
- // No-op on Windows; signals don't exist in the POSIX sense.
- // Console control events are handled via SetConsoleCtrlHandler.
- return iree_ok_status();
-}
-
-IREE_API_EXPORT iree_status_t iree_async_signal_ignore_broken_pipe(void) {
- // No-op on Windows; SIGPIPE doesn't exist.
- // Socket errors are returned from send/recv calls directly.
- return iree_ok_status();
-}
-
-#elif defined(IREE_PLATFORM_WASM)
-
-// Wasm has no POSIX signals.
+// POSIX signals are unavailable on this platform; these operations are no-ops.
IREE_API_EXPORT iree_status_t iree_async_signal_block_default(void) {
return iree_ok_status();
@@ -104,7 +88,7 @@
return iree_ok_status();
}
-#endif // POSIX
+#endif // IREE_POSIX_SIGNALS_ENABLE
//===----------------------------------------------------------------------===//
// Global signal ownership
@@ -251,7 +235,7 @@
// POSIX signal number conversion
//===----------------------------------------------------------------------===//
-#if !defined(IREE_PLATFORM_WINDOWS) && !defined(IREE_PLATFORM_WASM)
+#if IREE_POSIX_SIGNALS_ENABLE
int iree_async_signal_to_posix(iree_async_signal_t signal) {
switch (signal) {
@@ -301,4 +285,4 @@
sigaddset(mask, SIGUSR2);
}
-#endif // !IREE_PLATFORM_WINDOWS && !IREE_PLATFORM_WASM
+#endif // IREE_POSIX_SIGNALS_ENABLE
diff --git a/runtime/src/iree/async/util/signal.h b/runtime/src/iree/async/util/signal.h
index 678de57..ddedf4c 100644
--- a/runtime/src/iree/async/util/signal.h
+++ b/runtime/src/iree/async/util/signal.h
@@ -124,7 +124,7 @@
// POSIX signal number conversion
//===----------------------------------------------------------------------===//
-#if !defined(IREE_PLATFORM_WINDOWS)
+#if IREE_POSIX_SIGNALS_ENABLE
// Converts an IREE signal enum to the corresponding POSIX signal number.
// Returns 0 for IREE_ASYNC_SIGNAL_NONE or invalid signals.
@@ -138,7 +138,7 @@
// This is used by signalfd and pthread_sigmask.
void iree_async_signal_build_sigset(sigset_t* mask);
-#endif // !IREE_PLATFORM_WINDOWS
+#endif // IREE_POSIX_SIGNALS_ENABLE
#ifdef __cplusplus
} // extern "C"
diff --git a/runtime/src/iree/base/config.h b/runtime/src/iree/base/config.h
index 177bcca..ab2006c 100644
--- a/runtime/src/iree/base/config.h
+++ b/runtime/src/iree/base/config.h
@@ -172,13 +172,30 @@
// can be stripped out. Socket functions will still be defined but return
// IREE_STATUS_UNIMPLEMENTED. Override with -DIREE_SOCKETS_ENABLE=1 for custom
// platforms that provide a POSIX-compatible socket layer.
-#if defined(IREE_PLATFORM_WASM)
+#if defined(IREE_PLATFORM_WASM) || defined(IREE_PLATFORM_GENERIC)
#define IREE_SOCKETS_ENABLE 0
#else
#define IREE_SOCKETS_ENABLE 1
-#endif // IREE_PLATFORM_WASM
+#endif // IREE_PLATFORM_WASM || IREE_PLATFORM_GENERIC
#endif // !IREE_SOCKETS_ENABLE
+//===----------------------------------------------------------------------===//
+// POSIX signals
+//===----------------------------------------------------------------------===//
+
+#if !defined(IREE_POSIX_SIGNALS_ENABLE)
+// On platforms without POSIX signals (Windows, wasm, bare-metal), signal
+// handling can be stripped out. Signal functions will still be defined but
+// behave as no-ops. Override with -DIREE_POSIX_SIGNALS_ENABLE=1 for custom
+// platforms that provide the required POSIX-compatible signal APIs.
+#if defined(IREE_PLATFORM_WINDOWS) || defined(IREE_PLATFORM_WASM) || \
+ defined(IREE_PLATFORM_GENERIC)
+#define IREE_POSIX_SIGNALS_ENABLE 0
+#else
+#define IREE_POSIX_SIGNALS_ENABLE 1
+#endif // IREE_PLATFORM_WINDOWS || IREE_PLATFORM_WASM || IREE_PLATFORM_GENERIC
+#endif // !IREE_POSIX_SIGNALS_ENABLE
+
#if !defined(IREE_MAX_PATH)
// Maximum path C string length in characters excluding the NUL terminator.
// We stack allocate the path and want to keep it small enough to reasonably
diff --git a/runtime/src/iree/base/internal/csprng.c b/runtime/src/iree/base/internal/csprng.c
index 0377fa7..da012fa 100644
--- a/runtime/src/iree/base/internal/csprng.c
+++ b/runtime/src/iree/base/internal/csprng.c
@@ -92,6 +92,16 @@
return iree_ok_status();
}
+#elif defined(IREE_PLATFORM_GENERIC)
+
+// Generic platforms have no standard entropy source.
+
+IREE_API_EXPORT iree_status_t iree_csprng_fill(iree_byte_span_t buffer) {
+ (void)buffer;
+ return iree_make_status(IREE_STATUS_UNIMPLEMENTED,
+ "no CSPRNG available on generic platforms");
+}
+
#else
#error "CSPRNG not implemented for this platform"
diff --git a/runtime/src/iree/base/threading/thread.c b/runtime/src/iree/base/threading/thread.c
index 87e3055..82964c1 100644
--- a/runtime/src/iree/base/threading/thread.c
+++ b/runtime/src/iree/base/threading/thread.c
@@ -12,8 +12,8 @@
#include "iree/base/threading/mutex.h"
#include "iree/base/threading/thread_impl.h"
-#ifdef __NEWLIB__
-// newlib does not provide strnlen; provide a fallback implementation.
+#if defined(__NEWLIB__) || defined(IREE_PLATFORM_GENERIC)
+// newlib and strict ISO C libcs may not provide strnlen.
static size_t iree_strnlen(const char* s, size_t maxlen) {
size_t n = 0;
while (n < maxlen && s[n] != '\0') ++n;
diff --git a/runtime/src/iree/builtins/ukernel/arch/riscv_64/CMakeLists.txt b/runtime/src/iree/builtins/ukernel/arch/riscv_64/CMakeLists.txt
index 3ba7d6f..5d5f66f 100644
--- a/runtime/src/iree/builtins/ukernel/arch/riscv_64/CMakeLists.txt
+++ b/runtime/src/iree/builtins/ukernel/arch/riscv_64/CMakeLists.txt
@@ -195,6 +195,7 @@
# builtin. See: https://github.com/iree-org/iree/issues/22303.
# Once this issue is resolved, we won't need this check.
if(IREE_UK_TRY_RISCV_64_ZVFHMIN)
+ include(CheckCSourceCompiles)
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${IREE_UK_COPTS_RISCV_64_ZVFHMIN}")
string(JOIN "\n" IREE_UK_BUILD_RISCV_64_ZVFHMIN_ZVFH_TEST
"int main() {"
diff --git a/runtime/src/iree/testing/BUILD.bazel b/runtime/src/iree/testing/BUILD.bazel
index 065e690..b3ff7e2 100644
--- a/runtime/src/iree/testing/BUILD.bazel
+++ b/runtime/src/iree/testing/BUILD.bazel
@@ -116,6 +116,7 @@
iree_runtime_cc_library(
name = "coordinated_test",
+ testonly = True,
srcs = ["coordinated_test.c"],
hdrs = ["coordinated_test.h"],
deps = [
diff --git a/runtime/src/iree/testing/CMakeLists.txt b/runtime/src/iree/testing/CMakeLists.txt
index 73634ad..ac66250 100644
--- a/runtime/src/iree/testing/CMakeLists.txt
+++ b/runtime/src/iree/testing/CMakeLists.txt
@@ -101,6 +101,7 @@
"coordinated_test.c"
DEPS
iree::base
+ TESTONLY
PUBLIC
)