Skip to content

Commit b89e3d5

Browse files
committed
Use LLVMWriteBitcodeToFile directly instead of ModuleBuffer
This writes bitcode using LLVM 7's native function which should ensure the bitcode format is compatible with LLVM 7's parser. ModuleBuffer uses Rust's LLVM which might include newer bitcode features that LLVM 7 can't parse.
1 parent d2f1795 commit b89e3d5

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

crates/rustc_codegen_nvvm/src/back.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_codegen_ssa::{
1010
back::write::{CodegenContext, ModuleConfig},
1111
base::maybe_create_entry_wrapper,
1212
mono_item::MonoItemExt,
13-
traits::{BaseTypeCodegenMethods, ThinBufferMethods, ModuleBufferMethods},
13+
traits::BaseTypeCodegenMethods,
1414
};
1515
use rustc_errors::{DiagCtxtHandle, FatalError};
1616
use rustc_fs_util::path_to_c_string;
@@ -25,7 +25,7 @@ use rustc_target::spec::{CodeModel, RelocModel};
2525
use crate::common::AsCCharPtr;
2626
use crate::llvm::{self};
2727
use crate::override_fns::define_or_override_fn;
28-
use crate::{LlvmMod, NvvmCodegenBackend, builder::Builder, context::CodegenCx, lto::{ThinBuffer, ModuleBuffer}};
28+
use crate::{LlvmMod, NvvmCodegenBackend, builder::Builder, context::CodegenCx};
2929

3030
pub fn llvm_err(handle: DiagCtxtHandle, msg: &str) -> FatalError {
3131
match llvm::last_error() {
@@ -208,17 +208,16 @@ pub(crate) unsafe fn codegen(
208208
.prof
209209
.generic_activity_with_arg("NVVM_module_codegen_make_bitcode", &module.name[..]);
210210

211-
// Use ModuleBuffer instead of ThinBuffer to avoid ThinLTO bitcode format incompatibility
212-
let buffer = ModuleBuffer::new(llmod);
213-
214-
let data = buffer.data();
215-
216211
let _bc_emit_timer = cgcx
217212
.prof
218213
.generic_activity_with_arg("NVVM_module_codegen_emit_bitcode", &module.name[..]);
219214

220-
if let Err(e) = std::fs::write(&out, data) {
221-
let msg = format!("failed to write bytecode to {}: {}", out.display(), e);
215+
// Write bitcode directly using LLVM 7's function to ensure compatibility
216+
let out_c = path_to_c_string(&out);
217+
let result = unsafe { llvm::LLVMWriteBitcodeToFile(llmod, out_c.as_ptr()) };
218+
219+
if result != 0 {
220+
let msg = format!("failed to write bytecode to {}", out.display());
222221
dcx.err(msg);
223222
}
224223

crates/rustc_codegen_nvvm/src/builder.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,32 +1192,31 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
11921192
// In LLVM 7, we need to ensure the pointer type matches what we're storing
11931193
let val_ty = self.cx.val_ty(val);
11941194
let ptr_ty = self.cx.val_ty(ptr);
1195-
1195+
11961196
// Check if this is a pointer type
11971197
if self.cx.type_kind(ptr_ty) != TypeKind::Pointer {
11981198
return ptr;
11991199
}
1200-
1200+
12011201
// Get what the pointer points to
12021202
let pointee_ty = self.cx.element_type(ptr_ty);
1203-
1203+
12041204
// If types match, no conversion needed
12051205
if pointee_ty == val_ty {
12061206
return ptr;
12071207
}
1208-
1208+
12091209
// We need to bitcast the pointer to the correct type
12101210
let needed_ptr_ty = self.cx.type_ptr_to(val_ty);
12111211
trace!(
12121212
"check_store: bitcasting pointer from {:?} (pointing to {:?}) to {:?} (pointing to {:?})",
12131213
ptr_ty, pointee_ty, needed_ptr_ty, val_ty
12141214
);
1215-
1215+
12161216
// Use direct LLVM bitcast to avoid address space complications
12171217
unsafe { llvm::LLVMBuildBitCast(self.llbuilder, ptr, needed_ptr_ty, unnamed()) }
12181218
}
12191219

1220-
12211220
fn check_call<'b>(
12221221
&mut self,
12231222
typ: &str,

crates/rustc_codegen_nvvm/src/nvvm.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,19 @@ unsafe fn cleanup_dicompileunit(module: &Module) {
160160
fn merge_llvm_modules(modules: Vec<Vec<u8>>, llcx: &Context) -> &Module {
161161
let module = unsafe { crate::create_module(llcx, "merged_modules") };
162162
for (i, merged_module) in modules.iter().enumerate() {
163-
debug!("Merging object file #{} (size: {} bytes)", i, merged_module.len());
164-
163+
debug!(
164+
"Merging object file #{} (size: {} bytes)",
165+
i,
166+
merged_module.len()
167+
);
168+
165169
// Debug: dump bitcode to file for inspection
166170
if std::env::var("NVVM_DUMP_BITCODE").is_ok() {
167171
let filename = format!("/tmp/nvvm_module_{}.bc", i);
168172
std::fs::write(&filename, merged_module).unwrap();
169173
eprintln!("Dumped bitcode to {}", filename);
170174
}
171-
175+
172176
unsafe {
173177
let tmp = match LLVMRustParseBitcodeForLTO(
174178
llcx,

0 commit comments

Comments
 (0)