@@ -605,20 +605,10 @@ llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() {
605605 uint64_t VarSize =
606606 CGM.getDataLayout ().getTypeAllocSize (Var->getValueType ());
607607 if (Info.Flags .isManaged ()) {
608- auto *ManagedVar = new llvm::GlobalVariable (
609- CGM.getModule (), Var->getType (),
610- /* isConstant=*/ false , Var->getLinkage (),
611- /* Init=*/ Var->isDeclaration ()
612- ? nullptr
613- : llvm::ConstantPointerNull::get (Var->getType ()),
614- /* Name=*/ " " , /* InsertBefore=*/ nullptr ,
615- llvm::GlobalVariable::NotThreadLocal);
616- ManagedVar->setDSOLocal (Var->isDSOLocal ());
617- ManagedVar->setVisibility (Var->getVisibility ());
618- ManagedVar->setExternallyInitialized (true );
619- ManagedVar->takeName (Var);
620- Var->setName (Twine (ManagedVar->getName () + " .managed" ));
621- replaceManagedVar (Var, ManagedVar);
608+ assert (Var->getName ().ends_with (" .managed" ) &&
609+ " HIP managed variables not transformed" );
610+ auto *ManagedVar = CGM.getModule ().getNamedGlobal (
611+ Var->getName ().drop_back (StringRef (" .managed" ).size ()));
622612 llvm::Value *Args[] = {
623613 &GpuBinaryHandlePtr,
624614 ManagedVar,
@@ -1093,7 +1083,9 @@ void CGNVCUDARuntime::transformManagedVars() {
10931083 : llvm::ConstantPointerNull::get (Var->getType ()),
10941084 /* Name=*/ " " , /* InsertBefore=*/ nullptr ,
10951085 llvm::GlobalVariable::NotThreadLocal,
1096- CGM.getContext ().getTargetAddressSpace (LangAS::cuda_device));
1086+ CGM.getContext ().getTargetAddressSpace (CGM.getLangOpts ().CUDAIsDevice
1087+ ? LangAS::cuda_device
1088+ : LangAS::Default));
10971089 ManagedVar->setDSOLocal (Var->isDSOLocal ());
10981090 ManagedVar->setVisibility (Var->getVisibility ());
10991091 ManagedVar->setExternallyInitialized (true );
@@ -1102,7 +1094,7 @@ void CGNVCUDARuntime::transformManagedVars() {
11021094 Var->setName (Twine (ManagedVar->getName ()) + " .managed" );
11031095 // Keep managed variables even if they are not used in device code since
11041096 // they need to be allocated by the runtime.
1105- if (!Var->isDeclaration ()) {
1097+ if (CGM. getLangOpts (). CUDAIsDevice && !Var->isDeclaration ()) {
11061098 assert (!ManagedVar->isDeclaration ());
11071099 CGM.addCompilerUsedGlobal (Var);
11081100 CGM.addCompilerUsedGlobal (ManagedVar);
@@ -1160,9 +1152,8 @@ void CGNVCUDARuntime::createOffloadingEntries() {
11601152
11611153// Returns module constructor to be added.
11621154llvm::Function *CGNVCUDARuntime::finalizeModule () {
1155+ transformManagedVars ();
11631156 if (CGM.getLangOpts ().CUDAIsDevice ) {
1164- transformManagedVars ();
1165-
11661157 // Mark ODR-used device variables as compiler used to prevent it from being
11671158 // eliminated by optimization. This is necessary for device variables
11681159 // ODR-used by host functions. Sema correctly marks them as ODR-used no
0 commit comments