Skip to content

Commit b60aa32

Browse files
Emit llvm.{memcpy,memset}.inline when no-builtins attribute is enabled
1 parent 3b022d8 commit b60aa32

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,9 +1565,17 @@ extern "C" LLVMValueRef LLVMRustBuildMemCpy(LLVMBuilderRef B, LLVMValueRef Dst,
15651565
unsigned SrcAlign,
15661566
LLVMValueRef Size,
15671567
bool IsVolatile) {
1568-
return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
1569-
unwrap(Src), MaybeAlign(SrcAlign),
1570-
unwrap(Size), IsVolatile));
1568+
if (unwrap(B)->GetInsertBlock()->getParent()->hasFnAttribute("no-builtins") ||
1569+
unwrap(B)->GetInsertBlock()->getParent()->hasFnAttribute(
1570+
"no-builtin-memcpy")) {
1571+
return wrap(unwrap(B)->CreateMemCpyInline(unwrap(Dst), MaybeAlign(DstAlign),
1572+
unwrap(Src), MaybeAlign(SrcAlign),
1573+
unwrap(Size), IsVolatile));
1574+
} else {
1575+
return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
1576+
unwrap(Src), MaybeAlign(SrcAlign),
1577+
unwrap(Size), IsVolatile));
1578+
}
15711579
}
15721580

15731581
extern "C" LLVMValueRef
@@ -1583,8 +1591,16 @@ extern "C" LLVMValueRef LLVMRustBuildMemSet(LLVMBuilderRef B, LLVMValueRef Dst,
15831591
unsigned DstAlign, LLVMValueRef Val,
15841592
LLVMValueRef Size,
15851593
bool IsVolatile) {
1586-
return wrap(unwrap(B)->CreateMemSet(unwrap(Dst), unwrap(Val), unwrap(Size),
1587-
MaybeAlign(DstAlign), IsVolatile));
1594+
if (unwrap(B)->GetInsertBlock()->getParent()->hasFnAttribute("no-builtins") ||
1595+
unwrap(B)->GetInsertBlock()->getParent()->hasFnAttribute(
1596+
"no-builtin-memset")) {
1597+
return wrap(unwrap(B)->CreateMemSetInline(unwrap(Dst), MaybeAlign(DstAlign),
1598+
unwrap(Val), unwrap(Size),
1599+
IsVolatile));
1600+
} else {
1601+
return wrap(unwrap(B)->CreateMemSet(unwrap(Dst), unwrap(Val), unwrap(Size),
1602+
MaybeAlign(DstAlign), IsVolatile));
1603+
}
15881604
}
15891605

15901606
// Polyfill for `LLVMBuildCallBr`, which was added in LLVM 19.

tests/codegen/no_builtin-mem.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![crate_type = "lib"]
2+
#![no_builtins]
3+
4+
type T = [u8; 256];
5+
6+
#[no_mangle]
7+
pub fn f() -> [i32; 1000] {
8+
// CHECK: call void @llvm.memset.inline.{{.*}}(ptr nonnull align 4 %{{.*}}, i8 0, i64 4000, i1 false)
9+
[0; 1000]
10+
}
11+
12+
#[no_mangle]
13+
pub fn g(x: &[i32; 1000], y: &mut [i32; 1000]) {
14+
// CHECK: call void @llvm.memcpy.inline.{{.*}}(ptr nonnull align 4 %{{.*}}, ptr nonnull align 4 %{{.*}}, i64 4000, i1 false)
15+
*y = *x;
16+
}

0 commit comments

Comments
 (0)