|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <clc/mem_fence/clc_mem_fence.h> |
| 10 | + |
| 11 | +void __clc_amdgcn_s_waitcnt(unsigned flags); |
| 12 | + |
| 13 | +// s_waitcnt takes 16bit argument with a combined number of maximum allowed |
| 14 | +// pending operations: |
| 15 | +// [12:8] LGKM -- LDS, GDS, Konstant (SMRD), Messages |
| 16 | +// [7] -- undefined |
| 17 | +// [6:4] -- exports, GDS, and mem write |
| 18 | +// [3:0] -- vector memory operations |
| 19 | + |
| 20 | +// Newer clang supports __builtin_amdgcn_s_waitcnt |
| 21 | +#if __clang_major__ >= 5 |
| 22 | +#define __waitcnt(x) __builtin_amdgcn_s_waitcnt(x) |
| 23 | +#else |
| 24 | +#define __waitcnt(x) __clc_amdgcn_s_waitcnt(x) |
| 25 | +_CLC_DEF void __clc_amdgcn_s_waitcnt(unsigned) __asm("llvm.amdgcn.s.waitcnt"); |
| 26 | +#endif |
| 27 | + |
| 28 | +_CLC_OVERLOAD _CLC_DEF void __clc_mem_fence(int memory_scope, |
| 29 | + int memory_order) { |
| 30 | + if (memory_scope & __MEMORY_SCOPE_DEVICE) { |
| 31 | + // scalar loads are counted with LGKM but we don't know whether |
| 32 | + // the compiler turned any loads to scalar |
| 33 | + __waitcnt(0); |
| 34 | + } else if (memory_scope & __MEMORY_SCOPE_WRKGRP) |
| 35 | + __waitcnt(0xff); // LGKM is [12:8] |
| 36 | +} |
| 37 | +#undef __waitcnt |
0 commit comments