Skip to content

Commit 4b17e83

Browse files
committed
[CIR] Make ZeroAttr use AttrBuilderWithInferredContext
1 parent 099c13c commit 4b17e83

File tree

6 files changed

+19
-17
lines changed

6 files changed

+19
-17
lines changed

Diff for: clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,21 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
133133

134134
cir::BoolAttr getFalseAttr() { return getCIRBoolAttr(false); }
135135

136-
mlir::TypedAttr getZeroAttr(mlir::Type t) {
137-
return cir::ZeroAttr::get(getContext(), t);
138-
}
139-
140136
mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
141137
if (mlir::isa<cir::IntType>(ty))
142138
return cir::IntAttr::get(ty, 0);
143139
if (cir::isAnyFloatingPointType(ty))
144140
return cir::FPAttr::getZero(ty);
145141
if (auto complexType = mlir::dyn_cast<cir::ComplexType>(ty))
146-
return getZeroAttr(complexType);
142+
return cir::ZeroAttr::get(complexType);
147143
if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty))
148-
return getZeroAttr(arrTy);
144+
return cir::ZeroAttr::get(arrTy);
149145
if (auto vecTy = mlir::dyn_cast<cir::VectorType>(ty))
150-
return getZeroAttr(vecTy);
146+
return cir::ZeroAttr::get(vecTy);
151147
if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty))
152148
return getConstNullPtrAttr(ptrTy);
153149
if (auto RecordTy = mlir::dyn_cast<cir::RecordType>(ty))
154-
return getZeroAttr(RecordTy);
150+
return cir::ZeroAttr::get(RecordTy);
155151
if (auto methodTy = mlir::dyn_cast<cir::MethodType>(ty))
156152
return getNullMethodAttr(methodTy);
157153
if (mlir::isa<cir::BoolType>(ty)) {

Diff for: clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

+7
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ def ZeroAttr : CIR_Attr<"Zero", "zero", [TypedAttrInterface]> {
231231
}];
232232

233233
let parameters = (ins AttributeSelfTypeParameter<"">:$type);
234+
235+
let builders = [
236+
AttrBuilderWithInferredContext<(ins "mlir::Type":$type), [{
237+
return $_get(type.getContext(), type);
238+
}]>
239+
];
240+
234241
let assemblyFormat = [{}];
235242
}
236243

Diff for: clang/lib/CIR/CodeGen/CIRGenBuilder.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
169169
// a #cir.const_array.
170170
if (lastNonZeroPos == llvm::StringRef::npos) {
171171
auto arrayTy = cir::ArrayType::get(eltTy, finalSize);
172-
return getZeroAttr(arrayTy);
172+
return cir::ZeroAttr::get(arrayTy);
173173
}
174174
// We will use trailing zeros only if there are more than one zero
175175
// at the end
@@ -214,7 +214,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
214214

215215
// Return zero or anonymous constant record.
216216
if (isZero)
217-
return cir::ZeroAttr::get(getContext(), recordTy);
217+
return cir::ZeroAttr::get(recordTy);
218218
return cir::ConstRecordAttr::get(recordTy, arrayAttr);
219219
}
220220

@@ -602,7 +602,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
602602
assert((mlir::isa<cir::RecordType>(ty) || mlir::isa<cir::ArrayType>(ty) ||
603603
mlir::isa<cir::VectorType>(ty)) &&
604604
"NYI for other types");
605-
return create<cir::ConstantOp>(loc, ty, getZeroAttr(ty));
605+
return create<cir::ConstantOp>(loc, ty, cir::ZeroAttr::get(ty));
606606
}
607607

608608
//

Diff for: clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ static mlir::TypedAttr computePadding(CIRGenModule &CGM, CharUnits size) {
4848
auto arSize = size.getQuantity();
4949
auto &bld = CGM.getBuilder();
5050
if (size > CharUnits::One()) {
51-
SmallVector<mlir::Attribute, 4> elts(arSize, bld.getZeroAttr(eltTy));
51+
SmallVector<mlir::Attribute, 4> elts(arSize, cir::ZeroAttr::get(eltTy));
5252
return bld.getConstArray(mlir::ArrayAttr::get(bld.getContext(), elts),
5353
cir::ArrayType::get(eltTy, arSize));
5454
} else {
55-
return cir::ZeroAttr::get(bld.getContext(), eltTy);
55+
return cir::ZeroAttr::get(eltTy);
5656
}
5757
}
5858

@@ -1726,8 +1726,7 @@ mlir::Attribute ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
17261726
// assignments and whatnots). Since this is for globals shouldn't
17271727
// be a problem for the near future.
17281728
if (CD->isTrivial() && CD->isDefaultConstructor())
1729-
return cir::ZeroAttr::get(CGM.getBuilder().getContext(),
1730-
CGM.convertType(D.getType()));
1729+
return cir::ZeroAttr::get(CGM.convertType(D.getType()));
17311730
}
17321731
}
17331732
InConstantContext = D.hasConstantInitialization();

Diff for: clang/lib/CIR/CodeGen/CIRGenFunction.h

+1-1
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, getZeroAttr(ty));
810+
// create<cir::ConstantOp>(loc, ty, cir::ZeroAttr::get(ty));
811811
// CGF.getBuilder().const
812812
// return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
813813
// refExpr->getType());

Diff for: clang/lib/CIR/CodeGen/CIRGenModule.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ CIRGenModule::getConstantArrayFromStringLiteral(const StringLiteral *e) {
17161716
// If the string is full of null bytes, emit a #cir.zero instead.
17171717
if (std::all_of(elementValues.begin(), elementValues.end(),
17181718
[](uint32_t x) { return x == 0; }))
1719-
return builder.getZeroAttr(arrayTy);
1719+
return cir::ZeroAttr::get(arrayTy);
17201720

17211721
// Otherwise emit a constant array holding the characters.
17221722
SmallVector<mlir::Attribute, 32> elements;

0 commit comments

Comments
 (0)