Skip to content

Commit

Permalink
Attempt at using clang-offload-wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Engin Kayraklioglu <e-kayrakli@users.noreply.github.com>
  • Loading branch information
e-kayrakli committed Jul 7, 2022
1 parent 567272c commit cb80cee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
14 changes: 7 additions & 7 deletions compiler/codegen/codegen.cpp
Expand Up @@ -2501,15 +2501,15 @@ static void embedGpuCode() {
// reads this .fatbin file and dumps its contents into a global variable in the generated code.
// The compiled chapel program then calls into the runtime library, which reads this variable,
// sends the code off to the GPU, and launches kernels as needed.
SET_LINENO(rootModule);
//SET_LINENO(rootModule);
//std::string fatbinFilename = genIntermediateFilename("chpl__gpu.fatbin");
std::string omptargetImageFilename = genIntermediateFilename("chpl__gpu_image.fatbin");
//std::string omptargetImageFilename = genIntermediateFilename("chpl__gpu_image.fatbin");
std::string buffer;
chpl::ErrorMessage err;
chpl::readfile(omptargetImageFilename.c_str(), buffer, err);
if(!err.isEmpty()) {
USR_FATAL("%s", err.message().c_str());
}
//chpl::ErrorMessage err;
//chpl::readfile(omptargetImageFilename.c_str(), buffer, err);
//if(!err.isEmpty()) {
//USR_FATAL("%s", err.message().c_str());
//}

genGlobalRawString("chpl_gpuBinary", buffer, buffer.length());
}
Expand Down
57 changes: 39 additions & 18 deletions compiler/llvm/clangUtil.cpp
Expand Up @@ -3961,6 +3961,7 @@ void makeBinaryLLVM(void) {
std::string ptxObjectFilename;
std::string fatbinFilename;
std::string omptargetImageFilename;
omptargetImageFilename = genIntermediateFilename("chpl__gpu_image.bc");

if (gCodegenGPU == false) {
moduleFilename = genIntermediateFilename("chpl__module.o");
Expand All @@ -3975,7 +3976,6 @@ void makeBinaryLLVM(void) {
asmFilename = genIntermediateFilename("chpl__gpu_ptx.s");
ptxObjectFilename = genIntermediateFilename("chpl__gpu_ptx.o");
fatbinFilename = genIntermediateFilename("chpl__gpu.fatbin");
omptargetImageFilename = genIntermediateFilename("chpl__gpu_image.fatbin");
}

if( saveCDir[0] != '\0' ) {
Expand Down Expand Up @@ -4264,31 +4264,32 @@ void makeBinaryLLVM(void) {

mysystem(ptxCmd.c_str(), "PTX to object file");

if (strncmp(fCUDAArch, "sm_", 3) != 0 || strlen(fCUDAArch) != 5) {
USR_FATAL("Unrecognized CUDA arch");
}

std::string computeCap = std::string("compute_") + fCUDAArch[3] +
fCUDAArch[4];
std::string fatbinaryCmd = std::string("fatbinary -64 ") +
std::string("--create ") +
fatbinFilename.c_str() +
std::string(" --image=profile=") + fCUDAArch +
",file=" + ptxObjectFilename.c_str() +
std::string(" --image=profile=") + computeCap +
",file=" + asmFilename.c_str();

mysystem(fatbinaryCmd.c_str(), "object file to fatbinary");

std::string clangOffloadWrapper = "/home/engin/code/chapel/versions/1/chapel/third-party/llvm/install/linux64-x86_64/bin/clang-offload-wrapper";

std::string cowCmd = clangOffloadWrapper + std::string(" -o ") +
omptargetImageFilename +
std::string(" --target=nvptx64-nvidia-cuda ") +
fatbinFilename;
ptxObjectFilename;

mysystem(cowCmd.c_str(), "fatbinary to omptarget image");

//if (strncmp(fCUDAArch, "sm_", 3) != 0 || strlen(fCUDAArch) != 5) {
//USR_FATAL("Unrecognized CUDA arch");
//}

//std::string computeCap = std::string("compute_") + fCUDAArch[3] +
//fCUDAArch[4];
//std::string fatbinaryCmd = std::string("fatbinary -64 ") +
//std::string("--create ") +
//fatbinFilename.c_str() +
//std::string(" --image=profile=") + fCUDAArch +
//",file=" + ptxObjectFilename.c_str() +
//std::string(" --image=profile=") + computeCap +
//",file=" + asmFilename.c_str();

//mysystem(fatbinaryCmd.c_str(), "object file to fatbinary");


}
}

Expand All @@ -4299,6 +4300,26 @@ void makeBinaryLLVM(void) {
if (gCodegenGPU) {
return;
}
//omptargetImageFilename
{
GenInfo* info = gGenInfo;

// load libdevice as a new module
llvm::SMDiagnostic err;
auto libdevice =
llvm::parseIRFile("/home/engin/code/chapel/versions/1/chapel/gen_code/chpl__gpu_image.bc",
err, info->llvmContext);
//
// adjust it
//const llvm::Triple &Triple = info->clangInfo->Clang->getTarget().getTriple();
//libdevice->setTargetTriple(Triple.getTriple());
//libdevice->setDataLayout(info->clangInfo->asmTargetLayoutStr);

// link
//llvm::Linker::linkModules(*info->module, std::move(libdevice),
//llvm::Linker::Flags::None);

}

std::string options = "";

Expand Down

0 comments on commit cb80cee

Please sign in to comment.