Skip to content

Commit c9164ce

Browse files
committed
Merge remote-tracking branch 'origin/main' into std-vector-op
2 parents 2e60f8f + d45823a commit c9164ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1145
-484
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,18 +1310,27 @@ def CIR_CXXDtorAttr : CIR_Attr<"CXXDtor", "cxx_dtor"> {
13101310
let description = [{
13111311
Functions with this attribute are CXX destructors
13121312
}];
1313+
<<<<<<< HEAD
13131314
let parameters = (ins "mlir::Type":$type,
13141315
OptionalParameter<"std::optional<const clang::CXXRecordDecl *>">:$recordDecl);
1316+
=======
1317+
let parameters = (ins "mlir::Type":$type);
1318+
>>>>>>> origin/main
13151319

13161320
let assemblyFormat = [{
13171321
`<` $type `>`
13181322
}];
13191323

13201324
let builders = [
1325+
<<<<<<< HEAD
13211326
AttrBuilderWithInferredContext<(ins "mlir::Type":$type,
13221327
CArg<"std::optional<const clang::CXXRecordDecl *>",
13231328
"std::nullopt">:$recordDecl), [{
13241329
return $_get(type.getContext(), type, recordDecl);
1330+
=======
1331+
AttrBuilderWithInferredContext<(ins "mlir::Type":$type), [{
1332+
return $_get(type.getContext(), type);
1333+
>>>>>>> origin/main
13251334
}]>
13261335
];
13271336
}

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,21 @@ def CIR_ConstantOp : CIR_Op<"const",[
459459
return ptrAttr.isNullValue();
460460
return false;
461461
}
462+
463+
template <typename T>
464+
T getValueAttr() { return mlir::dyn_cast<T>(getValue()); }
465+
466+
llvm::APInt getIntValue() {
467+
if (const auto intAttr = getValueAttr<cir::IntAttr>())
468+
return intAttr.getValue();
469+
llvm_unreachable("Expected an IntAttr in ConstantOp");
470+
}
471+
472+
bool getBoolValue() {
473+
if (const auto boolAttr = getValueAttr<cir::BoolAttr>())
474+
return boolAttr.getValue();
475+
llvm_unreachable("Expected a BoolAttr in ConstantOp");
476+
}
462477
}];
463478

464479
let hasFolder = 1;
@@ -3132,6 +3147,58 @@ def CIR_GetMethodOp : CIR_Op<"get_method"> {
31323147
let hasVerifier = 1;
31333148
}
31343149

3150+
//===----------------------------------------------------------------------===//
3151+
// GetElementOp
3152+
//===----------------------------------------------------------------------===//
3153+
3154+
def CIR_GetElementOp : CIR_Op<"get_element"> {
3155+
let summary = "Get the address of an array element";
3156+
let description = [{
3157+
The `cir.get_element` operation gets the address of a particular element
3158+
from the `base` array.
3159+
3160+
It expects a pointer to the `base` array and the `index` of the element.
3161+
3162+
Example:
3163+
```mlir
3164+
// Suppose we have a array.
3165+
!s32i = !cir.int<s, 32>
3166+
!arr_ty = !cir.array<!s32i x 4>
3167+
3168+
// Get the address of the element at index 1.
3169+
%elem_1 = cir.get_element %0[1] : (!cir.ptr<!array_ty>, !s32i) -> !cir.ptr<!s32i>
3170+
3171+
// Get the address of the element at index %i.
3172+
%i = ...
3173+
%elem_i = cir.get_element %0[%i] : (!cir.ptr<!array_ty>, !s32i) -> !cir.ptr<!s32i>
3174+
```
3175+
}];
3176+
3177+
let arguments = (ins
3178+
Arg<CIR_PtrToArray, "the base address of the array ">:$base,
3179+
Arg<CIR_AnyFundamentalIntType, "the index of the element">:$index
3180+
);
3181+
3182+
let results = (outs CIR_PointerType:$result);
3183+
3184+
let assemblyFormat = [{
3185+
$base `[` $index `]` `:` `(` qualified(type($base)) `,` qualified(type($index)) `)`
3186+
`->` qualified(type($result)) attr-dict
3187+
}];
3188+
3189+
let extraClassDeclaration = [{
3190+
// Get the type of the element.
3191+
mlir::Type getElementType() {
3192+
return getType().getPointee();
3193+
}
3194+
cir::PointerType getBaseType() {
3195+
return mlir::cast<cir::PointerType>(getBase().getType());
3196+
}
3197+
}];
3198+
3199+
let hasVerifier = 1;
3200+
}
3201+
31353202
//===----------------------------------------------------------------------===//
31363203
// VecInsertOp
31373204
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/Address.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class Address {
179179
return nullptr;
180180
return getPointer().getDefiningOp();
181181
}
182+
183+
template <typename T> T getDefiningOp() const {
184+
return mlir::dyn_cast_or_null<T>(getDefiningOp());
185+
}
182186
};
183187

184188
} // namespace clang::CIRGen

clang/lib/CIR/CodeGen/CIRGenAtomic.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -330,40 +330,41 @@ Address AtomicInfo::CreateTempAlloca() const {
330330
return TempAlloca;
331331
}
332332

333-
// If the value comes from a ConstOp + IntAttr, retrieve and skip a series
334-
// of casts if necessary.
335-
//
336-
// FIXME(cir): figure out warning issue and move this to CIRBaseBuilder.h
337-
static cir::IntAttr getConstOpIntAttr(mlir::Value v) {
338-
mlir::Operation *op = v.getDefiningOp();
339-
cir::IntAttr constVal;
340-
while (auto c = dyn_cast<cir::CastOp>(op))
341-
op = c.getOperand().getDefiningOp();
342-
if (auto c = dyn_cast<cir::ConstantOp>(op)) {
343-
if (mlir::isa<cir::IntType>(c.getType()))
344-
constVal = mlir::cast<cir::IntAttr>(c.getValue());
345-
}
346-
return constVal;
333+
static mlir::Value stripCasts(mlir::Value value) {
334+
while (auto castOp = value.getDefiningOp<cir::CastOp>())
335+
value = castOp.getOperand();
336+
return value;
337+
}
338+
339+
static cir::ConstantOp extractConstant(mlir::Value v) {
340+
return stripCasts(v).getDefiningOp<cir::ConstantOp>();
341+
}
342+
343+
// If the value comes from a ConstOp + IntAttr, retrieve and skip a series of
344+
// casts if necessary.
345+
static cir::IntAttr extractIntAttr(mlir::Value v) {
346+
if (auto c = extractConstant(v))
347+
return c.getValueAttr<cir::IntAttr>();
348+
return {};
347349
}
348350

349351
// Inspect a value that is the strong/weak flag for a compare-exchange. If it
350352
// is a constant of intergral or boolean type, set `val` to the constant's
351353
// boolean value and return true. Otherwise leave `val` unchanged and return
352354
// false.
353355
static bool isCstWeak(mlir::Value weakVal, bool &val) {
354-
mlir::Operation *op = weakVal.getDefiningOp();
355-
while (auto c = dyn_cast<cir::CastOp>(op)) {
356-
op = c.getOperand().getDefiningOp();
357-
}
358-
if (auto c = dyn_cast<cir::ConstantOp>(op)) {
359-
if (mlir::isa<cir::IntType>(c.getType())) {
360-
val = mlir::cast<cir::IntAttr>(c.getValue()).getUInt() != 0;
356+
if (auto c = extractConstant(weakVal)) {
357+
if (auto attr = c.getValueAttr<cir::IntAttr>()) {
358+
val = attr.getUInt() != 0;
361359
return true;
362-
} else if (mlir::isa<cir::BoolType>(c.getType())) {
363-
val = mlir::cast<cir::BoolAttr>(c.getValue()).getValue();
360+
}
361+
362+
if (auto attr = c.getValueAttr<cir::BoolAttr>()) {
363+
val = attr.getValue();
364364
return true;
365365
}
366366
}
367+
367368
return false;
368369
}
369370

@@ -456,7 +457,7 @@ static void emitAtomicCmpXchgFailureSet(
456457
cir::MemOrder SuccessOrder, cir::MemScopeKind Scope) {
457458

458459
cir::MemOrder FailureOrder;
459-
if (auto ordAttr = getConstOpIntAttr(FailureOrderVal)) {
460+
if (auto ordAttr = extractIntAttr(FailureOrderVal)) {
460461
// We should not ever get to a case where the ordering isn't a valid CABI
461462
// value, but it's hard to enforce that in general.
462463
auto ord = ordAttr.getUInt();
@@ -805,7 +806,7 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *Expr, Address Dest,
805806
}
806807

807808
// Handle constant scope.
808-
if (getConstOpIntAttr(Scope)) {
809+
if (extractIntAttr(Scope)) {
809810
assert(!cir::MissingFeatures::syncScopeID());
810811
llvm_unreachable("NYI");
811812
return;
@@ -1208,7 +1209,7 @@ RValue CIRGenFunction::emitAtomicExpr(AtomicExpr *E) {
12081209
E->getOp() == AtomicExpr::AO__scoped_atomic_load ||
12091210
E->getOp() == AtomicExpr::AO__scoped_atomic_load_n;
12101211

1211-
if (auto ordAttr = getConstOpIntAttr(Order)) {
1212+
if (auto ordAttr = extractIntAttr(Order)) {
12121213
// We should not ever get to a case where the ordering isn't a valid CABI
12131214
// value, but it's hard to enforce that in general.
12141215
auto ord = ordAttr.getUInt();

clang/lib/CIR/CodeGen/CIRGenBuilder.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,48 @@ mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc,
2828
return arrayPtr;
2929
}
3030

31-
mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,
31+
mlir::Value CIRGenBuilderTy::promoteArrayIndex(const clang::TargetInfo &ti,
32+
mlir::Location loc,
33+
mlir::Value index) {
34+
// Get the array index type.
35+
auto arrayIndexWidth = ti.getTypeWidth(clang::TargetInfo::IntType::SignedInt);
36+
mlir::Type arrayIndexType = getSIntNTy(arrayIndexWidth);
37+
38+
// If this is a boolean, zero-extend it to the array index type.
39+
if (auto boolTy = mlir::dyn_cast<cir::BoolType>(index.getType()))
40+
return create<cir::CastOp>(loc, arrayIndexType, cir::CastKind::bool_to_int,
41+
index);
42+
43+
// If this an integer, ensure that it is at least as width as the array index
44+
// type.
45+
if (auto intTy = mlir::dyn_cast<cir::IntType>(index.getType())) {
46+
if (intTy.getWidth() < arrayIndexWidth)
47+
return create<cir::CastOp>(loc, arrayIndexType, cir::CastKind::integral,
48+
index);
49+
}
50+
51+
return index;
52+
}
53+
54+
mlir::Value CIRGenBuilderTy::getArrayElement(const clang::TargetInfo &ti,
55+
mlir::Location arrayLocBegin,
3256
mlir::Location arrayLocEnd,
3357
mlir::Value arrayPtr,
3458
mlir::Type eltTy, mlir::Value idx,
3559
bool shouldDecay) {
60+
auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType());
61+
assert(arrayPtrTy && "expected pointer type");
62+
63+
// If the array pointer is not decayed, emit a GetElementOp.
64+
auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
65+
if (shouldDecay && arrayTy && arrayTy == eltTy) {
66+
auto eltPtrTy =
67+
getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
68+
return create<cir::GetElementOp>(arrayLocEnd, eltPtrTy, arrayPtr,
69+
promoteArrayIndex(ti, arrayLocBegin, idx));
70+
}
71+
72+
// If we don't have sufficient type information, emit a PtrStrideOp.
3673
mlir::Value basePtr = arrayPtr;
3774
if (shouldDecay)
3875
basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy);

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "clang/AST/Decl.h"
1818
#include "clang/AST/Type.h"
19+
#include "clang/Basic/TargetInfo.h"
1920
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
2021
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2122
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
@@ -1030,10 +1031,15 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
10301031
return create<cir::GetRuntimeMemberOp>(loc, resultTy, objectPtr, memberPtr);
10311032
}
10321033

1034+
/// Promote a value for use as an array index.
1035+
mlir::Value promoteArrayIndex(const clang::TargetInfo &TargetInfo,
1036+
mlir::Location loc, mlir::Value index);
1037+
10331038
/// Create a cir.ptr_stride operation to get access to an array element.
10341039
/// idx is the index of the element to access, shouldDecay is true if the
10351040
/// result should decay to a pointer to the element type.
1036-
mlir::Value getArrayElement(mlir::Location arrayLocBegin,
1041+
mlir::Value getArrayElement(const clang::TargetInfo &targetInfo,
1042+
mlir::Location arrayLocBegin,
10371043
mlir::Location arrayLocEnd, mlir::Value arrayPtr,
10381044
mlir::Type eltTy, mlir::Value idx,
10391045
bool shouldDecay);

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,11 @@ static mlir::Value makeAtomicFenceValue(CIRGenFunction &cgf,
382382
auto &builder = cgf.getBuilder();
383383
mlir::Value orderingVal = cgf.emitScalarExpr(expr->getArg(0));
384384

385-
auto constOrdering =
386-
mlir::dyn_cast<cir::ConstantOp>(orderingVal.getDefiningOp());
385+
auto constOrdering = orderingVal.getDefiningOp<cir::ConstantOp>();
387386
if (!constOrdering)
388387
llvm_unreachable("NYI: variable ordering not supported");
389388

390-
auto constOrderingAttr =
391-
mlir::dyn_cast<cir::IntAttr>(constOrdering.getValue());
392-
if (constOrderingAttr) {
389+
if (auto constOrderingAttr = constOrdering.getValueAttr<cir::IntAttr>()) {
393390
cir::MemOrder ordering =
394391
static_cast<cir::MemOrder>(constOrderingAttr.getUInt());
395392

clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,10 +2123,7 @@ castVecOfFPTypeToVecOfIntWithSameWidth(CIRGenBuilderTy &builder,
21232123

21242124
/// Get integer from a mlir::Value that is an int constant or a constant op.
21252125
static int64_t getIntValueFromConstOp(mlir::Value val) {
2126-
auto constOp = mlir::cast<cir::ConstantOp>(val.getDefiningOp());
2127-
return (mlir::cast<cir::IntAttr>(constOp.getValue()))
2128-
.getValue()
2129-
.getSExtValue();
2126+
return val.getDefiningOp<cir::ConstantOp>().getIntValue().getSExtValue();
21302127
}
21312128

21322129
static mlir::Value emitNeonSplat(CIRGenBuilderTy &builder, mlir::Location loc,

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ translateX86ToMsvcIntrin(unsigned BuiltinID) {
6868

6969
/// Get integer from a mlir::Value that is an int constant or a constant op.
7070
static int64_t getIntValueFromConstOp(mlir::Value val) {
71-
auto constOp = mlir::cast<cir::ConstantOp>(val.getDefiningOp());
72-
return (mlir::cast<cir::IntAttr>(constOp.getValue()))
73-
.getValue()
74-
.getSExtValue();
71+
return val.getDefiningOp<cir::ConstantOp>().getIntValue().getSExtValue();
7572
}
7673

7774
// Convert the mask from an integer type to a vector of i1.
@@ -274,9 +271,8 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
274271
case X86::BI__builtin_ia32_vec_ext_v4di: {
275272
unsigned NumElts = cast<cir::VectorType>(Ops[0].getType()).getSize();
276273

277-
auto constOp = cast<cir::ConstantOp>(Ops[1].getDefiningOp());
278-
auto intAttr = cast<cir::IntAttr>(constOp.getValue());
279-
uint64_t index = intAttr.getValue().getZExtValue();
274+
uint64_t index =
275+
Ops[1].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
280276

281277
index &= NumElts - 1;
282278

@@ -301,9 +297,8 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
301297
case X86::BI__builtin_ia32_vec_set_v4di: {
302298
unsigned NumElts = cast<cir::VectorType>(Ops[0].getType()).getSize();
303299

304-
auto constOp = cast<cir::ConstantOp>(Ops[2].getDefiningOp());
305-
auto intAttr = cast<cir::IntAttr>(constOp.getValue());
306-
uint64_t index = intAttr.getValue().getZExtValue();
300+
uint64_t index =
301+
Ops[2].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
307302

308303
index &= NumElts - 1;
309304

@@ -568,7 +563,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
568563
case X86::BI__builtin_ia32_movdqa64store512_mask:
569564
case X86::BI__builtin_ia32_storeaps512_mask:
570565
case X86::BI__builtin_ia32_storeapd512_mask:
571-
llvm_unreachable("vfmaddsubph256_round_mask3 NYI");
566+
return emitX86MaskedStore(
567+
*this, Ops,
568+
getContext().getTypeAlignInChars(E->getArg(1)->getType()).getAsAlign(),
569+
getLoc(E->getExprLoc()));
572570

573571
case X86::BI__builtin_ia32_loadups128_mask:
574572
case X86::BI__builtin_ia32_loadups256_mask:

clang/lib/CIR/CodeGen/CIRGenCall.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CIRGenCallee {
5454
enum class SpecialKind : uintptr_t {
5555
Invalid,
5656
Builtin,
57-
PsuedoDestructor,
57+
PseudoDestructor,
5858
Virtual,
5959

6060
Last = Virtual
@@ -128,8 +128,20 @@ class CIRGenCallee {
128128
return result;
129129
}
130130

131-
bool isPsuedoDestructor() const {
132-
return KindOrFunctionPointer == SpecialKind::PsuedoDestructor;
131+
static CIRGenCallee
132+
forPseudoDestructor(const clang::CXXPseudoDestructorExpr *expr) {
133+
CIRGenCallee result(SpecialKind::PseudoDestructor);
134+
result.PseudoDestructorInfo.Expr = expr;
135+
return result;
136+
}
137+
138+
bool isPseudoDestructor() const {
139+
return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
140+
}
141+
142+
const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
143+
assert(isPseudoDestructor());
144+
return PseudoDestructorInfo.Expr;
133145
}
134146

135147
bool isOrdinary() const {

0 commit comments

Comments
 (0)