Skip to content

Commit 5fbe5da

Browse files
committed
Only try to link against offload functions if llvm.enzyme is enabled
1 parent 89d5059 commit 5fbe5da

File tree

9 files changed

+58
-6
lines changed

9 files changed

+58
-6
lines changed

compiler/rustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ check_only = ['rustc_driver_impl/check_only']
3131
jemalloc = ['dep:tikv-jemalloc-sys']
3232
llvm = ['rustc_driver_impl/llvm']
3333
llvm_enzyme = ['rustc_driver_impl/llvm_enzyme']
34+
llvm_offload = ['rustc_driver_impl/llvm_offload']
3435
max_level_info = ['rustc_driver_impl/max_level_info']
3536
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
3637
# tidy-alphabetical-end

compiler/rustc_codegen_llvm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ tracing = "0.1"
4747
# tidy-alphabetical-start
4848
check_only = ["rustc_llvm/check_only"]
4949
llvm_enzyme = []
50+
llvm_offload = []
5051
# tidy-alphabetical-end
5152

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,9 +1641,6 @@ unsafe extern "C" {
16411641
Name: *const c_char,
16421642
) -> &'a Value;
16431643

1644-
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1645-
pub(crate) fn LLVMRustBundleImages<'a>(M: &'a Module, TM: &'a TargetMachine) -> bool;
1646-
16471644
/// Writes a module to the specified path. Returns 0 on success.
16481645
pub(crate) fn LLVMWriteBitcodeToFile(M: &Module, Path: *const c_char) -> c_int;
16491646

@@ -1721,6 +1718,37 @@ unsafe extern "C" {
17211718
) -> &'a Value;
17221719
}
17231720

1721+
#[cfg(feature = "llvm_offload")]
1722+
pub(crate) use self::Offload::*;
1723+
1724+
#[cfg(feature = "llvm_offload")]
1725+
mod Offload {
1726+
use super::*;
1727+
unsafe extern "C" {
1728+
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1729+
pub(crate) fn LLVMRustBundleImages<'a>(M: &'a Module, TM: &'a TargetMachine) -> bool;
1730+
pub(crate) fn LLVMRustOffloadMapper<'a>(OldFn: &'a Value, NewFn: &'a Value);
1731+
}
1732+
}
1733+
1734+
#[cfg(not(feature = "llvm_offload"))]
1735+
pub(crate) use self::Offload_fallback::*;
1736+
1737+
#[cfg(not(feature = "llvm_offload"))]
1738+
mod Offload_fallback {
1739+
use super::*;
1740+
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1741+
/// Marked as unsafe to match the real offload wrapper which is unsafe due to FFI.
1742+
#[allow(unused_unsafe)]
1743+
pub(crate) unsafe fn LLVMRustBundleImages<'a>(_M: &'a Module, _TM: &'a TargetMachine) -> bool {
1744+
unimplemented!("This rustc version was not built with LLVM Offload support!");
1745+
}
1746+
#[allow(unused_unsafe)]
1747+
pub(crate) unsafe fn LLVMRustOffloadMapper<'a>(_OldFn: &'a Value, _NewFn: &'a Value) {
1748+
unimplemented!("This rustc version was not built with LLVM Offload support!");
1749+
}
1750+
}
1751+
17241752
// FFI bindings for `DIBuilder` functions in the LLVM-C API.
17251753
// Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
17261754
//
@@ -2028,7 +2056,6 @@ unsafe extern "C" {
20282056
) -> &Attribute;
20292057

20302058
// Operations on functions
2031-
pub(crate) fn LLVMRustOffloadMapper<'a>(Fn: &'a Value, Fn: &'a Value);
20322059
pub(crate) fn LLVMRustGetOrInsertFunction<'a>(
20332060
M: &'a Module,
20342061
Name: *const c_char,

compiler/rustc_driver_impl/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ ctrlc = "3.4.4"
7575
check_only = ['rustc_interface/check_only']
7676
llvm = ['rustc_interface/llvm']
7777
llvm_enzyme = ['rustc_interface/llvm_enzyme']
78+
llvm_offload = ['rustc_interface/llvm_offload']
7879
max_level_info = ['rustc_log/max_level_info']
7980
rustc_randomized_layouts = [
8081
'rustc_index/rustc_randomized_layouts',

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ rustc_abi = { path = "../rustc_abi" }
5959
check_only = ['rustc_codegen_llvm?/check_only']
6060
llvm = ['dep:rustc_codegen_llvm']
6161
llvm_enzyme = ['rustc_builtin_macros/llvm_enzyme', 'rustc_codegen_llvm/llvm_enzyme']
62+
llvm_offload = ['rustc_codegen_llvm/llvm_offload']
6263
# tidy-alphabetical-end

compiler/rustc_llvm/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ fn main() {
214214
cfg.define("ENZYME", None);
215215
}
216216

217+
if tracked_env_var_os("LLVM_OFFLOAD").is_some() {
218+
cfg.define("OFFLOAD", None);
219+
}
220+
217221
if tracked_env_var_os("LLVM_RUSTLLVM").is_some() {
218222
cfg.define("LLVM_RUSTLLVM", None);
219223
}

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "llvm/IR/Module.h"
2626
#include "llvm/IR/Value.h"
2727
#include "llvm/Object/COFFImportFile.h"
28-
#include "llvm/Object/OffloadBinary.h"
2928
#include "llvm/Remarks/RemarkFormat.h"
3029
#include "llvm/Remarks/RemarkSerializer.h"
3130
#include "llvm/Remarks/RemarkStreamer.h"
@@ -36,11 +35,18 @@
3635
#include "llvm/Support/Signals.h"
3736
#include "llvm/Support/Timer.h"
3837
#include "llvm/Support/ToolOutputFile.h"
39-
#include "llvm/Target/TargetMachine.h"
4038
#include "llvm/Transforms/Utils/Cloning.h"
4139
#include "llvm/Transforms/Utils/ValueMapper.h"
4240
#include <iostream>
4341

42+
// Some of the functions below rely on LLVM modules that may not always be
43+
// available. As such, we only try to build it in the first place, if
44+
// llvm.offload is enabled.
45+
#ifdef OFFLOAD
46+
#include "llvm/Object/OffloadBinary.h"
47+
#include "llvm/Target/TargetMachine.h"
48+
#endif
49+
4450
// for raw `write` in the bad-alloc handler
4551
#ifdef _MSC_VER
4652
#include <io.h>
@@ -146,6 +152,10 @@ extern "C" void LLVMRustPrintStatistics(RustStringRef OutBuf) {
146152
llvm::PrintStatistics(OS);
147153
}
148154

155+
// Some of the functions here rely on LLVM modules that may not always be
156+
// available. As such, we only try to build it in the first place, if
157+
// llvm.offload is enabled.
158+
#ifdef OFFLOAD
149159
static Error writeFile(StringRef Filename, StringRef Data) {
150160
Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
151161
FileOutputBuffer::create(Filename, Data.size());
@@ -210,6 +220,7 @@ extern "C" void LLVMRustOffloadMapper(LLVMValueRef OldFn, LLVMValueRef NewFn) {
210220
llvm::CloneFunctionChangeType::LocalChangesOnly,
211221
returns);
212222
}
223+
#endif
213224

214225
extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name,
215226
size_t NameLen) {

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,9 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
14361436
if builder.config.llvm_enzyme {
14371437
cargo.env("LLVM_ENZYME", "1");
14381438
}
1439+
if builder.config.llvm_offload {
1440+
cargo.env("LLVM_OFFLOAD", "1");
1441+
}
14391442
let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target });
14401443
cargo.env("LLVM_CONFIG", &host_llvm_config);
14411444

src/bootstrap/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,9 @@ impl Build {
873873
if self.config.llvm_enzyme {
874874
features.push("llvm_enzyme");
875875
}
876+
if self.config.llvm_offload {
877+
features.push("llvm_offload");
878+
}
876879
// keep in sync with `bootstrap/compile.rs:rustc_cargo_env`
877880
if self.config.rust_randomize_layout && check("rustc_randomized_layouts") {
878881
features.push("rustc_randomized_layouts");

0 commit comments

Comments
 (0)