Skip to content

Commit dc68166

Browse files
authored
[CIR] Simplify error emission to return failures directly (#141032)
Mirrors incubator changes from llvm/clangir#1634
1 parent 58ab005 commit dc68166

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,13 @@ void IntAttr::print(AsmPrinter &printer) const {
138138

139139
LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
140140
Type type, APInt value) {
141-
if (!mlir::isa<IntType>(type)) {
142-
emitError() << "expected 'simple.int' type";
143-
return failure();
144-
}
141+
if (!mlir::isa<IntType>(type))
142+
return emitError() << "expected 'simple.int' type";
145143

146144
auto intType = mlir::cast<IntType>(type);
147-
if (value.getBitWidth() != intType.getWidth()) {
148-
emitError() << "type and value bitwidth mismatch: " << intType.getWidth()
149-
<< " != " << value.getBitWidth();
150-
return failure();
151-
}
145+
if (value.getBitWidth() != intType.getWidth())
146+
return emitError() << "type and value bitwidth mismatch: "
147+
<< intType.getWidth() << " != " << value.getBitWidth();
152148

153149
return success();
154150
}
@@ -182,10 +178,8 @@ FPAttr FPAttr::getZero(Type type) {
182178
LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
183179
CIRFPTypeInterface fpType, APFloat value) {
184180
if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
185-
APFloat::SemanticsToEnum(value.getSemantics())) {
186-
emitError() << "floating-point semantics mismatch";
187-
return failure();
188-
}
181+
APFloat::SemanticsToEnum(value.getSemantics()))
182+
return emitError() << "floating-point semantics mismatch";
189183

190184
return success();
191185
}
@@ -195,22 +189,21 @@ LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
195189
//===----------------------------------------------------------------------===//
196190

197191
LogicalResult
198-
ConstArrayAttr::verify(function_ref<::mlir::InFlightDiagnostic()> emitError,
199-
Type type, Attribute elts, int trailingZerosNum) {
192+
ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type,
193+
Attribute elts, int trailingZerosNum) {
200194

201-
if (!(mlir::isa<ArrayAttr>(elts) || mlir::isa<StringAttr>(elts)))
195+
if (!(mlir::isa<ArrayAttr, StringAttr>(elts)))
202196
return emitError() << "constant array expects ArrayAttr or StringAttr";
203197

204198
if (auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
205199
const auto arrayTy = mlir::cast<ArrayType>(type);
206200
const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType());
207201

208202
// TODO: add CIR type for char.
209-
if (!intTy || intTy.getWidth() != 8) {
210-
emitError() << "constant array element for string literals expects "
211-
"!cir.int<u, 8> element type";
212-
return failure();
213-
}
203+
if (!intTy || intTy.getWidth() != 8)
204+
return emitError()
205+
<< "constant array element for string literals expects "
206+
"!cir.int<u, 8> element type";
214207
return success();
215208
}
216209

@@ -303,22 +296,20 @@ void ConstArrayAttr::print(AsmPrinter &printer) const {
303296
// CIR ConstVectorAttr
304297
//===----------------------------------------------------------------------===//
305298

306-
LogicalResult cir::ConstVectorAttr::verify(
307-
function_ref<::mlir::InFlightDiagnostic()> emitError, Type type,
308-
ArrayAttr elts) {
299+
LogicalResult
300+
cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError,
301+
Type type, ArrayAttr elts) {
309302

310-
if (!mlir::isa<cir::VectorType>(type)) {
303+
if (!mlir::isa<cir::VectorType>(type))
311304
return emitError() << "type of cir::ConstVectorAttr is not a "
312305
"cir::VectorType: "
313306
<< type;
314-
}
315307

316308
const auto vecType = mlir::cast<cir::VectorType>(type);
317309

318-
if (vecType.getSize() != elts.size()) {
310+
if (vecType.getSize() != elts.size())
319311
return emitError()
320312
<< "number of constant elements should match vector size";
321-
}
322313

323314
// Check if the types of the elements match
324315
LogicalResult elementTypeCheck = success();

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,8 @@ RecordType::verify(function_ref<mlir::InFlightDiagnostic()> emitError,
205205
llvm::ArrayRef<mlir::Type> members, mlir::StringAttr name,
206206
bool incomplete, bool packed, bool padded,
207207
RecordType::RecordKind kind) {
208-
if (name && name.getValue().empty()) {
209-
emitError() << "identified records cannot have an empty name";
210-
return mlir::failure();
211-
}
208+
if (name && name.getValue().empty())
209+
return emitError() << "identified records cannot have an empty name";
212210
return mlir::success();
213211
}
214212

@@ -421,12 +419,10 @@ uint64_t IntType::getABIAlignment(const mlir::DataLayout &dataLayout,
421419
mlir::LogicalResult
422420
IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
423421
unsigned width, bool isSigned) {
424-
if (width < IntType::minBitwidth() || width > IntType::maxBitwidth()) {
425-
emitError() << "IntType only supports widths from "
426-
<< IntType::minBitwidth() << " up to "
427-
<< IntType::maxBitwidth();
428-
return mlir::failure();
429-
}
422+
if (width < IntType::minBitwidth() || width > IntType::maxBitwidth())
423+
return emitError() << "IntType only supports widths from "
424+
<< IntType::minBitwidth() << " up to "
425+
<< IntType::maxBitwidth();
430426
return mlir::success();
431427
}
432428

@@ -631,10 +627,9 @@ mlir::LogicalResult
631627
FuncType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
632628
llvm::ArrayRef<mlir::Type> argTypes, mlir::Type returnType,
633629
bool isVarArg) {
634-
if (returnType && mlir::isa<cir::VoidType>(returnType)) {
635-
emitError() << "!cir.func cannot have an explicit 'void' return type";
636-
return mlir::failure();
637-
}
630+
if (mlir::isa_and_nonnull<cir::VoidType>(returnType))
631+
return emitError()
632+
<< "!cir.func cannot have an explicit 'void' return type";
638633
return mlir::success();
639634
}
640635

0 commit comments

Comments
 (0)