Skip to content

Commit d5f48dc

Browse files
authored
[CIR][NFC] Replace uses of isa/dyn_cast/cast/... member functions (#703)
Mechanical rewrite to use the corresponding free functions; fixes #702. I used a slightly modified version of the `clang-tidy` check provided in https://discourse.llvm.org/t/psa-deprecating-cast-isa-methods-in-some-classes/70909 to rewrite the C++ source files, regular expressions for the TableGen files, and manual cleanups where needed (e.g. chains like `x.foo().cast<A>().bar().cast<B>()`) I applied the following heuristic to determine which namespace prefix to use: - If the target type is not qualified, and the TU has `using namespace mlir` or the code is inside the `mlir` namespace -> use a plain `isa`/`cast`/... - Exception: Always qualify inside custom types and attributes, because their base classes define the very members we want to get rid of. - Else. i.e. the target type is qualified as `::mlir::` or `mlir::`, use that prefix. The `clang-tidy` check also rewrote `dyn_cast_or_null` to `dyn_cast_if_present`. I think that's useful because the former variant is going to be deprecated as well in the future. I'm using `-Werror=deprecated-declarations` to test the change (see 6b7420a); this required also changing two occurrences of `StringRef::equals` to `==`. I could also just drop the commit here; maybe we want to enable `-Werror` in general (there aren't too many other warnings left in the codebase). --------- Signed-off-by: Julian Oppermann <[email protected]>
1 parent d2f2fde commit d5f48dc

Some content is hidden

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

54 files changed

+725
-679
lines changed

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
133133
}
134134

135135
mlir::Value createShift(mlir::Value lhs, unsigned bits, bool isShiftLeft) {
136-
auto width = lhs.getType().dyn_cast<mlir::cir::IntType>().getWidth();
136+
auto width = mlir::dyn_cast<mlir::cir::IntType>(lhs.getType()).getWidth();
137137
auto shift = llvm::APInt(width, bits);
138138
return createShift(lhs, shift, isShiftLeft);
139139
}
@@ -197,7 +197,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
197197
mlir::Value dst, bool _volatile = false,
198198
::mlir::IntegerAttr align = {},
199199
::mlir::cir::MemOrderAttr order = {}) {
200-
if (dst.getType().cast<mlir::cir::PointerType>().getPointee() !=
200+
if (mlir::cast<mlir::cir::PointerType>(dst.getType()).getPointee() !=
201201
val.getType())
202202
dst = createPtrBitcast(dst, val.getType());
203203
return create<mlir::cir::StoreOp>(loc, val, dst, _volatile, align, order);
@@ -312,11 +312,12 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
312312
mlir::Value createGetMemberOp(mlir::Location &loc, mlir::Value structPtr,
313313
const char *fldName, unsigned idx) {
314314

315-
assert(structPtr.getType().isa<mlir::cir::PointerType>());
315+
assert(mlir::isa<mlir::cir::PointerType>(structPtr.getType()));
316316
auto structBaseTy =
317-
structPtr.getType().cast<mlir::cir::PointerType>().getPointee();
318-
assert(structBaseTy.isa<mlir::cir::StructType>());
319-
auto fldTy = structBaseTy.cast<mlir::cir::StructType>().getMembers()[idx];
317+
mlir::cast<mlir::cir::PointerType>(structPtr.getType()).getPointee();
318+
assert(mlir::isa<mlir::cir::StructType>(structBaseTy));
319+
auto fldTy =
320+
mlir::cast<mlir::cir::StructType>(structBaseTy).getMembers()[idx];
320321
auto fldPtrTy = ::mlir::cir::PointerType::get(getContext(), fldTy);
321322
return create<mlir::cir::GetMemberOp>(loc, fldPtrTy, structPtr, fldName,
322323
idx);
@@ -340,7 +341,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
340341
if (srcTy == newTy)
341342
return src;
342343

343-
if (srcTy.isa<mlir::cir::BoolType>() && newTy.isa<mlir::cir::IntType>())
344+
if (mlir::isa<mlir::cir::BoolType>(srcTy) &&
345+
mlir::isa<mlir::cir::IntType>(newTy))
344346
return createBoolToInt(src, newTy);
345347

346348
llvm_unreachable("unhandled extension cast");
@@ -360,7 +362,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
360362
}
361363

362364
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy) {
363-
assert(src.getType().isa<mlir::cir::PointerType>() && "expected ptr src");
365+
assert(mlir::isa<mlir::cir::PointerType>(src.getType()) &&
366+
"expected ptr src");
364367
return createBitcast(src, getPointerTo(newPointeeTy));
365368
}
366369

@@ -430,8 +433,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
430433
mlir::TypedAttr getConstPtrAttr(mlir::Type t, int64_t v) {
431434
auto val =
432435
mlir::IntegerAttr::get(mlir::IntegerType::get(t.getContext(), 64), v);
433-
return mlir::cir::ConstPtrAttr::get(getContext(),
434-
t.cast<mlir::cir::PointerType>(), val);
436+
return mlir::cir::ConstPtrAttr::get(
437+
getContext(), mlir::cast<mlir::cir::PointerType>(t), val);
435438
}
436439

437440
// Creates constant nullptr for pointer type ty.

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ def ConstArrayAttr : CIR_Attr<"ConstArray", "const_array", [TypedAttrInterface]>
125125
AttrBuilderWithInferredContext<(ins "mlir::cir::ArrayType":$type,
126126
"Attribute":$elts), [{
127127
int zeros = 0;
128-
auto typeSize = type.cast<mlir::cir::ArrayType>().getSize();
129-
if (auto str = elts.dyn_cast<mlir::StringAttr>())
128+
auto typeSize = mlir::cast<mlir::cir::ArrayType>(type).getSize();
129+
if (auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
130130
zeros = typeSize - str.size();
131131
else
132-
zeros = typeSize - elts.cast<mlir::ArrayAttr>().size();
132+
zeros = typeSize - mlir::cast<mlir::ArrayAttr>(elts).size();
133133

134134
return $_get(type.getContext(), type, elts, zeros);
135135
}]>
@@ -200,7 +200,7 @@ def IntAttr : CIR_Attr<"Int", "int", [TypedAttrInterface]> {
200200
return $_get(type.getContext(), type, value);
201201
}]>,
202202
AttrBuilderWithInferredContext<(ins "Type":$type, "int64_t":$value), [{
203-
IntType intType = type.cast<IntType>();
203+
IntType intType = mlir::cast<IntType>(type);
204204
mlir::APInt apValue(intType.getWidth(), value, intType.isSigned());
205205
return $_get(intType.getContext(), intType, apValue);
206206
}]>,
@@ -209,7 +209,7 @@ def IntAttr : CIR_Attr<"Int", "int", [TypedAttrInterface]> {
209209
int64_t getSInt() const { return getValue().getSExtValue(); }
210210
uint64_t getUInt() const { return getValue().getZExtValue(); }
211211
bool isNullValue() const { return getValue() == 0; }
212-
uint64_t getBitWidth() const { return getType().cast<IntType>().getWidth(); }
212+
uint64_t getBitWidth() const { return mlir::cast<IntType>(getType()).getWidth(); }
213213
}];
214214
let genVerifyDecl = 1;
215215
let hasCustomAssemblyFormat = 1;
@@ -257,11 +257,11 @@ def ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> {
257257
}];
258258
let builders = [
259259
AttrBuilderWithInferredContext<(ins "Type":$type, "mlir::IntegerAttr":$value), [{
260-
return $_get(type.getContext(), type.cast<mlir::cir::PointerType>(), value);
260+
return $_get(type.getContext(), mlir::cast<mlir::cir::PointerType>(type), value);
261261
}]>,
262262
AttrBuilder<(ins "Type":$type,
263263
"mlir::IntegerAttr":$value), [{
264-
return $_get($_ctxt, type.cast<mlir::cir::PointerType>(), value);
264+
return $_get($_ctxt, mlir::cast<mlir::cir::PointerType>(type), value);
265265
}]>,
266266
];
267267
let extraClassDeclaration = [{

clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ class CIRDataLayout {
4444
// `useABI` is `true` if not using prefered alignment.
4545
unsigned getAlignment(mlir::Type ty, bool useABI) const {
4646
if (llvm::isa<mlir::cir::StructType>(ty)) {
47-
auto sTy = ty.cast<mlir::cir::StructType>();
47+
auto sTy = mlir::cast<mlir::cir::StructType>(ty);
4848
if (sTy.getPacked() && useABI)
4949
return 1;
5050
} else if (llvm::isa<mlir::cir::ArrayType>(ty)) {
51-
return getAlignment(ty.cast<mlir::cir::ArrayType>().getEltType(), useABI);
51+
return getAlignment(mlir::cast<mlir::cir::ArrayType>(ty).getEltType(),
52+
useABI);
5253
}
5354

5455
return useABI ? layout.getTypeABIAlignment(ty)
@@ -86,7 +87,7 @@ class CIRDataLayout {
8687
}
8788

8889
unsigned getPointerTypeSizeInBits(mlir::Type Ty) const {
89-
assert(Ty.isa<mlir::cir::PointerType>() &&
90+
assert(mlir::isa<mlir::cir::PointerType>(Ty) &&
9091
"This should only be called with a pointer type");
9192
return layout.getTypeSizeInBits(Ty);
9293
}
@@ -96,7 +97,7 @@ class CIRDataLayout {
9697
}
9798

9899
mlir::Type getIntPtrType(mlir::Type Ty) const {
99-
assert(Ty.isa<mlir::cir::PointerType>() && "Expected pointer type");
100+
assert(mlir::isa<mlir::cir::PointerType>(Ty) && "Expected pointer type");
100101
auto IntTy = mlir::cir::IntType::get(Ty.getContext(),
101102
getPointerTypeSizeInBits(Ty), false);
102103
return IntTy;

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def PtrStrideOp : CIR_Op<"ptr_stride",
302302
let extraClassDeclaration = [{
303303
// Get type pointed by the base pointer.
304304
mlir::Type getElementTy() {
305-
return getBase().getType().cast<mlir::cir::PointerType>().getPointee();
305+
return mlir::cast<mlir::cir::PointerType>(getBase().getType()).getPointee();
306306
}
307307
}];
308308

@@ -343,7 +343,7 @@ def ConstantOp : CIR_Op<"const",
343343

344344
let extraClassDeclaration = [{
345345
bool isNullPtr() {
346-
if (const auto ptrAttr = getValue().dyn_cast<mlir::cir::ConstPtrAttr>())
346+
if (const auto ptrAttr = mlir::dyn_cast<mlir::cir::ConstPtrAttr>(getValue()))
347347
return ptrAttr.isNullValue();
348348
return false;
349349
}
@@ -389,7 +389,7 @@ class AllocaTypesMatchWith<string summary, string lhsArg, string rhsArg,
389389
def AllocaOp : CIR_Op<"alloca", [
390390
AllocaTypesMatchWith<"'allocaType' matches pointee type of 'addr'",
391391
"addr", "allocaType",
392-
"$_self.cast<PointerType>().getPointee()">]> {
392+
"cast<PointerType>($_self).getPointee()">]> {
393393
let summary = "Defines a scope-local variable";
394394
let description = [{
395395
The `cir.alloca` operation defines a scope-local variable.
@@ -449,7 +449,7 @@ def AllocaOp : CIR_Op<"alloca", [
449449

450450
let extraClassDeclaration = [{
451451
// Whether the alloca input type is a pointer.
452-
bool isPointerType() { return getAllocaType().isa<::mlir::cir::PointerType>(); }
452+
bool isPointerType() { return ::mlir::isa<::mlir::cir::PointerType>(getAllocaType()); }
453453

454454
bool isDynamic() { return (bool)getDynAllocSize(); }
455455
}];
@@ -473,7 +473,7 @@ def AllocaOp : CIR_Op<"alloca", [
473473
def LoadOp : CIR_Op<"load", [
474474
TypesMatchWith<"type of 'result' matches pointee type of 'addr'",
475475
"addr", "result",
476-
"$_self.cast<PointerType>().getPointee()">]> {
476+
"cast<PointerType>($_self).getPointee()">]> {
477477

478478
let summary = "Load value from memory adddress";
479479
let description = [{
@@ -531,7 +531,7 @@ def LoadOp : CIR_Op<"load", [
531531
def StoreOp : CIR_Op<"store", [
532532
TypesMatchWith<"type of 'value' matches pointee type of 'addr'",
533533
"addr", "value",
534-
"$_self.cast<PointerType>().getPointee()">]> {
534+
"cast<PointerType>($_self).getPointee()">]> {
535535

536536
let summary = "Store value to memory address";
537537
let description = [{
@@ -1459,7 +1459,7 @@ def CmpThreeWayOp : CIR_Op<"cmp3way", [Pure, SameTypeOperands]> {
14591459

14601460
/// Determine whether this three-way comparison compares integral operands.
14611461
bool isIntegralComparison() {
1462-
return getLhs().getType().isa<mlir::cir::IntType>();
1462+
return mlir::isa<mlir::cir::IntType>(getLhs().getType());
14631463
}
14641464
}];
14651465
}
@@ -2269,7 +2269,7 @@ def GetMemberOp : CIR_Op<"get_member"> {
22692269

22702270
/// Return the result type.
22712271
mlir::cir::PointerType getResultTy() {
2272-
return getResult().getType().cast<mlir::cir::PointerType>();
2272+
return mlir::cast<mlir::cir::PointerType>(getResult().getType());
22732273
}
22742274
}];
22752275

@@ -2339,7 +2339,7 @@ def GetRuntimeMemberOp : CIR_Op<"get_runtime_member"> {
23392339

23402340
def VecInsertOp : CIR_Op<"vec.insert", [Pure,
23412341
TypesMatchWith<"argument type matches vector element type", "vec", "value",
2342-
"$_self.cast<VectorType>().getEltType()">,
2342+
"cast<VectorType>($_self).getEltType()">,
23432343
AllTypesMatch<["result", "vec"]>]> {
23442344

23452345
let summary = "Insert one element into a vector object";
@@ -2366,7 +2366,7 @@ def VecInsertOp : CIR_Op<"vec.insert", [Pure,
23662366

23672367
def VecExtractOp : CIR_Op<"vec.extract", [Pure,
23682368
TypesMatchWith<"type of 'result' matches element type of 'vec'", "vec",
2369-
"result", "$_self.cast<VectorType>().getEltType()">]> {
2369+
"result", "cast<VectorType>($_self).getEltType()">]> {
23702370

23712371
let summary = "Extract one element from a vector object";
23722372
let description = [{
@@ -2419,7 +2419,7 @@ def VecCreateOp : CIR_Op<"vec.create", [Pure]> {
24192419

24202420
def VecSplatOp : CIR_Op<"vec.splat", [Pure,
24212421
TypesMatchWith<"type of 'value' matches element type of 'result'", "result",
2422-
"value", "$_self.cast<VectorType>().getEltType()">]> {
2422+
"value", "cast<VectorType>($_self).getEltType()">]> {
24232423

24242424
let summary = "Convert a scalar into a vector";
24252425
let description = [{
@@ -2834,7 +2834,7 @@ def CallOp : CIR_CallOp<"call"> {
28342834
$_state.addOperands(operands);
28352835
if (callee)
28362836
$_state.addAttribute("callee", callee);
2837-
if (resType && !resType.isa<VoidType>())
2837+
if (resType && !isa<VoidType>(resType))
28382838
$_state.addTypes(resType);
28392839
}]>,
28402840
OpBuilder<(ins "Value":$ind_target,
@@ -3427,7 +3427,7 @@ def VAArgOp : CIR_Op<"va.arg">,
34273427
def AllocException : CIR_Op<"alloc_exception", [
34283428
AllocaTypesMatchWith<"'allocType' matches pointee type of 'addr'",
34293429
"addr", "allocType",
3430-
"$_self.cast<PointerType>().getPointee()">]> {
3430+
"cast<PointerType>($_self).getPointee()">]> {
34313431
let summary = "Defines a scope-local variable";
34323432
let description = [{
34333433
Implements a slightly higher level __cxa_allocate_exception:

0 commit comments

Comments
 (0)