Skip to content

Commit 3d88550

Browse files
committed
[CIR] Add initial support for atomic types
This patch adds the initial support for C11 atomic types, including: - Convert QualType that represents atomic types to CIR types; - Start emitting code for atomic value initializers.
1 parent 67af2f6 commit 3d88550

File tree

9 files changed

+316
-3
lines changed

9 files changed

+316
-3
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ struct MissingFeatures {
162162
static bool addressIsKnownNonNull() { return false; }
163163
static bool addressPointerAuthInfo() { return false; }
164164

165+
// Atomic
166+
static bool atomicExpr() { return false; }
167+
static bool atomicInfo() { return false; }
168+
static bool atomicInfoGetAtomicPointer() { return false; }
169+
static bool atomicInfoGetAtomicAddress() { return false; }
170+
static bool atomicUseLibCall() { return false; }
171+
165172
// Misc
166173
static bool abiArgInfo() { return false; }
167174
static bool addHeapAllocSiteMetadata() { return false; }
@@ -197,6 +204,7 @@ struct MissingFeatures {
197204
static bool cudaSupport() { return false; }
198205
static bool cxxRecordStaticMembers() { return false; }
199206
static bool dataLayoutTypeAllocSize() { return false; }
207+
static bool dataLayoutTypeStoreSize() { return false; }
200208
static bool deferredCXXGlobalInit() { return false; }
201209
static bool ehCleanupFlags() { return false; }
202210
static bool ehCleanupScope() { return false; }
@@ -232,6 +240,7 @@ struct MissingFeatures {
232240
static bool objCBlocks() { return false; }
233241
static bool objCGC() { return false; }
234242
static bool objCLifetime() { return false; }
243+
static bool openCL() { return false; }
235244
static bool openMP() { return false; }
236245
static bool opGlobalViewAttr() { return false; }
237246
static bool opTBAA() { return false; }
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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+
assert(!cir::MissingFeatures::dataLayoutTypeStoreSize());
123+
return true;
124+
}
125+
126+
/// Does the atomic type require memsetting to zero before initialization?
127+
///
128+
/// The IR type is provided as a way of making certain queries faster.
129+
bool AtomicInfo::requiresMemSetZero(mlir::Type ty) const {
130+
// If the atomic type has size padding, we definitely need a memset.
131+
if (hasPadding())
132+
return true;
133+
134+
// Otherwise, do some simple heuristics to try to avoid it:
135+
switch (getEvaluationKind()) {
136+
// For scalars and complexes, check whether the store size of the
137+
// type uses the full size.
138+
case cir::TEK_Scalar:
139+
return !isFullSizeType(cgf.cgm, ty, atomicSizeInBits);
140+
case cir::TEK_Complex:
141+
cgf.cgm.errorNYI(loc, "AtomicInfo::requiresMemSetZero: complex type");
142+
return false;
143+
144+
// Padding in structs has an undefined bit pattern. User beware.
145+
case cir::TEK_Aggregate:
146+
return false;
147+
}
148+
llvm_unreachable("bad evaluation kind");
149+
}
150+
151+
bool AtomicInfo::emitMemSetZeroIfNecessary() const {
152+
assert(lvalue.isSimple());
153+
Address addr = lvalue.getAddress();
154+
if (!requiresMemSetZero(addr.getElementType()))
155+
return false;
156+
157+
cgf.cgm.errorNYI(loc,
158+
"AtomicInfo::emitMemSetZeroIfNecessary: emit memset zero");
159+
return false;
160+
}
161+
162+
/// Copy an r-value into memory as part of storing to an atomic type.
163+
/// This needs to create a bit-pattern suitable for atomic operations.
164+
void AtomicInfo::emitCopyIntoMemory(RValue rvalue) const {
165+
assert(lvalue.isSimple());
166+
167+
// If we have an r-value, the rvalue should be of the atomic type,
168+
// which means that the caller is responsible for having zeroed
169+
// any padding. Just do an aggregate copy of that type.
170+
if (rvalue.isAggregate()) {
171+
cgf.cgm.errorNYI("copying aggregate into atomic lvalue");
172+
return;
173+
}
174+
175+
// Okay, otherwise we're copying stuff.
176+
177+
// Zero out the buffer if necessary.
178+
emitMemSetZeroIfNecessary();
179+
180+
// Drill past the padding if present.
181+
LValue tempLValue = projectValue();
182+
183+
// Okay, store the rvalue in.
184+
if (rvalue.isScalar()) {
185+
cgf.emitStoreOfScalar(rvalue.getValue(), tempLValue, /*isInit=*/true);
186+
} else {
187+
cgf.cgm.errorNYI("copying complex into atomic lvalue");
188+
}
189+
}
190+
191+
RValue CIRGenFunction::emitAtomicExpr(AtomicExpr *e) {
192+
QualType atomicTy = e->getPtr()->getType()->getPointeeType();
193+
QualType memTy = atomicTy;
194+
if (const auto *ty = atomicTy->getAs<AtomicType>())
195+
memTy = ty->getValueType();
196+
197+
Address ptr = emitPointerWithAlignment(e->getPtr());
198+
199+
assert(!cir::MissingFeatures::openCL());
200+
if (e->getOp() == AtomicExpr::AO__c11_atomic_init) {
201+
LValue lvalue = makeAddrLValue(ptr, atomicTy);
202+
emitAtomicInit(e->getVal1(), lvalue);
203+
return RValue::get(nullptr);
204+
}
205+
206+
assert(!cir::MissingFeatures::atomicExpr());
207+
cgm.errorNYI(e->getSourceRange(), "atomic expr is NYI");
208+
return RValue::get(nullptr);
209+
}
210+
211+
void CIRGenFunction::emitAtomicInit(Expr *init, LValue dest) {
212+
AtomicInfo atomics(*this, dest, getLoc(init->getSourceRange()));
213+
214+
switch (atomics.getEvaluationKind()) {
215+
case cir::TEK_Scalar: {
216+
mlir::Value value = emitScalarExpr(init);
217+
atomics.emitCopyIntoMemory(RValue::get(value));
218+
return;
219+
}
220+
221+
case cir::TEK_Complex:
222+
cgm.errorNYI(init->getSourceRange(), "emitAtomicInit: complex type");
223+
return;
224+
225+
case cir::TEK_Aggregate:
226+
cgm.errorNYI(init->getSourceRange(), "emitAtomicInit: aggregate type");
227+
return;
228+
}
229+
230+
llvm_unreachable("bad evaluation kind");
231+
}

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ Address CIRGenFunction::emitPointerWithAlignment(const Expr *expr,
184184
if (const UnaryOperator *uo = dyn_cast<UnaryOperator>(expr)) {
185185
// TODO(cir): maybe we should use cir.unary for pointers here instead.
186186
if (uo->getOpcode() == UO_AddrOf) {
187-
cgm.errorNYI(expr->getSourceRange(), "emitPointerWithAlignment: unary &");
188-
return Address::invalid();
187+
LValue lv = emitLValue(uo->getSubExpr());
188+
if (baseInfo)
189+
*baseInfo = lv.getBaseInfo();
190+
assert(!cir::MissingFeatures::opTBAA());
191+
return lv.getAddress();
189192
}
190193
}
191194

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
10601060

10611061
return maybePromoteBoolResult(resOp.getResult(), resTy);
10621062
}
1063+
1064+
mlir::Value VisitAtomicExpr(AtomicExpr *e) {
1065+
return cgf.emitAtomicExpr(e).getValue();
1066+
}
10631067
};
10641068

10651069
LValue ScalarExprEmitter::emitCompoundAssignLValue(

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,9 @@ class CIRGenFunction : public CIRGenTypeCache {
893893

894894
Address emitArrayToPointerDecay(const Expr *array);
895895

896+
RValue emitAtomicExpr(AtomicExpr *e);
897+
void emitAtomicInit(Expr *init, LValue dest);
898+
896899
AutoVarEmission emitAutoVarAlloca(const clang::VarDecl &d,
897900
mlir::OpBuilder::InsertPoint ip = {});
898901

@@ -1197,7 +1200,7 @@ class CIRGenFunction : public CIRGenTypeCache {
11971200
/// reasonable to just ignore the returned alignment when it isn't from an
11981201
/// explicit source.
11991202
Address emitPointerWithAlignment(const clang::Expr *expr,
1200-
LValueBaseInfo *baseInfo);
1203+
LValueBaseInfo *baseInfo = nullptr);
12011204

12021205
/// Emits a reference binding to the passed in expression.
12031206
RValue emitReferenceBindingToExpr(const Expr *e);

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,20 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
491491
break;
492492
}
493493

494+
case Type::Atomic: {
495+
QualType valueType = cast<AtomicType>(ty)->getValueType();
496+
resultType = convertTypeForMem(valueType);
497+
498+
// Pad out to the inflated size if necessary.
499+
uint64_t valueSize = astContext.getTypeSize(valueType);
500+
uint64_t atomicSize = astContext.getTypeSize(ty);
501+
if (valueSize != atomicSize) {
502+
cgm.errorNYI("convertType: atomic type value size != atomic size");
503+
}
504+
505+
break;
506+
}
507+
494508
default:
495509
cgm.errorNYI(SourceLocation(), "processing of type",
496510
type->getTypeClassName());

clang/lib/CIR/CodeGen/CIRGenValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class LValue {
190190
bool isSimple() const { return lvType == Simple; }
191191
bool isVectorElt() const { return lvType == VectorElt; }
192192
bool isBitField() const { return lvType == BitField; }
193+
bool isGlobalReg() const { return lvType == GlobalReg; }
193194
bool isVolatile() const { return quals.hasVolatile(); }
194195

195196
bool isVolatileQualified() const { return quals.hasVolatile(); }

clang/lib/CIR/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
88

99
add_clang_library(clangCIR
1010
CIRGenerator.cpp
11+
CIRGenAtomic.cpp
1112
CIRGenBuilder.cpp
1213
CIRGenCall.cpp
1314
CIRGenClass.cpp

clang/test/CIR/CodeGen/atomic.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7+
8+
void f1(void) {
9+
_Atomic(int) x = 42;
10+
}
11+
12+
// CIR-LABEL: @f1
13+
// CIR: %[[SLOT:.+]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init] {alignment = 4 : i64}
14+
// CIR-NEXT: %[[INIT:.+]] = cir.const #cir.int<42> : !s32i
15+
// CIR-NEXT: cir.store align(4) %[[INIT]], %[[SLOT]] : !s32i, !cir.ptr<!s32i>
16+
// CIR: }
17+
18+
// LLVM-LABEL: @f1
19+
// LLVM: %[[SLOT:.+]] = alloca i32, i64 1, align 4
20+
// LLVM-NEXT: store i32 42, ptr %[[SLOT]], align 4
21+
// LLVM: }
22+
23+
// OGCG-LABEL: @f1
24+
// OGCG: %[[SLOT:.+]] = alloca i32, align 4
25+
// OGCG-NEXT: store i32 42, ptr %[[SLOT]], align 4
26+
// OGCG: }
27+
28+
void f2(void) {
29+
_Atomic(int) x;
30+
__c11_atomic_init(&x, 42);
31+
}
32+
33+
// CIR-LABEL: @f2
34+
// CIR: %[[SLOT:.+]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x"] {alignment = 4 : i64}
35+
// CIR-NEXT: %[[INIT:.+]] = cir.const #cir.int<42> : !s32i
36+
// CIR-NEXT: cir.store align(4) %[[INIT]], %[[SLOT]] : !s32i, !cir.ptr<!s32i>
37+
// CIR: }
38+
39+
// LLVM-LABEL: @f2
40+
// LLVM: %[[SLOT:.+]] = alloca i32, i64 1, align 4
41+
// LLVM-NEXT: store i32 42, ptr %[[SLOT]], align 4
42+
// LLVM: }
43+
44+
// OGCG-LABEL: @f2
45+
// OGCG: %[[SLOT:.+]] = alloca i32, align 4
46+
// OGCG-NEXT: store i32 42, ptr %[[SLOT]], align 4
47+
// OGCG: }

0 commit comments

Comments
 (0)