From 647e473998c75bcb3529a86878a42b4c90ab7d23 Mon Sep 17 00:00:00 2001 From: Artem Belevich Date: Fri, 12 Aug 2016 18:44:01 +0000 Subject: [PATCH] [CUDA] Place GPU binary into .nv_fatbin section and align it by 8. This matches the way nvcc encapsulates GPU binaries into host object file. Now cuobjdump can deal with clang-compiled object files. Differential Revision: https://reviews.llvm.org/D23429 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@278549 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCUDANV.cpp | 11 ++++++++++- test/CodeGenCUDA/device-stub.cu | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/CodeGen/CGCUDANV.cpp b/lib/CodeGen/CGCUDANV.cpp index 3e1f2b5d1d..dacc53b16e 100644 --- a/lib/CodeGen/CGCUDANV.cpp +++ b/lib/CodeGen/CGCUDANV.cpp @@ -55,10 +55,18 @@ private: /// where the C code specifies const char*. llvm::Constant *makeConstantString(const std::string &Str, const std::string &Name = "", + const std::string &SectionName = "", unsigned Alignment = 0) { llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0), llvm::ConstantInt::get(SizeTy, 0)}; auto ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str()); + llvm::GlobalVariable *GV = + cast(ConstStr.getPointer()); + if (!SectionName.empty()) + GV->setSection(SectionName); + if (Alignment) + GV->setAlignment(Alignment); + return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(), ConstStr.getPointer(), Zeros); } @@ -285,7 +293,8 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { llvm::Constant *Values[] = { llvm::ConstantInt::get(IntTy, 0x466243b1), // Fatbin wrapper magic. llvm::ConstantInt::get(IntTy, 1), // Fatbin version. - makeConstantString(GpuBinaryOrErr.get()->getBuffer(), "", 16), // Data. + makeConstantString(GpuBinaryOrErr.get()->getBuffer(), // Data. + "", ".nv_fatbin", 8), // llvm::ConstantPointerNull::get(VoidPtrTy)}; // Unused in fatbin v1. llvm::GlobalVariable *FatbinWrapper = new llvm::GlobalVariable( TheModule, FatbinWrapperTy, true, llvm::GlobalValue::InternalLinkage, diff --git a/test/CodeGenCUDA/device-stub.cu b/test/CodeGenCUDA/device-stub.cu index 5979ba3fce..3376803c50 100644 --- a/test/CodeGenCUDA/device-stub.cu +++ b/test/CodeGenCUDA/device-stub.cu @@ -45,10 +45,12 @@ void use_pointers() { // * constant unnamed string with the kernel name // CHECK: private unnamed_addr constant{{.*}}kernelfunc{{.*}}\00" // * constant unnamed string with GPU binary -// CHECK: private unnamed_addr constant{{.*}}\00" +// CHECK: private unnamed_addr constant{{.*GPU binary would be here.*}}\00" +// CHECK-SAME: section ".nv_fatbin", align 8 // * constant struct that wraps GPU binary // CHECK: @__cuda_fatbin_wrapper = internal constant { i32, i32, i8*, i8* } -// CHECK: { i32 1180844977, i32 1, {{.*}}, i8* null } +// CHECK-SAME: { i32 1180844977, i32 1, {{.*}}, i8* null } +// CHECK-SAME: section ".nvFatBinSegment" // * variable to save GPU binary handle after initialization // CHECK: @__cuda_gpubin_handle = internal global i8** null // * Make sure our constructor/destructor was added to global ctor/dtor list. -- 2.40.0