Skip to content

Commit 82468a7

Browse files
committed
Use TypeSwitch
1 parent ef42ce8 commit 82468a7

1 file changed

Lines changed: 24 additions & 35 deletions

File tree

mlir/lib/Target/DXSA/BinaryWriter.cpp

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "llvm/ADT/ArrayRef.h"
66
#include "llvm/ADT/DenseMap.h"
77
#include "llvm/ADT/SmallVector.h"
8+
#include "llvm/ADT/TypeSwitch.h"
89
#include "llvm/Support/Debug.h"
910
#include "llvm/Support/DebugLog.h"
1011
#include "llvm/Support/EndianStream.h"
@@ -101,21 +102,17 @@ class Writer {
101102
return emitError(value.getLoc(), "undefined operand");
102103
}
103104

104-
if (auto operand = dyn_cast<dxsa::Operand>(*op)) {
105-
if (failed(emitOperand(operand))) {
106-
return failure();
107-
}
108-
continue;
105+
auto result =
106+
llvm::TypeSwitch<Operation &, LogicalResult>(*op)
107+
.Case<dxsa::Operand>([this](auto op) { return emitOperand(op); })
108+
.Case<dxsa::OperandImm>(
109+
[this](auto op) { return emitOperandImm(op); })
110+
.Default([this](auto &op) {
111+
return emitError(op.getLoc(), "unexpected operand kind");
112+
});
113+
if (failed(result)) {
114+
return result;
109115
}
110-
111-
if (auto operand = dyn_cast<dxsa::OperandImm>(*op)) {
112-
if (failed(emitOperandImm(operand))) {
113-
return failure();
114-
}
115-
continue;
116-
}
117-
118-
return emitError(op->getLoc(), "unexpected operand kind");
119116
}
120117

121118
// Fixup instruction length after all operands are accumulated in
@@ -204,28 +201,20 @@ class Writer {
204201
return emitError(value.getLoc(), "index must be defined");
205202
}
206203

207-
if (auto indexImm = dyn_cast<dxsa::IndexImm>(*index)) {
208-
if (failed(emitIndexImm(indexImm))) {
209-
return failure();
210-
}
211-
continue;
212-
}
213-
214-
if (auto indexRel = dyn_cast<dxsa::IndexRel>(*index)) {
215-
if (failed(emitIndexRel(indexRel))) {
216-
return failure();
217-
}
218-
continue;
204+
auto result = llvm::TypeSwitch<Operation &, LogicalResult>(*index)
205+
.Case<dxsa::IndexImm>(
206+
[this](auto op) { return emitIndexImm(op); })
207+
.Case<dxsa::IndexRel>(
208+
[this](auto op) { return emitIndexRel(op); })
209+
.Case<dxsa::IndexRelImm>(
210+
[this](auto op) { return emitIndexRelImm(op); })
211+
.Default([this](auto &op) {
212+
return emitError(op.getLoc(), "invalid index type");
213+
});
214+
215+
if (failed(result)) {
216+
return result;
219217
}
220-
221-
if (auto indexRelImm = dyn_cast<dxsa::IndexRelImm>(*index)) {
222-
if (failed(emitIndexRelImm(indexRelImm))) {
223-
return failure();
224-
}
225-
continue;
226-
}
227-
228-
return emitError(value.getLoc(), "invalid index type");
229218
}
230219

231220
return success();

0 commit comments

Comments
 (0)