Skip to content

Commit 7275734

Browse files
committed
[CUDA/NVPTX] Improve handling of memcpy for -Os compilations.
We had some instances when LLVM would not inline fixed-count memcpy and ended up attempting to lower it a a libcall, which would not work on NVPTX as there's no standard library to call. The patch relaxes the threshold used for -Os compilation so we're always allowed to inline memory copy functions. Differential Revision: https://reviews.llvm.org/D158226
1 parent d22883e commit 7275734

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// RUN: %clang_cc1 -x cuda -triple nvptx64-nvidia-cuda- -fcuda-is-device \
2+
// RUN: -O3 -S %s -o - | FileCheck -check-prefix=PTX %s
3+
// RUN: %clang_cc1 -x cuda -triple nvptx64-nvidia-cuda- -fcuda-is-device \
4+
// RUN: -Os -S %s -o - | FileCheck -check-prefix=PTX %s
5+
#include "Inputs/cuda.h"
6+
7+
// PTX-LABEL: .func _Z12copy_genericPvPKv(
8+
void __device__ copy_generic(void *dest, const void *src) {
9+
__builtin_memcpy(dest, src, 32);
10+
// PTX: ld.u8
11+
// PTX: st.u8
12+
}
13+
14+
// PTX-LABEL: .entry _Z11copy_globalPvS_(
15+
void __global__ copy_global(void *dest, void * src) {
16+
__builtin_memcpy(dest, src, 32);
17+
// PTX: ld.global.u8
18+
// PTX: st.global.u8
19+
}
20+
21+
struct S {
22+
int data[8];
23+
};
24+
25+
// PTX-LABEL: .entry _Z20copy_param_to_globalP1SS_(
26+
void __global__ copy_param_to_global(S *global, S param) {
27+
__builtin_memcpy(global, &param, sizeof(S));
28+
// PTX: ld.param.u32
29+
// PTX: st.global.u32
30+
}
31+
32+
// PTX-LABEL: .entry _Z19copy_param_to_localPU3AS51SS_(
33+
void __global__ copy_param_to_local(__attribute__((address_space(5))) S *local,
34+
S param) {
35+
__builtin_memcpy(local, &param, sizeof(S));
36+
// PTX: ld.param.u32
37+
// PTX: st.local.u32
38+
}
39+
40+
// PTX-LABEL: .func _Z21copy_local_to_genericP1SPU3AS5S_(
41+
void __device__ copy_local_to_generic(S *generic,
42+
__attribute__((address_space(5))) S *src) {
43+
__builtin_memcpy(generic, src, sizeof(S));
44+
// PTX: ld.local.u32
45+
// PTX: st.u32
46+
}
47+
48+
__shared__ S shared;
49+
50+
// PTX-LABEL: .entry _Z20copy_param_to_shared1S(
51+
void __global__ copy_param_to_shared( S param) {
52+
__builtin_memcpy(&shared, &param, sizeof(S));
53+
// PTX: ld.param.u32
54+
// PTX: st.shared.u32
55+
}
56+
57+
void __device__ copy_shared_to_generic(S *generic) {
58+
__builtin_memcpy(generic, &shared, sizeof(S));
59+
// PTX: ld.shared.u32
60+
// PTX: st.u32
61+
}

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,9 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
386386
// always lower memset, memcpy, and memmove intrinsics to load/store
387387
// instructions, rather
388388
// then generating calls to memset, mempcy or memmove.
389-
MaxStoresPerMemset = (unsigned) 0xFFFFFFFF;
390-
MaxStoresPerMemcpy = (unsigned) 0xFFFFFFFF;
391-
MaxStoresPerMemmove = (unsigned) 0xFFFFFFFF;
389+
MaxStoresPerMemset = MaxStoresPerMemsetOptSize = (unsigned)0xFFFFFFFF;
390+
MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = (unsigned) 0xFFFFFFFF;
391+
MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize = (unsigned) 0xFFFFFFFF;
392392

393393
setBooleanContents(ZeroOrNegativeOneBooleanContent);
394394
setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);

0 commit comments

Comments
 (0)