#ifndef LLVM_CLANG_BASIC_SYNCSCOPE_H
#define LLVM_CLANG_BASIC_SYNCSCOPE_H
#include "clang/Basic/LangOptions.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include <memory>
namespace clang {
enum class SyncScope {
HIPSingleThread,
HIPWavefront,
HIPWorkgroup,
HIPAgent,
HIPSystem,
OpenCLWorkGroup,
OpenCLDevice,
OpenCLAllSVMDevices,
OpenCLSubGroup,
Last = OpenCLSubGroup
};
inline llvm::StringRef getAsString(SyncScope S) {
switch (S) {
case SyncScope::HIPSingleThread:
return "hip_singlethread";
case SyncScope::HIPWavefront:
return "hip_wavefront";
case SyncScope::HIPWorkgroup:
return "hip_workgroup";
case SyncScope::HIPAgent:
return "hip_agent";
case SyncScope::HIPSystem:
return "hip_system";
case SyncScope::OpenCLWorkGroup:
return "opencl_workgroup";
case SyncScope::OpenCLDevice:
return "opencl_device";
case SyncScope::OpenCLAllSVMDevices:
return "opencl_allsvmdevices";
case SyncScope::OpenCLSubGroup:
return "opencl_subgroup";
}
llvm_unreachable("Invalid synch scope");
}
enum class AtomicScopeModelKind { None, OpenCL, HIP };
class AtomicScopeModel {
public:
virtual ~AtomicScopeModel() {}
virtual SyncScope map(unsigned S) const = 0;
virtual bool isValid(unsigned S) const = 0;
virtual ArrayRef<unsigned> getRuntimeValues() const = 0;
virtual unsigned getFallBackValue() const = 0;
static std::unique_ptr<AtomicScopeModel> create(AtomicScopeModelKind K);
};
class AtomicScopeOpenCLModel : public AtomicScopeModel {
public:
enum ID {
WorkGroup = 1,
Device = 2,
AllSVMDevices = 3,
SubGroup = 4,
Last = SubGroup
};
AtomicScopeOpenCLModel() {}
SyncScope map(unsigned S) const override {
switch (static_cast<ID>(S)) {
case WorkGroup:
return SyncScope::OpenCLWorkGroup;
case Device:
return SyncScope::OpenCLDevice;
case AllSVMDevices:
return SyncScope::OpenCLAllSVMDevices;
case SubGroup:
return SyncScope::OpenCLSubGroup;
}
llvm_unreachable("Invalid language synch scope value");
}
bool isValid(unsigned S) const override {
return S >= static_cast<unsigned>(WorkGroup) &&
S <= static_cast<unsigned>(Last);
}
ArrayRef<unsigned> getRuntimeValues() const override {
static_assert(Last == SubGroup, "Does not include all synch scopes");
static const unsigned Scopes[] = {
static_cast<unsigned>(WorkGroup), static_cast<unsigned>(Device),
static_cast<unsigned>(AllSVMDevices), static_cast<unsigned>(SubGroup)};
return llvm::makeArrayRef(Scopes);
}
unsigned getFallBackValue() const override {
return static_cast<unsigned>(AllSVMDevices);
}
};
class AtomicScopeHIPModel : public AtomicScopeModel {
public:
enum ID {
SingleThread = 1,
Wavefront = 2,
Workgroup = 3,
Agent = 4,
System = 5,
Last = System
};
AtomicScopeHIPModel() {}
SyncScope map(unsigned S) const override {
switch (static_cast<ID>(S)) {
case SingleThread:
return SyncScope::HIPSingleThread;
case Wavefront:
return SyncScope::HIPWavefront;
case Workgroup:
return SyncScope::HIPWorkgroup;
case Agent:
return SyncScope::HIPAgent;
case System:
return SyncScope::HIPSystem;
}
llvm_unreachable("Invalid language synch scope value");
}
bool isValid(unsigned S) const override {
return S >= static_cast<unsigned>(SingleThread) &&
S <= static_cast<unsigned>(Last);
}
ArrayRef<unsigned> getRuntimeValues() const override {
static_assert(Last == System, "Does not include all synch 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)};
return llvm::makeArrayRef(Scopes);
}
unsigned getFallBackValue() const override {
return static_cast<unsigned>(System);
}
};
inline std::unique_ptr<AtomicScopeModel>
AtomicScopeModel::create(AtomicScopeModelKind K) {
switch (K) {
case AtomicScopeModelKind::None:
return std::unique_ptr<AtomicScopeModel>{};
case AtomicScopeModelKind::OpenCL:
return std::make_unique<AtomicScopeOpenCLModel>();
case AtomicScopeModelKind::HIP:
return std::make_unique<AtomicScopeHIPModel>();
}
llvm_unreachable("Invalid atomic scope model kind");
}
}
#endif