@@ -605,20 +605,10 @@ llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() {
605
605
uint64_t VarSize =
606
606
CGM.getDataLayout ().getTypeAllocSize (Var->getValueType ());
607
607
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 ()));
622
612
llvm::Value *Args[] = {
623
613
&GpuBinaryHandlePtr,
624
614
ManagedVar,
@@ -1093,7 +1083,9 @@ void CGNVCUDARuntime::transformManagedVars() {
1093
1083
: llvm::ConstantPointerNull::get (Var->getType ()),
1094
1084
/* Name=*/ " " , /* InsertBefore=*/ nullptr ,
1095
1085
llvm::GlobalVariable::NotThreadLocal,
1096
- CGM.getContext ().getTargetAddressSpace (LangAS::cuda_device));
1086
+ CGM.getContext ().getTargetAddressSpace (CGM.getLangOpts ().CUDAIsDevice
1087
+ ? LangAS::cuda_device
1088
+ : LangAS::Default));
1097
1089
ManagedVar->setDSOLocal (Var->isDSOLocal ());
1098
1090
ManagedVar->setVisibility (Var->getVisibility ());
1099
1091
ManagedVar->setExternallyInitialized (true );
@@ -1102,7 +1094,7 @@ void CGNVCUDARuntime::transformManagedVars() {
1102
1094
Var->setName (Twine (ManagedVar->getName ()) + " .managed" );
1103
1095
// Keep managed variables even if they are not used in device code since
1104
1096
// they need to be allocated by the runtime.
1105
- if (!Var->isDeclaration ()) {
1097
+ if (CGM. getLangOpts (). CUDAIsDevice && !Var->isDeclaration ()) {
1106
1098
assert (!ManagedVar->isDeclaration ());
1107
1099
CGM.addCompilerUsedGlobal (Var);
1108
1100
CGM.addCompilerUsedGlobal (ManagedVar);
@@ -1160,9 +1152,8 @@ void CGNVCUDARuntime::createOffloadingEntries() {
1160
1152
1161
1153
// Returns module constructor to be added.
1162
1154
llvm::Function *CGNVCUDARuntime::finalizeModule () {
1155
+ transformManagedVars ();
1163
1156
if (CGM.getLangOpts ().CUDAIsDevice ) {
1164
- transformManagedVars ();
1165
-
1166
1157
// Mark ODR-used device variables as compiler used to prevent it from being
1167
1158
// eliminated by optimization. This is necessary for device variables
1168
1159
// ODR-used by host functions. Sema correctly marks them as ODR-used no
0 commit comments