Skip to content

[flang] Add executable tests for conditional expressions (F2023)#369

Open
cenewcombe wants to merge 2 commits intollvm:mainfrom
cenewcombe:test_cond_expr
Open

[flang] Add executable tests for conditional expressions (F2023)#369
cenewcombe wants to merge 2 commits intollvm:mainfrom
cenewcombe:test_cond_expr

Conversation

@cenewcombe
Copy link
Copy Markdown
Contributor

@cenewcombe cenewcombe commented Mar 13, 2026

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).

kparzysz pushed a commit to llvm/llvm-project that referenced this pull request Apr 8, 2026
…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.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Apr 8, 2026
… (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.
kwyatt-ext pushed a commit to llvm/llvm-project that referenced this pull request Apr 13, 2026
## 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.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Apr 13, 2026
… (#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.
cpullvm-upstream-sync bot pushed a commit to navaneethshan/cpullvm-toolchain-1 that referenced this pull request Apr 13, 2026
… (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.
cpullvm-upstream-sync bot pushed a commit to navaneethshan/cpullvm-toolchain-1 that referenced this pull request Apr 13, 2026
… (#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.
@cenewcombe cenewcombe marked this pull request as ready for review April 14, 2026 15:41
@cenewcombe
Copy link
Copy Markdown
Contributor Author

@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!

Copy link
Copy Markdown
Contributor

@eugeneepshteyn eugeneepshteyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with some minor typos/questions.

I assume all these pass with the latest flang.

Please wait for Tarun to respond before merging.

Comment on lines +3 to +4
! Constructors subject to a conditional expression should not be
! evaluated when the controling condition is not satistified.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typos: "controling" → "controlling", "satistified" → "satisfied".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✔️

if ( present(X) ) then
A = X
else
A = 99.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REAL constant assigned to INTEGER array?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@cenewcombe
Copy link
Copy Markdown
Contributor Author

I assume all these pass with the latest flang.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants