Skip to content

Commit

Permalink
Merge branch 'master' into llvm_15
Browse files Browse the repository at this point in the history
  • Loading branch information
richardmembarth committed Aug 2, 2023
2 parents 98f2b50 + 97a534a commit 3967152
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
9 changes: 7 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ endif()

# Necessary to specify whether or not to build shared libraries on a per-project basis
option(AnyDSL_runtime_BUILD_SHARED "Builds a shared library for the runtime (and JIT, if enabled)" ${BUILD_SHARED_LIBS})
if (AnyDSL_runtime_BUILD_SHARED)
if(AnyDSL_runtime_BUILD_SHARED)
set(AnyDSL_runtime_BUILD "SHARED")
else()
set(AnyDSL_runtime_BUILD "STATIC")
Expand Down Expand Up @@ -131,11 +131,16 @@ set(AnyDSL_runtime_HAS_HSA_SUPPORT ${hsa-runtime64_FOUND} CACHE INTERNAL "enable
# look for LLVM for nvptx and gcn
find_package(LLVM)
if(LLVM_FOUND)
if(NOT DEFINED AnyDSL_LLVM_LINK_SHARED)
if(LLVM_LINK_LLVM_DYLIB)
set(AnyDSL_LLVM_LINK_SHARED "USE_SHARED")
endif()
endif()
add_definitions(${LLVM_DEFINITIONS})
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
set(AnyDSL_runtime_LLVM_COMPONENTS irreader support ${LLVM_TARGETS_TO_BUILD})
set(AnyDSL_runtime_JIT_LLVM_COMPONENTS ${AnyDSL_runtime_LLVM_COMPONENTS} mcjit)
if(AnyDSL_runtime_HAS_HSA_SUPPORT)
if(AnyDSL_runtime_HAS_HSA_SUPPORT AND RUNTIME_JIT)
find_package(LLD REQUIRED)
target_link_libraries(runtime_hsa PRIVATE lldELF lldCommon)
llvm_config(runtime_hsa ${AnyDSL_LLVM_LINK_SHARED} lto option ${LLVM_TARGETS_TO_BUILD})
Expand Down
4 changes: 4 additions & 0 deletions src/cuda_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ CudaPlatform::CudaPlatform(Runtime* runtime)
#endif

CUresult err = cuInit(0);
if (err == CUDA_ERROR_NO_DEVICE) {
info("CUDA backend did not initialise because no devices were found (CUDA_ERROR_NO_DEVICE).");
return;
}
CHECK_CUDA(err, "cuInit()");

err = cuDeviceGetCount(&device_count);
Expand Down
39 changes: 23 additions & 16 deletions src/hsa_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ hsa_status_t HSAPlatform::iterate_agents_callback(hsa_agent_t agent, void* data)
char isa_name[64] = { 0 };
status = hsa_isa_get_info_alt(isa, HSA_ISA_INFO_NAME, isa_name);
debug(" Device ISA: %", isa_name);
std::string isa_name_str = isa_name;
auto dash_pos = isa_name_str.rfind('-');
isa_name_str = dash_pos != std::string::npos ? isa_name_str.substr(dash_pos + 1) : "";

hsa_device_type_t device_type;
status = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &device_type);
Expand Down Expand Up @@ -155,7 +152,7 @@ hsa_status_t HSAPlatform::iterate_agents_callback(hsa_agent_t agent, void* data)
device->agent = agent;
device->profile = profile;
device->float_mode = float_mode;
device->isa = isa_name_str;
device->isa = agent_name;
device->queue = queue;
device->kernarg_region.handle = { 0 };
device->finegrained_region.handle = { 0 };
Expand Down Expand Up @@ -251,7 +248,12 @@ HSAPlatform::HSAPlatform(Runtime* runtime)
: Platform(runtime)
{
hsa_status_t status = hsa_init();
if (status == HSA_STATUS_ERROR_OUT_OF_RESOURCES) {
info("HSA runtime failed to initialize (HSA_STATUS_ERROR_OUT_OF_RESOURCES). This is likely caused by a lack of suitable HSA devices and may be ignored.");
return;
}
CHECK_HSA(status, "hsa_init()");
initialized_ = true;

uint16_t version_major, version_minor;
status = hsa_system_get_info(HSA_SYSTEM_INFO_VERSION_MAJOR, &version_major);
Expand Down Expand Up @@ -291,7 +293,8 @@ HSAPlatform::~HSAPlatform() {
}
}

hsa_shut_down();
if (initialized_)
hsa_shut_down();
}

void* HSAPlatform::alloc_hsa(int64_t size, hsa_region_t region) {
Expand Down Expand Up @@ -596,12 +599,16 @@ std::string HSAPlatform::emit_gcn(const std::string& program, const std::string&
llvm::SMDiagnostic diagnostic_err;
std::unique_ptr<llvm::Module> llvm_module = llvm::parseIR(llvm::MemoryBuffer::getMemBuffer(program)->getMemBufferRef(), diagnostic_err, llvm_context);

if (!llvm_module) {
auto get_diag_msg = [&] () -> std::string {
std::string stream;
llvm::raw_string_ostream llvm_stream(stream);
diagnostic_err.print("", llvm_stream);
error("Parsing IR file %: %", filename, llvm_stream.str());
}
llvm_stream.flush();
return stream;
};

if (!llvm_module)
error("Parsing IR file %:\n%", filename, get_diag_msg());

auto triple_str = llvm_module->getTargetTriple();
std::string error_str;
Expand Down Expand Up @@ -629,17 +636,17 @@ std::string HSAPlatform::emit_gcn(const std::string& program, const std::string&
@__oclc_correctly_rounded_sqrt32 = addrspace(4) constant i8 0
@__oclc_wavefrontsize64 = addrspace(4) constant i8 )" + wavefrontsize64;
std::unique_ptr<llvm::Module> isa_module(llvm::parseIRFile(isa_file, diagnostic_err, llvm_context));
if (isa_module == nullptr)
error("Can't create isa module for '%'", isa_file);
if (!isa_module)
error("Can't create isa module for '%':\n%", isa_file, get_diag_msg());
std::unique_ptr<llvm::Module> config_module = llvm::parseIR(llvm::MemoryBuffer::getMemBuffer(ocml_config)->getMemBufferRef(), diagnostic_err, llvm_context);
if (config_module == nullptr)
error("Can't create ocml config module");
if (!config_module)
error("Can't create ocml config module:\n%", get_diag_msg());
std::unique_ptr<llvm::Module> ocml_module(llvm::parseIRFile(ocml_file, diagnostic_err, llvm_context));
if (ocml_module == nullptr)
error("Can't create ocml module for '%'", ocml_file);
if (!ocml_module)
error("Can't create ocml module for '%':\n%", ocml_file, get_diag_msg());
std::unique_ptr<llvm::Module> ockl_module(llvm::parseIRFile(ockl_file, diagnostic_err, llvm_context));
if (ockl_module == nullptr)
error("Can't create ockl module for '%'", ockl_file);
if (!ockl_module)
error("Can't create ockl module for '%':\n%", ockl_file, get_diag_msg());

// override data layout with the one coming from the target machine
llvm_module->setDataLayout(machine->createDataLayout());
Expand Down
1 change: 1 addition & 0 deletions src/hsa_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class HSAPlatform : public Platform {

uint64_t frequency_;
std::vector<DeviceData> devices_;
bool initialized_;

void* alloc_hsa(int64_t, hsa_region_t);
void* alloc_hsa(int64_t, hsa_amd_memory_pool_t);
Expand Down

0 comments on commit 3967152

Please sign in to comment.