Skip to content

Commit 31f86db

Browse files
committed
Generate MLIR code for the builtin arith increments
Signed-off-by: Roberto Raggi <roberto.raggi@gmail.com>
1 parent 61da5e6 commit 31f86db

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

src/mlir/cxx/mlir/codegen_expressions.cc

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,73 @@ auto Codegen::ExpressionVisitor::operator()(MemberExpressionAST* ast)
675675

676676
auto Codegen::ExpressionVisitor::operator()(PostIncrExpressionAST* ast)
677677
-> ExpressionResult {
678+
auto expressionResult = gen.expression(ast->baseExpression);
679+
680+
if (control()->is_integral_or_unscoped_enum(ast->baseExpression->type)) {
681+
auto loc = gen.getLocation(ast->firstSourceLocation());
682+
auto ptrTy =
683+
mlir::cast<mlir::cxx::PointerType>(expressionResult.value.getType());
684+
auto elementTy = ptrTy.getElementType();
685+
auto loadOp = mlir::cxx::LoadOp::create(gen.builder_, loc, elementTy,
686+
expressionResult.value);
687+
auto resultTy = gen.convertType(ast->baseExpression->type);
688+
auto oneOp = mlir::cxx::IntConstantOp::create(
689+
gen.builder_, loc, resultTy,
690+
ast->op == TokenKind::T_PLUS_PLUS ? 1 : -1);
691+
auto addOp =
692+
mlir::cxx::AddIOp::create(gen.builder_, loc, resultTy, loadOp, oneOp);
693+
mlir::cxx::StoreOp::create(gen.builder_, loc, addOp,
694+
expressionResult.value);
695+
return {loadOp};
696+
}
697+
if (control()->is_floating_point(ast->baseExpression->type)) {
698+
auto loc = gen.getLocation(ast->firstSourceLocation());
699+
auto ptrTy =
700+
mlir::cast<mlir::cxx::PointerType>(expressionResult.value.getType());
701+
auto elementTy = ptrTy.getElementType();
702+
auto loadOp = mlir::cxx::LoadOp::create(gen.builder_, loc, elementTy,
703+
expressionResult.value);
704+
auto resultTy = gen.convertType(ast->baseExpression->type);
705+
706+
mlir::Value one;
707+
double v = ast->op == TokenKind::T_PLUS_PLUS ? 1 : -1;
708+
709+
switch (control()->remove_cvref(ast->baseExpression->type)->kind()) {
710+
case TypeKind::kFloat:
711+
one = mlir::cxx::FloatConstantOp::create(
712+
gen.builder_, gen.getLocation(ast->opLoc),
713+
gen.convertType(ast->baseExpression->type),
714+
gen.builder_.getF32FloatAttr(v));
715+
break;
716+
717+
case TypeKind::kDouble:
718+
one = mlir::cxx::FloatConstantOp::create(
719+
gen.builder_, gen.getLocation(ast->opLoc),
720+
gen.convertType(ast->baseExpression->type),
721+
gen.builder_.getF64FloatAttr(v));
722+
break;
723+
724+
case TypeKind::kLongDouble:
725+
one = mlir::cxx::FloatConstantOp::create(
726+
gen.builder_, gen.getLocation(ast->opLoc),
727+
gen.convertType(ast->baseExpression->type),
728+
gen.builder_.getF64FloatAttr(v));
729+
break;
730+
731+
default:
732+
// Handle other float types if necessary
733+
auto op = gen.emitTodoExpr(ast->firstSourceLocation(),
734+
"unsupported float type");
735+
return {op};
736+
}
737+
738+
auto addOp =
739+
mlir::cxx::AddFOp::create(gen.builder_, loc, resultTy, loadOp, one);
740+
mlir::cxx::StoreOp::create(gen.builder_, loc, addOp,
741+
expressionResult.value);
742+
return {loadOp};
743+
}
744+
678745
auto op =
679746
gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind()));
680747

@@ -952,6 +1019,10 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast)
9521019
auto storeOp = mlir::cxx::StoreOp::create(gen.builder_, loc, addOp,
9531020
expressionResult.value);
9541021

1022+
if (is_glvalue(ast)) {
1023+
return expressionResult;
1024+
}
1025+
9551026
auto op = mlir::cxx::LoadOp::create(gen.builder_, loc, resultType,
9561027
expressionResult.value);
9571028

@@ -982,10 +1053,38 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast)
9821053
auto storeOp = mlir::cxx::StoreOp::create(gen.builder_, loc, addOp,
9831054
expressionResult.value);
9841055

1056+
if (is_glvalue(ast)) {
1057+
return expressionResult;
1058+
}
1059+
9851060
auto op = mlir::cxx::LoadOp::create(gen.builder_, loc, resultType,
9861061
expressionResult.value);
9871062

9881063
return {op};
1064+
} else if (control()->is_pointer(ast->expression->type)) {
1065+
auto loc = gen.getLocation(ast->firstSourceLocation());
1066+
auto intTy =
1067+
mlir::cxx::IntegerType::get(gen.builder_.getContext(), 32, true);
1068+
auto one = mlir::cxx::IntConstantOp::create(
1069+
gen.builder_, loc, intTy,
1070+
ast->op == TokenKind::T_MINUS_MINUS ? -1 : 1);
1071+
auto ptrTy = mlir::cast<mlir::cxx::PointerType>(
1072+
expressionResult.value.getType());
1073+
auto elementTy = ptrTy.getElementType();
1074+
auto loadOp = mlir::cxx::LoadOp::create(gen.builder_, loc, elementTy,
1075+
expressionResult.value);
1076+
auto addOp = mlir::cxx::PtrAddOp::create(gen.builder_, loc, elementTy,
1077+
loadOp, one);
1078+
mlir::cxx::StoreOp::create(gen.builder_, loc, addOp,
1079+
expressionResult.value);
1080+
1081+
if (is_glvalue(ast)) {
1082+
return expressionResult;
1083+
}
1084+
1085+
auto op = mlir::cxx::LoadOp::create(gen.builder_, loc, elementTy,
1086+
expressionResult.value);
1087+
return {op};
9891088
}
9901089

9911090
auto op =
@@ -1357,6 +1456,14 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast)
13571456
return {op};
13581457
}
13591458

1459+
if (control()->is_pointer(ast->leftExpression->type) &&
1460+
control()->is_integer(ast->rightExpression->type)) {
1461+
auto op = mlir::cxx::PtrAddOp::create(gen.builder_, loc, resultType,
1462+
leftExpressionResult.value,
1463+
rightExpressionResult.value);
1464+
return {op};
1465+
}
1466+
13601467
break;
13611468
}
13621469

@@ -1796,4 +1903,4 @@ auto Codegen::NewInitializerVisitor::operator()(NewBracedInitializerAST* ast)
17961903
return {};
17971904
}
17981905

1799-
} // namespace cxx
1906+
} // namespace cxx

0 commit comments

Comments
 (0)