27 CharUnits atomicAlign;
30 bool useLibCall =
true;
35 AtomicInfo(CIRGenFunction &cgf, LValue &lvalue, mlir::Location loc)
36 : cgf(cgf), loc(loc) {
37 assert(!lvalue.isGlobalReg());
38 ASTContext &ctx = cgf.getContext();
39 if (lvalue.isSimple()) {
40 atomicTy = lvalue.getType();
41 if (
auto *ty = atomicTy->getAs<AtomicType>())
42 valueTy = ty->getValueType();
45 evaluationKind = cgf.getEvaluationKind(valueTy);
48 TypeInfo atomicTypeInfo = ctx.
getTypeInfo(atomicTy);
51 valueSizeInBits = valueTypeInfo.
Width;
52 atomicSizeInBits = atomicTypeInfo.
Width;
53 assert(valueSizeInBits <= atomicSizeInBits);
54 assert(valueAlignInBits <= atomicAlignInBits);
58 if (lvalue.getAlignment().isZero())
59 lvalue.setAlignment(atomicAlign);
61 this->lvalue = lvalue;
64 cgf.cgm.errorNYI(loc,
"AtomicInfo: non-simple lvalue");
67 atomicSizeInBits, ctx.
toBits(lvalue.getAlignment()));
70 QualType getValueType()
const {
return valueTy; }
71 QualType getAtomicType()
const {
return atomicTy; }
72 CharUnits getAtomicAlignment()
const {
return atomicAlign; }
74 mlir::Value getAtomicPointer()
const {
75 if (lvalue.isSimple())
76 return lvalue.getPointer();
80 bool shouldUseLibCall()
const {
return useLibCall; }
81 const LValue &getAtomicLValue()
const {
return lvalue; }
82 Address getAtomicAddress()
const {
84 if (lvalue.isSimple()) {
85 elemTy = lvalue.getAddress().getElementType();
88 cgf.cgm.errorNYI(loc,
"AtomicInfo::getAtomicAddress: non-simple lvalue");
90 return Address(getAtomicPointer(), elemTy, getAtomicAlignment());
99 bool hasPadding()
const {
return (valueSizeInBits != atomicSizeInBits); }
101 bool emitMemSetZeroIfNecessary()
const;
103 mlir::Value getScalarRValValueOrNull(RValue rvalue)
const;
107 Address castToAtomicIntPointer(Address addr)
const;
112 Address convertToAtomicIntPointer(Address addr, mlir::Location loc)
const;
115 RValue convertAtomicTempToRValue(Address addr, AggValueSlot resultSlot,
116 SourceLocation loc,
bool asValue)
const;
119 mlir::Value convertRValueToInt(RValue rvalue, mlir::Location loc,
120 bool cmpxchg =
false)
const;
122 RValue convertToValueOrAtomic(mlir::Value intVal, AggValueSlot resultSlot,
123 SourceLocation loc,
bool asValue,
124 bool cmpxchg =
false)
const;
127 void emitCopyIntoMemory(RValue rvalue)
const;
130 LValue projectValue()
const {
131 assert(lvalue.isSimple());
132 Address addr = getAtomicAddress();
134 addr = cgf.getBuilder().createGetMember(loc, addr,
"value",
136 cgf.cgm.getDataLayout());
139 return LValue::makeAddr(addr, getValueType(), lvalue.getBaseInfo());
144 RValue emitAtomicLoad(AggValueSlot resultSlot, SourceLocation loc,
145 bool asValue, cir::MemOrder order,
bool isVolatile);
148 Address materializeRValue(RValue rvalue, mlir::Location loc)
const;
151 Address createTempAlloca()
const;
154 bool requiresMemSetZero(mlir::Type ty)
const;
157 mlir::Value emitAtomicLoadOp(cir::MemOrder order,
bool isVolatile,
158 bool cmpxchg =
false);
175 bool isAMDGCN = triple.isAMDGCN();
176 bool isAMDSPIRV = triple.isSPIRV() && triple.getVendor() == llvm::Triple::AMD;
177 if (!isAMDGCN && !isAMDSPIRV)
181 mlir::UnitAttr unit = cgf.
getBuilder().getUnitAttr();
182 if (isAMDGCN &&
expr->threadPrivateMemoryAtomicsAreUndefined())
183 op->setAttr(cir::CIRDialect::getAMDGPUNoPrivateAddrSpaceAttrName(), unit);
187 if (!mlir::isa<cir::AtomicFetchOp, cir::AtomicXchgOp>(op))
192 op->setAttr(cir::CIRDialect::getAMDGPUNoFineGrainedMemoryAttrName(), unit);
194 op->setAttr(cir::CIRDialect::getAMDGPUNoRemoteMemoryAttrName(), unit);
196 auto fetchOp = mlir::dyn_cast<cir::AtomicFetchOp>(op);
199 fetchOp.getBinop() == cir::AtomicFetchKind::Add &&
200 mlir::isa<cir::SingleType>(fetchOp.getVal().getType()))
201 op->setAttr(cir::CIRDialect::getAMDGPUIgnoreDenormalModeAttrName(), unit);
223bool AtomicInfo::requiresMemSetZero(mlir::Type ty)
const {
229 switch (getEvaluationKind()) {
236 mlir::cast<cir::ComplexType>(ty).getElementType(),
237 atomicSizeInBits / 2);
242 llvm_unreachable(
"bad evaluation kind");
245Address AtomicInfo::convertToAtomicIntPointer(Address addr,
246 mlir::Location loc)
const {
249 if (sourceSizeInBits != atomicSizeInBits) {
252 Address tmp = createTempAlloca();
264 std::min(atomicSizeInBits, sourceSizeInBits) / 8));
268 return castToAtomicIntPointer(addr);
271RValue AtomicInfo::convertAtomicTempToRValue(Address addr,
272 AggValueSlot resultSlot,
274 bool asValue)
const {
275 if (lvalue.isSimple()) {
282 "AtomicInfo::convertAtomicTempToRValue: hasPadding");
292 loc,
"AtomicInfo::convertAtomicTempToRValue: lvalue is not simple");
296RValue AtomicInfo::emitAtomicLoad(AggValueSlot resultSlot, SourceLocation loc,
297 bool asValue, cir::MemOrder order,
300 if (shouldUseLibCall()) {
302 cgf.
cgm.
errorNYI(loc,
"emitAtomicLoad: emit atomic lib call");
307 mlir::Value loadOp = emitAtomicLoadOp(order, isVolatile);
315 return convertToValueOrAtomic(loadOp, resultSlot, loc, asValue);
318Address AtomicInfo::createTempAlloca()
const {
321 QualType tmpTy = (lvalue.isBitField() && valueSizeInBits > atomicSizeInBits)
325 cgf.
createMemTemp(tmpTy, getAtomicAlignment(), loc,
"atomic-temp");
328 if (lvalue.isBitField()) {
329 cgf.
cgm.
errorNYI(loc,
"AtomicInfo::createTempAlloca: bitfield lvalue");
335mlir::Value AtomicInfo::getScalarRValValueOrNull(RValue rvalue)
const {
336 if (rvalue.
isScalar() && (!hasPadding() || !lvalue.isSimple()))
341Address AtomicInfo::castToAtomicIntPointer(Address addr)
const {
344 if (intTy && intTy.getWidth() == atomicSizeInBits)
350bool AtomicInfo::emitMemSetZeroIfNecessary()
const {
351 assert(lvalue.isSimple());
352 Address addr = lvalue.getAddress();
372 if (cir::isAnyFloatingPointType(valueTy))
377mlir::Value AtomicInfo::emitAtomicLoadOp(cir::MemOrder order,
bool isVolatile,
379 Address addr = getAtomicAddress();
381 addr = castToAtomicIntPointer(addr);
385 op.setMemOrder(order);
391mlir::Value AtomicInfo::convertRValueToInt(RValue rvalue, mlir::Location loc,
392 bool cmpxchg)
const {
395 if (mlir::Value value = getScalarRValValueOrNull(rvalue)) {
400 loc,
"AtomicInfo::convertRValueToInt: cast scalar rvalue to int");
406 Address addr = materializeRValue(rvalue, loc);
409 addr = castToAtomicIntPointer(addr);
414RValue AtomicInfo::convertToValueOrAtomic(mlir::Value intVal,
415 AggValueSlot resultSlot,
416 SourceLocation loc,
bool asValue,
417 bool cmpxchg)
const {
419 assert((mlir::isa<cir::IntType, cir::PointerType, cir::FPTypeInterface>(
420 intVal.getType())) &&
421 "Expected integer, pointer or floating point value when converting "
424 !lvalue.isBitField() || lvalue.getBitFieldInfo().size == valueSizeInBits;
426 ((isWholeValue && !hasPadding()) || !asValue)) {
428 : getAtomicAddress().getElementType();
430 assert((!mlir::isa<cir::IntType>(valTy) || intVal.getType() == valTy) &&
431 "Different integer types.");
435 cgf.
cgm.
errorNYI(
"convertToValueOrAtomic: convert through bitcast");
442 bool tempIsVolatile =
false;
448 temp = createTempAlloca();
452 Address castTemp = castToAtomicIntPointer(temp);
455 return convertAtomicTempToRValue(temp, resultSlot, loc, asValue);
460void AtomicInfo::emitCopyIntoMemory(RValue rvalue)
const {
461 assert(lvalue.isSimple());
467 cgf.
cgm.
errorNYI(
"copying aggregate into atomic lvalue");
474 emitMemSetZeroIfNecessary();
477 LValue tempLValue = projectValue();
490Address AtomicInfo::materializeRValue(RValue rvalue, mlir::Location loc)
const {
497 LValue tempLV = cgf.
makeAddrLValue(createTempAlloca(), getAtomicType());
498 AtomicInfo atomics(cgf, tempLV, loc);
500 atomics.emitCopyIntoMemory(rvalue);
501 return tempLV.getAddress();
505 mlir::ArrayAttr valuesAttr = builder.getArrayAttr({});
506 mlir::OpBuilder::InsertPoint insertPoint;
507 cir::CaseOp::create(builder, loc, valuesAttr, cir::CaseOpKind::Default,
509 builder.restoreInsertionPoint(insertPoint);
515 mlir::Type orderType,
518 for (cir::MemOrder order : orders)
519 orderAttrs.push_back(cir::IntAttr::get(orderType,
static_cast<int>(order)));
520 mlir::ArrayAttr ordersAttr = builder.getArrayAttr(orderAttrs);
522 mlir::OpBuilder::InsertPoint insertPoint;
523 cir::CaseOp::create(builder, loc, ordersAttr, cir::CaseOpKind::Anyof,
525 builder.restoreInsertionPoint(insertPoint);
531 uint64_t size, cir::MemOrder successOrder,
532 cir::MemOrder failureOrder,
533 cir::SyncScopeKind scope) {
537 mlir::Value expected = builder.
createLoad(loc, val1);
538 mlir::Value desired = builder.
createLoad(loc, val2);
540 auto cmpxchg = cir::AtomicCmpXchgOp::create(
549 cmpxchg.setWeak(isWeak);
552 mlir::Value failed = builder.
createNot(cmpxchg.getSuccess());
554 builder, loc, failed,
false,
555 [&](mlir::OpBuilder &, mlir::Location) {
559 if (expectedSizeInBytes == size) {
561 builder, cmpxchg.getOld().getType());
562 builder.
createStore(loc, cmpxchg.getOld(), storeAddr);
566 cmpxchg.getOld().getType(), ptr.
getAlignment(), loc,
"old.tmp");
567 builder.
createStore(loc, cmpxchg.getOld(), oldTmp);
574 loc, expectedVoid, oldTmpVoid,
589 uint64_t size, cir::MemOrder successOrder, cir::SyncScopeKind scope) {
594 cir::MemOrder failureOrder;
596 failureOrder = cir::MemOrder::Relaxed;
598 switch ((cir::MemOrder)failureOrderInt) {
599 case cir::MemOrder::Relaxed:
602 case cir::MemOrder::Release:
603 case cir::MemOrder::AcquireRelease:
604 failureOrder = cir::MemOrder::Relaxed;
606 case cir::MemOrder::Consume:
607 case cir::MemOrder::Acquire:
608 failureOrder = cir::MemOrder::Acquire;
610 case cir::MemOrder::SequentiallyConsistent:
611 failureOrder = cir::MemOrder::SequentiallyConsistent;
621 size, successOrder, failureOrder, scope);
629 mlir::Value failureOrderVal = cgf.
emitScalarExpr(failureOrderExpr);
631 cir::SwitchOp::create(
633 [&](mlir::OpBuilder &b, mlir::Location loc, mlir::OperationState &os) {
634 mlir::Block *switchBlock = cgf.getBuilder().getBlock();
646 emitDefaultCaseLabel(cgf.getBuilder(), atomicLoc);
647 emitAtomicCmpXchg(cgf, e, isWeak, dest, ptr, val1, val2, expectedResult,
648 size, successOrder, cir::MemOrder::Relaxed, scope);
649 cgf.getBuilder().createBreak(atomicLoc);
650 cgf.getBuilder().setInsertionPointToEnd(switchBlock);
654 emitMemOrderCaseLabel(cgf.getBuilder(), loc, failureOrderVal.getType(),
655 {cir::MemOrder::Consume, cir::MemOrder::Acquire});
657 size, successOrder, cir::MemOrder::Acquire, scope);
659 cgf.
getBuilder().setInsertionPointToEnd(switchBlock);
663 {cir::MemOrder::SequentiallyConsistent});
666 cir::MemOrder::SequentiallyConsistent, scope);
668 cgf.
getBuilder().setInsertionPointToEnd(switchBlock);
680 Expr *failureOrderExpr,
uint64_t size, cir::MemOrder successOrder,
681 cir::SyncScopeKind scope) {
693 [&](mlir::OpBuilder &b, mlir::Location loc) {
694 emitAtomicCmpXchgFailureSet(cgf, e, true, dest, ptr, val1,
695 val2, expectedResult, failureOrderExpr,
696 size, successOrder, scope);
697 cgf.getBuilder().createYield(atomicLoc);
699 [&](mlir::OpBuilder &b, mlir::Location loc) {
700 emitAtomicCmpXchgFailureSet(cgf, e, false, dest, ptr, val1,
701 val2, expectedResult, failureOrderExpr,
702 size, successOrder, scope);
703 cgf.getBuilder().createYield(atomicLoc);
711 cir::MemOrder order, cir::SyncScopeKind scope) {
713 llvm::StringRef opName;
716 mlir::Location loc = cgf.
getLoc(
expr->getSourceRange());
717 auto orderAttr = cir::MemOrderAttr::get(builder.getContext(), order);
718 auto scopeAttr = cir::SyncScopeKindAttr::get(builder.getContext(), scope);
719 cir::AtomicFetchKindAttr fetchAttr;
720 bool fetchFirst =
true;
722 auto handleFetchOp = [&](cir::AtomicFetchKind
kind) {
723 opName = cir::AtomicFetchOp::getOperationName();
724 fetchAttr = cir::AtomicFetchKindAttr::get(builder.getContext(),
kind);
727 switch (
expr->getOp()) {
728 case AtomicExpr::AO__c11_atomic_init:
729 llvm_unreachable(
"already handled!");
731 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
732 case AtomicExpr::AO__hip_atomic_compare_exchange_strong:
733 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
735 val2, expectedResult, failureOrderExpr, size,
739 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
740 case AtomicExpr::AO__hip_atomic_compare_exchange_weak:
741 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
743 val2, expectedResult, failureOrderExpr, size,
747 case AtomicExpr::AO__atomic_compare_exchange:
748 case AtomicExpr::AO__atomic_compare_exchange_n:
749 case AtomicExpr::AO__scoped_atomic_compare_exchange:
750 case AtomicExpr::AO__scoped_atomic_compare_exchange_n: {
754 expectedResult, failureOrderExpr, size, order,
758 cgf,
expr, isWeakExpr, dest, ptr, val1, val2, expectedResult,
759 failureOrderExpr, size, order, scope);
764 case AtomicExpr::AO__c11_atomic_load:
765 case AtomicExpr::AO__atomic_load_n:
766 case AtomicExpr::AO__atomic_load:
767 case AtomicExpr::AO__scoped_atomic_load_n:
768 case AtomicExpr::AO__scoped_atomic_load:
769 case AtomicExpr::AO__hip_atomic_load:
770 case AtomicExpr::AO__opencl_atomic_load: {
774 load->setAttr(
"mem_order", orderAttr);
775 load->setAttr(
"sync_scope", scopeAttr);
777 builder.
createStore(loc, load->getResult(0), dest);
781 case AtomicExpr::AO__c11_atomic_store:
782 case AtomicExpr::AO__atomic_store_n:
783 case AtomicExpr::AO__atomic_store:
784 case AtomicExpr::AO__scoped_atomic_store:
785 case AtomicExpr::AO__scoped_atomic_store_n:
786 case AtomicExpr::AO__hip_atomic_store:
787 case AtomicExpr::AO__opencl_atomic_store: {
788 cir::LoadOp loadVal1 = builder.
createLoad(loc, val1);
794 mlir::IntegerAttr{}, scopeAttr, orderAttr);
798 case AtomicExpr::AO__c11_atomic_exchange:
799 case AtomicExpr::AO__atomic_exchange_n:
800 case AtomicExpr::AO__atomic_exchange:
801 case AtomicExpr::AO__scoped_atomic_exchange_n:
802 case AtomicExpr::AO__scoped_atomic_exchange:
803 case AtomicExpr::AO__hip_atomic_exchange:
804 case AtomicExpr::AO__opencl_atomic_exchange:
805 opName = cir::AtomicXchgOp::getOperationName();
808 case AtomicExpr::AO__atomic_add_fetch:
809 case AtomicExpr::AO__scoped_atomic_add_fetch:
812 case AtomicExpr::AO__c11_atomic_fetch_add:
813 case AtomicExpr::AO__atomic_fetch_add:
814 case AtomicExpr::AO__scoped_atomic_fetch_add:
815 case AtomicExpr::AO__hip_atomic_fetch_add:
816 case AtomicExpr::AO__opencl_atomic_fetch_add:
817 handleFetchOp(cir::AtomicFetchKind::Add);
820 case AtomicExpr::AO__atomic_sub_fetch:
821 case AtomicExpr::AO__scoped_atomic_sub_fetch:
824 case AtomicExpr::AO__c11_atomic_fetch_sub:
825 case AtomicExpr::AO__atomic_fetch_sub:
826 case AtomicExpr::AO__scoped_atomic_fetch_sub:
827 case AtomicExpr::AO__hip_atomic_fetch_sub:
828 case AtomicExpr::AO__opencl_atomic_fetch_sub:
829 handleFetchOp(cir::AtomicFetchKind::Sub);
832 case AtomicExpr::AO__atomic_min_fetch:
833 case AtomicExpr::AO__scoped_atomic_min_fetch:
836 case AtomicExpr::AO__c11_atomic_fetch_min:
837 case AtomicExpr::AO__atomic_fetch_min:
838 case AtomicExpr::AO__scoped_atomic_fetch_min:
839 case AtomicExpr::AO__hip_atomic_fetch_min:
840 case AtomicExpr::AO__opencl_atomic_fetch_min:
841 handleFetchOp(cir::AtomicFetchKind::Min);
844 case AtomicExpr::AO__atomic_fetch_fminimum:
845 case AtomicExpr::AO__scoped_atomic_fetch_fminimum:
846 assert(
expr->getValueType()->isFloatingType() &&
847 "fminimum operations only support floating-point types");
848 handleFetchOp(cir::AtomicFetchKind::Minimum);
851 case AtomicExpr::AO__atomic_fetch_fminimum_num:
852 case AtomicExpr::AO__scoped_atomic_fetch_fminimum_num:
853 assert(
expr->getValueType()->isFloatingType() &&
854 "fminimum_num operations only support floating-point types");
855 handleFetchOp(cir::AtomicFetchKind::MinimumNum);
858 case AtomicExpr::AO__atomic_max_fetch:
859 case AtomicExpr::AO__scoped_atomic_max_fetch:
862 case AtomicExpr::AO__c11_atomic_fetch_max:
863 case AtomicExpr::AO__atomic_fetch_max:
864 case AtomicExpr::AO__scoped_atomic_fetch_max:
865 case AtomicExpr::AO__hip_atomic_fetch_max:
866 case AtomicExpr::AO__opencl_atomic_fetch_max:
867 handleFetchOp(cir::AtomicFetchKind::Max);
870 case AtomicExpr::AO__atomic_fetch_fmaximum:
871 case AtomicExpr::AO__scoped_atomic_fetch_fmaximum:
872 assert(
expr->getValueType()->isFloatingType() &&
873 "fmaximum operations only support floating-point types");
874 handleFetchOp(cir::AtomicFetchKind::Maximum);
877 case AtomicExpr::AO__atomic_fetch_fmaximum_num:
878 case AtomicExpr::AO__scoped_atomic_fetch_fmaximum_num:
879 assert(
expr->getValueType()->isFloatingType() &&
880 "fmaximum_num operations only support floating-point types");
881 handleFetchOp(cir::AtomicFetchKind::MaximumNum);
884 case AtomicExpr::AO__atomic_and_fetch:
885 case AtomicExpr::AO__scoped_atomic_and_fetch:
888 case AtomicExpr::AO__c11_atomic_fetch_and:
889 case AtomicExpr::AO__atomic_fetch_and:
890 case AtomicExpr::AO__scoped_atomic_fetch_and:
891 case AtomicExpr::AO__hip_atomic_fetch_and:
892 case AtomicExpr::AO__opencl_atomic_fetch_and:
893 handleFetchOp(cir::AtomicFetchKind::And);
896 case AtomicExpr::AO__atomic_or_fetch:
897 case AtomicExpr::AO__scoped_atomic_or_fetch:
900 case AtomicExpr::AO__c11_atomic_fetch_or:
901 case AtomicExpr::AO__atomic_fetch_or:
902 case AtomicExpr::AO__scoped_atomic_fetch_or:
903 case AtomicExpr::AO__hip_atomic_fetch_or:
904 case AtomicExpr::AO__opencl_atomic_fetch_or:
905 handleFetchOp(cir::AtomicFetchKind::Or);
908 case AtomicExpr::AO__atomic_xor_fetch:
909 case AtomicExpr::AO__scoped_atomic_xor_fetch:
912 case AtomicExpr::AO__c11_atomic_fetch_xor:
913 case AtomicExpr::AO__atomic_fetch_xor:
914 case AtomicExpr::AO__scoped_atomic_fetch_xor:
915 case AtomicExpr::AO__hip_atomic_fetch_xor:
916 case AtomicExpr::AO__opencl_atomic_fetch_xor:
917 handleFetchOp(cir::AtomicFetchKind::Xor);
920 case AtomicExpr::AO__atomic_nand_fetch:
921 case AtomicExpr::AO__scoped_atomic_nand_fetch:
924 case AtomicExpr::AO__c11_atomic_fetch_nand:
925 case AtomicExpr::AO__atomic_fetch_nand:
926 case AtomicExpr::AO__scoped_atomic_fetch_nand:
927 handleFetchOp(cir::AtomicFetchKind::Nand);
930 case AtomicExpr::AO__atomic_test_and_set: {
931 auto op = cir::AtomicTestAndSetOp::create(
939 case AtomicExpr::AO__atomic_clear: {
940 cir::AtomicClearOp::create(
947 case AtomicExpr::AO__atomic_fetch_uinc:
948 case AtomicExpr::AO__scoped_atomic_fetch_uinc:
949 handleFetchOp(cir::AtomicFetchKind::UIncWrap);
952 case AtomicExpr::AO__atomic_fetch_udec:
953 case AtomicExpr::AO__scoped_atomic_fetch_udec:
954 handleFetchOp(cir::AtomicFetchKind::UDecWrap);
957 case AtomicExpr::AO__opencl_atomic_init:
962 assert(!opName.empty() &&
"expected operation name to build");
963 mlir::Value loadVal1 = builder.
createLoad(loc, val1);
967 mlir::Operation *rmwOp = builder.create(loc, builder.getStringAttr(opName),
968 atomicOperands, atomicResTys);
971 rmwOp->setAttr(
"binop", fetchAttr);
972 rmwOp->setAttr(
"mem_order", orderAttr);
973 rmwOp->setAttr(
"sync_scope", scopeAttr);
975 if (
expr->isVolatile())
976 rmwOp->setAttr(
"is_volatile", builder.getUnitAttr());
977 if (fetchFirst && opName == cir::AtomicFetchOp::getOperationName())
978 rmwOp->setAttr(
"fetch_first", builder.getUnitAttr());
982 mlir::Value result = rmwOp->getResult(0);
992 return cir::SyncScopeKind::SingleThread;
994 return cir::SyncScopeKind::System;
996 return cir::SyncScopeKind::Device;
998 return cir::SyncScopeKind::Workgroup;
1000 return cir::SyncScopeKind::Wavefront;
1002 return cir::SyncScopeKind::Cluster;
1005 return cir::SyncScopeKind::HIPSingleThread;
1007 return cir::SyncScopeKind::HIPSystem;
1009 return cir::SyncScopeKind::HIPAgent;
1011 return cir::SyncScopeKind::HIPWorkgroup;
1013 return cir::SyncScopeKind::HIPWavefront;
1015 return cir::SyncScopeKind::HIPCluster;
1018 return cir::SyncScopeKind::OpenCLWorkGroup;
1020 return cir::SyncScopeKind::OpenCLDevice;
1022 return cir::SyncScopeKind::OpenCLAllSVMDevices;
1024 return cir::SyncScopeKind::OpenCLSubGroup;
1027 llvm_unreachable(
"unhandled sync scope");
1030static std::optional<cir::MemOrder>
1040 if (oriOrder == cir::MemOrder::Consume ||
1041 oriOrder == cir::MemOrder::Acquire ||
1042 oriOrder == cir::MemOrder::AcquireRelease)
1043 return std::nullopt;
1044 }
else if (isLoad) {
1045 if (oriOrder == cir::MemOrder::Release ||
1046 oriOrder == cir::MemOrder::AcquireRelease)
1047 return std::nullopt;
1048 }
else if (isFence) {
1049 if (oriOrder == cir::MemOrder::Relaxed)
1050 return std::nullopt;
1054 if (oriOrder == cir::MemOrder::Consume)
1055 return cir::MemOrder::Acquire;
1060 CIRGenFunction &cgf, mlir::Value order,
bool isStore,
bool isLoad,
1061 bool isFence, llvm::function_ref<
void(cir::MemOrder)> emitAtomicOpFn) {
1069 cir::SwitchOp::create(
1070 builder, order.getLoc(), order,
1071 [&](mlir::OpBuilder &, mlir::Location loc, mlir::OperationState &) {
1072 mlir::Block *switchBlock = builder.getBlock();
1074 auto emitMemOrderCase = [&](llvm::ArrayRef<cir::MemOrder> caseOrders) {
1076 for (int i = 1, e = caseOrders.size(); i < e; i++)
1077 assert((getEffectiveAtomicMemOrder(caseOrders[i - 1], isStore,
1079 getEffectiveAtomicMemOrder(caseOrders[i], isStore, isLoad,
1081 "Effective memory order must be same!");
1083 if (caseOrders.empty()) {
1084 emitDefaultCaseLabel(builder, loc);
1088 emitAtomicOpFn(cir::MemOrder::Relaxed);
1089 } else if (std::optional<cir::MemOrder> actualOrder =
1090 getEffectiveAtomicMemOrder(caseOrders[0], isStore,
1093 if (!isFence && actualOrder == cir::MemOrder::Relaxed)
1098 emitMemOrderCaseLabel(builder, loc, order.getType(), caseOrders);
1099 emitAtomicOpFn(actualOrder.value());
1104 builder.createBreak(loc);
1105 builder.setInsertionPointToEnd(switchBlock);
1108 emitMemOrderCase( {});
1109 emitMemOrderCase({cir::MemOrder::Relaxed});
1110 emitMemOrderCase({cir::MemOrder::Consume, cir::MemOrder::Acquire});
1111 emitMemOrderCase({cir::MemOrder::Release});
1112 emitMemOrderCase({cir::MemOrder::AcquireRelease});
1113 emitMemOrderCase({cir::MemOrder::SequentiallyConsistent});
1121 llvm::function_ref<
void(cir::SyncScopeKind)>
emitAtomicOp) {
1126 cir::SwitchOp::create(
1127 builder, scope.getLoc(), scope,
1128 [&](mlir::OpBuilder &, mlir::Location loc, mlir::OperationState &) {
1129 mlir::Block *switchBlock = builder.getBlock();
1132 cir::SyncScopeKind fallbackScope =
1133 convertSyncScopeToCIR(cgf, scopeModel->map(fallback));
1134 emitDefaultCaseLabel(builder, loc);
1135 emitAtomicOp(fallbackScope);
1136 builder.createBreak(loc);
1137 builder.setInsertionPointToEnd(switchBlock);
1140 for (unsigned runtimeScopeValue : allScopes) {
1141 if (runtimeScopeValue == fallback)
1144 cir::SyncScopeKind cirScope =
1145 convertSyncScopeToCIR(cgf, scopeModel->map(runtimeScopeValue));
1147 mlir::ArrayAttr casesAttr = builder.getArrayAttr(
1148 {cir::IntAttr::get(scope.getType(), runtimeScopeValue)});
1149 mlir::OpBuilder::InsertPoint insertPoint;
1150 cir::CaseOp::create(builder, loc, casesAttr, cir::CaseOpKind::Equal,
1153 builder.restoreInsertionPoint(insertPoint);
1154 emitAtomicOp(cirScope);
1155 builder.createBreak(loc);
1156 builder.setInsertionPointToEnd(switchBlock);
1165 llvm::function_ref<
void(cir::MemOrder)> emitAtomicOpFn) {
1166 if (order.
eval.has_value()) {
1167 uint64_t constOrder = order.
eval->Val.getInt().getZExtValue();
1172 cir::MemOrder oriOrder =
static_cast<cir::MemOrder
>(constOrder);
1173 if (std::optional<cir::MemOrder> actualOrder =
1175 emitAtomicOpFn(actualOrder.value());
1181 isFence, emitAtomicOpFn);
1186 llvm::function_ref<
void(cir::SyncScopeKind)>
emitAtomicOp) {
1187 if (!scopeModel || !scope.
value) {
1192 if (scope.
eval.has_value()) {
1194 *
this, scopeModel->
map(scope.
eval->Val.getInt().getZExtValue()));
1210 mlir::NamedAttrList fnAttrs;
1234 auto castToGenericAddrSpace = [&](mlir::Value v,
QualType pt) {
1239 cgf.
cgm.
errorNYI(loc,
"emitLibCallForAtomicExpr: openCL");
1246 mlir::Value order = successOrder.
value;
1249 llvm::StringRef calleeName;
1251 bool hasRetTy =
false;
1252 switch (e->
getOp()) {
1253 case AtomicExpr::AO__c11_atomic_init:
1254 case AtomicExpr::AO__opencl_atomic_init:
1255 llvm_unreachable(
"Already handled!");
1262 case AtomicExpr::AO__atomic_compare_exchange:
1263 case AtomicExpr::AO__atomic_compare_exchange_n:
1264 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
1265 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
1266 case AtomicExpr::AO__hip_atomic_compare_exchange_weak:
1267 case AtomicExpr::AO__hip_atomic_compare_exchange_strong:
1268 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
1269 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
1270 case AtomicExpr::AO__scoped_atomic_compare_exchange:
1271 case AtomicExpr::AO__scoped_atomic_compare_exchange_n: {
1272 calleeName =
"__atomic_compare_exchange";
1289 case AtomicExpr::AO__atomic_exchange:
1290 case AtomicExpr::AO__atomic_exchange_n:
1291 case AtomicExpr::AO__c11_atomic_exchange:
1292 case AtomicExpr::AO__hip_atomic_exchange:
1293 case AtomicExpr::AO__opencl_atomic_exchange:
1294 case AtomicExpr::AO__scoped_atomic_exchange:
1295 case AtomicExpr::AO__scoped_atomic_exchange_n:
1296 calleeName =
"__atomic_exchange";
1303 case AtomicExpr::AO__atomic_store:
1304 case AtomicExpr::AO__atomic_store_n:
1305 case AtomicExpr::AO__c11_atomic_store:
1306 case AtomicExpr::AO__hip_atomic_store:
1307 case AtomicExpr::AO__opencl_atomic_store:
1308 case AtomicExpr::AO__scoped_atomic_store:
1309 case AtomicExpr::AO__scoped_atomic_store_n:
1310 calleeName =
"__atomic_store";
1319 case AtomicExpr::AO__atomic_load:
1320 case AtomicExpr::AO__atomic_load_n:
1321 case AtomicExpr::AO__c11_atomic_load:
1322 case AtomicExpr::AO__hip_atomic_load:
1323 case AtomicExpr::AO__opencl_atomic_load:
1324 case AtomicExpr::AO__scoped_atomic_load:
1325 case AtomicExpr::AO__scoped_atomic_load_n:
1326 calleeName =
"__atomic_load";
1329 case AtomicExpr::AO__atomic_add_fetch:
1330 case AtomicExpr::AO__scoped_atomic_add_fetch:
1331 case AtomicExpr::AO__atomic_fetch_add:
1332 case AtomicExpr::AO__c11_atomic_fetch_add:
1333 case AtomicExpr::AO__hip_atomic_fetch_add:
1334 case AtomicExpr::AO__opencl_atomic_fetch_add:
1335 case AtomicExpr::AO__scoped_atomic_fetch_add:
1336 case AtomicExpr::AO__atomic_and_fetch:
1337 case AtomicExpr::AO__scoped_atomic_and_fetch:
1338 case AtomicExpr::AO__atomic_fetch_and:
1339 case AtomicExpr::AO__c11_atomic_fetch_and:
1340 case AtomicExpr::AO__hip_atomic_fetch_and:
1341 case AtomicExpr::AO__opencl_atomic_fetch_and:
1342 case AtomicExpr::AO__scoped_atomic_fetch_and:
1343 case AtomicExpr::AO__atomic_or_fetch:
1344 case AtomicExpr::AO__scoped_atomic_or_fetch:
1345 case AtomicExpr::AO__atomic_fetch_or:
1346 case AtomicExpr::AO__c11_atomic_fetch_or:
1347 case AtomicExpr::AO__hip_atomic_fetch_or:
1348 case AtomicExpr::AO__opencl_atomic_fetch_or:
1349 case AtomicExpr::AO__scoped_atomic_fetch_or:
1350 case AtomicExpr::AO__atomic_sub_fetch:
1351 case AtomicExpr::AO__scoped_atomic_sub_fetch:
1352 case AtomicExpr::AO__atomic_fetch_sub:
1353 case AtomicExpr::AO__c11_atomic_fetch_sub:
1354 case AtomicExpr::AO__hip_atomic_fetch_sub:
1355 case AtomicExpr::AO__opencl_atomic_fetch_sub:
1356 case AtomicExpr::AO__scoped_atomic_fetch_sub:
1357 case AtomicExpr::AO__atomic_xor_fetch:
1358 case AtomicExpr::AO__scoped_atomic_xor_fetch:
1359 case AtomicExpr::AO__atomic_fetch_xor:
1360 case AtomicExpr::AO__c11_atomic_fetch_xor:
1361 case AtomicExpr::AO__hip_atomic_fetch_xor:
1362 case AtomicExpr::AO__opencl_atomic_fetch_xor:
1363 case AtomicExpr::AO__scoped_atomic_fetch_xor:
1364 case AtomicExpr::AO__atomic_nand_fetch:
1365 case AtomicExpr::AO__atomic_fetch_nand:
1366 case AtomicExpr::AO__c11_atomic_fetch_nand:
1367 case AtomicExpr::AO__scoped_atomic_fetch_nand:
1368 case AtomicExpr::AO__scoped_atomic_nand_fetch:
1369 case AtomicExpr::AO__atomic_min_fetch:
1370 case AtomicExpr::AO__atomic_fetch_min:
1371 case AtomicExpr::AO__c11_atomic_fetch_min:
1372 case AtomicExpr::AO__hip_atomic_fetch_min:
1373 case AtomicExpr::AO__opencl_atomic_fetch_min:
1374 case AtomicExpr::AO__scoped_atomic_fetch_min:
1375 case AtomicExpr::AO__scoped_atomic_min_fetch:
1376 case AtomicExpr::AO__atomic_max_fetch:
1377 case AtomicExpr::AO__atomic_fetch_max:
1378 case AtomicExpr::AO__c11_atomic_fetch_max:
1379 case AtomicExpr::AO__hip_atomic_fetch_max:
1380 case AtomicExpr::AO__opencl_atomic_fetch_max:
1381 case AtomicExpr::AO__scoped_atomic_fetch_max:
1382 case AtomicExpr::AO__scoped_atomic_max_fetch:
1383 case AtomicExpr::AO__scoped_atomic_fetch_uinc:
1384 case AtomicExpr::AO__scoped_atomic_fetch_udec:
1385 case AtomicExpr::AO__atomic_fetch_fmaximum:
1386 case AtomicExpr::AO__atomic_fetch_fmaximum_num:
1387 case AtomicExpr::AO__atomic_fetch_fminimum:
1388 case AtomicExpr::AO__atomic_fetch_fminimum_num:
1389 case AtomicExpr::AO__scoped_atomic_fetch_fmaximum:
1390 case AtomicExpr::AO__scoped_atomic_fetch_fmaximum_num:
1391 case AtomicExpr::AO__scoped_atomic_fetch_fminimum:
1392 case AtomicExpr::AO__scoped_atomic_fetch_fminimum_num:
1393 case AtomicExpr::AO__atomic_test_and_set:
1394 case AtomicExpr::AO__atomic_clear:
1395 case AtomicExpr::AO__atomic_fetch_uinc:
1396 case AtomicExpr::AO__atomic_fetch_udec:
1397 llvm_unreachable(
"Integral atomic operations always become atomicrmw!");
1402 cgf.
cgm.
errorNYI(loc,
"emitLibCallForAtomicExpr: openCL");
1418 cgf.
cgm.
errorNYI(loc,
"emitLibCallForAtomicExpr: openCL");
1440 memTy = ty->getValueType();
1442 Expr *isWeakExpr =
nullptr;
1443 Expr *orderFailExpr =
nullptr;
1451 if (e->
getOp() == AtomicExpr::AO__c11_atomic_init) {
1465 switch (e->
getOp()) {
1470 case AtomicExpr::AO__c11_atomic_init:
1471 llvm_unreachable(
"already handled above with emitAtomicInit");
1473 case AtomicExpr::AO__atomic_load_n:
1474 case AtomicExpr::AO__scoped_atomic_load_n:
1475 case AtomicExpr::AO__c11_atomic_load:
1476 case AtomicExpr::AO__opencl_atomic_load:
1477 case AtomicExpr::AO__hip_atomic_load:
1478 case AtomicExpr::AO__atomic_test_and_set:
1479 case AtomicExpr::AO__atomic_clear:
1482 case AtomicExpr::AO__atomic_load:
1483 case AtomicExpr::AO__scoped_atomic_load:
1487 case AtomicExpr::AO__atomic_store:
1488 case AtomicExpr::AO__scoped_atomic_store:
1492 case AtomicExpr::AO__atomic_exchange:
1493 case AtomicExpr::AO__scoped_atomic_exchange:
1498 case AtomicExpr::AO__atomic_compare_exchange:
1499 case AtomicExpr::AO__atomic_compare_exchange_n:
1500 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
1501 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
1502 case AtomicExpr::AO__scoped_atomic_compare_exchange:
1503 case AtomicExpr::AO__scoped_atomic_compare_exchange_n:
1504 case AtomicExpr::AO__hip_atomic_compare_exchange_strong:
1505 case AtomicExpr::AO__hip_atomic_compare_exchange_weak:
1506 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
1507 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
1509 if (e->
getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1510 e->
getOp() == AtomicExpr::AO__scoped_atomic_compare_exchange)
1515 if (e->
getOp() == AtomicExpr::AO__atomic_compare_exchange_n ||
1516 e->
getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1517 e->
getOp() == AtomicExpr::AO__scoped_atomic_compare_exchange_n ||
1518 e->
getOp() == AtomicExpr::AO__scoped_atomic_compare_exchange)
1522 case AtomicExpr::AO__c11_atomic_fetch_add:
1523 case AtomicExpr::AO__c11_atomic_fetch_sub:
1534 mlir::Value scale = builder.getConstInt(loc, val1Scalar.getType(),
1536 val1Scalar = builder.createMul(loc, val1Scalar, scale);
1542 case AtomicExpr::AO__atomic_fetch_add:
1543 case AtomicExpr::AO__atomic_fetch_sub:
1544 case AtomicExpr::AO__atomic_add_fetch:
1545 case AtomicExpr::AO__atomic_sub_fetch:
1555 case AtomicExpr::AO__atomic_fetch_max:
1556 case AtomicExpr::AO__atomic_fetch_min:
1557 case AtomicExpr::AO__atomic_max_fetch:
1558 case AtomicExpr::AO__atomic_min_fetch:
1559 case AtomicExpr::AO__c11_atomic_fetch_max:
1560 case AtomicExpr::AO__c11_atomic_fetch_min:
1561 case AtomicExpr::AO__scoped_atomic_fetch_add:
1562 case AtomicExpr::AO__scoped_atomic_fetch_max:
1563 case AtomicExpr::AO__scoped_atomic_fetch_min:
1564 case AtomicExpr::AO__scoped_atomic_fetch_sub:
1565 case AtomicExpr::AO__scoped_atomic_fetch_fminimum:
1566 case AtomicExpr::AO__scoped_atomic_fetch_fmaximum:
1567 case AtomicExpr::AO__scoped_atomic_fetch_fminimum_num:
1568 case AtomicExpr::AO__scoped_atomic_fetch_fmaximum_num:
1569 case AtomicExpr::AO__scoped_atomic_add_fetch:
1570 case AtomicExpr::AO__scoped_atomic_max_fetch:
1571 case AtomicExpr::AO__scoped_atomic_min_fetch:
1572 case AtomicExpr::AO__scoped_atomic_sub_fetch:
1575 case AtomicExpr::AO__atomic_fetch_and:
1576 case AtomicExpr::AO__atomic_fetch_nand:
1577 case AtomicExpr::AO__atomic_fetch_or:
1578 case AtomicExpr::AO__atomic_fetch_xor:
1579 case AtomicExpr::AO__atomic_and_fetch:
1580 case AtomicExpr::AO__atomic_nand_fetch:
1581 case AtomicExpr::AO__atomic_or_fetch:
1582 case AtomicExpr::AO__atomic_xor_fetch:
1583 case AtomicExpr::AO__atomic_exchange_n:
1584 case AtomicExpr::AO__atomic_store_n:
1585 case AtomicExpr::AO__c11_atomic_fetch_and:
1586 case AtomicExpr::AO__c11_atomic_fetch_nand:
1587 case AtomicExpr::AO__c11_atomic_fetch_or:
1588 case AtomicExpr::AO__c11_atomic_fetch_xor:
1589 case AtomicExpr::AO__c11_atomic_exchange:
1590 case AtomicExpr::AO__c11_atomic_store:
1591 case AtomicExpr::AO__scoped_atomic_fetch_and:
1592 case AtomicExpr::AO__scoped_atomic_fetch_nand:
1593 case AtomicExpr::AO__scoped_atomic_fetch_or:
1594 case AtomicExpr::AO__scoped_atomic_fetch_xor:
1595 case AtomicExpr::AO__scoped_atomic_and_fetch:
1596 case AtomicExpr::AO__scoped_atomic_nand_fetch:
1597 case AtomicExpr::AO__scoped_atomic_or_fetch:
1598 case AtomicExpr::AO__scoped_atomic_xor_fetch:
1599 case AtomicExpr::AO__scoped_atomic_store_n:
1600 case AtomicExpr::AO__scoped_atomic_exchange_n:
1601 case AtomicExpr::AO__atomic_fetch_uinc:
1602 case AtomicExpr::AO__atomic_fetch_udec:
1603 case AtomicExpr::AO__scoped_atomic_fetch_uinc:
1604 case AtomicExpr::AO__scoped_atomic_fetch_udec:
1605 case AtomicExpr::AO__atomic_fetch_fminimum:
1606 case AtomicExpr::AO__atomic_fetch_fmaximum:
1607 case AtomicExpr::AO__atomic_fetch_fminimum_num:
1608 case AtomicExpr::AO__atomic_fetch_fmaximum_num:
1609 case AtomicExpr::AO__hip_atomic_exchange:
1610 case AtomicExpr::AO__hip_atomic_store:
1611 case AtomicExpr::AO__hip_atomic_fetch_add:
1612 case AtomicExpr::AO__hip_atomic_fetch_sub:
1613 case AtomicExpr::AO__hip_atomic_fetch_min:
1614 case AtomicExpr::AO__hip_atomic_fetch_max:
1615 case AtomicExpr::AO__hip_atomic_fetch_and:
1616 case AtomicExpr::AO__hip_atomic_fetch_or:
1617 case AtomicExpr::AO__hip_atomic_fetch_xor:
1618 case AtomicExpr::AO__opencl_atomic_exchange:
1619 case AtomicExpr::AO__opencl_atomic_store:
1620 case AtomicExpr::AO__opencl_atomic_fetch_add:
1621 case AtomicExpr::AO__opencl_atomic_fetch_sub:
1622 case AtomicExpr::AO__opencl_atomic_fetch_min:
1623 case AtomicExpr::AO__opencl_atomic_fetch_max:
1624 case AtomicExpr::AO__opencl_atomic_fetch_and:
1625 case AtomicExpr::AO__opencl_atomic_fetch_or:
1626 case AtomicExpr::AO__opencl_atomic_fetch_xor:
1633 bool shouldCastToIntPtrTy =
1641 AtomicInfo atomics(*
this, atomicValue, loc);
1647 if (shouldCastToIntPtrTy) {
1648 ptr = atomics.castToAtomicIntPointer(ptr);
1650 val1 = atomics.convertToAtomicIntPointer(val1, loc);
1652 val2 = atomics.convertToAtomicIntPointer(val2, loc);
1655 if (shouldCastToIntPtrTy)
1656 dest = atomics.castToAtomicIntPointer(dest);
1659 }
else if (e->
getOp() == AtomicExpr::AO__atomic_test_and_set) {
1662 dest = atomics.createTempAlloca();
1663 if (shouldCastToIntPtrTy)
1664 dest = atomics.castToAtomicIntPointer(dest);
1667 bool powerOf2Size = (size & (size - 1)) == 0;
1668 bool useLibCall = !powerOf2Size || (size > 16);
1683 bool isStore = e->
getOp() == AtomicExpr::AO__c11_atomic_store ||
1684 e->
getOp() == AtomicExpr::AO__opencl_atomic_store ||
1685 e->
getOp() == AtomicExpr::AO__hip_atomic_store ||
1686 e->
getOp() == AtomicExpr::AO__atomic_store ||
1687 e->
getOp() == AtomicExpr::AO__atomic_store_n ||
1688 e->
getOp() == AtomicExpr::AO__scoped_atomic_store ||
1689 e->
getOp() == AtomicExpr::AO__scoped_atomic_store_n ||
1690 e->
getOp() == AtomicExpr::AO__atomic_clear;
1691 bool isLoad = e->
getOp() == AtomicExpr::AO__c11_atomic_load ||
1692 e->
getOp() == AtomicExpr::AO__opencl_atomic_load ||
1693 e->
getOp() == AtomicExpr::AO__hip_atomic_load ||
1694 e->
getOp() == AtomicExpr::AO__atomic_load ||
1695 e->
getOp() == AtomicExpr::AO__atomic_load_n ||
1696 e->
getOp() == AtomicExpr::AO__scoped_atomic_load ||
1697 e->
getOp() == AtomicExpr::AO__scoped_atomic_load_n;
1700 order, isStore, isLoad,
false, [&](cir::MemOrder memOrder) {
1701 std::unique_ptr<AtomicScopeModel> scopeModel = e->
getScopeModel();
1703 scopeModel.get(), scope, [&](cir::SyncScopeKind scope) {
1704 emitAtomicOp(*this, e, dest, ptr, val1, val2, originalVal1,
1705 isWeakExpr, orderFailExpr, size, memOrder, scope);
1720 return emitAtomicLoad(lvalue, loc, cir::MemOrder::SequentiallyConsistent,
1727 cir::MemOrder order,
bool isVolatile,
1729 AtomicInfo info(*
this, lvalue,
getLoc(loc));
1730 return info.emitAtomicLoad(slot, loc,
true, order, isVolatile);
1735 auto order = cir::MemOrder::SequentiallyConsistent;
1748 cir::MemOrder order,
bool isVolatile,
1752 mlir::Location loc = dest.
getPointer().getLoc();
1757 AtomicInfo atomics(*
this, dest, loc);
1758 LValue lvalue = atomics.getAtomicLValue();
1763 atomics.emitCopyIntoMemory(rvalue);
1768 if (atomics.shouldUseLibCall()) {
1770 cgm.errorNYI(loc,
"emitAtomicStore: atomic store with library call");
1775 mlir::Value valueToStore = atomics.convertRValueToInt(rvalue, loc);
1778 Address addr = atomics.getAtomicAddress();
1779 if (mlir::Value value = atomics.getScalarRValValueOrNull(rvalue)) {
1781 addr = atomics.castToAtomicIntPointer(addr);
1786 cir::StoreOp store = builder.createStore(loc, valueToStore, addr);
1791 store.setMemOrder(order);
1796 store.setIsVolatile(
true);
1802 cgm.errorNYI(loc,
"emitAtomicStore: non-simple atomic lvalue");
1809 switch (atomics.getEvaluationKind()) {
1825 bool zeroed =
false;
1827 zeroed = atomics.emitMemSetZeroIfNecessary();
1828 dest = atomics.projectValue();
1843 llvm_unreachable(
"bad evaluation kind");
static bool shouldCastToInt(mlir::Type valueTy, bool cmpxchg)
Return true if.
static Address emitValToTemp(CIRGenFunction &cgf, Expr *e)
static void emitAtomicExprWithDynamicSyncScope(CIRGenFunction &cgf, const AtomicScopeModel *scopeModel, mlir::Value scope, llvm::function_ref< void(cir::SyncScopeKind)> emitAtomicOp)
static void emitMemOrderCaseLabel(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Type orderType, llvm::ArrayRef< cir::MemOrder > orders)
static cir::SyncScopeKind convertSyncScopeToCIR(CIRGenFunction &cgf, clang::SyncScope scope)
static void emitAtomicExprWithDynamicMemOrder(CIRGenFunction &cgf, mlir::Value order, bool isStore, bool isLoad, bool isFence, llvm::function_ref< void(cir::MemOrder)> emitAtomicOpFn)
static void emitDefaultCaseLabel(CIRGenBuilderTy &builder, mlir::Location loc)
static void emitAtomicCmpXchgFailureSetCheckWeak(CIRGenFunction &cgf, AtomicExpr *e, Expr *isWeakExpr, Address dest, Address ptr, Address val1, Address val2, Address expectedResult, Expr *failureOrderExpr, uint64_t size, cir::MemOrder successOrder, cir::SyncScopeKind scope)
static RValue emitLibCallForAtomicExpr(CIRGenFunction &cgf, AtomicExpr *e, Address atomicPtr, Address dest, Address val1, Address val2, uint64_t atomicTySize, QualType resultTy, const CIRGenFunction::EmittedOrderOrScope &successOrder)
static void setAMDGPUAtomicMetadata(CIRGenFunction &cgf, mlir::Operation *op, const AtomicExpr *expr)
Attach the AMDGPU atomic metadata markers that the current atomic options call for.
static bool isFullSizeType(CIRGenModule &cgm, mlir::Type ty, uint64_t expectedSize)
Does a store of the given IR type modify the full expected width?
static void emitAtomicCmpXchgFailureSet(CIRGenFunction &cgf, AtomicExpr *e, bool isWeak, Address dest, Address ptr, Address val1, Address val2, Address expectedResult, Expr *failureOrderExpr, uint64_t size, cir::MemOrder successOrder, cir::SyncScopeKind scope)
static void emitAtomicCmpXchg(CIRGenFunction &cgf, AtomicExpr *e, bool isWeak, Address dest, Address ptr, Address val1, Address val2, Address expectedResult, uint64_t size, cir::MemOrder successOrder, cir::MemOrder failureOrder, cir::SyncScopeKind scope)
static void emitAtomicOp(CIRGenFunction &cgf, AtomicExpr *expr, Address dest, Address ptr, Address val1, Address val2, Address expectedResult, Expr *isWeakExpr, Expr *failureOrderExpr, int64_t size, cir::MemOrder order, cir::SyncScopeKind scope)
static std::optional< cir::MemOrder > getEffectiveAtomicMemOrder(cir::MemOrder oriOrder, bool isStore, bool isLoad, bool isFence)
static RValue emitAtomicLibCall(CIRGenFunction &cgf, llvm::StringRef funcName, QualType resultType, CallArgList &args)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
cir::BreakOp createBreak(mlir::Location loc)
Create a break operation.
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment)
mlir::Value createNot(mlir::Location loc, mlir::Value value)
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
cir::BoolType getBoolTy()
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const
llvm::TypeSize getTypeStoreSize(mlir::Type ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
TypeInfo getTypeInfo(const Type *T) const
Get the size and alignment of the specified complete type in bits.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
const TargetInfo & getTargetInfo() const
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, __atomic_load,...
static std::unique_ptr< AtomicScopeModel > getScopeModel(AtomicOp Op)
Get atomic scope model for the atomic op code.
Expr * getOrderFail() const
Defines the interface for sync scope model.
virtual ArrayRef< unsigned > getRuntimeValues() const =0
Get all possible sync scope values that might be encountered at runtime for the current language.
virtual SyncScope map(unsigned S) const =0
Maps language specific sync scope values to internal SyncScope enum.
virtual unsigned getFallBackValue() const =0
If atomic builtin function is called with invalid sync scope value at runtime, it will fall back to a...
mlir::Value getPointer() const
mlir::Type getElementType() const
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
clang::CharUnits getAlignment() const
mlir::Value emitRawPointer() const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed)
Address getAddress() const
cir::LoadOp createLoad(mlir::Location loc, Address addr, bool isVolatile=false, bool isNontemporal=false)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, bool isNontemporal=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
cir::MemCpyOp createMemCpy(mlir::Location loc, Address dst, Address src, mlir::Value len)
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
cir::MemSetOp createMemSet(mlir::Location loc, mlir::Value dst, mlir::Value val, mlir::Value len)
cir::IntType getUIntNTy(int n)
static CIRGenCallee forDirect(mlir::Operation *funcPtr, const CIRGenCalleeInfo &abstractInfo=CIRGenCalleeInfo())
RValue convertTempToRValue(Address addr, clang::QualType type, clang::SourceLocation loc)
Given the address of a temporary variable, produce an r-value of its type.
Address emitPointerWithAlignment(const clang::Expr *expr, LValueBaseInfo *baseInfo=nullptr)
Given an expression with a pointer type, emit the value and compute our best estimate of the alignmen...
cir::AllocaOp createTempAlloca(mlir::Type ty, mlir::Location loc, const Twine &name="tmp", mlir::Value arraySize=nullptr, bool insertIntoFnEntryBlock=false)
This creates an alloca and inserts it into the entry block if ArraySize is nullptr,...
mlir::Value emitComplexExpr(const Expr *e)
Emit the computation of the specified expression of complex type, returning the result.
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
void emitAnyExprToMem(const Expr *e, Address location, Qualifiers quals, bool isInitializer)
Emits the code necessary to evaluate an arbitrary expression into the given memory location.
RValue emitAtomicExpr(AtomicExpr *e)
RValue emitAtomicLoad(LValue lvalue, SourceLocation loc, AggValueSlot slot=AggValueSlot::ignored())
mlir::Type convertTypeForMem(QualType t)
void emitStoreOfScalar(mlir::Value value, Address addr, bool isVolatile, clang::QualType ty, LValueBaseInfo baseInfo, bool isInit=false, bool isNontemporal=false)
void emitStoreOfComplex(mlir::Location loc, mlir::Value v, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
void emitAtomicExprWithSyncScope(const AtomicScopeModel *scopeModel, const EmittedOrderOrScope &scope, llvm::function_ref< void(cir::SyncScopeKind)> emitAtomicOp)
mlir::Value emitToMemory(mlir::Value value, clang::QualType ty)
Given a value and its clang type, returns the value casted to its memory representation.
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
void emitAtomicExprWithMemOrder(const EmittedOrderOrScope &order, bool isStore, bool isLoad, bool isFence, llvm::function_ref< void(cir::MemOrder)> emitAtomicOp)
CIRGenBuilderTy & getBuilder()
mlir::MLIRContext & getMLIRContext()
void emitAtomicInit(Expr *init, LValue dest)
RValue emitCall(const CIRGenFunctionInfo &funcInfo, const CIRGenCallee &callee, ReturnValueSlot returnValue, const CallArgList &args, cir::CIRCallOpInterface *callOp, bool isMustTail, SourceRange clangLoc)
LValue makeAddrLValue(Address addr, QualType ty, AlignmentSource source=AlignmentSource::Type)
void emitAtomicStore(RValue rvalue, LValue dest, bool isInit)
clang::ASTContext & getContext() const
mlir::Value emitFromMemory(mlir::Value value, clang::QualType ty)
EmitFromMemory - Change a scalar value from its memory representation to its value representation.
Address createMemTemp(QualType t, mlir::Location loc, const Twine &name="tmp", Address *alloca=nullptr, mlir::OpBuilder::InsertPoint ip={})
Create a temporary memory object of the given type, with appropriate alignmen and cast it to the defa...
void emitAggExpr(const clang::Expr *e, AggValueSlot slot)
This class organizes the cross-function state that is used while generating CIR code.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
clang::AtomicOptions getAtomicOpts() const
The atomic options in effect at the point currently being emitted.
const llvm::Triple & getTriple() const
const cir::CIRDataLayout & getDataLayout() const
cir::FuncOp createRuntimeFunction(cir::FuncType ty, llvm::StringRef name, mlir::NamedAttrList extraAttrs={}, bool isLocal=false, bool assumeConvergent=false)
const CIRGenFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
A builtin function is a freestanding function using the default C conventions.
cir::FuncType getFunctionType(const CIRGenFunctionInfo &info)
Get the CIR function type for.
void add(RValue rvalue, clang::QualType type)
Address getAddress() const
clang::QualType getType() const
mlir::Value getPointer() const
bool isVolatileQualified() const
This trivial value class is used to represent the result of an expression that is evaluated.
Address getAggregateAddress() const
Return the value of the address of the aggregate.
static RValue get(mlir::Value v)
static RValue getAggregate(Address addr, bool isVolatile=false)
Convert an Address to an RValue.
mlir::Value getValue() const
Return the value of this scalar value.
mlir::Value getComplexValue() const
Return the value of this complex value.
Contains the address where the return value of a function can be stored, and whether the address is v...
This is an opaque type for sizes expressed in character units.
llvm::Align getAsAlign() const
Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit bytes.
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
This represents one expression.
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsBooleanCondition - Return true if this is a constant which we can fold and convert to a boo...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
A (possibly-)qualified type.
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Encodes a location in the source.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
virtual bool hasBuiltinAtomic(uint64_t AtomicSizeInBits, uint64_t AlignmentInBits) const
Returns true if the given target supports lock-free atomic operations at the specified width and alig...
bool isPointerType() const
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isAtomicType() const
const T * getAs() const
Member-template getAs<specific type>'.
bool isValidCIRAtomicOrderingCABI(Int value)
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
@ Address
A pointer to a ValueDecl.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
SyncScope
Defines sync scope values used internally by clang.
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
static bool atomicAMDGPUAvailableVisibleMMRA()
static bool atomicInfoGetAtomicPointer()
static bool aggValueSlotGC()
static bool opLoadStoreAtomic()
static bool opLoadStoreTbaa()
static bool opFuncExtraAttrs()
static bool atomicUseLibCall()
static bool atomicOpenMP()
static bool atomicMicrosoftVolatile()
static bool atomicSyncScopeID()
static bool generateDebugInfo()
static bool atomicInfoGetAtomicAddress()
bool getOption(AtomicOptionKind Kind) const
An emitted atomic order or scope value with optional constant evaluation.
std::optional< Expr::EvalResult > eval
EvalResult is a struct with detailed info about an evaluated expression.
APValue Val
Val - This is the value the expression can be folded to.