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