Skip to content

Commit 4d9468e

Browse files
committed
temporarily remove enum support
1 parent b20c79e commit 4d9468e

File tree

7 files changed

+12
-115
lines changed

7 files changed

+12
-115
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+6-70
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
44
use std::sync::Arc;
55
use std::{fs, slice, str};
66

7-
use libc::{c_char, c_int, c_uint, c_void, size_t};
7+
use libc::{c_char, c_int, c_void, size_t};
88
use llvm::{
99
LLVMRustLLVMHasZlibCompressionForDebugSymbols, LLVMRustLLVMHasZstdCompressionForDebugSymbols,
1010
};
@@ -41,14 +41,8 @@ use crate::errors::{
4141
CopyBitcode, FromLlvmDiag, FromLlvmOptimizationDiag, LlvmError, UnknownCompression,
4242
WithLlvmError, WriteBytecode,
4343
};
44-
use crate::llvm::AttributePlace::Function;
4544
use crate::llvm::diagnostic::OptimizationDiagnosticKind::*;
46-
use crate::llvm::{
47-
self, AttributeKind, DiagnosticInfo, LLVMGetFirstFunction,
48-
LLVMGetNextFunction, LLVMGetStringAttributeAtIndex, LLVMIsEnumAttribute, LLVMIsStringAttribute,
49-
LLVMRemoveStringAttributeAtIndex, LLVMRustGetEnumAttributeAtIndex,
50-
LLVMRustRemoveEnumAttributeAtIndex, PassManager,
51-
};
45+
use crate::llvm::{self, DiagnosticInfo, PassManager};
5246
use crate::type_::Type;
5347
use crate::{LlvmCodegenBackend, ModuleLlvm, base, common, llvm_util};
5448

@@ -689,39 +683,8 @@ pub(crate) fn differentiate(
689683
crate::builder::generate_enzyme_call(llmod, llcx, fn_def, fn_target, item.attrs.clone());
690684
}
691685

692-
// We needed the SanitizeHWAddress attribute to prevent LLVM from optimizing enums in a way
693-
// which Enzyme doesn't understand.
694-
unsafe {
695-
let mut f = LLVMGetFirstFunction(llmod);
696-
loop {
697-
if let Some(lf) = f {
698-
f = LLVMGetNextFunction(lf);
699-
let myhwattr = "enzyme_hw";
700-
let attr = LLVMGetStringAttributeAtIndex(
701-
lf,
702-
c_uint::MAX,
703-
myhwattr.as_ptr() as *const c_char,
704-
myhwattr.as_bytes().len() as c_uint,
705-
);
706-
if LLVMIsStringAttribute(attr) {
707-
LLVMRemoveStringAttributeAtIndex(
708-
lf,
709-
c_uint::MAX,
710-
myhwattr.as_ptr() as *const c_char,
711-
myhwattr.as_bytes().len() as c_uint,
712-
);
713-
} else {
714-
LLVMRustRemoveEnumAttributeAtIndex(
715-
lf,
716-
c_uint::MAX,
717-
AttributeKind::SanitizeHWAddress,
718-
);
719-
}
720-
} else {
721-
break;
722-
}
723-
}
724-
}
686+
// FIXME(ZuseZ4): In the following upstream PR, we want to add code to handle SanitizeHWAddress,
687+
// to prevent some illegal/unsupported optimizations.
725688

726689
if let Some(opt_level) = config.opt_level {
727690
let opt_stage = match cgcx.lto {
@@ -773,35 +736,8 @@ pub(crate) unsafe fn optimize(
773736
unsafe { llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr()) };
774737
}
775738

776-
// This code enables Enzyme to differentiate code containing Rust enums.
777-
// By adding the SanitizeHWAddress attribute we prevent LLVM from Optimizing
778-
// away the enums and allows Enzyme to understand why a value can be of different types in
779-
// different code sections. We remove this attribute after Enzyme is done, to not affect the
780-
// rest of the compilation.
781-
//#[cfg(llvm_enzyme)]
782-
unsafe {
783-
let mut f = LLVMGetFirstFunction(llmod);
784-
loop {
785-
if let Some(lf) = f {
786-
f = LLVMGetNextFunction(lf);
787-
let myhwattr = "enzyme_hw";
788-
let prevattr = LLVMRustGetEnumAttributeAtIndex(
789-
lf,
790-
c_uint::MAX,
791-
AttributeKind::SanitizeHWAddress,
792-
);
793-
if LLVMIsEnumAttribute(prevattr) {
794-
let attr = llvm::CreateAttrString(llcx, myhwattr);
795-
crate::attributes::apply_to_llfn(lf, Function, &[attr]);
796-
} else {
797-
let attr = AttributeKind::SanitizeHWAddress.create_attr(llcx);
798-
crate::attributes::apply_to_llfn(lf, Function, &[attr]);
799-
}
800-
} else {
801-
break;
802-
}
803-
}
804-
}
739+
// FIXME(ZuseZ4): In the following PR, we have to add code to apply the sanitize_hwaddress
740+
// attribute to all functions in the module, to prevent some illegal/unsupported optimizations.
805741

806742
if let Some(opt_level) = config.opt_level {
807743
let opt_stage = match cgcx.lto {

compiler/rustc_codegen_llvm/src/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ pub(crate) fn generate_enzyme_call<'ll>(
259259
}
260260
// Now that we copied the metadata, get rid of dummy code.
261261
llvm::LLVMRustEraseInstBefore(entry, last_inst);
262+
llvm::LLVMRustEraseInstFromParent(last_inst);
262263

263264
let void_ty = llvm::LLVMVoidTypeInContext(llcx);
264265
if llvm::LLVMTypeOf(call) != void_ty {

compiler/rustc_codegen_llvm/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ impl CodegenBackend for LlvmCodegenBackend {
400400
}
401401
}
402402

403-
#[allow(dead_code)]
404403
pub struct ModuleLlvm {
405404
llcx: &'static mut llvm::Context,
406405
llmod_raw: *const llvm::Module,

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
use libc::{c_char, c_uint, size_t};
44

5-
use super::ffi::*;
6-
5+
use super::ffi::{Attribute, BasicBlock, Builder, Metadata, Module, Type, Value};
76
extern "C" {
87
// Enzyme
98
pub fn LLVMRustHasMetadata(I: &Value, KindID: c_uint) -> bool;
@@ -12,13 +11,7 @@ extern "C" {
1211
pub fn LLVMRustDIGetInstMetadata(I: &Value) -> &Metadata;
1312
pub fn LLVMRustEraseInstFromParent(V: &Value);
1413
pub fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value;
15-
pub fn LLVMRustRemoveEnumAttributeAtIndex(V: &Value, index: c_uint, attr: AttributeKind);
16-
pub fn LLVMRustGetEnumAttributeAtIndex(
17-
V: &Value,
18-
index: c_uint,
19-
attr: AttributeKind,
20-
) -> &Attribute;
21-
pub fn LLVMRustAddParamAttr<'a>(Instr: &'a Value, index: c_uint, Attr: &'a Attribute);
14+
pub fn LLVMRustGetFunctionType(fnc: &Value) -> &Type;
2215

2316
pub fn LLVMGetReturnType(T: &Type) -> &Type;
2417
pub fn LLVMDumpModule(M: &Module);
@@ -33,21 +26,9 @@ extern "C" {
3326
num_args: size_t,
3427
name: *const c_char,
3528
) -> &'a Value;
36-
pub fn LLVMGetFirstFunction(M: &Module) -> Option<&Value>;
37-
pub fn LLVMGetNextFunction(V: &Value) -> Option<&Value>;
3829
pub fn LLVMGetNamedFunction(M: &Module, Name: *const c_char) -> Option<&Value>;
39-
pub fn LLVMRustGetFunctionType(fnc: &Value) -> &Type;
40-
41-
pub fn LLVMRemoveStringAttributeAtIndex(F: &Value, Idx: c_uint, K: *const c_char, KLen: c_uint);
42-
pub fn LLVMGetStringAttributeAtIndex(
43-
F: &Value,
44-
Idx: c_uint,
45-
K: *const c_char,
46-
KLen: c_uint,
47-
) -> &Attribute;
4830
pub fn LLVMIsEnumAttribute(A: &Attribute) -> bool;
4931
pub fn LLVMIsStringAttribute(A: &Attribute) -> bool;
50-
5132
}
5233

5334
#[repr(C)]

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1661,12 +1661,6 @@ unsafe extern "C" {
16611661
Attrs: *const &'a Attribute,
16621662
AttrsLen: size_t,
16631663
);
1664-
pub fn LLVMRustRemoveFunctionAttributes<'a>(
1665-
Fn: &'a Value,
1666-
index: c_uint,
1667-
Attrs: *const &'a Attribute,
1668-
AttrsLen: size_t,
1669-
);
16701664

16711665
// Operations on call sites
16721666
pub fn LLVMRustAddCallSiteAttributes<'a>(

compiler/rustc_codegen_llvm/src/llvm/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod enzyme_ffi;
2626
mod ffi;
2727

2828
pub use self::enzyme_ffi::*;
29-
pub use self::ffi::*;
3029

3130
impl LLVMRustResult {
3231
pub fn into_result(self) -> Result<(), ()> {
@@ -42,11 +41,6 @@ pub fn AddFunctionAttributes<'ll>(llfn: &'ll Value, idx: AttributePlace, attrs:
4241
LLVMRustAddFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
4342
}
4443
}
45-
//pub fn RemoveFunctionAttributes<'ll>(llfn: &'ll Value, idx: AttributePlace, attrs: &[&'ll Attribute]) {
46-
// unsafe {
47-
// LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
48-
// }
49-
//}
5044

5145
pub fn AddCallSiteAttributes<'ll>(
5246
callsite: &'ll Value,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,11 @@ extern "C" LLVMTypeRef LLVMRustGetFunctionType(LLVMValueRef Fn) {
404404
return wrap(Ftype);
405405
}
406406

407-
extern "C" void LLVMRustRemoveEnumAttributeAtIndex(LLVMValueRef F, size_t index,
408-
LLVMRustAttribute RustAttr) {
409-
LLVMRemoveEnumAttributeAtIndex(F, index, fromRust(RustAttr));
410-
}
411-
412407
extern "C" LLVMAttributeRef
413408
LLVMRustCreateAttrNoValue(LLVMContextRef C, LLVMRustAttributeKind RustAttr) {
414409
return wrap(Attribute::get(*unwrap(C), fromRust(RustAttr)));
415410
}
416411

417-
extern "C" LLVMAttributeRef
418-
LLVMRustGetEnumAttributeAtIndex(LLVMValueRef F, size_t index,
419-
LLVMRustAttribute RustAttr) {
420-
return LLVMGetEnumAttributeAtIndex(F, index, fromRust(RustAttr));
421-
}
422-
423412
extern "C" LLVMAttributeRef LLVMRustCreateAlignmentAttr(LLVMContextRef C,
424413
uint64_t Bytes) {
425414
return wrap(Attribute::getWithAlignment(*unwrap(C), llvm::Align(Bytes)));
@@ -994,7 +983,10 @@ extern "C" void LLVMRustEraseInstBefore(LLVMBasicBlockRef bb, LLVMValueRef I) {
994983
auto It = BB.begin();
995984
while (&*It != &Inst)
996985
++It;
986+
// Make sure we found the Instruction.
997987
assert(It != BB.end());
988+
// We don't want to erase the instruction itself.
989+
It--;
998990
// Delete in rev order to ensure no dangling references.
999991
while (It != BB.begin()) {
1000992
auto Prev = std::prev(It);

0 commit comments

Comments
 (0)