Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/HIPSupport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ Predefined Macros
- Represents wavefront memory scope in HIP (value is 2).
* - ``__HIP_MEMORY_SCOPE_WORKGROUP``
- Represents workgroup memory scope in HIP (value is 3).
* - ``__HIP_MEMORY_SCOPE_CLUSTER``
- Represents cluster memory scope in HIP (value is 6).
* - ``__HIP_MEMORY_SCOPE_AGENT``
- Represents agent memory scope in HIP (value is 4).
* - ``__HIP_MEMORY_SCOPE_SYSTEM``
Expand Down
1 change: 1 addition & 0 deletions clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4846,6 +4846,7 @@ currently supported:
* ``__MEMORY_SCOPE_SYSTEM``
* ``__MEMORY_SCOPE_DEVICE``
* ``__MEMORY_SCOPE_WRKGRP``
* ``__MEMORY_SCOPE_CLUSTR``
* ``__MEMORY_SCOPE_WVFRNT``
* ``__MEMORY_SCOPE_SINGLE``

Expand Down
45 changes: 35 additions & 10 deletions clang/include/clang/Basic/SyncScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ enum class SyncScope {
SystemScope,
DeviceScope,
WorkgroupScope,
ClusterScope,
WavefrontScope,
SingleScope,
HIPSingleThread,
HIPWavefront,
HIPWorkgroup,
HIPCluster,
HIPAgent,
HIPSystem,
OpenCLWorkGroup,
Expand All @@ -65,6 +67,8 @@ inline llvm::StringRef getAsString(SyncScope S) {
return "device_scope";
case SyncScope::WorkgroupScope:
return "workgroup_scope";
case SyncScope::ClusterScope:
return "cluster_scope";
case SyncScope::WavefrontScope:
return "wavefront_scope";
case SyncScope::SingleScope:
Expand All @@ -75,6 +79,8 @@ inline llvm::StringRef getAsString(SyncScope S) {
return "hip_wavefront";
case SyncScope::HIPWorkgroup:
return "hip_workgroup";
case SyncScope::HIPCluster:
return "hip_cluster";
case SyncScope::HIPAgent:
return "hip_agent";
case SyncScope::HIPSystem:
Expand Down Expand Up @@ -174,13 +180,18 @@ class AtomicScopeHIPModel : public AtomicScopeModel {
/// The enum values match the pre-defined macros
/// __HIP_MEMORY_SCOPE_*, which are used to define memory_scope_*
/// enums in hip-c.h.
/// These may be present in pch files or bitcode so preserve existing values
/// when adding a new ID.
enum ID {
SingleThread = 1,
Wavefront = 2,
Workgroup = 3,
Agent = 4,
System = 5,
Last = System
Cluster = 6,
End,
Last = End - 1,
Count = Last
};

AtomicScopeHIPModel() {}
Expand All @@ -193,10 +204,14 @@ class AtomicScopeHIPModel : public AtomicScopeModel {
return SyncScope::HIPWavefront;
case Workgroup:
return SyncScope::HIPWorkgroup;
case Cluster:
return SyncScope::HIPCluster;
case Agent:
return SyncScope::HIPAgent;
case System:
return SyncScope::HIPSystem;
case End:
break;
}
llvm_unreachable("Invalid language sync scope value");
}
Expand All @@ -207,11 +222,12 @@ class AtomicScopeHIPModel : public AtomicScopeModel {
}

ArrayRef<unsigned> getRuntimeValues() const override {
static_assert(Last == System, "Does not include all sync scopes");
static const unsigned Scopes[] = {
static_cast<unsigned>(SingleThread), static_cast<unsigned>(Wavefront),
static_cast<unsigned>(Workgroup), static_cast<unsigned>(Agent),
static_cast<unsigned>(System)};
static_cast<unsigned>(Workgroup), static_cast<unsigned>(Cluster),
static_cast<unsigned>(System), static_cast<unsigned>(Agent)};
static_assert(sizeof(Scopes) / sizeof(Scopes[0]) == Count,
"Does not include all sync scopes");
return llvm::ArrayRef(Scopes);
}

Expand All @@ -223,14 +239,18 @@ class AtomicScopeHIPModel : public AtomicScopeModel {
/// Defines the generic atomic scope model.
class AtomicScopeGenericModel : public AtomicScopeModel {
public:
/// The enum values match predefined built-in macros __ATOMIC_SCOPE_*.
/// The enum values match predefined built-in macros __MEMORY_SCOPE_*.
/// These may be present in pch files or bitcode so preserve existing values
/// when adding a new ID.
enum ID {
System = 0,
Device = 1,
Workgroup = 2,
Wavefront = 3,
Single = 4,
Last = Single
Cluster = 5,
Count,
Last = Count - 1
};

AtomicScopeGenericModel() = default;
Expand All @@ -243,10 +263,14 @@ class AtomicScopeGenericModel : public AtomicScopeModel {
return SyncScope::SystemScope;
case Workgroup:
return SyncScope::WorkgroupScope;
case Cluster:
return SyncScope::ClusterScope;
case Wavefront:
return SyncScope::WavefrontScope;
case Single:
return SyncScope::SingleScope;
case Count:
break;
}
llvm_unreachable("Invalid language sync scope value");
}
Expand All @@ -256,11 +280,12 @@ class AtomicScopeGenericModel : public AtomicScopeModel {
}

ArrayRef<unsigned> getRuntimeValues() const override {
static_assert(Last == Single, "Does not include all sync scopes");
static const unsigned Scopes[] = {
static_cast<unsigned>(Device), static_cast<unsigned>(System),
static_cast<unsigned>(Workgroup), static_cast<unsigned>(Wavefront),
static_cast<unsigned>(Single)};
static_cast<unsigned>(System), static_cast<unsigned>(Device),
static_cast<unsigned>(Workgroup), static_cast<unsigned>(Cluster),
static_cast<unsigned>(Wavefront), static_cast<unsigned>(Single)};
static_assert(sizeof(Scopes) / sizeof(Scopes[0]) == Count,
"Does not include all sync scopes");
return llvm::ArrayRef(Scopes);
}

Expand Down
29 changes: 15 additions & 14 deletions clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "CGBuiltin.h"
#include "CodeGenFunction.h"
#include "clang/Basic/SyncScope.h"
#include "clang/Basic/TargetBuiltins.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "llvm/Analysis/ValueTracking.h"
Expand Down Expand Up @@ -313,33 +314,33 @@ void CodeGenFunction::ProcessOrderScopeAMDGCN(Value *Order, Value *Scope,
}

// Older builtins had an enum argument for the memory scope.
const char *SSN = nullptr;
int scope = cast<llvm::ConstantInt>(Scope)->getZExtValue();
switch (scope) {
case 0: // __MEMORY_SCOPE_SYSTEM
case AtomicScopeGenericModel::System: // __MEMORY_SCOPE_SYSTEM
SSID = llvm::SyncScope::System;
break;
case 1: // __MEMORY_SCOPE_DEVICE
if (getTarget().getTriple().isSPIRV())
SSID = getLLVMContext().getOrInsertSyncScopeID("device");
else
SSID = getLLVMContext().getOrInsertSyncScopeID("agent");
case AtomicScopeGenericModel::Device: // __MEMORY_SCOPE_DEVICE
SSN = getTarget().getTriple().isSPIRV() ? "device" : "agent";
break;
case 2: // __MEMORY_SCOPE_WRKGRP
SSID = getLLVMContext().getOrInsertSyncScopeID("workgroup");
case AtomicScopeGenericModel::Workgroup: // __MEMORY_SCOPE_WRKGRP
SSN = "workgroup";
break;
case 3: // __MEMORY_SCOPE_WVFRNT
if (getTarget().getTriple().isSPIRV())
SSID = getLLVMContext().getOrInsertSyncScopeID("subgroup");
else
SSID = getLLVMContext().getOrInsertSyncScopeID("wavefront");
case AtomicScopeGenericModel::Cluster: // __MEMORY_SCOPE_CLUSTR
SSN = getTarget().getTriple().isSPIRV() ? "workgroup" : "cluster";
break;
case AtomicScopeGenericModel::Wavefront: // __MEMORY_SCOPE_WVFRNT
SSN = getTarget().getTriple().isSPIRV() ? "subgroup" : "wavefront";
break;
case 4: // __MEMORY_SCOPE_SINGLE
case AtomicScopeGenericModel::Single: // __MEMORY_SCOPE_SINGLE
SSID = llvm::SyncScope::SingleThread;
break;
default:
SSID = llvm::SyncScope::System;
break;
}
if (SSN)
SSID = getLLVMContext().getOrInsertSyncScopeID(SSN);
}

llvm::Value *CodeGenFunction::EmitScalarOrConstFoldImmArg(unsigned ICEArguments,
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CodeGen/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
case SyncScope::WavefrontScope:
Name = "wavefront";
break;
case SyncScope::HIPCluster:
case SyncScope::ClusterScope:
Name = "cluster";
break;
case SyncScope::HIPWorkgroup:
case SyncScope::OpenCLWorkGroup:
case SyncScope::WorkgroupScope:
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CodeGen/Targets/SPIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ inline StringRef mapClangSyncScopeToLLVM(SyncScope Scope) {
case SyncScope::OpenCLSubGroup:
case SyncScope::WavefrontScope:
return "subgroup";
case SyncScope::HIPCluster:
case SyncScope::ClusterScope:
case SyncScope::HIPWorkgroup:
case SyncScope::OpenCLWorkGroup:
case SyncScope::WorkgroupScope:
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
Builder.defineMacro("__HIP_MEMORY_SCOPE_WORKGROUP", "3");
Builder.defineMacro("__HIP_MEMORY_SCOPE_AGENT", "4");
Builder.defineMacro("__HIP_MEMORY_SCOPE_SYSTEM", "5");
Builder.defineMacro("__HIP_MEMORY_SCOPE_CLUSTER", "6");
if (LangOpts.HIPStdPar) {
Builder.defineMacro("__HIPSTDPAR__");
if (LangOpts.HIPStdParInterposeAlloc) {
Expand Down Expand Up @@ -873,6 +874,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
Builder.defineMacro("__MEMORY_SCOPE_WRKGRP", "2");
Builder.defineMacro("__MEMORY_SCOPE_WVFRNT", "3");
Builder.defineMacro("__MEMORY_SCOPE_SINGLE", "4");
Builder.defineMacro("__MEMORY_SCOPE_CLUSTR", "5");

// Define macros for the OpenCL memory scope.
// The values should match AtomicScopeOpenCLModel::ID enum.
Expand Down
Loading