Skip to content

Commit 9995511

Browse files
committed
Guard C++ fallback code in context_bridge.cc with SPIRV_RUST_TARGET_ENV
When building with SPIRV_RUST_TARGET_ENV defined, the C++ fallback implementations that use spvtools::SpirvTools and spvtools::reduce::Reducer are not needed since Rust provides these implementations. This avoids link-time dependencies on C++ SPIRV-Tools libraries that would create circular dependencies: spirv-tools-ffi needs symbols from SPIRV-Tools, but SPIRV-Tools links against spirv-tools-ffi. The guard conditionally excludes: - #include of libspirv.hpp and reducer.h - FormatDiagnostic helper function - C++ SpirvTools usage in validate_binary_with_options - C++ Reducer usage in reduce_with_cpp
1 parent 6423a56 commit 9995511

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

rust/spirv-tools-ffi/src/context_bridge.cc

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55

66
#include "spirv-tools-ffi/src/lib.rs.h"
77
#include "rust/cxxbridge/spirv-tools-ffi/src/context_bridge.h"
8-
#include "source/reduce/reducer.h"
9-
#include "source/spirv_reducer_options.h"
108
#include "source/table.h"
119
#include "spirv-tools/libspirv.h"
10+
11+
// When building with Rust target env, we don't need C++ fallback implementations
12+
// since Rust provides them. This avoids link dependencies on C++ SPIRV-Tools
13+
// libraries (libspirv.hpp, reducer.h) that would create circular dependencies.
14+
#ifndef SPIRV_RUST_TARGET_ENV
15+
#include "source/reduce/reducer.h"
16+
#include "source/spirv_reducer_options.h"
1217
#include "spirv-tools/libspirv.hpp"
18+
#endif
1319

1420
namespace {
21+
#ifndef SPIRV_RUST_TARGET_ENV
1522
std::string FormatDiagnostic(spv_message_level_t, const spv_position_t& position,
1623
const char* message) {
1724
std::ostringstream oss;
@@ -22,6 +29,7 @@ std::string FormatDiagnostic(spv_message_level_t, const spv_position_t& position
2229
oss << ": " << message;
2330
return oss.str();
2431
}
32+
#endif
2533
} // namespace
2634

2735
namespace spvtools::ffi {
@@ -58,6 +66,10 @@ void dispatch_context_message(std::uintptr_t context_ptr, std::uint32_t level,
5866
ValidateResult validate_binary_with_options(
5967
std::uint32_t env, rust::Slice<const std::uint32_t> words,
6068
const ValidatorOptions& options) {
69+
#ifdef SPIRV_RUST_TARGET_ENV
70+
// When built with Rust target env, always use Rust validator
71+
return validate_binary_rust(env, words, options);
72+
#else
6173
ValidateResult result{false, ::rust::String()};
6274
if (rust_validator_enabled()) {
6375
return validate_binary_rust(env, words, options);
@@ -78,6 +90,7 @@ ValidateResult validate_binary_with_options(
7890
result.message = ::rust::String(diagnostics);
7991
}
8092
return result;
93+
#endif
8194
}
8295

8396
ValidateResult validate_binary(std::uint32_t env,
@@ -89,6 +102,18 @@ ValidateResult validate_binary(std::uint32_t env,
89102
ReduceResult reduce_with_cpp(std::uint32_t env,
90103
rust::Slice<const std::uint32_t> words,
91104
const ReduceOptions& options) {
105+
#ifdef SPIRV_RUST_TARGET_ENV
106+
// When built with Rust target env, C++ reducer is not available.
107+
// The Rust side should handle reduction.
108+
(void)env;
109+
(void)words;
110+
(void)options;
111+
ReduceResult result{/*success=*/false,
112+
ToolError::Disabled,
113+
::rust::String("C++ reducer unavailable in Rust build; use Rust reducer"),
114+
::rust::Vec<std::uint32_t>()};
115+
return result;
116+
#else
92117
ReduceResult result{/*success=*/false,
93118
ToolError::Parse,
94119
::rust::String(),
@@ -154,6 +179,7 @@ ReduceResult reduce_with_cpp(std::uint32_t env,
154179
}
155180

156181
return result;
182+
#endif
157183
}
158184

159185
FuzzResult fuzz_with_cpp(std::uint32_t env,

0 commit comments

Comments
 (0)