-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[libclc] Move mem_fence and barrier to clc library #151446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wenju-he
wants to merge
2
commits into
llvm:main
Choose a base branch
from
wenju-he:libclc-barrier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __CLC_MEM_FENCE_CLC_MEM_FENCE_H__ | ||
#define __CLC_MEM_FENCE_CLC_MEM_FENCE_H__ | ||
|
||
#include <clc/internal/clc.h> | ||
#include <clc/mem_fence/clc_mem_scope_semantics.h> | ||
|
||
_CLC_OVERLOAD _CLC_DECL void __clc_mem_fence(Scope scope, | ||
MemorySemantics semantics); | ||
|
||
#endif // __CLC_MEM_FENCE_CLC_MEM_FENCE_H__ |
36 changes: 36 additions & 0 deletions
36
libclc/clc/include/clc/mem_fence/clc_mem_scope_semantics.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __CLC_MEM_FENCE_CLC_MEM_SCOPE_SEMANTICS_H__ | ||
#define __CLC_MEM_FENCE_CLC_MEM_SCOPE_SEMANTICS_H__ | ||
|
||
// Scope values are defined in SPIR-V spec. | ||
typedef enum Scope { | ||
CrossDevice = 0, | ||
Device = 1, | ||
Workgroup = 2, | ||
Subgroup = 3, | ||
Invocation = 4, | ||
} Scope; | ||
|
||
// MemorySemantics values are defined in SPIR-V spec. | ||
typedef enum MemorySemantics { | ||
None = 0x0, | ||
Acquire = 0x2, | ||
Release = 0x4, | ||
AcquireRelease = 0x8, | ||
SequentiallyConsistent = 0x10, | ||
UniformMemory = 0x40, | ||
SubgroupMemory = 0x80, | ||
WorkgroupMemory = 0x100, | ||
CrossWorkgroupMemory = 0x200, | ||
AtomicCounterMemory = 0x400, | ||
ImageMemory = 0x800, | ||
} MemorySemantics; | ||
|
||
#endif // __CLC_MEM_FENCE_CLC_MEM_SCOPE_SEMANTICS_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __CLC_SYNCHRONIZATION_CLC_BARRIER_H__ | ||
#define __CLC_SYNCHRONIZATION_CLC_BARRIER_H__ | ||
|
||
#include <clc/internal/clc.h> | ||
#include <clc/mem_fence/clc_mem_scope_semantics.h> | ||
|
||
_CLC_OVERLOAD _CLC_DECL void __clc_barrier(Scope scope, | ||
MemorySemantics semantics); | ||
|
||
#endif // __CLC_SYNCHRONIZATION_CLC_BARRIER_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <clc/mem_fence/clc_mem_fence.h> | ||
|
||
void __clc_amdgcn_s_waitcnt(unsigned flags); | ||
|
||
// s_waitcnt takes 16bit argument with a combined number of maximum allowed | ||
// pending operations: | ||
// [12:8] LGKM -- LDS, GDS, Konstant (SMRD), Messages | ||
// [7] -- undefined | ||
// [6:4] -- exports, GDS, and mem write | ||
// [3:0] -- vector memory operations | ||
|
||
// Newer clang supports __builtin_amdgcn_s_waitcnt | ||
#if __clang_major__ >= 5 | ||
#define __waitcnt(x) __builtin_amdgcn_s_waitcnt(x) | ||
#else | ||
#define __waitcnt(x) __clc_amdgcn_s_waitcnt(x) | ||
_CLC_DEF void __clc_amdgcn_s_waitcnt(unsigned) __asm("llvm.amdgcn.s.waitcnt"); | ||
#endif | ||
|
||
_CLC_OVERLOAD _CLC_DEF void __clc_mem_fence(Scope scope, | ||
MemorySemantics semantics) { | ||
if (semantics & CrossWorkgroupMemory) { | ||
// scalar loads are counted with LGKM but we don't know whether | ||
// the compiler turned any loads to scalar | ||
__waitcnt(0); | ||
} else if (semantics & WorkgroupMemory) | ||
__waitcnt(0xff); // LGKM is [12:8] | ||
} | ||
#undef __waitcnt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <clc/mem_fence/clc_mem_fence.h> | ||
#include <clc/synchronization/clc_barrier.h> | ||
|
||
_CLC_OVERLOAD _CLC_DEF void __clc_barrier(Scope scope, | ||
MemorySemantics semantics) { | ||
__clc_mem_fence(scope, semantics); | ||
__builtin_amdgcn_s_barrier(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <clc/mem_fence/clc_mem_fence.h> | ||
|
||
_CLC_OVERLOAD _CLC_DEF void __clc_mem_fence(Scope scope, | ||
MemorySemantics semantics) { | ||
if (semantics & (CrossWorkgroupMemory | WorkgroupMemory)) | ||
__nvvm_membar_cta(); | ||
} |
14 changes: 14 additions & 0 deletions
14
libclc/clc/lib/ptx-nvidiacl/synchronization/clc_barrier.cl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <clc/synchronization/clc_barrier.h> | ||
|
||
_CLC_OVERLOAD _CLC_DEF void __clc_barrier(Scope scope, | ||
MemorySemantics semantics) { | ||
__syncthreads(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __CLC_OPENCL_SYNCHRONIZATION_UTILS_H__ | ||
#define __CLC_OPENCL_SYNCHRONIZATION_UTILS_H__ | ||
|
||
#include <clc/internal/clc.h> | ||
#include <clc/mem_fence/clc_mem_scope_semantics.h> | ||
#include <clc/opencl/synchronization/cl_mem_fence_flags.h> | ||
|
||
_CLC_INLINE Scope getCLCScope(memory_scope memory_scope) { | ||
switch (memory_scope) { | ||
case memory_scope_work_item: | ||
return Invocation; | ||
#if defined(cl_intel_subgroups) || defined(cl_khr_subgroups) || \ | ||
defined(__opencl_c_subgroups) | ||
case memory_scope_sub_group: | ||
return Subgroup; | ||
#endif | ||
case memory_scope_work_group: | ||
return Workgroup; | ||
case memory_scope_device: | ||
return Device; | ||
default: | ||
break; | ||
} | ||
#ifdef __opencl_c_atomic_scope_all_devices | ||
return CrossDevice; | ||
#else | ||
return Device; | ||
#endif | ||
} | ||
|
||
_CLC_INLINE MemorySemantics getCLCMemorySemantics(cl_mem_fence_flags flag) { | ||
MemorySemantics semantics = SequentiallyConsistent; | ||
if (flag & CLK_GLOBAL_MEM_FENCE) | ||
semantics |= CrossWorkgroupMemory; | ||
if (flag & CLK_LOCAL_MEM_FENCE) | ||
semantics |= WorkgroupMemory; | ||
if (flag & CLK_IMAGE_MEM_FENCE) | ||
semantics |= ImageMemory; | ||
return semantics; | ||
} | ||
|
||
#endif // __CLC_OPENCL_SYNCHRONIZATION_UTILS_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should stick to OpenCL spec and terminology; I thought clang already had predefined enums for both of these