1818
1919#include " clang/AST/ASTContext.h"
2020#include " clang/AST/ASTLambda.h"
21+ #include " clang/AST/Attrs.inc"
2122#include " clang/AST/DeclBase.h"
2223#include " clang/AST/DeclOpenACC.h"
2324#include " clang/AST/GlobalDecl.h"
@@ -58,7 +59,7 @@ static CIRGenCXXABI *createCXXABI(CIRGenModule &cgm) {
5859 case TargetCXXABI::WebAssembly:
5960 case TargetCXXABI::XL:
6061 case TargetCXXABI::Microsoft:
61- cgm.errorNYI (" C++ ABI kind not yet implemented " );
62+ cgm.errorNYI (" createCXXABI: C++ ABI kind" );
6263 return nullptr ;
6364 }
6465
@@ -749,7 +750,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
749750 cir::GlobalOp entry;
750751 if (mlir::Operation *v = getGlobalValue (mangledName)) {
751752 if (!isa<cir::GlobalOp>(v))
752- errorNYI (d->getSourceRange (), " global with non-GlobalOp type" );
753+ errorNYI (d->getSourceRange (),
754+ " getOrCreateCIRGlobal: global with non-GlobalOp type" );
753755 entry = cast<cir::GlobalOp>(v);
754756 }
755757
@@ -770,7 +772,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
770772 // recognizing the global as a declaration, for now only check if
771773 // initializer is present.
772774 if (isForDefinition && !entry.isDeclaration ()) {
773- errorNYI (d->getSourceRange (), " global with conflicting type" );
775+ errorNYI (d->getSourceRange (),
776+ " getOrCreateCIRGlobal: global with conflicting type" );
774777 }
775778
776779 // Address space check removed because it is unnecessary because CIR records
@@ -820,15 +823,16 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
820823 // Handle things which are present even on external declarations.
821824 if (d) {
822825 if (langOpts.OpenMP && !langOpts.OpenMPSimd )
823- errorNYI (d->getSourceRange (), " OpenMP target global variable" );
826+ errorNYI (d->getSourceRange (),
827+ " getOrCreateCIRGlobal: OpenMP target global variable" );
824828
825829 gv.setAlignmentAttr (getSize (astContext.getDeclAlign (d)));
826830
827831 setLinkageForGV (gv, d);
828832
829833 if (d->getTLSKind ()) {
830834 if (d->getTLSKind () == VarDecl::TLS_Dynamic)
831- errorNYI (d->getSourceRange (), " TLS dynamic" );
835+ errorNYI (d->getSourceRange (), " getOrCreateCIRGlobal: TLS dynamic" );
832836 setTLSMode (gv, *d);
833837 }
834838
@@ -837,25 +841,41 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
837841 // If required by the ABI, treat declarations of static data members with
838842 // inline initializers as definitions.
839843 if (astContext.isMSStaticDataMemberInlineDefinition (d))
840- errorNYI (d->getSourceRange (), " MS static data member inline definition" );
844+ errorNYI (d->getSourceRange (),
845+ " getOrCreateCIRGlobal: MS static data member inline definition" );
841846
842847 assert (!cir::MissingFeatures::opGlobalSection ());
843848 gv.setGlobalVisibilityAttr (getGlobalVisibilityAttrFromDecl (d));
844849
845850 // Handle XCore specific ABI requirements.
846851 if (getTriple ().getArch () == llvm::Triple::xcore)
847- errorNYI (d->getSourceRange (), " XCore specific ABI requirements" );
852+ errorNYI (d->getSourceRange (),
853+ " getOrCreateCIRGlobal: XCore specific ABI requirements" );
848854
849855 // Check if we a have a const declaration with an initializer, we may be
850856 // able to emit it as available_externally to expose it's value to the
851857 // optimizer.
852858 if (getLangOpts ().CPlusPlus && gv.isPublic () &&
853859 d->getType ().isConstQualified () && gv.isDeclaration () &&
854860 !d->hasDefinition () && d->hasInit () && !d->hasAttr <DLLImportAttr>())
861+ errorNYI (
862+ d->getSourceRange (),
863+ " getOrCreateCIRGlobal: external const declaration with initializer" );
864+ }
865+
866+ if (d &&
867+ d->isThisDeclarationADefinition (astContext) == VarDecl::DeclarationOnly) {
868+ getTargetCIRGenInfo ().setTargetAttributes (d, gv, *this );
869+ // TODO(cir): set target attributes
870+ // External HIP managed variables needed to be recorded for transformation
871+ // in both device and host compilations.
872+ if (getLangOpts ().CUDA && d && d->hasAttr <HIPManagedAttr>() &&
873+ d->hasExternalStorage ())
855874 errorNYI (d->getSourceRange (),
856- " external const declaration with initializer " );
875+ " getOrCreateCIRGlobal: HIP managed attribute " );
857876 }
858877
878+ assert (!cir::MissingFeatures::addressSpace ());
859879 return gv;
860880}
861881
@@ -907,7 +927,8 @@ cir::GlobalViewAttr CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *d) {
907927void CIRGenModule::emitGlobalVarDefinition (const clang::VarDecl *vd,
908928 bool isTentative) {
909929 if (getLangOpts ().OpenCL || getLangOpts ().OpenMPIsTargetDevice ) {
910- errorNYI (vd->getSourceRange (), " emit OpenCL/OpenMP global variable" );
930+ errorNYI (vd->getSourceRange (),
931+ " emitGlobalVarDefinition: emit OpenCL/OpenMP global variable" );
911932 return ;
912933 }
913934
@@ -939,9 +960,19 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
939960
940961 assert (!cir::MissingFeatures::cudaSupport ());
941962
942- if (vd->hasAttr <LoaderUninitializedAttr>()) {
943- errorNYI (vd->getSourceRange (), " loader uninitialized attribute" );
944- return ;
963+ // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
964+ // as part of their declaration." Sema has already checked for
965+ // error cases, so we just need to set Init to UndefValue.
966+ bool isCUDASharedVar =
967+ getLangOpts ().CUDAIsDevice && vd->hasAttr <CUDASharedAttr>();
968+ // TODO(cir): implement isCUDAShadowVar and isCUDADeviceShadowVar, reference:
969+ // OGCG
970+
971+ if (getLangOpts ().CUDA && isCUDASharedVar) {
972+ init = cir::UndefAttr::get (&getMLIRContext (), convertType (vd->getType ()));
973+ } else if (vd->hasAttr <LoaderUninitializedAttr>()) {
974+ errorNYI (vd->getSourceRange (),
975+ " emitGlobalVarDefinition: loader uninitialized attribute" );
945976 } else if (!initExpr) {
946977 // This is a tentative definition; tentative definitions are
947978 // implicitly initialized with { 0 }.
@@ -964,12 +995,14 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
964995
965996 if (getLangOpts ().CPlusPlus ) {
966997 if (initDecl->hasFlexibleArrayInit (astContext))
967- errorNYI (vd->getSourceRange (), " flexible array initializer" );
998+ errorNYI (vd->getSourceRange (),
999+ " emitGlobalVarDefinition: flexible array initializer" );
9681000 init = builder.getZeroInitAttr (convertType (qt));
9691001 if (!isDefinitionAvailableExternally)
9701002 needsGlobalCtor = true ;
9711003 } else {
972- errorNYI (vd->getSourceRange (), " static initializer" );
1004+ errorNYI (vd->getSourceRange (),
1005+ " emitGlobalVarDefinition: static initializer" );
9731006 }
9741007 } else {
9751008 init = initializer;
@@ -982,7 +1015,9 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
9821015
9831016 mlir::Type initType;
9841017 if (mlir::isa<mlir::SymbolRefAttr>(init)) {
985- errorNYI (vd->getSourceRange (), " global initializer is a symbol reference" );
1018+ errorNYI (
1019+ vd->getSourceRange (),
1020+ " emitGlobalVarDefinition: global initializer is a symbol reference" );
9861021 return ;
9871022 } else {
9881023 assert (mlir::isa<mlir::TypedAttr>(init) && " This should have a type" );
@@ -996,18 +1031,49 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
9961031 // TODO(cir): Strip off pointer casts from Entry if we get them?
9971032
9981033 if (!gv || gv.getSymType () != initType) {
999- errorNYI (vd->getSourceRange (), " global initializer with type mismatch" );
1034+ errorNYI (vd->getSourceRange (),
1035+ " emitGlobalVarDefinition: global initializer with type mismatch" );
10001036 return ;
10011037 }
10021038
10031039 assert (!cir::MissingFeatures::maybeHandleStaticInExternC ());
10041040
10051041 if (vd->hasAttr <AnnotateAttr>()) {
1006- errorNYI (vd->getSourceRange (), " annotate global variable" );
1042+ errorNYI (vd->getSourceRange (),
1043+ " emitGlobalVarDefinition: annotate global variable" );
10071044 }
10081045
1046+ // Set CIR's linkage type as appropriate.
1047+ cir::GlobalLinkageKind linkage =
1048+ getCIRLinkageVarDefinition (vd, /* IsConstant=*/ false );
1049+
1050+ // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
1051+ // the device. [...]"
1052+ // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
1053+ // __device__, declares a variable that: [...]
1054+ // Is accessible from all the threads within the grid and from the host
1055+ // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
1056+ // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
10091057 if (langOpts.CUDA ) {
1010- errorNYI (vd->getSourceRange (), " CUDA global variable" );
1058+ if (langOpts.CUDAIsDevice ) {
1059+ // __shared__ variables is not marked as externally initialized,
1060+ // because they must not be initialized.
1061+ if (linkage != cir::GlobalLinkageKind::InternalLinkage &&
1062+ !vd->isConstexpr () && !vd->getType ().isConstQualified () &&
1063+ (vd->hasAttr <CUDADeviceAttr>() || vd->hasAttr <CUDAConstantAttr>() ||
1064+ vd->getType ()->isCUDADeviceBuiltinSurfaceType () ||
1065+ vd->getType ()->isCUDADeviceBuiltinTextureType ())) {
1066+ gv->setAttr (cir::CUDAExternallyInitializedAttr::getMnemonic (),
1067+ cir::CUDAExternallyInitializedAttr::get (&getMLIRContext ()));
1068+ }
1069+ } else {
1070+ // TODO(cir):
1071+ // Adjust linkage of shadow variables in host compilation
1072+ // getCUDARuntime().internalizeDeviceSideVar(vd, linkage);
1073+ }
1074+ // TODO(cir):
1075+ // Handle variable registration
1076+ // getCUDARuntime().handleVarRegistration(vd, gv);
10111077 }
10121078
10131079 // Set initializer and finalize emission
@@ -1024,10 +1090,6 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
10241090 /* ExcludeDtor=*/ true )));
10251091 assert (!cir::MissingFeatures::opGlobalSection ());
10261092
1027- // Set CIR's linkage type as appropriate.
1028- cir::GlobalLinkageKind linkage =
1029- getCIRLinkageVarDefinition (vd, /* IsConstant=*/ false );
1030-
10311093 // Set CIR linkage and DLL storage class.
10321094 gv.setLinkage (linkage);
10331095 // FIXME(cir): setLinkage should likely set MLIR's visibility automatically.
0 commit comments