Skip to content

Apply kind code check on exitstat and cmdstat #78286

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 12 commits into from
Jan 29, 2024

Conversation

yiwu0b11
Copy link
Contributor

@yiwu0b11 yiwu0b11 commented Jan 16, 2024

When testing on gcc, both exitstat and cmdstat must be a kind=4 integer, e.g. DefaultInt. This patch changes the input arg requirement from AnyInt to TypePattern{IntType, KindCode::greaterOrEqualToKind, n}.

The standard stated in 16.9.73

  • EXITSTAT (optional) shall be a scalar of type integer with a decimal exponent range of at least nine.
  • CMDSTAT (optional) shall be a scalar of type integer with a decimal exponent range of at least four.
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar 
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program

When testing the above program with exitstatvar kind<4, an error would occur:

$ ../build-release/bin/flang-new test.f90 
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)

When testing the above program with exitstatvar kind<2, an error would occur:

$ ../build-release/bin/flang-new test.f90 
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)

Test file for this semantics has been added to flang/test/Semantic
Fixes: #77990

Verified

This commit was signed with the committer’s verified signature.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Jan 16, 2024
@yiwu0b11 yiwu0b11 requested a review from psteinfeld January 16, 2024 14:15
@llvmbot
Copy link
Member

llvmbot commented Jan 16, 2024

@llvm/pr-subscribers-flang-semantics

Author: Yi Wu (yi-wu-arm)

Changes

When testing on gcc, both exitstat and cmdstat must be a kind=4 integer, e.g. DefaultInt. This patch change the input arg requirement from AnyInt to DefaultInt.

The standard stated in 16.9.73

  • EXITSTAT (optional) shall be a scalar of type integer with a decimal exponent range of at least nine.
  • CMDSTAT (optional) shall be a scalar of type integer with a decimal exponent range of at least four.
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar 
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program

When testing the above program with kind != 4, an error would occur:

$ ../build-release/bin/flang-new test.f90 
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)

Fixes: #77990


Full diff: https://github.com/llvm/llvm-project/pull/78286.diff

1 Files Affected:

  • (modified) flang/lib/Evaluate/intrinsics.cpp (+2-2)
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index da6d5970089884c..0b9bdac88a78dcf 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -1314,9 +1314,9 @@ static const IntrinsicInterface intrinsicSubroutine[]{
     {"execute_command_line",
         {{"command", DefaultChar, Rank::scalar},
             {"wait", AnyLogical, Rank::scalar, Optionality::optional},
-            {"exitstat", AnyInt, Rank::scalar, Optionality::optional,
+            {"exitstat", DefaultInt, Rank::scalar, Optionality::optional,
                 common::Intent::InOut},
-            {"cmdstat", AnyInt, Rank::scalar, Optionality::optional,
+            {"cmdstat", DefaultInt, Rank::scalar, Optionality::optional,
                 common::Intent::Out},
             {"cmdmsg", DefaultChar, Rank::scalar, Optionality::optional,
                 common::Intent::InOut}},

@yiwu0b11 yiwu0b11 requested a review from klausler January 16, 2024 14:16
Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

Absolutely not. If the standard doesn't restrict the types of these arguments, neither can we.

@yiwu0b11
Copy link
Contributor Author

yiwu0b11 commented Jan 16, 2024

Absolutely not. If the standard doesn't restrict the types of these arguments, neither can we.

Should it be Defaulnt for existstat, an int32_t(kind==4) would cover +-10^9, and an int16_t(kind==2) that covers +-10^4 for cmdstat?

@klausler
Copy link
Contributor

exitstat can be any kind >= 4. cmdstat can be any kind >= 2.

Now execute_command_line will accept exitstat kind>=4, cmdstat kind >=2
@yiwu0b11
Copy link
Contributor Author

exitstat can be any kind >= 4. cmdstat can be any kind >= 2.

Added a KindCode::greaterAndEqualTo in intrinsics to allow this kind check.

@yiwu0b11 yiwu0b11 changed the title change exitstat and cmsstat from AnyInt to DefaultInt Apply kind code check on exitstat and cmdstat Jan 16, 2024
@kiranchandramohan
Copy link
Contributor

You need a test for this patch.

@yiwu0b11 yiwu0b11 requested a review from klausler January 23, 2024 10:34
@klausler klausler removed their request for review January 23, 2024 16:06
Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

Is there another patch that adds integer kind handling for these two arguments to the runtime implementation?

@yiwu0b11
Copy link
Contributor Author

Is there another patch that adds integer kind handling for these two arguments to the runtime implementation?

Not now, sorry, let me add that right now.

@@ -122,10 +122,22 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,

if (exitstat) {
RUNTIME_CHECK(terminator, IsValidIntDescriptor(exitstat));
auto exitstatKind{exitstat->type().GetCategoryAndKind()->second};
if (exitstatKind < 4) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The runtime code doesn't need to check these descriptors -- that will have been done statically when the intrinsic procedure reference was analyzed!

What has to be done in the runtime is using those descriptors to store the integer result values properly, and I think that the code already does that.

Copy link
Contributor Author

@yiwu0b11 yiwu0b11 Jan 23, 2024

Choose a reason for hiding this comment

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

Sorry I misunderstood your comment. removed

@yiwu0b11
Copy link
Contributor Author

yiwu0b11 commented Jan 23, 2024

Is there another patch that adds integer kind handling for these two arguments to the runtime implementation?

Thanks for mention the kind code in the runtime! I've added kind code check in runtime and provide test, an incorrect kind code would result in a terminator.Crash "exitstat/cmdstat must have an integer kind greater or equal to 4/2 but have: %d". The tests also test the terminator printout.
kind code checks at runtime are removed.

@yiwu0b11
Copy link
Contributor Author

build and pass all test on Windows on Arm MSVC native.

@yiwu0b11 yiwu0b11 merged commit 14a1510 into llvm:main Jan 29, 2024
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Jan 30, 2024
When testing on gcc, both exitstat and cmdstat must be a kind=4 integer,
e.g. DefaultInt. This patch changes the input arg requirement from
`AnyInt` to `TypePattern{IntType, KindCode::greaterOrEqualToKind, n}`.

The standard stated in 16.9.73
- EXITSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least nine.
- CMDSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least four.

```fortran
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program
```
When testing the above program with exitstatvar kind<4, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)
```

When testing the above program with exitstatvar kind<2, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)
```

Test file for this semantics has been added to `flang/test/Semantic`
Fixes: llvm#77990

(cherry picked from commit 14a1510)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
When testing on gcc, both exitstat and cmdstat must be a kind=4 integer,
e.g. DefaultInt. This patch changes the input arg requirement from
`AnyInt` to `TypePattern{IntType, KindCode::greaterOrEqualToKind, n}`.

The standard stated in 16.9.73
- EXITSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least nine.
- CMDSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least four.

```fortran
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program
```
When testing the above program with exitstatvar kind<4, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)
```

When testing the above program with exitstatvar kind<2, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)
```

Test file for this semantics has been added to `flang/test/Semantic`
Fixes: llvm#77990

(cherry picked from commit 14a1510)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
When testing on gcc, both exitstat and cmdstat must be a kind=4 integer,
e.g. DefaultInt. This patch changes the input arg requirement from
`AnyInt` to `TypePattern{IntType, KindCode::greaterOrEqualToKind, n}`.

The standard stated in 16.9.73
- EXITSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least nine.
- CMDSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least four.

```fortran
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program
```
When testing the above program with exitstatvar kind<4, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)
```

When testing the above program with exitstatvar kind<2, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)
```

Test file for this semantics has been added to `flang/test/Semantic`
Fixes: llvm#77990

(cherry picked from commit 14a1510)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
When testing on gcc, both exitstat and cmdstat must be a kind=4 integer,
e.g. DefaultInt. This patch changes the input arg requirement from
`AnyInt` to `TypePattern{IntType, KindCode::greaterOrEqualToKind, n}`.

The standard stated in 16.9.73
- EXITSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least nine.
- CMDSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least four.

```fortran
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program
```
When testing the above program with exitstatvar kind<4, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)
```

When testing the above program with exitstatvar kind<2, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)
```

Test file for this semantics has been added to `flang/test/Semantic`
Fixes: llvm#77990

(cherry picked from commit 14a1510)
tstellar pushed a commit to tstellar/llvm-project that referenced this pull request Feb 14, 2024
When testing on gcc, both exitstat and cmdstat must be a kind=4 integer,
e.g. DefaultInt. This patch changes the input arg requirement from
`AnyInt` to `TypePattern{IntType, KindCode::greaterOrEqualToKind, n}`.

The standard stated in 16.9.73
- EXITSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least nine.
- CMDSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least four.

```fortran
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program
```
When testing the above program with exitstatvar kind<4, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)
```

When testing the above program with exitstatvar kind<2, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)
```

Test file for this semantics has been added to `flang/test/Semantic`
Fixes: llvm#77990

(cherry picked from commit 14a1510)
@pointhex pointhex mentioned this pull request May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No checks for the integer arguments of intrinsic "execute_command_line"
4 participants