Skip to content

Commit 8c264de

Browse files
authored
[CIR] Let ConstantOp builder infer its type automatically (#1578)
1 parent efc735d commit 8c264de

File tree

15 files changed

+44
-62
lines changed

15 files changed

+44
-62
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
4646
mlir::Value getConstAPSInt(mlir::Location loc, const llvm::APSInt &val) {
4747
auto ty =
4848
cir::IntType::get(getContext(), val.getBitWidth(), val.isSigned());
49-
return create<cir::ConstantOp>(loc, ty, getAttr<cir::IntAttr>(ty, val));
49+
return create<cir::ConstantOp>(loc, getAttr<cir::IntAttr>(ty, val));
5050
}
5151

5252
mlir::Value getSignedInt(mlir::Location loc, int64_t val, unsigned numBits) {
@@ -63,20 +63,20 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
6363

6464
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ,
6565
const llvm::APInt &val) {
66-
return create<cir::ConstantOp>(loc, typ, getAttr<cir::IntAttr>(typ, val));
66+
return create<cir::ConstantOp>(loc, getAttr<cir::IntAttr>(typ, val));
6767
}
6868

6969
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr) {
70-
return create<cir::ConstantOp>(loc, attr.getType(), attr);
70+
return create<cir::ConstantOp>(loc, attr);
7171
}
7272

7373
// Creates constant null value for integral type ty.
7474
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc) {
75-
return create<cir::ConstantOp>(loc, ty, getZeroInitAttr(ty));
75+
return create<cir::ConstantOp>(loc, getZeroInitAttr(ty));
7676
}
7777

7878
cir::ConstantOp getBool(bool state, mlir::Location loc) {
79-
return create<cir::ConstantOp>(loc, getBoolTy(), getCIRBoolAttr(state));
79+
return create<cir::ConstantOp>(loc, getCIRBoolAttr(state));
8080
}
8181
cir::ConstantOp getFalse(mlir::Location loc) { return getBool(false, loc); }
8282
cir::ConstantOp getTrue(mlir::Location loc) { return getBool(true, loc); }
@@ -630,7 +630,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
630630
// Creates constant nullptr for pointer type ty.
631631
cir::ConstantOp getNullPtr(mlir::Type ty, mlir::Location loc) {
632632
assert(!MissingFeatures::targetCodeGenInfoGetNullPointer());
633-
return create<cir::ConstantOp>(loc, ty, getConstPtrAttr(ty, 0));
633+
return create<cir::ConstantOp>(loc, getConstPtrAttr(ty, 0));
634634
}
635635

636636
/// Create a loop condition.

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,6 @@ def ConstantOp : CIR_Op<"const",
407407
// The constant operation returns a single value of CIR_AnyType.
408408
let results = (outs CIR_AnyType:$res);
409409

410-
let builders = [
411-
OpBuilder<(ins "cir::BoolAttr":$value), [{
412-
build($_builder, $_state, value.getType(), value);
413-
}]>
414-
];
415-
416410
let assemblyFormat = "attr-dict $value";
417411

418412
let hasVerifier = 1;
@@ -5704,9 +5698,9 @@ def SignBitOp : CIR_Op<"signbit", [Pure]> {
57045698
def LinkerOptionsOp : CIR_Op<"linker_options"> {
57055699
let summary = "Options to pass to the linker when the object file is linked";
57065700
let description = [{
5707-
Pass the given options to the linker when the resulting object file
5701+
Pass the given options to the linker when the resulting object file
57085702
is linked.
5709-
This is used extensively on Windows to determine the C runtime that the
5703+
This is used extensively on Windows to determine the C runtime that the
57105704
object files should link against.
57115705

57125706
Examples:

clang/lib/CIR/CodeGen/CIRGenBuilder.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
5858
}
5959

6060
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,
61-
uint64_t C) {
62-
auto intTy = mlir::dyn_cast<cir::IntType>(t);
63-
assert(intTy && "expected cir::IntType");
64-
return create<cir::ConstantOp>(loc, intTy, cir::IntAttr::get(t, C));
61+
uint64_t c) {
62+
assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType");
63+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(t, c));
6564
}
6665

6766
void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset(

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -546,27 +546,23 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
546546
//
547547
cir::ConstantOp getUInt8(uint8_t c, mlir::Location loc) {
548548
auto uInt8Ty = getUInt8Ty();
549-
return create<cir::ConstantOp>(loc, uInt8Ty, cir::IntAttr::get(uInt8Ty, c));
549+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(uInt8Ty, c));
550550
}
551551
cir::ConstantOp getSInt32(int32_t c, mlir::Location loc) {
552552
auto sInt32Ty = getSInt32Ty();
553-
return create<cir::ConstantOp>(loc, sInt32Ty,
554-
cir::IntAttr::get(sInt32Ty, c));
553+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(sInt32Ty, c));
555554
}
556555
cir::ConstantOp getUInt32(uint32_t C, mlir::Location loc) {
557556
auto uInt32Ty = getUInt32Ty();
558-
return create<cir::ConstantOp>(loc, uInt32Ty,
559-
cir::IntAttr::get(uInt32Ty, C));
557+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(uInt32Ty, C));
560558
}
561559
cir::ConstantOp getSInt64(uint64_t C, mlir::Location loc) {
562560
auto sInt64Ty = getSInt64Ty();
563-
return create<cir::ConstantOp>(loc, sInt64Ty,
564-
cir::IntAttr::get(sInt64Ty, C));
561+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(sInt64Ty, C));
565562
}
566563
cir::ConstantOp getUInt64(uint64_t C, mlir::Location loc) {
567564
auto uInt64Ty = getUInt64Ty();
568-
return create<cir::ConstantOp>(loc, uInt64Ty,
569-
cir::IntAttr::get(uInt64Ty, C));
565+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(uInt64Ty, C));
570566
}
571567

572568
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal);
@@ -579,7 +575,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
579575
llvm::APFloat fpVal) {
580576
assert((mlir::isa<cir::SingleType, cir::DoubleType>(t)) &&
581577
"expected cir::SingleType or cir::DoubleType");
582-
return create<cir::ConstantOp>(loc, t, getAttr<cir::FPAttr>(t, fpVal));
578+
return create<cir::ConstantOp>(loc, getAttr<cir::FPAttr>(t, fpVal));
583579
}
584580

585581
cir::IsFPClassOp createIsFPClass(mlir::Location loc, mlir::Value src,
@@ -590,19 +586,19 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
590586
/// Create constant nullptr for pointer-to-data-member type ty.
591587
cir::ConstantOp getNullDataMemberPtr(cir::DataMemberType ty,
592588
mlir::Location loc) {
593-
return create<cir::ConstantOp>(loc, ty, getNullDataMemberAttr(ty));
589+
return create<cir::ConstantOp>(loc, getNullDataMemberAttr(ty));
594590
}
595591

596592
cir::ConstantOp getNullMethodPtr(cir::MethodType ty, mlir::Location loc) {
597-
return create<cir::ConstantOp>(loc, ty, getNullMethodAttr(ty));
593+
return create<cir::ConstantOp>(loc, getNullMethodAttr(ty));
598594
}
599595

600596
cir::ConstantOp getZero(mlir::Location loc, mlir::Type ty) {
601597
// TODO: dispatch creation for primitive types.
602598
assert((mlir::isa<cir::RecordType>(ty) || mlir::isa<cir::ArrayType>(ty) ||
603599
mlir::isa<cir::VectorType>(ty)) &&
604600
"NYI for other types");
605-
return create<cir::ConstantOp>(loc, ty, cir::ZeroAttr::get(ty));
601+
return create<cir::ConstantOp>(loc, cir::ZeroAttr::get(ty));
606602
}
607603

608604
//

clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,7 +2151,7 @@ static mlir::Value emitNeonShiftVector(CIRGenBuilderTy &builder,
21512151
cir::IntAttr::get(vecTy.getEltType(), shiftAmt)};
21522152
cir::ConstVectorAttr constVecAttr = cir::ConstVectorAttr::get(
21532153
vecTy, mlir::ArrayAttr::get(builder.getContext(), vecAttr));
2154-
return builder.create<cir::ConstantOp>(loc, vecTy, constVecAttr);
2154+
return builder.create<cir::ConstantOp>(loc, constVecAttr);
21552155
}
21562156

21572157
/// Build ShiftOp of vector type whose shift amount is a vector built
@@ -2202,7 +2202,7 @@ static void vecExtendIntValue(CIRGenFunction &cgf, cir::VectorType argVTy,
22022202
arg = builder.createIntCast(arg, eltTy);
22032203
mlir::Value zero = builder.getConstInt(loc, cgf.SizeTy, 0);
22042204
mlir::Value poison = builder.create<cir::ConstantOp>(
2205-
loc, eltTy, builder.getAttr<cir::PoisonAttr>(eltTy));
2205+
loc, builder.getAttr<cir::PoisonAttr>(eltTy));
22062206
arg = builder.create<cir::VecInsertOp>(
22072207
loc, builder.create<cir::VecSplatOp>(loc, argVTy, poison), arg, zero);
22082208
}

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,19 +2033,19 @@ mlir::Value CIRGenModule::emitMemberPointerConstant(const UnaryOperator *E) {
20332033
auto ty = mlir::cast<cir::MethodType>(convertType(E->getType()));
20342034
if (methodDecl->isVirtual())
20352035
return builder.create<cir::ConstantOp>(
2036-
loc, ty, getCXXABI().buildVirtualMethodAttr(ty, methodDecl));
2036+
loc, getCXXABI().buildVirtualMethodAttr(ty, methodDecl));
20372037

20382038
auto methodFuncOp = GetAddrOfFunction(methodDecl);
20392039
return builder.create<cir::ConstantOp>(
2040-
loc, ty, builder.getMethodAttr(ty, methodFuncOp));
2040+
loc, builder.getMethodAttr(ty, methodFuncOp));
20412041
}
20422042

20432043
auto ty = mlir::cast<cir::DataMemberType>(convertType(E->getType()));
20442044

20452045
// Otherwise, a member data pointer.
20462046
const auto *fieldDecl = cast<FieldDecl>(decl);
20472047
return builder.create<cir::ConstantOp>(
2048-
loc, ty, builder.getDataMemberAttr(ty, fieldDecl->getFieldIndex()));
2048+
loc, builder.getDataMemberAttr(ty, fieldDecl->getFieldIndex()));
20492049
}
20502050

20512051
mlir::Attribute ConstantEmitter::emitAbstract(const Expr *E,

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
174174
mlir::Value VisitIntegerLiteral(const IntegerLiteral *E) {
175175
mlir::Type Ty = CGF.convertType(E->getType());
176176
return Builder.create<cir::ConstantOp>(
177-
CGF.getLoc(E->getExprLoc()), Ty,
177+
CGF.getLoc(E->getExprLoc()),
178178
Builder.getAttr<cir::IntAttr>(Ty, E->getValue()));
179179
}
180180

@@ -186,14 +186,14 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
186186
assert(mlir::isa<cir::CIRFPTypeInterface>(Ty) &&
187187
"expect floating-point type");
188188
return Builder.create<cir::ConstantOp>(
189-
CGF.getLoc(E->getExprLoc()), Ty,
189+
CGF.getLoc(E->getExprLoc()),
190190
Builder.getAttr<cir::FPAttr>(Ty, E->getValue()));
191191
}
192192
mlir::Value VisitCharacterLiteral(const CharacterLiteral *E) {
193193
mlir::Type Ty = CGF.convertType(E->getType());
194194
auto loc = CGF.getLoc(E->getExprLoc());
195195
auto init = cir::IntAttr::get(Ty, E->getValue());
196-
return Builder.create<cir::ConstantOp>(loc, Ty, init);
196+
return Builder.create<cir::ConstantOp>(loc, init);
197197
}
198198
mlir::Value VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
199199
llvm_unreachable("NYI");
@@ -2000,7 +2000,7 @@ mlir::Value ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
20002000
// Zero-initialize any remaining values.
20012001
if (NumInitElements < VectorType.getSize()) {
20022002
mlir::Value ZeroValue = CGF.getBuilder().create<cir::ConstantOp>(
2003-
CGF.getLoc(E->getSourceRange()), VectorType.getEltType(),
2003+
CGF.getLoc(E->getSourceRange()),
20042004
CGF.getBuilder().getZeroInitAttr(VectorType.getEltType()));
20052005
for (uint64_t i = NumInitElements; i < VectorType.getSize(); ++i) {
20062006
Elements.push_back(ZeroValue);

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ class CIRGenFunction : public CIRGenTypeCache {
807807
bool isReference() const { return ValueAndIsReference.getInt(); }
808808
LValue getReferenceLValue(CIRGenFunction &CGF, Expr *refExpr) const {
809809
assert(isReference());
810-
// create<cir::ConstantOp>(loc, ty, cir::ZeroAttr::get(ty));
810+
// create<cir::ConstantOp>(loc, cir::ZeroAttr::get(ty));
811811
// CGF.getBuilder().const
812812
// return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
813813
// refExpr->getType());

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,7 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp oldSym, cir::GlobalOp newSym) {
10351035
auto ar = cast<ConstArrayAttr>(init);
10361036
mlir::OpBuilder::InsertionGuard guard(builder);
10371037
builder.setInsertionPointAfter(c);
1038-
auto newUser =
1039-
builder.create<cir::ConstantOp>(c.getLoc(), ar.getType(), ar);
1038+
auto newUser = builder.create<cir::ConstantOp>(c.getLoc(), ar);
10401039
c.replaceAllUsesWith(newUser.getOperation());
10411040
}
10421041
}

clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,10 @@ class CIRSwitchOpFlattening : public mlir::OpRewritePattern<cir::SwitchOp> {
654654
auto uIntType = cir::IntType::get(op.getContext(), 32, false);
655655

656656
auto rangeLength = rewriter.create<cir::ConstantOp>(
657-
op.getLoc(), sIntType,
658-
cir::IntAttr::get(op.getContext(), sIntType, upperBound - lowerBound));
657+
op.getLoc(), cir::IntAttr::get(sIntType, upperBound - lowerBound));
659658

660659
auto lowerBoundValue = rewriter.create<cir::ConstantOp>(
661-
op.getLoc(), sIntType,
662-
cir::IntAttr::get(op.getContext(), sIntType, lowerBound));
660+
op.getLoc(), cir::IntAttr::get(sIntType, lowerBound));
663661
auto diffValue =
664662
rewriter.create<cir::BinOp>(op.getLoc(), sIntType, cir::BinOpKind::Sub,
665663
op.getCondition(), lowerBoundValue);

0 commit comments

Comments
 (0)