Skip to content

[mlir][EmitC] Remove unreachable code and fix Windows build warning #80677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ inline LogicalResult interleaveCommaWithError(const Container &c,

/// Return the precedence of a operator as an integer, higher values
/// imply higher precedence.
static int getOperatorPrecedence(Operation *operation) {
return llvm::TypeSwitch<Operation *, int>(operation)
static FailureOr<int> getOperatorPrecedence(Operation *operation) {
return llvm::TypeSwitch<Operation *, FailureOr<int>>(operation)
.Case<emitc::AddOp>([&](auto op) { return 11; })
.Case<emitc::ApplyOp>([&](auto op) { return 13; })
.Case<emitc::CastOp>([&](auto op) { return 13; })
.Case<emitc::CmpOp>([&](auto op) {
.Case<emitc::CmpOp>([&](auto op) -> FailureOr<int> {
switch (op.getPredicate()) {
case emitc::CmpPredicate::eq:
case emitc::CmpPredicate::ne:
Expand All @@ -86,13 +86,14 @@ static int getOperatorPrecedence(Operation *operation) {
case emitc::CmpPredicate::three_way:
return 10;
}
return op->emitError("unsupported cmp predicate");
})
.Case<emitc::DivOp>([&](auto op) { return 12; })
.Case<emitc::MulOp>([&](auto op) { return 12; })
.Case<emitc::RemOp>([&](auto op) { return 12; })
.Case<emitc::SubOp>([&](auto op) { return 11; })
.Case<emitc::CallOpaqueOp>([&](auto op) { return 14; });
llvm_unreachable("Unsupported operator");
.Case<emitc::CallOpaqueOp>([&](auto op) { return 14; })
.Default([](auto op) { return op->emitError("unsupported operation"); });
}

namespace {
Expand Down Expand Up @@ -1072,7 +1073,10 @@ LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {
Operation *rootOp = expressionOp.getRootOp();

emittedExpression = expressionOp;
pushExpressionPrecedence(getOperatorPrecedence(rootOp));
FailureOr<int> precedence = getOperatorPrecedence(rootOp);
if (failed(precedence))
return failure();
pushExpressionPrecedence(precedence.value());

if (failed(emitOperation(*rootOp, /*trailingSemicolon=*/false)))
return failure();
Expand All @@ -1089,13 +1093,15 @@ LogicalResult CppEmitter::emitOperand(Value value) {
if (isPartOfCurrentExpression(value)) {
Operation *def = value.getDefiningOp();
assert(def && "Expected operand to be defined by an operation");
int precedence = getOperatorPrecedence(def);
bool encloseInParenthesis = precedence < getExpressionPrecedence();
FailureOr<int> precedence = getOperatorPrecedence(def);
if (failed(precedence))
return failure();
bool encloseInParenthesis = precedence.value() < getExpressionPrecedence();
if (encloseInParenthesis) {
os << "(";
pushExpressionPrecedence(lowestPrecedence());
} else
pushExpressionPrecedence(precedence);
pushExpressionPrecedence(precedence.value());

if (failed(emitOperation(*def, /*trailingSemicolon=*/false)))
return failure();
Expand Down