From a3ce7f5cd2ae236bec7752e343f4b63ddda7ebe7 Mon Sep 17 00:00:00 2001 From: "Yaxun (Sam) Liu" Date: Fri, 5 Feb 2021 14:40:16 -0500 Subject: [HIP] Fix managed variable linkage Currently managed variables are emitted as undefined symbols, which causes difficulty for diagnosing undefined symbols for non-managed variables. This patch transforms managed variables in device compilation so that they can be emitted as normal variables. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D96195 --- clang/lib/CodeGen/CodeGenModule.cpp | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index d568e3d7095c..7e45ae3acb3e 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -459,10 +459,8 @@ void CodeGenModule::Release() { if (ObjCRuntime) if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) AddGlobalCtor(ObjCInitFunction); - if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice && - CUDARuntime) { - if (llvm::Function *CudaCtorFunction = - CUDARuntime->makeModuleCtorFunction()) + if (Context.getLangOpts().CUDA && CUDARuntime) { + if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule()) AddGlobalCtor(CudaCtorFunction); } if (OpenMPRuntime) { @@ -3833,8 +3831,14 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, } } - if (GV->isDeclaration()) + if (GV->isDeclaration()) { getTargetCodeGenInfo().setTargetAttributes(D, GV, *this); + // External HIP managed variables needed to be recorded for transformation + // in both device and host compilations. + if (getLangOpts().CUDA && D && D->hasAttr() && + D->hasExternalStorage()) + getCUDARuntime().handleVarRegistration(D, *GV); + } LangAS ExpectedAS = D ? D->getType().getAddressSpace() @@ -4142,12 +4146,8 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, bool NeedsGlobalDtor = D->needsDestruction(getContext()) == QualType::DK_cxx_destructor; - bool IsHIPManagedVarOnDevice = - getLangOpts().CUDAIsDevice && D->hasAttr(); - const VarDecl *InitDecl; - const Expr *InitExpr = - IsHIPManagedVarOnDevice ? nullptr : D->getAnyInitializer(InitDecl); + const Expr *InitExpr = D->getAnyInitializer(InitDecl); Optional emitter; @@ -4158,15 +4158,15 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, getLangOpts().CUDAIsDevice && D->hasAttr(); // Shadows of initialized device-side global variables are also left // undefined. + // Managed Variables should be initialized on both host side and device side. bool IsCUDAShadowVar = !getLangOpts().CUDAIsDevice && !D->hasAttr() && (D->hasAttr() || D->hasAttr() || D->hasAttr()); bool IsCUDADeviceShadowVar = - getLangOpts().CUDAIsDevice && + getLangOpts().CUDAIsDevice && !D->hasAttr() && (D->getType()->isCUDADeviceBuiltinSurfaceType() || - D->getType()->isCUDADeviceBuiltinTextureType() || - D->hasAttr()); + D->getType()->isCUDADeviceBuiltinTextureType()); if (getLangOpts().CUDA && (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar)) Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy)); @@ -4273,14 +4273,11 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, GV->setExternallyInitialized(true); } else { getCUDARuntime().internalizeDeviceSideVar(D, Linkage); - getCUDARuntime().handleVarRegistration(D, *GV); } + getCUDARuntime().handleVarRegistration(D, *GV); } - // HIP managed variables need to be emitted as declarations in device - // compilation. - if (!IsHIPManagedVarOnDevice) - GV->setInitializer(Init); + GV->setInitializer(Init); if (emitter) emitter->finalize(GV); -- cgit v1.2.3