1717#include " CIRGenOpenMPRuntime.h"
1818#include " TargetInfo.h"
1919#include " clang/AST/ASTContext.h"
20+ #include " clang/Basic/SyncScope.h"
2021#include " clang/CIR/Dialect/IR/CIRAttrs.h"
2122#include " clang/CIR/Dialect/IR/CIRDataLayout.h"
2223#include " clang/CIR/Dialect/IR/CIRDialect.h"
@@ -350,6 +351,20 @@ static cir::IntAttr extractIntAttr(mlir::Value v) {
350351 return {};
351352}
352353
354+ // Maps SyncScope::SingleScope to SyncScopeKind::SingleThread,
355+ // SyncScope::SystemScope to SyncScopeKind::System,
356+ // and asserts (llvm_unreachable) for anything else.
357+ static cir::SyncScopeKind convertSyncScopeToCIR (clang::SyncScope scope) {
358+ switch (scope) {
359+ case clang::SyncScope::SingleScope:
360+ return cir::SyncScopeKind::SingleThread;
361+ case clang::SyncScope::SystemScope:
362+ return cir::SyncScopeKind::System;
363+ default :
364+ llvm_unreachable (" NYI" );
365+ }
366+ }
367+
353368// Inspect a value that is the strong/weak flag for a compare-exchange. If it
354369// is a constant of intergral or boolean type, set `val` to the constant's
355370// boolean value and return true. Otherwise leave `val` unchanged and return
@@ -418,7 +433,7 @@ static void emitAtomicCmpXchg(CIRGenFunction &CGF, AtomicExpr *E, bool IsWeak,
418433 Address Val2, uint64_t Size,
419434 cir::MemOrder SuccessOrder,
420435 cir::MemOrder FailureOrder,
421- cir::MemScopeKind Scope) {
436+ cir::SyncScopeKind Scope) {
422437 auto &builder = CGF.getBuilder ();
423438 auto loc = CGF.getLoc (E->getSourceRange ());
424439 auto Expected = builder.createLoad (loc, Val1);
@@ -428,7 +443,7 @@ static void emitAtomicCmpXchg(CIRGenFunction &CGF, AtomicExpr *E, bool IsWeak,
428443 builder, loc, Expected.getType (), boolTy, Ptr.getPointer (), Expected,
429444 Desired, cir::MemOrderAttr::get (&CGF.getMLIRContext (), SuccessOrder),
430445 cir::MemOrderAttr::get (&CGF.getMLIRContext (), FailureOrder),
431- cir::MemScopeKindAttr ::get (&CGF.getMLIRContext (), Scope),
446+ cir::SyncScopeKindAttr ::get (&CGF.getMLIRContext (), Scope),
432447 builder.getI64IntegerAttr (Ptr.getAlignment ().getAsAlign ().value ()));
433448 cmpxchg.setIsVolatile (E->isVolatile ());
434449 cmpxchg.setWeak (IsWeak);
@@ -456,7 +471,7 @@ static void emitAtomicCmpXchg(CIRGenFunction &CGF, AtomicExpr *E, bool IsWeak,
456471static void emitAtomicCmpXchgFailureSet (
457472 CIRGenFunction &CGF, AtomicExpr *E, bool IsWeak, Address Dest, Address Ptr,
458473 Address Val1, Address Val2, mlir::Value FailureOrderVal, uint64_t Size,
459- cir::MemOrder SuccessOrder, cir::MemScopeKind Scope) {
474+ cir::MemOrder SuccessOrder, cir::SyncScopeKind Scope) {
460475
461476 cir::MemOrder FailureOrder;
462477 if (auto ordAttr = extractIntAttr (FailureOrderVal)) {
@@ -546,7 +561,7 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *E, Address Dest,
546561 Address Ptr, Address Val1, Address Val2,
547562 mlir::Value IsWeak, mlir::Value FailureOrder,
548563 uint64_t Size, cir::MemOrder Order,
549- cir::MemScopeKind Scope) {
564+ cir::SyncScopeKind Scope) {
550565 assert (!cir::MissingFeatures::syncScopeID ());
551566 StringRef Op;
552567
@@ -594,9 +609,7 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *E, Address Dest,
594609 case AtomicExpr::AO__scoped_atomic_load_n:
595610 case AtomicExpr::AO__scoped_atomic_load: {
596611 auto load = builder.createLoad (loc, Ptr);
597- // FIXME(cir): add scope information.
598- assert (!cir::MissingFeatures::syncScopeID ());
599- load.setMemOrder (Order);
612+ load.setAtomic (Order, Scope);
600613 load.setIsVolatile (E->isVolatile ());
601614
602615 // TODO(cir): this logic should be part of createStore, but doing so
@@ -748,7 +761,7 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *E, Address Dest,
748761 case AtomicExpr::AO__atomic_test_and_set: {
749762 auto op = cir::AtomicTestAndSetOp::create (
750763 builder, loc, Ptr.getPointer (), Order,
751- cir::MemScopeKindAttr ::get (&CGF.getMLIRContext (), Scope),
764+ cir::SyncScopeKindAttr ::get (&CGF.getMLIRContext (), Scope),
752765 builder.getI64IntegerAttr (Ptr.getAlignment ().getQuantity ()),
753766 E->isVolatile ());
754767 builder.createStore (loc, op, Dest);
@@ -758,7 +771,7 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *E, Address Dest,
758771 case AtomicExpr::AO__atomic_clear: {
759772 cir::AtomicClearOp::create (
760773 builder, loc, Ptr.getPointer (), Order,
761- cir::MemScopeKindAttr ::get (&CGF.getMLIRContext (), Scope),
774+ cir::SyncScopeKindAttr ::get (&CGF.getMLIRContext (), Scope),
762775 builder.getI64IntegerAttr (Ptr.getAlignment ().getQuantity ()),
763776 E->isVolatile ());
764777 return ;
@@ -813,14 +826,17 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *Expr, Address Dest,
813826 if (!ScopeModel) {
814827 assert (!cir::MissingFeatures::syncScopeID ());
815828 emitAtomicOp (CGF, Expr, Dest, Ptr, Val1, Val2, IsWeak, FailureOrder, Size,
816- Order, cir::MemScopeKind ::System);
829+ Order, cir::SyncScopeKind ::System);
817830 return ;
818831 }
819832
820833 // Handle constant scope.
821- if (extractIntAttr (Scope)) {
834+ if (auto scopeAttr = extractIntAttr (Scope)) {
822835 assert (!cir::MissingFeatures::syncScopeID ());
823- llvm_unreachable (" NYI" );
836+ auto mappedScope =
837+ convertSyncScopeToCIR (ScopeModel->map (scopeAttr.getUInt ()));
838+ emitAtomicOp (CGF, Expr, Dest, Ptr, Val1, Val2, IsWeak, FailureOrder, Size,
839+ Order, mappedScope);
824840 return ;
825841 }
826842
0 commit comments