Skip to content

Commit f653d3a

Browse files
committed
Add elementtype attributes for llvm.arm.ldrex/strex intrinsics
These intrinsics (and a few more, but there are the only ones exposed by stdarch) require an elementtype attribute in LLVM 15.
1 parent ff693dc commit f653d3a

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,22 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
569569
&[cmse_nonsecure_call],
570570
);
571571
}
572+
573+
// Some intrinsics require that an elementtype attribute (with the pointee type of a
574+
// pointer argument) is added to the callsite.
575+
let element_type_index = unsafe { llvm::LLVMRustGetElementTypeArgIndex(callsite) };
576+
if element_type_index >= 0 {
577+
let arg_ty = self.args[element_type_index as usize].layout.ty;
578+
let pointee_ty = arg_ty.builtin_deref(true).expect("Must be pointer argument").ty;
579+
let element_type_attr = unsafe {
580+
llvm::LLVMRustCreateElementTypeAttr(bx.llcx, bx.layout_of(pointee_ty).llvm_type(bx))
581+
};
582+
attributes::apply_to_callsite(
583+
callsite,
584+
llvm::AttributePlace::Argument(element_type_index as u32),
585+
&[element_type_attr],
586+
);
587+
}
572588
}
573589
}
574590

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ extern "C" {
11921192
pub fn LLVMRustCreateDereferenceableOrNullAttr(C: &Context, bytes: u64) -> &Attribute;
11931193
pub fn LLVMRustCreateByValAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
11941194
pub fn LLVMRustCreateStructRetAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
1195+
pub fn LLVMRustCreateElementTypeAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
11951196
pub fn LLVMRustCreateUWTableAttr(C: &Context, async_: bool) -> &Attribute;
11961197
pub fn LLVMRustCreateAllocSizeAttr(C: &Context, size_arg: u32) -> &Attribute;
11971198
pub fn LLVMRustCreateAllocKindAttr(C: &Context, size_arg: u64) -> &Attribute;
@@ -2541,4 +2542,6 @@ extern "C" {
25412542

25422543
#[allow(improper_ctypes)]
25432544
pub fn LLVMRustGetMangledName(V: &Value, out: &RustString);
2545+
2546+
pub fn LLVMRustGetElementTypeArgIndex(CallSite: &Value) -> i32;
25442547
}

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "llvm/IR/GlobalVariable.h"
77
#include "llvm/IR/Instructions.h"
88
#include "llvm/IR/Intrinsics.h"
9+
#include "llvm/IR/IntrinsicsARM.h"
910
#include "llvm/IR/Mangler.h"
1011
#include "llvm/Object/Archive.h"
1112
#include "llvm/Object/COFFImportFile.h"
@@ -300,6 +301,14 @@ extern "C" LLVMAttributeRef LLVMRustCreateStructRetAttr(LLVMContextRef C, LLVMTy
300301
return wrap(Attribute::getWithStructRetType(*unwrap(C), unwrap(Ty)));
301302
}
302303

304+
extern "C" LLVMAttributeRef LLVMRustCreateElementTypeAttr(LLVMContextRef C, LLVMTypeRef Ty) {
305+
#if LLVM_VERSION_GE(15, 0)
306+
return wrap(Attribute::get(*unwrap(C), Attribute::ElementType, unwrap(Ty)));
307+
#else
308+
report_fatal_error("Should not be needed on LLVM < 15");
309+
#endif
310+
}
311+
303312
extern "C" LLVMAttributeRef LLVMRustCreateUWTableAttr(LLVMContextRef C, bool Async) {
304313
#if LLVM_VERSION_LT(15, 0)
305314
return wrap(Attribute::get(*unwrap(C), Attribute::UWTable));
@@ -1943,3 +1952,16 @@ extern "C" LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
19431952
return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
19441953
}
19451954
#endif
1955+
1956+
extern "C" int32_t LLVMRustGetElementTypeArgIndex(LLVMValueRef CallSite) {
1957+
#if LLVM_VERSION_GE(15, 0)
1958+
auto *CB = unwrap<CallBase>(CallSite);
1959+
switch (CB->getIntrinsicID()) {
1960+
case Intrinsic::arm_ldrex:
1961+
return 0;
1962+
case Intrinsic::arm_strex:
1963+
return 1;
1964+
}
1965+
#endif
1966+
return -1;
1967+
}

0 commit comments

Comments
 (0)