|
| 1 | +//===--- CIRGenAtomic.cpp - Emit CIR for atomic operations ----------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file contains the code for emitting atomic operations. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "CIRGenFunction.h" |
| 14 | +#include "clang/CIR/MissingFeatures.h" |
| 15 | + |
| 16 | +using namespace clang; |
| 17 | +using namespace clang::CIRGen; |
| 18 | +using namespace cir; |
| 19 | + |
| 20 | +namespace { |
| 21 | +class AtomicInfo { |
| 22 | + CIRGenFunction &cgf; |
| 23 | + QualType atomicTy; |
| 24 | + QualType valueTy; |
| 25 | + uint64_t atomicSizeInBits = 0; |
| 26 | + uint64_t valueSizeInBits = 0; |
| 27 | + CharUnits atomicAlign; |
| 28 | + CharUnits valueAlign; |
| 29 | + TypeEvaluationKind evaluationKind = cir::TEK_Scalar; |
| 30 | + LValue lvalue; |
| 31 | + mlir::Location loc; |
| 32 | + |
| 33 | +public: |
| 34 | + AtomicInfo(CIRGenFunction &cgf, LValue &lvalue, mlir::Location loc) |
| 35 | + : cgf(cgf), loc(loc) { |
| 36 | + assert(!lvalue.isGlobalReg()); |
| 37 | + ASTContext &ctx = cgf.getContext(); |
| 38 | + if (lvalue.isSimple()) { |
| 39 | + atomicTy = lvalue.getType(); |
| 40 | + if (auto *ty = atomicTy->getAs<AtomicType>()) |
| 41 | + valueTy = ty->getValueType(); |
| 42 | + else |
| 43 | + valueTy = atomicTy; |
| 44 | + evaluationKind = cgf.getEvaluationKind(valueTy); |
| 45 | + |
| 46 | + TypeInfo valueTypeInfo = ctx.getTypeInfo(valueTy); |
| 47 | + TypeInfo atomicTypeInfo = ctx.getTypeInfo(atomicTy); |
| 48 | + uint64_t valueAlignInBits = valueTypeInfo.Align; |
| 49 | + uint64_t atomicAlignInBits = atomicTypeInfo.Align; |
| 50 | + valueSizeInBits = valueTypeInfo.Width; |
| 51 | + atomicSizeInBits = atomicTypeInfo.Width; |
| 52 | + assert(valueSizeInBits <= atomicSizeInBits); |
| 53 | + assert(valueAlignInBits <= atomicAlignInBits); |
| 54 | + |
| 55 | + atomicAlign = ctx.toCharUnitsFromBits(atomicAlignInBits); |
| 56 | + valueAlign = ctx.toCharUnitsFromBits(valueAlignInBits); |
| 57 | + if (lvalue.getAlignment().isZero()) |
| 58 | + lvalue.setAlignment(atomicAlign); |
| 59 | + |
| 60 | + this->lvalue = lvalue; |
| 61 | + } else { |
| 62 | + assert(!cir::MissingFeatures::atomicInfo()); |
| 63 | + cgf.cgm.errorNYI(loc, "AtomicInfo: non-simple lvalue"); |
| 64 | + } |
| 65 | + |
| 66 | + assert(!cir::MissingFeatures::atomicUseLibCall()); |
| 67 | + } |
| 68 | + |
| 69 | + QualType getValueType() const { return valueTy; } |
| 70 | + CharUnits getAtomicAlignment() const { return atomicAlign; } |
| 71 | + TypeEvaluationKind getEvaluationKind() const { return evaluationKind; } |
| 72 | + mlir::Value getAtomicPointer() const { |
| 73 | + if (lvalue.isSimple()) |
| 74 | + return lvalue.getPointer(); |
| 75 | + assert(!cir::MissingFeatures::atomicInfoGetAtomicPointer()); |
| 76 | + return nullptr; |
| 77 | + } |
| 78 | + Address getAtomicAddress() const { |
| 79 | + mlir::Type elemTy; |
| 80 | + if (lvalue.isSimple()) { |
| 81 | + elemTy = lvalue.getAddress().getElementType(); |
| 82 | + } else { |
| 83 | + assert(!cir::MissingFeatures::atomicInfoGetAtomicAddress()); |
| 84 | + cgf.cgm.errorNYI(loc, "AtomicInfo::getAtomicAddress: non-simple lvalue"); |
| 85 | + } |
| 86 | + return Address(getAtomicPointer(), elemTy, getAtomicAlignment()); |
| 87 | + } |
| 88 | + |
| 89 | + /// Is the atomic size larger than the underlying value type? |
| 90 | + /// |
| 91 | + /// Note that the absence of padding does not mean that atomic |
| 92 | + /// objects are completely interchangeable with non-atomic |
| 93 | + /// objects: we might have promoted the alignment of a type |
| 94 | + /// without making it bigger. |
| 95 | + bool hasPadding() const { return (valueSizeInBits != atomicSizeInBits); } |
| 96 | + |
| 97 | + bool emitMemSetZeroIfNecessary() const; |
| 98 | + |
| 99 | + /// Copy an atomic r-value into atomic-layout memory. |
| 100 | + void emitCopyIntoMemory(RValue rvalue) const; |
| 101 | + |
| 102 | + /// Project an l-value down to the value field. |
| 103 | + LValue projectValue() const { |
| 104 | + assert(lvalue.isSimple()); |
| 105 | + Address addr = getAtomicAddress(); |
| 106 | + if (hasPadding()) { |
| 107 | + cgf.cgm.errorNYI(loc, "AtomicInfo::projectValue: padding"); |
| 108 | + } |
| 109 | + |
| 110 | + assert(!cir::MissingFeatures::opTBAA()); |
| 111 | + return LValue::makeAddr(addr, getValueType(), lvalue.getBaseInfo()); |
| 112 | + } |
| 113 | + |
| 114 | +private: |
| 115 | + bool requiresMemSetZero(mlir::Type ty) const; |
| 116 | +}; |
| 117 | +} // namespace |
| 118 | + |
| 119 | +/// Does a store of the given IR type modify the full expected width? |
| 120 | +static bool isFullSizeType(CIRGenModule &cgm, mlir::Type ty, |
| 121 | + uint64_t expectedSize) { |
| 122 | + return cgm.getDataLayout().getTypeStoreSize(ty) * 8 == expectedSize; |
| 123 | +} |
| 124 | + |
| 125 | +/// Does the atomic type require memsetting to zero before initialization? |
| 126 | +/// |
| 127 | +/// The IR type is provided as a way of making certain queries faster. |
| 128 | +bool AtomicInfo::requiresMemSetZero(mlir::Type ty) const { |
| 129 | + // If the atomic type has size padding, we definitely need a memset. |
| 130 | + if (hasPadding()) |
| 131 | + return true; |
| 132 | + |
| 133 | + // Otherwise, do some simple heuristics to try to avoid it: |
| 134 | + switch (getEvaluationKind()) { |
| 135 | + // For scalars and complexes, check whether the store size of the |
| 136 | + // type uses the full size. |
| 137 | + case cir::TEK_Scalar: |
| 138 | + return !isFullSizeType(cgf.cgm, ty, atomicSizeInBits); |
| 139 | + case cir::TEK_Complex: |
| 140 | + cgf.cgm.errorNYI(loc, "AtomicInfo::requiresMemSetZero: complex type"); |
| 141 | + return false; |
| 142 | + |
| 143 | + // Padding in structs has an undefined bit pattern. User beware. |
| 144 | + case cir::TEK_Aggregate: |
| 145 | + return false; |
| 146 | + } |
| 147 | + llvm_unreachable("bad evaluation kind"); |
| 148 | +} |
| 149 | + |
| 150 | +bool AtomicInfo::emitMemSetZeroIfNecessary() const { |
| 151 | + assert(lvalue.isSimple()); |
| 152 | + Address addr = lvalue.getAddress(); |
| 153 | + if (!requiresMemSetZero(addr.getElementType())) |
| 154 | + return false; |
| 155 | + |
| 156 | + cgf.cgm.errorNYI(loc, |
| 157 | + "AtomicInfo::emitMemSetZeroIfNecessary: emit memset zero"); |
| 158 | + return false; |
| 159 | +} |
| 160 | + |
| 161 | +/// Copy an r-value into memory as part of storing to an atomic type. |
| 162 | +/// This needs to create a bit-pattern suitable for atomic operations. |
| 163 | +void AtomicInfo::emitCopyIntoMemory(RValue rvalue) const { |
| 164 | + assert(lvalue.isSimple()); |
| 165 | + |
| 166 | + // If we have an r-value, the rvalue should be of the atomic type, |
| 167 | + // which means that the caller is responsible for having zeroed |
| 168 | + // any padding. Just do an aggregate copy of that type. |
| 169 | + if (rvalue.isAggregate()) { |
| 170 | + cgf.cgm.errorNYI("copying aggregate into atomic lvalue"); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + // Okay, otherwise we're copying stuff. |
| 175 | + |
| 176 | + // Zero out the buffer if necessary. |
| 177 | + emitMemSetZeroIfNecessary(); |
| 178 | + |
| 179 | + // Drill past the padding if present. |
| 180 | + LValue tempLValue = projectValue(); |
| 181 | + |
| 182 | + // Okay, store the rvalue in. |
| 183 | + if (rvalue.isScalar()) { |
| 184 | + cgf.emitStoreOfScalar(rvalue.getValue(), tempLValue, /*isInit=*/true); |
| 185 | + } else { |
| 186 | + cgf.cgm.errorNYI("copying complex into atomic lvalue"); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +RValue CIRGenFunction::emitAtomicExpr(AtomicExpr *e) { |
| 191 | + QualType atomicTy = e->getPtr()->getType()->getPointeeType(); |
| 192 | + QualType memTy = atomicTy; |
| 193 | + if (const auto *ty = atomicTy->getAs<AtomicType>()) |
| 194 | + memTy = ty->getValueType(); |
| 195 | + |
| 196 | + Address ptr = emitPointerWithAlignment(e->getPtr()); |
| 197 | + |
| 198 | + assert(!cir::MissingFeatures::openCL()); |
| 199 | + if (e->getOp() == AtomicExpr::AO__c11_atomic_init) { |
| 200 | + LValue lvalue = makeAddrLValue(ptr, atomicTy); |
| 201 | + emitAtomicInit(e->getVal1(), lvalue); |
| 202 | + return RValue::get(nullptr); |
| 203 | + } |
| 204 | + |
| 205 | + assert(!cir::MissingFeatures::atomicExpr()); |
| 206 | + cgm.errorNYI(e->getSourceRange(), "atomic expr is NYI"); |
| 207 | + return RValue::get(nullptr); |
| 208 | +} |
| 209 | + |
| 210 | +void CIRGenFunction::emitAtomicInit(Expr *init, LValue dest) { |
| 211 | + AtomicInfo atomics(*this, dest, getLoc(init->getSourceRange())); |
| 212 | + |
| 213 | + switch (atomics.getEvaluationKind()) { |
| 214 | + case cir::TEK_Scalar: { |
| 215 | + mlir::Value value = emitScalarExpr(init); |
| 216 | + atomics.emitCopyIntoMemory(RValue::get(value)); |
| 217 | + return; |
| 218 | + } |
| 219 | + |
| 220 | + case cir::TEK_Complex: |
| 221 | + cgm.errorNYI(init->getSourceRange(), "emitAtomicInit: complex type"); |
| 222 | + return; |
| 223 | + |
| 224 | + case cir::TEK_Aggregate: |
| 225 | + cgm.errorNYI(init->getSourceRange(), "emitAtomicInit: aggregate type"); |
| 226 | + return; |
| 227 | + } |
| 228 | + |
| 229 | + llvm_unreachable("bad evaluation kind"); |
| 230 | +} |
0 commit comments