[flang] Add executable tests for conditional expressions (F2023)#369
[flang] Add executable tests for conditional expressions (F2023)#369cenewcombe wants to merge 2 commits intollvm:mainfrom
Conversation
…186489) ## Implement Fortran 2023 Conditional Expressions (R1002) ***This PR contains the implementation for parsing and semantic analysis. Lowering is implemented in a separate PR (#186490)*** Implements Fortran 2023 conditional expressions with syntax: `result = (condition ? value1 : condition2 ? value2 : ... : elseValue)` Issue: #176999 Discourse: https://discourse.llvm.org/t/rfc-adding-conditional-expressions-in-flang-f2023/89869/1 -- note that some of the details provided in the RFC post are no longer accurate ### Implementation Details **Parser:** - Added ConditionalExpr as primary expression (F2023 R1002) - Right-associative chaining for multi-way conditionals **Semantics:** - Expression tree node ConditionalExpr<T> with N conditions and N+1 values - Strict type checking: all values must have identical type, kind, and rank - Conditions must be scalar LOGICAL **LIT Testing:** - Parser tests: Syntax validation, precedence, nesting - Semantic tests: Type checking, error messages - Note: Executable tests will be added to the llvm-test-suite repo (llvm/llvm-test-suite#369) **Limitations** - Conditional arguments are not yet supported. This work is planned - #180592 - Polymorphic types (CLASS) not yet supported in lowering - Both limitations will emit clear error message if encountered ### Examples ``` ! Simple conditional x = (flag ? 10 : 20) ! Chained result = (x > 0 ? 1 : x < 0 ? -1 : 0) ! Examples from F2023 ( ABS (RESIDUAL)<=TOLERANCE ? ’ok’ : ’did not converge’ ) ( I>0 .AND. I<=SIZE (A) ? A (I) : PRESENT (VAL) ? VAL : 0.0 ) ``` AI Usage Disclosure: AI tools (Claude Sonnet 4.5) were used to assist with implementation of this feature and test code generation. I have reviewed, modified, and tested all AI-generated code.
… (F2023) (#186489) ## Implement Fortran 2023 Conditional Expressions (R1002) ***This PR contains the implementation for parsing and semantic analysis. Lowering is implemented in a separate PR (#186490)*** Implements Fortran 2023 conditional expressions with syntax: `result = (condition ? value1 : condition2 ? value2 : ... : elseValue)` Issue: #176999 Discourse: https://discourse.llvm.org/t/rfc-adding-conditional-expressions-in-flang-f2023/89869/1 -- note that some of the details provided in the RFC post are no longer accurate ### Implementation Details **Parser:** - Added ConditionalExpr as primary expression (F2023 R1002) - Right-associative chaining for multi-way conditionals **Semantics:** - Expression tree node ConditionalExpr<T> with N conditions and N+1 values - Strict type checking: all values must have identical type, kind, and rank - Conditions must be scalar LOGICAL **LIT Testing:** - Parser tests: Syntax validation, precedence, nesting - Semantic tests: Type checking, error messages - Note: Executable tests will be added to the llvm-test-suite repo (llvm/llvm-test-suite#369) **Limitations** - Conditional arguments are not yet supported. This work is planned - #180592 - Polymorphic types (CLASS) not yet supported in lowering - Both limitations will emit clear error message if encountered ### Examples ``` ! Simple conditional x = (flag ? 10 : 20) ! Chained result = (x > 0 ? 1 : x < 0 ? -1 : 0) ! Examples from F2023 ( ABS (RESIDUAL)<=TOLERANCE ? ’ok’ : ’did not converge’ ) ( I>0 .AND. I<=SIZE (A) ? A (I) : PRESENT (VAL) ? VAL : 0.0 ) ``` AI Usage Disclosure: AI tools (Claude Sonnet 4.5) were used to assist with implementation of this feature and test code generation. I have reviewed, modified, and tested all AI-generated code.
## Implement Lowering for Fortran 2023 Conditional Expressions (R1002) ***This PR contains the lowering steps only for ease of review. DO NOT MERGE until #186489 is merged.*** Implements Fortran 2023 conditional expressions with syntax: `result = (condition ? value1 : condition2 ? value2 : ... : elseValue)` Issue: #176999 Discourse: https://discourse.llvm.org/t/rfc-adding-conditional-expressions-in-flang-f2023/89869/1 -- note that some of the details provided in the RFC post are no longer accurate ### Implementation Details **Lowering to HLFIR:** - Lazy evaluation via nested if-then-else control flow - Only the selected branch is evaluated - Temporary allocation with proper cleanup - Special handling for: - CHARACTER types with deferred length - Arrays (shape determined by selected branch per F2023 10.1.4(7)) - Derived types **LIT Testing:** - Lowering tests: HLFIR code generation verification - Note: Executable tests will be added to the llvm-test-suite repo (llvm/llvm-test-suite#369) **Limitations** - Conditional arguments are not yet supported. This work is planned - #180592 - Polymorphic types (CLASS) not yet supported in lowering - Both limitations will emit clear error message if encountered ### Examples ``` ! Simple conditional x = (flag ? 10 : 20) ! Chained result = (x > 0 ? 1 : x < 0 ? -1 : 0) ! Examples from F2023 ( ABS (RESIDUAL)<=TOLERANCE ? ’ok’ : ’did not converge’ ) ( I>0 .AND. I<=SIZE (A) ? A (I) : PRESENT (VAL) ? VAL : 0.0 ) ``` AI Usage Disclosure: AI tools (Claude Sonnet 4.5) were used to assist with implementation of this feature and test code generation. I have reviewed, modified, and tested all AI-generated code.
… (#186490) ## Implement Lowering for Fortran 2023 Conditional Expressions (R1002) ***This PR contains the lowering steps only for ease of review. DO NOT MERGE until #186489 is merged.*** Implements Fortran 2023 conditional expressions with syntax: `result = (condition ? value1 : condition2 ? value2 : ... : elseValue)` Issue: #176999 Discourse: https://discourse.llvm.org/t/rfc-adding-conditional-expressions-in-flang-f2023/89869/1 -- note that some of the details provided in the RFC post are no longer accurate ### Implementation Details **Lowering to HLFIR:** - Lazy evaluation via nested if-then-else control flow - Only the selected branch is evaluated - Temporary allocation with proper cleanup - Special handling for: - CHARACTER types with deferred length - Arrays (shape determined by selected branch per F2023 10.1.4(7)) - Derived types **LIT Testing:** - Lowering tests: HLFIR code generation verification - Note: Executable tests will be added to the llvm-test-suite repo (llvm/llvm-test-suite#369) **Limitations** - Conditional arguments are not yet supported. This work is planned - #180592 - Polymorphic types (CLASS) not yet supported in lowering - Both limitations will emit clear error message if encountered ### Examples ``` ! Simple conditional x = (flag ? 10 : 20) ! Chained result = (x > 0 ? 1 : x < 0 ? -1 : 0) ! Examples from F2023 ( ABS (RESIDUAL)<=TOLERANCE ? ’ok’ : ’did not converge’ ) ( I>0 .AND. I<=SIZE (A) ? A (I) : PRESENT (VAL) ? VAL : 0.0 ) ``` AI Usage Disclosure: AI tools (Claude Sonnet 4.5) were used to assist with implementation of this feature and test code generation. I have reviewed, modified, and tested all AI-generated code.
… (F2023) (#186489) ## Implement Fortran 2023 Conditional Expressions (R1002) ***This PR contains the implementation for parsing and semantic analysis. Lowering is implemented in a separate PR (#186490)*** Implements Fortran 2023 conditional expressions with syntax: `result = (condition ? value1 : condition2 ? value2 : ... : elseValue)` Issue: #176999 Discourse: https://discourse.llvm.org/t/rfc-adding-conditional-expressions-in-flang-f2023/89869/1 -- note that some of the details provided in the RFC post are no longer accurate ### Implementation Details **Parser:** - Added ConditionalExpr as primary expression (F2023 R1002) - Right-associative chaining for multi-way conditionals **Semantics:** - Expression tree node ConditionalExpr<T> with N conditions and N+1 values - Strict type checking: all values must have identical type, kind, and rank - Conditions must be scalar LOGICAL **LIT Testing:** - Parser tests: Syntax validation, precedence, nesting - Semantic tests: Type checking, error messages - Note: Executable tests will be added to the llvm-test-suite repo (llvm/llvm-test-suite#369) **Limitations** - Conditional arguments are not yet supported. This work is planned - #180592 - Polymorphic types (CLASS) not yet supported in lowering - Both limitations will emit clear error message if encountered ### Examples ``` ! Simple conditional x = (flag ? 10 : 20) ! Chained result = (x > 0 ? 1 : x < 0 ? -1 : 0) ! Examples from F2023 ( ABS (RESIDUAL)<=TOLERANCE ? ’ok’ : ’did not converge’ ) ( I>0 .AND. I<=SIZE (A) ? A (I) : PRESENT (VAL) ? VAL : 0.0 ) ``` AI Usage Disclosure: AI tools (Claude Sonnet 4.5) were used to assist with implementation of this feature and test code generation. I have reviewed, modified, and tested all AI-generated code.
… (#186490) ## Implement Lowering for Fortran 2023 Conditional Expressions (R1002) ***This PR contains the lowering steps only for ease of review. DO NOT MERGE until #186489 is merged.*** Implements Fortran 2023 conditional expressions with syntax: `result = (condition ? value1 : condition2 ? value2 : ... : elseValue)` Issue: #176999 Discourse: https://discourse.llvm.org/t/rfc-adding-conditional-expressions-in-flang-f2023/89869/1 -- note that some of the details provided in the RFC post are no longer accurate ### Implementation Details **Lowering to HLFIR:** - Lazy evaluation via nested if-then-else control flow - Only the selected branch is evaluated - Temporary allocation with proper cleanup - Special handling for: - CHARACTER types with deferred length - Arrays (shape determined by selected branch per F2023 10.1.4(7)) - Derived types **LIT Testing:** - Lowering tests: HLFIR code generation verification - Note: Executable tests will be added to the llvm-test-suite repo (llvm/llvm-test-suite#369) **Limitations** - Conditional arguments are not yet supported. This work is planned - #180592 - Polymorphic types (CLASS) not yet supported in lowering - Both limitations will emit clear error message if encountered ### Examples ``` ! Simple conditional x = (flag ? 10 : 20) ! Chained result = (x > 0 ? 1 : x < 0 ? -1 : 0) ! Examples from F2023 ( ABS (RESIDUAL)<=TOLERANCE ? ’ok’ : ’did not converge’ ) ( I>0 .AND. I<=SIZE (A) ? A (I) : PRESENT (VAL) ? VAL : 0.0 ) ``` AI Usage Disclosure: AI tools (Claude Sonnet 4.5) were used to assist with implementation of this feature and test code generation. I have reviewed, modified, and tested all AI-generated code.
1db5404 to
7ffeb56
Compare
|
@eugeneepshteyn and/or @tarunprabhu -- this PR should be ready for review now that flang conditional expressions are merged. Let me know if you have capacity or if I should ping someone else. Thanks! |
eugeneepshteyn
left a comment
There was a problem hiding this comment.
LGTM, with some minor typos/questions.
I assume all these pass with the latest flang.
Please wait for Tarun to respond before merging.
| ! Constructors subject to a conditional expression should not be | ||
| ! evaluated when the controling condition is not satistified. |
There was a problem hiding this comment.
Typos: "controling" → "controlling", "satistified" → "satisfied".
| if ( present(X) ) then | ||
| A = X | ||
| else | ||
| A = 99.0 |
There was a problem hiding this comment.
REAL constant assigned to INTEGER array?
Yes, I have been using this branch while working on conditional expressions development. All of the conditional expressions tests pass. The tests for conditional arguments will need to be verified before we enable them. I will work with Vineet to make sure that gets done; implementation of the conditional arguments feature is still in progress. Thanks for the review! |
This PR is ready to review and safe to merge now that the functional implementation of conditional expressions is complete for flang:
llvm/llvm-project#186489
llvm/llvm-project#186490
This PR ports the HPE Cray tst90 tests for conditional expressions and conditional arguments, and enables execution of tests for conditional expressions only. Conditional arguments are not yet implemented, so CMakeLists.txt excludes these tests from execution. They can be enabled once the feature is implemented with support for .NIL. (this work is in progress, see llvm/llvm-project#180592).