diff options
| author | Yaxun (Sam) Liu <yaxun.liu@amd.com> | 2021-02-05 14:40:16 -0500 |
|---|---|---|
| committer | Yaxun (Sam) Liu <yaxun.liu@amd.com> | 2021-02-23 22:34:45 -0500 |
| commit | a3ce7f5cd2ae236bec7752e343f4b63ddda7ebe7 (patch) | |
| tree | ede44cfe97ed0a431125c5d459c411875d068dcb /clang/lib/CodeGen/CodeGenModule.cpp | |
| parent | eb55203e009ec98351f2be6e5655b7178b604920 (diff) | |
[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
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
| -rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 33 |
1 files changed, 15 insertions, 18 deletions
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<HIPManagedAttr>() && + 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<HIPManagedAttr>(); - const VarDecl *InitDecl; - const Expr *InitExpr = - IsHIPManagedVarOnDevice ? nullptr : D->getAnyInitializer(InitDecl); + const Expr *InitExpr = D->getAnyInitializer(InitDecl); Optional<ConstantEmitter> emitter; @@ -4158,15 +4158,15 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>(); // 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<HIPManagedAttr>() && (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDASharedAttr>()); bool IsCUDADeviceShadowVar = - getLangOpts().CUDAIsDevice && + getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() && (D->getType()->isCUDADeviceBuiltinSurfaceType() || - D->getType()->isCUDADeviceBuiltinTextureType() || - D->hasAttr<HIPManagedAttr>()); + 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); |
