@@ -510,6 +510,23 @@ const ABIInfo &CIRGenModule::getABIInfo() {
510
510
return getTargetCIRGenInfo ().getABIInfo ();
511
511
}
512
512
513
+ bool CIRGenModule::shouldEmitCUDAGlobalVar (const VarDecl *global) const {
514
+ assert (langOpts.CUDA && " Should not be called by non-CUDA languages" );
515
+ // We need to emit host-side 'shadows' for all global
516
+ // device-side variables because the CUDA runtime needs their
517
+ // size and host-side address in order to provide access to
518
+ // their device-side incarnations.
519
+
520
+ if (global->hasAttr <CUDAConstantAttr>() ||
521
+ global->hasAttr <CUDASharedAttr>() ||
522
+ global->getType ()->isCUDADeviceBuiltinSurfaceType () ||
523
+ global->getType ()->isCUDADeviceBuiltinTextureType ()) {
524
+ llvm_unreachable (" NYI" );
525
+ }
526
+
527
+ return !langOpts.CUDAIsDevice || global->hasAttr <CUDADeviceAttr>();
528
+ }
529
+
513
530
void CIRGenModule::emitGlobal (GlobalDecl GD) {
514
531
llvm::TimeTraceScope scope (" build CIR Global" , [&]() -> std::string {
515
532
auto *ND = dyn_cast<NamedDecl>(GD.getDecl ());
@@ -554,8 +571,10 @@ void CIRGenModule::emitGlobal(GlobalDecl GD) {
554
571
}
555
572
}
556
573
557
- if (dyn_cast<VarDecl>(Global))
558
- llvm_unreachable (" NYI" );
574
+ if (const auto *VD = dyn_cast<VarDecl>(Global)) {
575
+ if (!shouldEmitCUDAGlobalVar (VD))
576
+ return ;
577
+ }
559
578
}
560
579
561
580
if (langOpts.OpenMP ) {
@@ -599,7 +618,6 @@ void CIRGenModule::emitGlobal(GlobalDecl GD) {
599
618
return ;
600
619
}
601
620
} else {
602
- assert (!langOpts.CUDA && " NYI" );
603
621
const auto *VD = cast<VarDecl>(Global);
604
622
assert (VD->isFileVarDecl () && " Cannot emit local var decl as global." );
605
623
if (VD->isThisDeclarationADefinition () != VarDecl::Definition &&
@@ -1149,8 +1167,11 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
1149
1167
1150
1168
// External HIP managed variables needed to be recorded for transformation
1151
1169
// in both device and host compilations.
1152
- if (getLangOpts ().CUDA )
1153
- assert (0 && " not implemented" );
1170
+ // External HIP managed variables needed to be recorded for transformation
1171
+ // in both device and host compilations.
1172
+ if (getLangOpts ().CUDA && D && D->hasAttr <HIPManagedAttr>() &&
1173
+ D->hasExternalStorage ())
1174
+ llvm_unreachable (" NYI" );
1154
1175
}
1155
1176
1156
1177
// TODO(cir): address space cast when needed for DAddrSpace.
@@ -1422,9 +1443,6 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *D,
1422
1443
// the device. [...]"
1423
1444
// CUDA B.2.2 "The __constant__ qualifier, optionally used together with
1424
1445
// __device__, declares a variable that: [...]
1425
- if (GV && getLangOpts ().CUDA ) {
1426
- assert (0 && " not implemented" );
1427
- }
1428
1446
1429
1447
// Set initializer and finalize emission
1430
1448
CIRGenModule::setInitializer (GV, Init);
@@ -4012,9 +4030,17 @@ LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *D) {
4012
4030
llvm_unreachable (" NYI" );
4013
4031
4014
4032
if (langOpts.CUDA && langOpts.CUDAIsDevice ) {
4015
- if (D && D->hasAttr <CUDASharedAttr>())
4016
- return LangAS::cuda_shared;
4017
- llvm_unreachable (" NYI" );
4033
+ if (D) {
4034
+ if (D->hasAttr <CUDAConstantAttr>())
4035
+ return LangAS::cuda_constant;
4036
+ if (D->hasAttr <CUDASharedAttr>())
4037
+ return LangAS::cuda_shared;
4038
+ if (D->hasAttr <CUDADeviceAttr>())
4039
+ return LangAS::cuda_device;
4040
+ if (D->getType ().isConstQualified ())
4041
+ return LangAS::cuda_constant;
4042
+ }
4043
+ return LangAS::cuda_device;
4018
4044
}
4019
4045
4020
4046
if (langOpts.OpenMP )
0 commit comments