Skip to content
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

Semantic checks for Select-rank construct(#R1148) #895

Closed
wants to merge 16 commits into from

Conversation

Sameeranjoshi
Copy link
Contributor

@Sameeranjoshi Sameeranjoshi commented Dec 25, 2019

Semantic checks for R1148

Copy link
Collaborator

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

A good start.

@Sameeranjoshi
Copy link
Contributor Author

A good start.

Done

@Sameeranjoshi
Copy link
Contributor Author

The specification doesn't speak on what happens when selector is an expression.
For example SELECT RANK(a+b) or SELECT RANK(0+1) ....
Is if (std::holds_alternative<parser::Expr>(selectRankStmtSel.u)) always expected to throw a semantic error?

@klausler
Copy link
Collaborator

The specification doesn't speak on what happens when selector is an expression.
For example SELECT RANK(a+b) or SELECT RANK(0+1) ....
Is if (std::holds_alternative<parser::Expr>(selectRankStmtSel.u)) always expected to throw a semantic error?

R1150 requires that the expression be a scalar integer constant (which will be packaged as an Expr<SomeType>).

@Sameeranjoshi
Copy link
Contributor Author

The specification doesn't speak on what happens when selector is an expression.
For example SELECT RANK(a+b) or SELECT RANK(0+1) ....
Is if (std::holds_alternative<parser::Expr>(selectRankStmtSel.u)) always expected to throw a semantic error?

R1150 requires that the expression be a scalar integer constant (which will be packaged as an Expr<SomeType>).

I am unable to figure out how to handle to TODO case.
If selector is an expression
Can you please point how to move ahead?
Thanks

lib/semantics/check-select-stmt.cc Outdated Show resolved Hide resolved
lib/semantics/check-select-stmt.cc Outdated Show resolved Hide resolved
lib/semantics/check-select-stmt.cc Outdated Show resolved Hide resolved
lib/semantics/check-select-stmt.cc Outdated Show resolved Hide resolved
!ERROR: Selector is not an assumed-rank array variable
SELECT RANK(y)
RANK (0)
print *, "PRINT RANK 0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be a better test here if y were to be used in some context that required it to be a scalar variable. Similarly, in the other RANK cases, use the associate-name in some context that will elicit an error if the required rank were incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added some tests are they as expected ones ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No. Instead of just having a PRINT statement, try using the SELECT RANK construct entity in the test in some way that would work only when the construct entity has the expected rank, using something with a compile-time test of the rank.

SELECT RANK(x)
RANK(0)
  j = INT(0, KIND=MERGE(KIND(0), -1, RANK(x) == 0)) ! will fail when RANK(x) is not zero here
END SELECT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the patch submitted(currently passed a non zero value to pass the drone.io build) , test Fails with below error, when the rank is 0, which is supposed to fail instead when rank is not zero.
_../f18/test/semantics/select-rank.f90:39:11: error: 'kind=' argument must be a constant scalar integer whose value is a supported kind for the intrinsic result type

j = INT(0, KIND=MERGE(KIND(0), -1, RANK(x) == 0)) ! will fail when RANK(x) is not zero ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _
Is this an issue with the RANK construct or select rank ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That seems to indicate a problem with the semantics or name resolution of xin theRANK. If the rank of xin that context were zero, as it should be, theKIND=argument would have had a valid value. You can test that hypothesis by changing==to/=` in that expression and seeing whether it (incorrectly) does not emit an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Conformed for above discussed test with suggested changes,
j = INT(0, KIND=MERGE(KIND(0), -1, RANK(x) /= 0)) !Is evaluating to true, which means RANK is not resolving properly the value of x.

test/semantics/select-rank.f90 Outdated Show resolved Hide resolved
private:
MaybeExpr expr_;
std::optional<int> associationRank_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this new field relevant to the shape analysis code in lib/Evaluate/shape.cpp? I think that it must be, so that code should be extended to use the specific case ranks so that operations that depend on the shape of an array will be compiled correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you please explain more on what do you mean by

new field relevant to the shape analysis code

The tests other than select rank which depend on shape of array seem to be successful for ninja test check-all

What I can understand from comments is that,
MaybeExpr expr_ private member function in class AssocEntityDetails is being used in lib/Evaluate/shape.cpp inside function GetShape()
and my patch doesn't seem to handle the expr_ inside SetRankFromParserNode but is handled in SetTypeFromAssociation

Copy link
Collaborator

Choose a reason for hiding this comment

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

Make sure that your tests for SELECT RANK include operations in the rank cases that will force the compiler to analyze the shape of the array at the specific rank. Use FORALL statements, the SHAPE intrinsic, array assignments, &c. There is probably code in the shape analysis framework that will need to be updated to handle this new representation of construct entities correctly, using the rank of the construct entity and the descriptor of the assumed-rank dummy argument for inquiries.

@Sameeranjoshi
Copy link
Contributor Author

@klausler I have tried to formulate some tests from my understanding of Fortran language.
Pasting the godbolt.org links for keeping the comments clean.
Can you please suggest if they need more changes?
https://godbolt.org/z/UiRcRg = SHAPE intrinsics
https://godbolt.org/z/oW2Zyl = SHAPE intrinsics
https://godbolt.org/z/N86jMD = using FORALL statement
https://godbolt.org/z/YKWY9A = using array assignment statement
https://godbolt.org/z/raN-5f = using LOC for address of array

@Sameeranjoshi
Copy link
Contributor Author

@klausler I have tried to formulate some tests from my understanding of Fortran language.
Pasting the godbolt.org links for keeping the comments clean.
Can you please suggest if they need more changes?
https://godbolt.org/z/UiRcRg = SHAPE intrinsics
https://godbolt.org/z/oW2Zyl = SHAPE intrinsics
https://godbolt.org/z/N86jMD = using FORALL statement
https://godbolt.org/z/YKWY9A = using array assignment statement
https://godbolt.org/z/raN-5f = using LOC for address of array

If the tests seem to be written as expected, I see all passing except the following condition.

 select rank (arr) 
            rank(2)
                j = INT(0, KIND=MERGE(KIND(0), -1, RANK(arr) == SIZE(SHAPE(arr)))) !here aarr is dummy 
        end select

Checking
lib/Evaluate/shape.cpp at this function
Seems like assoc.expr() might not have rank information.Is that the issue you see?
@klausler can you please point what do you expect this to return ? as we don't have shape_ in AssocEntityDetails. to fill in the shape vector to return.

@klausler
Copy link
Collaborator

Seems like assoc.expr() might not have rank information.Is that the issue you see?
@klausler can you please point what do you expect this to return ? as we don't have shape_ in AssocEntityDetails. to fill in the shape vector to return.

assoc.expr() is the assumed-rank dummy argument itself.

Within a RANK case, shape analysis should return a shape vector of the appropriate rank populated with descriptor inquiries into the extents of the leading dimensions of the assumed-rank dummy argument.

@Sameeranjoshi
Copy link
Contributor Author

@klausler thanks for explaining, but there seems to be regression for other tests and the select-rank cases, not sure why are they regressing(They tend to Abort the compiler).
Did I create a wrong patch?

A better implementation of querying the Shape from `DescriptorInquiry` using `GetExtent`.
include/flang/Semantics/symbol.h Outdated Show resolved Hide resolved
lib/Evaluate/shape.cpp Outdated Show resolved Hide resolved
[&](const semantics::AssocEntityDetails &assoc) {
return (*this)(assoc.expr());
Shape shape;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about association entities that are not RANK cases, like ASSOCIATE(x=>array)? This code used to work for them, and now it won't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I have added a guard using if (!assoc.associationRank()), the regression pass now.
Is there any related function you know apart from above usage, or suggest some other design pattern you think, like adding something in class AssocEntityDetails like IsAssumedRank() function similar to ObjectEentityDetails? I think the only way we can identify select rank is the -fdebug-dump-symbols will have a new rank: field.

lib/Evaluate/shape.cpp Outdated Show resolved Hide resolved
@@ -0,0 +1,126 @@
//===-- lib/semantics/check-select-stmt.cpp -------------------------------===//
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not a good name for this file. Fortran has three different SELECT statements and this file only checks one of the three.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about check-select-rank.[cpp|h]?

test/Semantics/select-rank.f90 Outdated Show resolved Hide resolved
test/Semantics/select-rank.f90 Show resolved Hide resolved
INTEGER :: j
select rank (arr)
rank(2)
j = INT(0, KIND=MERGE(KIND(0), -1, RANK(arr) == SIZE(SHAPE(arr)))) !arr is dummy
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't test RANK(arr) and SIZE(SHAPE(arr)) against each other when you know what their values should be (2). They could both be wrong.


select rank (arr)
rank(2)
j = INT(0, KIND=MERGE(KIND(0), -1, RANK(arr) == SIZE(SHAPE(brr)))) !brr is local to subroutine
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand what's being tested here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I was trying to test with a local copy and the dummy arguments.
Remove this test?

integer,dimension(-1:10, 20:30) :: brr
select rank (arr)
rank(2)
j = LOC(brr(0,25))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test is just working with a local array and doesn't depend on the effective shape of the assumed-rank array at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, changing it to arr.

@Sameeranjoshi
Copy link
Contributor Author

@klausler I have incorporated most of the changes.
Some queries I have asked in for the review comments.
Thank you.

@Sameeranjoshi
Copy link
Contributor Author

Ping..

@Sameeranjoshi
Copy link
Contributor Author

Merged in llvm-project/master.

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.

None yet

4 participants