Skip to content

Commit 62b442c

Browse files
committed
adressing feedback
1 parent 689933b commit 62b442c

File tree

6 files changed

+25
-42
lines changed

6 files changed

+25
-42
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use rustc_span::{BytePos, InnerSpan, Pos, SpanData, SyntaxContext, sym};
2929
use rustc_target::spec::{CodeModel, RelocModel, SanitizerSet, SplitDebuginfo, TlsModel};
3030
use tracing::{debug, trace};
3131

32-
//use crate::back::autodiff::*;
3332
use crate::back::lto::ThinBuffer;
3433
use crate::back::owned_target_machine::OwnedTargetMachine;
3534
use crate::back::profiling::{

compiler/rustc_codegen_llvm/src/builder.rs

-20
Original file line numberDiff line numberDiff line change
@@ -1492,26 +1492,6 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
14921492
}
14931493
}
14941494

1495-
pub(crate) fn call2(
1496-
&mut self,
1497-
llty: &'ll Type,
1498-
llfn: &'ll Value,
1499-
args: &[&'ll Value],
1500-
fn_name: &core::ffi::CStr,
1501-
) -> &'ll Value {
1502-
let call = unsafe {
1503-
llvm::LLVMBuildCall2(
1504-
self.llbuilder,
1505-
llty,
1506-
llfn,
1507-
args.as_ptr(),
1508-
args.len().try_into().unwrap(),
1509-
fn_name.as_ptr(),
1510-
)
1511-
};
1512-
call
1513-
}
1514-
15151495
pub(crate) fn callbr(
15161496
&mut self,
15171497
llty: &'ll Type,

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::ffi::CString;
21
use std::ptr;
32

43
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, AutoDiffItem, DiffActivity, DiffMode};
54
use rustc_codegen_ssa::ModuleCodegen;
65
use rustc_codegen_ssa::back::write::ModuleConfig;
7-
use rustc_codegen_ssa::traits::{
8-
BaseTypeCodegenMethods, BuilderMethods, TypeMembershipCodegenMethods,
9-
};
6+
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods};
107
use rustc_errors::FatalError;
118
use rustc_middle::ty::TyCtxt;
129
use rustc_session::config::Lto;
@@ -141,11 +138,11 @@ fn generate_enzyme_call<'ll, 'tcx>(
141138
let mut args = Vec::with_capacity(num_args as usize + 1);
142139
args.push(fn_to_diff);
143140

144-
let enzyme_const = cx.typeid_metadata("enzyme_const".to_string()).unwrap();
145-
let enzyme_out = cx.typeid_metadata("enzyme_out".to_string()).unwrap();
146-
let enzyme_dup = cx.typeid_metadata("enzyme_dup".to_string()).unwrap();
147-
let enzyme_dupnoneed = cx.typeid_metadata("enzyme_dupnoneed".to_string()).unwrap();
148-
let enzyme_primal_ret = cx.typeid_metadata("enzyme_primal_return".to_string()).unwrap();
141+
let enzyme_const = cx.create_metadata("enzyme_const".to_string()).unwrap();
142+
let enzyme_out = cx.create_metadata("enzyme_out".to_string()).unwrap();
143+
let enzyme_dup = cx.create_metadata("enzyme_dup".to_string()).unwrap();
144+
let enzyme_dupnoneed = cx.create_metadata("enzyme_dupnoneed".to_string()).unwrap();
145+
let enzyme_primal_ret = cx.create_metadata("enzyme_primal_return".to_string()).unwrap();
149146

150147
match output {
151148
DiffActivity::Dual => {
@@ -239,8 +236,7 @@ fn generate_enzyme_call<'ll, 'tcx>(
239236
}
240237
}
241238

242-
let c_ad_name = CString::new(ad_name.clone()).unwrap();
243-
let call = builder.call2(enzyme_ty, ad_fn, &args, &c_ad_name);
239+
let call = builder.call(enzyme_ty, None, None, ad_fn, &args, None, None);
244240

245241
// This part is a bit iffy. LLVM requires that a call to an inlineable function has some
246242
// metadata attachted to it, but we just created this code oota. Given that the
@@ -250,8 +246,9 @@ fn generate_enzyme_call<'ll, 'tcx>(
250246
// FIXME(ZuseZ4): Work with Enzyme core devs to clarify what debug metadata issues we have,
251247
// and how to best improve it for enzyme core and rust-enzyme.
252248
let md_ty = cx.get_md_kind_id("dbg");
253-
if llvm::LLVMRustHasMetadata(last_inst, md_ty) {
254-
let md = llvm::LLVMRustDIGetInstMetadata(last_inst);
249+
if llvm::LLVMRustHasMetadata(last_inst, md_ty) == True {
250+
let md = llvm::LLVMRustDIGetInstMetadata(last_inst)
251+
.expect("failed to get instruction metadata");
255252
let md_todiff = cx.get_metadata_value(md);
256253
llvm::LLVMSetMetadata(call, md_ty, md_todiff);
257254
} else {

compiler/rustc_codegen_llvm/src/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,12 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
619619
)
620620
}
621621
}
622+
623+
pub(crate) fn create_metadata(&self, name: String) -> Option<&'ll Metadata> {
624+
Some(unsafe {
625+
llvm::LLVMMDStringInContext2(self.llcx, name.as_ptr() as *const c_char, name.len())
626+
})
627+
}
622628
}
623629

624630
impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {

compiler/rustc_codegen_llvm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use std::mem::ManuallyDrop;
2828

2929
use back::owned_target_machine::OwnedTargetMachine;
3030
use back::write::{create_informational_target_machine, create_target_machine};
31-
pub use llvm_util::target_features_cfg;
3231
use errors::{AutoDiffWithoutLTO, ParseTargetMachineConfig};
32+
pub use llvm_util::target_features_cfg;
3333
use rustc_ast::expand::allocator::AllocatorKind;
3434
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
3535
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
#![allow(non_camel_case_types)]
22

3-
use libc::{c_char, c_uint, size_t};
3+
use libc::{c_char, c_uint};
44

5+
use super::Bool;
56
use super::ffi::{Attribute, BasicBlock, Builder, Metadata, Module, Type, Value};
67
extern "C" {
78
// Enzyme
8-
pub fn LLVMRustHasMetadata(I: &Value, KindID: c_uint) -> bool;
9+
pub fn LLVMRustHasMetadata(I: &Value, KindID: c_uint) -> Bool;
910
pub fn LLVMRustEraseInstBefore(BB: &BasicBlock, I: &Value);
1011
pub fn LLVMRustGetLastInstruction<'a>(BB: &BasicBlock) -> Option<&'a Value>;
11-
pub fn LLVMRustDIGetInstMetadata(I: &Value) -> &Metadata;
12+
pub fn LLVMRustDIGetInstMetadata(I: &Value) -> Option<&Metadata>;
1213
pub fn LLVMRustEraseInstFromParent(V: &Value);
1314
pub fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value;
1415
pub fn LLVMGetFunctionCallConv(F: &Value) -> c_uint;
1516

1617
pub fn LLVMGetReturnType(T: &Type) -> &Type;
1718
pub fn LLVMDumpModule(M: &Module);
1819
pub fn LLVMCountStructElementTypes(T: &Type) -> c_uint;
19-
pub fn LLVMVerifyFunction(V: &Value, action: LLVMVerifierFailureAction) -> bool;
20+
pub fn LLVMVerifyFunction(V: &Value, action: LLVMVerifierFailureAction) -> Bool;
2021
pub fn LLVMGetParams(Fnc: &Value, parms: *mut &Value);
2122
pub fn LLVMBuildCall2<'a>(
2223
arg1: &Builder<'a>,
2324
ty: &Type,
2425
func: &Value,
2526
args: *const &Value,
26-
num_args: size_t,
27+
num_args: usize,
2728
name: *const c_char,
2829
) -> &'a Value;
2930
pub fn LLVMGetNamedFunction(M: &Module, Name: *const c_char) -> Option<&Value>;
30-
pub fn LLVMIsEnumAttribute(A: &Attribute) -> bool;
31-
pub fn LLVMIsStringAttribute(A: &Attribute) -> bool;
31+
pub fn LLVMIsEnumAttribute(A: &Attribute) -> Bool;
32+
pub fn LLVMIsStringAttribute(A: &Attribute) -> Bool;
3233
}
3334

3435
#[repr(C)]

0 commit comments

Comments
 (0)