[flang-commits] [flang] [flang] Implement conditional expressions lowering (F2023) (PR #186490)
Caroline Newcombe via flang-commits
flang-commits at lists.llvm.org
Thu Apr 9 08:11:46 PDT 2026
================
@@ -1821,6 +1821,158 @@ class HlfirBuilder {
llvm_unreachable("unknown descriptor inquiry");
}
+ /// Build nested if-then-else chain by walking the right-skewed
+ /// ConditionalExpr tree. The assignValue callback generates and assigns
+ /// each value to avoid evaluating non-taken branches.
+ template <typename T, typename Callback>
+ void
+ buildConditionalIfChain(const Fortran::evaluate::ConditionalExpr<T> &condExpr,
+ const Callback &assignValue) {
+ const mlir::Location loc{getLoc()};
+ fir::FirOpBuilder &builder{getBuilder()};
+ getStmtCtx().pushScope();
+ const hlfir::EntityWithAttributes condEntity{gen(condExpr.condition())};
+ mlir::Value condition{hlfir::loadTrivialScalar(loc, builder, condEntity)};
+ condition = builder.createConvert(loc, builder.getI1Type(), condition);
+ builder.genIfOp(loc, {}, condition, /*withElseRegion=*/true)
+ .genThen([&]() {
+ getStmtCtx().pushScope();
+ assignValue(condExpr.thenValue());
+ getStmtCtx().finalizeAndPop();
+ })
+ .genElse([&]() {
+ getStmtCtx().pushScope();
+ assignValue(condExpr.elseValue());
+ getStmtCtx().finalizeAndPop();
+ })
+ .end();
+ getStmtCtx().finalizeAndPop();
+ }
+
+ /// Generate scalar conditional with lazy evaluation using assignment.
+ /// Creates a temporary and assigns the selected branch value to it.
+ template <typename T>
+ hlfir::Entity
+ genScalarConditional(const Fortran::evaluate::ConditionalExpr<T> &condExpr,
+ mlir::Type elementType,
+ const llvm::SmallVector<mlir::Value, 1> &typeParams) {
+ const mlir::Location loc{getLoc()};
+ fir::FirOpBuilder &builder{getBuilder()};
+ const mlir::Value tempStorage{builder.createTemporary(
+ loc, elementType, ".cond.scalar",
+ /*shape=*/mlir::ValueRange{}, /*typeParams=*/typeParams)};
+ const hlfir::DeclareOp tempDecl{hlfir::DeclareOp::create(
+ builder, loc, tempStorage, ".cond.result",
+ /*shape=*/mlir::Value{}, /*typeParams=*/typeParams)};
+ const hlfir::Entity temp{tempDecl};
+ buildConditionalIfChain(
+ condExpr, [&](const Fortran::evaluate::Expr<T> &expr) {
+ hlfir::Entity entity{gen(expr)};
+ entity = hlfir::derefPointersAndAllocatables(loc, builder, entity);
----------------
cenewcombe wrote:
Done ✔️
https://github.com/llvm/llvm-project/pull/186490
More information about the flang-commits
mailing list