@@ -569,13 +569,13 @@ bool CIRGenModule::shouldEmitCUDAGlobalVar(const VarDecl *global) const {
569
569
// their device-side incarnations.
570
570
571
571
if (global->hasAttr <CUDAConstantAttr>() ||
572
- global->hasAttr <CUDASharedAttr>() ||
573
572
global->getType ()->isCUDADeviceBuiltinSurfaceType () ||
574
573
global->getType ()->isCUDADeviceBuiltinTextureType ()) {
575
574
llvm_unreachable (" NYI" );
576
575
}
577
576
578
- return !langOpts.CUDAIsDevice || global->hasAttr <CUDADeviceAttr>();
577
+ return !langOpts.CUDAIsDevice || global->hasAttr <CUDADeviceAttr>() ||
578
+ global->hasAttr <CUDASharedAttr>();
579
579
}
580
580
581
581
void CIRGenModule::emitGlobal (GlobalDecl gd) {
@@ -598,8 +598,10 @@ void CIRGenModule::emitGlobal(GlobalDecl gd) {
598
598
assert (!global->hasAttr <CPUDispatchAttr>() && " NYI" );
599
599
600
600
if (langOpts.CUDA || langOpts.HIP ) {
601
- // clang uses the same flag when building HIP code
602
- if (langOpts.CUDAIsDevice ) {
601
+ if (const auto *vd = dyn_cast<VarDecl>(global)) {
602
+ if (!shouldEmitCUDAGlobalVar (vd))
603
+ return ;
604
+ } else if (langOpts.CUDAIsDevice ) {
603
605
// This will implicitly mark templates and their
604
606
// specializations as __host__ __device__.
605
607
if (langOpts.OffloadImplicitHostDeviceTemplates )
@@ -621,11 +623,6 @@ void CIRGenModule::emitGlobal(GlobalDecl gd) {
621
623
return ;
622
624
}
623
625
}
624
-
625
- if (const auto *vd = dyn_cast<VarDecl>(global)) {
626
- if (!shouldEmitCUDAGlobalVar (vd))
627
- return ;
628
- }
629
626
}
630
627
631
628
if (langOpts.OpenMP ) {
@@ -1394,7 +1391,7 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
1394
1391
d->getType ()->isCUDADeviceBuiltinTextureType ());
1395
1392
if (getLangOpts ().CUDA &&
1396
1393
(isCudaSharedVar || isCudaShadowVar || isCudaDeviceShadowVar))
1397
- assert ( 0 && " not implemented " );
1394
+ init = UndefAttr::get (& getMLIRContext (), convertType (d-> getType ()) );
1398
1395
else if (d->hasAttr <LoaderUninitializedAttr>())
1399
1396
assert (0 && " not implemented" );
1400
1397
else if (!initExpr) {
@@ -1490,11 +1487,19 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
1490
1487
cir::GlobalLinkageKind linkage =
1491
1488
getCIRLinkageVarDefinition (d, /* IsConstant=*/ false );
1492
1489
1493
- // TODO(cir):
1494
1490
// CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
1495
1491
// the device. [...]"
1496
1492
// CUDA B.2.2 "The __constant__ qualifier, optionally used together with
1497
1493
// __device__, declares a variable that: [...]
1494
+ if (langOpts.CUDA && langOpts.CUDAIsDevice ) {
1495
+ // __shared__ variables is not marked as externally initialized,
1496
+ // because they must not be initialized.
1497
+ if (linkage != cir::GlobalLinkageKind::InternalLinkage &&
1498
+ (d->hasAttr <CUDADeviceAttr>())) {
1499
+ gv->setAttr (CUDAExternallyInitializedAttr::getMnemonic (),
1500
+ CUDAExternallyInitializedAttr::get (&getMLIRContext ()));
1501
+ }
1502
+ }
1498
1503
1499
1504
// Set initializer and finalize emission
1500
1505
CIRGenModule::setInitializer (gv, init);
0 commit comments