Skip to content

Commit

Permalink
libclc: Make all built-ins overloadable
Browse files Browse the repository at this point in the history
The SPIR spec states that all OpenCL built-in functions should be
overloadable and mangled, to ensure consistency.

Add the overload attribute to functions which were missing them:
work dimensions, memory barriers and fences, and events.

Reviewed By: tstellar, jenatali

Differential Revision: https://reviews.llvm.org/D82078
  • Loading branch information
fooishbar authored and tstellar committed Aug 17, 2020
1 parent 3a7051d commit 3d21fa5
Show file tree
Hide file tree
Showing 41 changed files with 191 additions and 173 deletions.
11 changes: 5 additions & 6 deletions libclc/amdgcn-amdhsa/lib/workitem/get_global_size.cl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
CONST_AS uchar * __clc_amdgcn_dispatch_ptr(void) __asm("llvm.amdgcn.dispatch.ptr");
#endif

_CLC_DEF size_t get_global_size(uint dim)
{
CONST_AS uint * ptr = (CONST_AS uint *) __dispatch_ptr();
if (dim < 3)
return ptr[3 + dim];
return 1;
_CLC_DEF _CLC_OVERLOAD size_t get_global_size(uint dim) {
CONST_AS uint *ptr = (CONST_AS uint *)__dispatch_ptr();
if (dim < 3)
return ptr[3 + dim];
return 1;
}
23 changes: 11 additions & 12 deletions libclc/amdgcn-amdhsa/lib/workitem/get_local_size.cl
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
CONST_AS char * __clc_amdgcn_dispatch_ptr(void) __asm("llvm.amdgcn.dispatch.ptr");
#endif

_CLC_DEF size_t get_local_size(uint dim)
{
CONST_AS uint * ptr = (CONST_AS uint *) __dispatch_ptr();
switch (dim) {
case 0:
return ptr[1] & 0xffffu;
case 1:
return ptr[1] >> 16;
case 2:
return ptr[2] & 0xffffu;
}
return 1;
_CLC_DEF _CLC_OVERLOAD size_t get_local_size(uint dim) {
CONST_AS uint *ptr = (CONST_AS uint *)__dispatch_ptr();
switch (dim) {
case 0:
return ptr[1] & 0xffffu;
case 1:
return ptr[1] >> 16;
case 2:
return ptr[2] & 0xffffu;
}
return 1;
}
2 changes: 1 addition & 1 deletion libclc/amdgcn-amdhsa/lib/workitem/get_num_groups.cl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include <clc/clc.h>

_CLC_DEF size_t get_num_groups(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_num_groups(uint dim) {
size_t global_size = get_global_size(dim);
size_t local_size = get_local_size(dim);
size_t num_groups = global_size / local_size;
Expand Down
25 changes: 11 additions & 14 deletions libclc/amdgcn/lib/mem_fence/fence.cl
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@ void __clc_amdgcn_s_waitcnt(unsigned flags);
_CLC_DEF void __clc_amdgcn_s_waitcnt(unsigned) __asm("llvm.amdgcn.s.waitcnt");
#endif

_CLC_DEF void mem_fence(cl_mem_fence_flags flags)
{
if (flags & CLK_GLOBAL_MEM_FENCE) {
// scalar loads are counted with LGKM but we don't know whether
// the compiler turned any loads to scalar
__waitcnt(0);
} else if (flags & CLK_LOCAL_MEM_FENCE)
__waitcnt(0xff); // LGKM is [12:8]
_CLC_DEF _CLC_OVERLOAD void mem_fence(cl_mem_fence_flags flags) {
if (flags & CLK_GLOBAL_MEM_FENCE) {
// scalar loads are counted with LGKM but we don't know whether
// the compiler turned any loads to scalar
__waitcnt(0);
} else if (flags & CLK_LOCAL_MEM_FENCE)
__waitcnt(0xff); // LGKM is [12:8]
}
#undef __waitcnt

// We don't have separate mechanism for read and write fences
_CLC_DEF void read_mem_fence(cl_mem_fence_flags flags)
{
mem_fence(flags);
_CLC_DEF _CLC_OVERLOAD void read_mem_fence(cl_mem_fence_flags flags) {
mem_fence(flags);
}

_CLC_DEF void write_mem_fence(cl_mem_fence_flags flags)
{
mem_fence(flags);
_CLC_DEF _CLC_OVERLOAD void write_mem_fence(cl_mem_fence_flags flags) {
mem_fence(flags);
}
7 changes: 3 additions & 4 deletions libclc/amdgcn/lib/synchronization/barrier.cl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <clc/clc.h>

_CLC_DEF void barrier(cl_mem_fence_flags flags)
{
mem_fence(flags);
__builtin_amdgcn_s_barrier();
_CLC_DEF _CLC_OVERLOAD void barrier(cl_mem_fence_flags flags) {
mem_fence(flags);
__builtin_amdgcn_s_barrier();
}
12 changes: 5 additions & 7 deletions libclc/amdgcn/lib/workitem/get_global_offset.cl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
#define CONST_AS __attribute__((address_space(2)))
#endif

_CLC_DEF size_t get_global_offset(uint dim)
{
CONST_AS uint * ptr =
(CONST_AS uint *) __builtin_amdgcn_implicitarg_ptr();
if (dim < 3)
return ptr[dim + 1];
return 0;
_CLC_DEF _CLC_OVERLOAD size_t get_global_offset(uint dim) {
CONST_AS uint *ptr = (CONST_AS uint *)__builtin_amdgcn_implicitarg_ptr();
if (dim < 3)
return ptr[dim + 1];
return 0;
}
19 changes: 11 additions & 8 deletions libclc/amdgcn/lib/workitem/get_global_size.cl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ uint __clc_amdgcn_get_global_size_x(void) __asm("llvm.r600.read.global.size.x");
uint __clc_amdgcn_get_global_size_y(void) __asm("llvm.r600.read.global.size.y");
uint __clc_amdgcn_get_global_size_z(void) __asm("llvm.r600.read.global.size.z");

_CLC_DEF size_t get_global_size(uint dim)
{
switch (dim) {
case 0: return __clc_amdgcn_get_global_size_x();
case 1: return __clc_amdgcn_get_global_size_y();
case 2: return __clc_amdgcn_get_global_size_z();
default: return 1;
}
_CLC_DEF _CLC_OVERLOAD size_t get_global_size(uint dim) {
switch (dim) {
case 0:
return __clc_amdgcn_get_global_size_x();
case 1:
return __clc_amdgcn_get_global_size_y();
case 2:
return __clc_amdgcn_get_global_size_z();
default:
return 1;
}
}
19 changes: 11 additions & 8 deletions libclc/amdgcn/lib/workitem/get_group_id.cl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <clc/clc.h>

_CLC_DEF size_t get_group_id(uint dim)
{
switch(dim) {
case 0: return __builtin_amdgcn_workgroup_id_x();
case 1: return __builtin_amdgcn_workgroup_id_y();
case 2: return __builtin_amdgcn_workgroup_id_z();
default: return 1;
}
_CLC_DEF _CLC_OVERLOAD size_t get_group_id(uint dim) {
switch (dim) {
case 0:
return __builtin_amdgcn_workgroup_id_x();
case 1:
return __builtin_amdgcn_workgroup_id_y();
case 2:
return __builtin_amdgcn_workgroup_id_z();
default:
return 1;
}
}
19 changes: 11 additions & 8 deletions libclc/amdgcn/lib/workitem/get_local_id.cl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <clc/clc.h>

_CLC_DEF size_t get_local_id(uint dim)
{
switch(dim) {
case 0: return __builtin_amdgcn_workitem_id_x();
case 1: return __builtin_amdgcn_workitem_id_y();
case 2: return __builtin_amdgcn_workitem_id_z();
default: return 1;
}
_CLC_DEF _CLC_OVERLOAD size_t get_local_id(uint dim) {
switch (dim) {
case 0:
return __builtin_amdgcn_workitem_id_x();
case 1:
return __builtin_amdgcn_workitem_id_y();
case 2:
return __builtin_amdgcn_workitem_id_z();
default:
return 1;
}
}
19 changes: 11 additions & 8 deletions libclc/amdgcn/lib/workitem/get_local_size.cl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ uint __clc_amdgcn_get_local_size_x(void) __asm("llvm.r600.read.local.size.x");
uint __clc_amdgcn_get_local_size_y(void) __asm("llvm.r600.read.local.size.y");
uint __clc_amdgcn_get_local_size_z(void) __asm("llvm.r600.read.local.size.z");

_CLC_DEF size_t get_local_size(uint dim)
{
switch (dim) {
case 0: return __clc_amdgcn_get_local_size_x();
case 1: return __clc_amdgcn_get_local_size_y();
case 2: return __clc_amdgcn_get_local_size_z();
default: return 1;
}
_CLC_DEF _CLC_OVERLOAD size_t get_local_size(uint dim) {
switch (dim) {
case 0:
return __clc_amdgcn_get_local_size_x();
case 1:
return __clc_amdgcn_get_local_size_y();
case 2:
return __clc_amdgcn_get_local_size_z();
default:
return 1;
}
}
19 changes: 11 additions & 8 deletions libclc/amdgcn/lib/workitem/get_num_groups.cl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ uint __clc_amdgcn_get_num_groups_x(void) __asm("llvm.r600.read.ngroups.x");
uint __clc_amdgcn_get_num_groups_y(void) __asm("llvm.r600.read.ngroups.y");
uint __clc_amdgcn_get_num_groups_z(void) __asm("llvm.r600.read.ngroups.z");

_CLC_DEF size_t get_num_groups(uint dim)
{
switch (dim) {
case 0: return __clc_amdgcn_get_num_groups_x();
case 1: return __clc_amdgcn_get_num_groups_y();
case 2: return __clc_amdgcn_get_num_groups_z();
default: return 1;
}
_CLC_DEF _CLC_OVERLOAD size_t get_num_groups(uint dim) {
switch (dim) {
case 0:
return __clc_amdgcn_get_num_groups_x();
case 1:
return __clc_amdgcn_get_num_groups_y();
case 2:
return __clc_amdgcn_get_num_groups_z();
default:
return 1;
}
}
8 changes: 3 additions & 5 deletions libclc/amdgcn/lib/workitem/get_work_dim.cl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#define CONST_AS __attribute__((address_space(2)))
#endif

_CLC_DEF uint get_work_dim(void)
{
CONST_AS uint * ptr =
(CONST_AS uint *) __builtin_amdgcn_implicitarg_ptr();
return ptr[0];
_CLC_DEF _CLC_OVERLOAD uint get_work_dim(void) {
CONST_AS uint *ptr = (CONST_AS uint *)__builtin_amdgcn_implicitarg_ptr();
return ptr[0];
}
3 changes: 2 additions & 1 deletion libclc/generic/include/clc/async/wait_group_events.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
void wait_group_events(int num_events, event_t *event_list);
_CLC_DECL _CLC_OVERLOAD void wait_group_events(int num_events,
event_t *event_list);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
_CLC_DECL void mem_fence(cl_mem_fence_flags flags);
_CLC_DECL void read_mem_fence(cl_mem_fence_flags flags);
_CLC_DECL void write_mem_fence(cl_mem_fence_flags flags);
_CLC_DECL _CLC_OVERLOAD void mem_fence(cl_mem_fence_flags flags);
_CLC_DECL _CLC_OVERLOAD void read_mem_fence(cl_mem_fence_flags flags);
_CLC_DECL _CLC_OVERLOAD void write_mem_fence(cl_mem_fence_flags flags);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/synchronization/barrier.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL void barrier(cl_mem_fence_flags flags);
_CLC_DECL _CLC_OVERLOAD void barrier(cl_mem_fence_flags flags);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_global_id.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL size_t get_global_id(uint dim);
_CLC_DECL _CLC_OVERLOAD size_t get_global_id(uint dim);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_global_offset.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL size_t get_global_offset(uint dim);
_CLC_DECL _CLC_OVERLOAD size_t get_global_offset(uint dim);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_global_size.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL size_t get_global_size(uint dim);
_CLC_DECL _CLC_OVERLOAD size_t get_global_size(uint dim);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_group_id.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL size_t get_group_id(uint dim);
_CLC_DECL _CLC_OVERLOAD size_t get_group_id(uint dim);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_local_id.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL size_t get_local_id(uint dim);
_CLC_DECL _CLC_OVERLOAD size_t get_local_id(uint dim);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_local_size.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL size_t get_local_size(uint dim);
_CLC_DECL _CLC_OVERLOAD size_t get_local_size(uint dim);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_num_groups.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL size_t get_num_groups(uint dim);
_CLC_DECL _CLC_OVERLOAD size_t get_num_groups(uint dim);
2 changes: 1 addition & 1 deletion libclc/generic/include/clc/workitem/get_work_dim.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_CLC_DECL uint get_work_dim(void);
_CLC_DECL _CLC_OVERLOAD uint get_work_dim(void);
3 changes: 2 additions & 1 deletion libclc/generic/lib/async/wait_group_events.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <clc/clc.h>

_CLC_DEF void wait_group_events(int num_events, event_t *event_list) {
_CLC_DEF _CLC_OVERLOAD void wait_group_events(int num_events,
event_t *event_list) {
barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE);
}
2 changes: 1 addition & 1 deletion libclc/generic/lib/workitem/get_global_id.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <clc/clc.h>

_CLC_DEF size_t get_global_id(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_global_id(uint dim) {
return get_group_id(dim) * get_local_size(dim) + get_local_id(dim) + get_global_offset(dim);
}
2 changes: 1 addition & 1 deletion libclc/generic/lib/workitem/get_global_size.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <clc/clc.h>

_CLC_DEF size_t get_global_size(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_global_size(uint dim) {
return get_num_groups(dim)*get_local_size(dim);
}
10 changes: 5 additions & 5 deletions libclc/ptx-nvidiacl/lib/mem_fence/fence.cl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include <clc/clc.h>

_CLC_DEF void mem_fence(cl_mem_fence_flags flags) {
if (flags & (CLK_GLOBAL_MEM_FENCE | CLK_LOCAL_MEM_FENCE))
__nvvm_membar_cta();
_CLC_DEF _CLC_OVERLOAD void mem_fence(cl_mem_fence_flags flags) {
if (flags & (CLK_GLOBAL_MEM_FENCE | CLK_LOCAL_MEM_FENCE))
__nvvm_membar_cta();
}

// We do not have separate mechanism for read and write fences.
_CLC_DEF void read_mem_fence(cl_mem_fence_flags flags) {
_CLC_DEF _CLC_OVERLOAD void read_mem_fence(cl_mem_fence_flags flags) {
mem_fence(flags);
}

_CLC_DEF void write_mem_fence(cl_mem_fence_flags flags) {
_CLC_DEF _CLC_OVERLOAD void write_mem_fence(cl_mem_fence_flags flags) {
mem_fence(flags);
}
3 changes: 1 addition & 2 deletions libclc/ptx-nvidiacl/lib/synchronization/barrier.cl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <clc/clc.h>

_CLC_DEF void barrier(cl_mem_fence_flags flags) {
_CLC_DEF _CLC_OVERLOAD void barrier(cl_mem_fence_flags flags) {
__syncthreads();
}

2 changes: 1 addition & 1 deletion libclc/ptx-nvidiacl/lib/workitem/get_global_id.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <clc/clc.h>

_CLC_DEF size_t get_global_id(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_global_id(uint dim) {
return get_group_id(dim) * get_local_size(dim) + get_local_id(dim);
}
2 changes: 1 addition & 1 deletion libclc/ptx-nvidiacl/lib/workitem/get_group_id.cl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <clc/clc.h>

_CLC_DEF size_t get_group_id(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_group_id(uint dim) {
switch (dim) {
case 0: return __nvvm_read_ptx_sreg_ctaid_x();
case 1: return __nvvm_read_ptx_sreg_ctaid_y();
Expand Down
2 changes: 1 addition & 1 deletion libclc/ptx-nvidiacl/lib/workitem/get_local_id.cl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <clc/clc.h>

_CLC_DEF size_t get_local_id(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_local_id(uint dim) {
switch (dim) {
case 0: return __nvvm_read_ptx_sreg_tid_x();
case 1: return __nvvm_read_ptx_sreg_tid_y();
Expand Down
2 changes: 1 addition & 1 deletion libclc/ptx-nvidiacl/lib/workitem/get_local_size.cl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <clc/clc.h>

_CLC_DEF size_t get_local_size(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_local_size(uint dim) {
switch (dim) {
case 0: return __nvvm_read_ptx_sreg_ntid_x();
case 1: return __nvvm_read_ptx_sreg_ntid_y();
Expand Down
2 changes: 1 addition & 1 deletion libclc/ptx-nvidiacl/lib/workitem/get_num_groups.cl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <clc/clc.h>

_CLC_DEF size_t get_num_groups(uint dim) {
_CLC_DEF _CLC_OVERLOAD size_t get_num_groups(uint dim) {
switch (dim) {
case 0: return __nvvm_read_ptx_sreg_nctaid_x();
case 1: return __nvvm_read_ptx_sreg_nctaid_y();
Expand Down
3 changes: 1 addition & 2 deletions libclc/r600/lib/synchronization/barrier.cl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

_CLC_DEF void __clc_r600_barrier(void) __asm("llvm.r600.group.barrier");

_CLC_DEF void barrier(uint flags)
{
_CLC_DEF _CLC_OVERLOAD void barrier(uint flags) {
// We should call mem_fence here, but that is not implemented for r600 yet
__clc_r600_barrier();
}
Loading

0 comments on commit 3d21fa5

Please sign in to comment.