@@ -637,11 +637,11 @@ mlir::Value CIRGenModule::getGlobalValue(const Decl *D) {
637
637
return CurCGF->symbolTable .lookup (D);
638
638
}
639
639
640
- mlir::cir::GlobalOp CIRGenModule::createGlobalOp (CIRGenModule &CGM,
641
- mlir::Location loc,
642
- StringRef name, mlir::Type t,
643
- bool isCst ,
644
- mlir::Operation *insertPoint) {
640
+ mlir::cir::GlobalOp
641
+ CIRGenModule::createGlobalOp (CIRGenModule &CGM, mlir::Location loc,
642
+ StringRef name, mlir::Type t, bool isCst ,
643
+ mlir::cir::AddressSpaceAttr addrSpace ,
644
+ mlir::Operation *insertPoint) {
645
645
mlir::cir::GlobalOp g;
646
646
auto &builder = CGM.getBuilder ();
647
647
{
@@ -655,7 +655,8 @@ mlir::cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &CGM,
655
655
if (curCGF)
656
656
builder.setInsertionPoint (curCGF->CurFn );
657
657
658
- g = builder.create <mlir::cir::GlobalOp>(loc, name, t, isCst);
658
+ g = builder.create <mlir::cir::GlobalOp>(
659
+ loc, name, t, isCst, GlobalLinkageKind::ExternalLinkage, addrSpace);
659
660
if (!curCGF) {
660
661
if (insertPoint)
661
662
CGM.getModule ().insert (insertPoint, g);
@@ -742,6 +743,12 @@ void CIRGenModule::replaceGlobal(mlir::cir::GlobalOp Old,
742
743
// If the types does not match, update all references to Old to the new type.
743
744
auto OldTy = Old.getSymType ();
744
745
auto NewTy = New.getSymType ();
746
+ mlir::cir::AddressSpaceAttr oldAS = Old.getAddrSpaceAttr ();
747
+ mlir::cir::AddressSpaceAttr newAS = New.getAddrSpaceAttr ();
748
+ // TODO(cir): If the AS differs, we should also update all references.
749
+ if (oldAS != newAS) {
750
+ llvm_unreachable (" NYI" );
751
+ }
745
752
if (OldTy != NewTy) {
746
753
auto OldSymUses = Old.getSymbolUses (theModule.getOperation ());
747
754
if (OldSymUses.has_value ()) {
@@ -809,7 +816,7 @@ void CIRGenModule::setTLSMode(mlir::Operation *Op, const VarDecl &D) const {
809
816
// / mangled name but some other type.
810
817
mlir::cir::GlobalOp
811
818
CIRGenModule::getOrCreateCIRGlobal (StringRef MangledName, mlir::Type Ty,
812
- LangAS AddrSpace , const VarDecl *D,
819
+ LangAS langAS , const VarDecl *D,
813
820
ForDefinition_t IsForDefinition) {
814
821
// Lookup the entry, lazily creating it if necessary.
815
822
mlir::cir::GlobalOp Entry;
@@ -818,8 +825,9 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
818
825
Entry = dyn_cast_or_null<mlir::cir::GlobalOp>(V);
819
826
}
820
827
821
- // unsigned TargetAS = astCtx.getTargetAddressSpace(AddrSpace );
828
+ mlir::cir::AddressSpaceAttr cirAS = builder. getAddrSpaceAttr (langAS );
822
829
if (Entry) {
830
+ auto entryCIRAS = Entry.getAddrSpaceAttr ();
823
831
if (WeakRefReferences.erase (Entry)) {
824
832
if (D && !D->hasAttr <WeakAttr>()) {
825
833
auto LT = mlir::cir::GlobalLinkageKind::ExternalLinkage;
@@ -837,8 +845,7 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
837
845
if (langOpts.OpenMP && !langOpts.OpenMPSimd && D)
838
846
getOpenMPRuntime ().registerTargetGlobalVariable (D, Entry);
839
847
840
- // TODO(cir): check TargetAS matches Entry address space
841
- if (Entry.getSymType () == Ty && !MissingFeatures::addressSpaceInGlobalVar ())
848
+ if (Entry.getSymType () == Ty && entryCIRAS == cirAS)
842
849
return Entry;
843
850
844
851
// If there are two attempts to define the same mangled name, issue an
@@ -867,14 +874,16 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
867
874
868
875
// TODO(cir): LLVM codegen makes sure the result is of the correct type
869
876
// by issuing a address space cast.
877
+ if (entryCIRAS != cirAS)
878
+ llvm_unreachable (" NYI" );
870
879
871
880
// (If global is requested for a definition, we always need to create a new
872
881
// global, not just return a bitcast.)
873
882
if (!IsForDefinition)
874
883
return Entry;
875
884
}
876
885
877
- // TODO(cir): auto DAddrSpace = GetGlobalVarAddressSpace(D );
886
+ auto declCIRAS = builder. getAddrSpaceAttr ( getGlobalVarAddressSpace (D) );
878
887
// TODO(cir): do we need to strip pointer casts for Entry?
879
888
880
889
auto loc = getLoc (D->getSourceRange ());
@@ -883,6 +892,7 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
883
892
// mark it as such.
884
893
auto GV = CIRGenModule::createGlobalOp (*this , loc, MangledName, Ty,
885
894
/* isConstant=*/ false ,
895
+ /* addrSpace=*/ declCIRAS,
886
896
/* insertPoint=*/ Entry.getOperation ());
887
897
888
898
// If we already created a global with the same mangled name (but different
@@ -992,8 +1002,7 @@ mlir::Value CIRGenModule::getAddrOfGlobalVar(const VarDecl *D, mlir::Type Ty,
992
1002
993
1003
bool tlsAccess = D->getTLSKind () != VarDecl::TLS_None;
994
1004
auto g = buildGlobal (D, Ty, IsForDefinition);
995
- auto ptrTy =
996
- mlir::cir::PointerType::get (builder.getContext (), g.getSymType ());
1005
+ auto ptrTy = builder.getPointerTo (g.getSymType (), g.getAddrSpaceAttr ());
997
1006
return builder.create <mlir::cir::GetGlobalOp>(
998
1007
getLoc (D->getSourceRange ()), ptrTy, g.getSymName (), tlsAccess);
999
1008
}
@@ -1076,7 +1085,8 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D,
1076
1085
// If this is OpenMP device, check if it is legal to emit this global
1077
1086
// normally.
1078
1087
QualType ASTTy = D->getType ();
1079
- if (getLangOpts ().OpenCL || getLangOpts ().OpenMPIsTargetDevice )
1088
+ if ((getLangOpts ().OpenCL && ASTTy->isSamplerT ()) ||
1089
+ getLangOpts ().OpenMPIsTargetDevice )
1080
1090
llvm_unreachable (" not implemented" );
1081
1091
1082
1092
// TODO(cir): LLVM's codegen uses a llvm::TrackingVH here. Is that
@@ -1409,7 +1419,7 @@ LangAS CIRGenModule::getLangTempAllocaAddressSpace() const {
1409
1419
if (getLangOpts ().OpenCL )
1410
1420
return LangAS::opencl_private;
1411
1421
if (getLangOpts ().SYCLIsDevice || getLangOpts ().CUDAIsDevice ||
1412
- (getLangOpts ().OpenMP && getLangOpts ().OpenMPIsTargetDevice ))
1422
+ (getLangOpts ().OpenMP && getLangOpts ().OpenMPIsTargetDevice ))
1413
1423
llvm_unreachable (" NYI" );
1414
1424
return LangAS::Default;
1415
1425
}
@@ -3100,3 +3110,25 @@ mlir::cir::SourceLanguage CIRGenModule::getCIRSourceLanguage() {
3100
3110
// TODO(cir): support remaining source languages.
3101
3111
llvm_unreachable (" CIR does not yet support the given source language" );
3102
3112
}
3113
+
3114
+ LangAS CIRGenModule::getGlobalVarAddressSpace (const VarDecl *D) {
3115
+ if (langOpts.OpenCL ) {
3116
+ LangAS AS = D ? D->getType ().getAddressSpace () : LangAS::opencl_global;
3117
+ assert (AS == LangAS::opencl_global || AS == LangAS::opencl_global_device ||
3118
+ AS == LangAS::opencl_global_host || AS == LangAS::opencl_constant ||
3119
+ AS == LangAS::opencl_local || AS >= LangAS::FirstTargetAddressSpace);
3120
+ return AS;
3121
+ }
3122
+
3123
+ if (langOpts.SYCLIsDevice &&
3124
+ (!D || D->getType ().getAddressSpace () == LangAS::Default))
3125
+ llvm_unreachable (" NYI" );
3126
+
3127
+ if (langOpts.CUDA && langOpts.CUDAIsDevice )
3128
+ llvm_unreachable (" NYI" );
3129
+
3130
+ if (langOpts.OpenMP )
3131
+ llvm_unreachable (" NYI" );
3132
+
3133
+ return getTargetCIRGenInfo ().getGlobalVarAddressSpace (*this , D);
3134
+ }
0 commit comments