[Matrix] Implement compound assignment operators
| Bugzilla Link | 46164 |
| Resolution | FIXED |
| Resolved on | Mar 08, 2021 01:51 |
| Version | unspecified |
| OS | All |
| Blocks | llvm/llvm-project#45508 |
| CC | @ash-tamraz,@DougGregor,@fhahn,@zygoloid,@SaurabhJha |
| Fixed by commit(s) | 63851a701eac |
Extended Description
Support for +/- operators for matrix types has been added in https://reviews.llvm.org/rG6f6e91d19337315548f550479f94cbc0af93c8fe and there is a patch to implement the * operator: https://reviews.llvm.org/D76794
But the compound assignment operators still need to be implemented.
Fix submitted in https://reviews.llvm.org/rG63851a701eac
Great, thanks.
Hi Florian,
Thanks for accepting the revision. Two questions:
- Does it mean it's in main branch now as I did not see it in main branch on GitHub https://github.com/llvm/llvm-project/commits/main
- What's the process of updating "Fixed by Commit(s)" on this bug. Is it done manually and we enter the commit hash there?
It sounds like you do not have commit access. In that case, I can commit the change tomorrow on your behalf, using the name & email you used during the review.
Hi Florian,
Thanks for accepting the revision. Two questions:
- Does it mean it's in main branch now as I did not see it in main branch on GitHub https://github.com/llvm/llvm-project/commits/main
- What's the process of updating "Fixed by Commit(s)" on this bug. Is it done manually and we enter the commit hash there?
Cheers, Saurabh
Hi Florian,
Here is the Phabricator revision https://reviews.llvm.org/D98075. I have requested you for review there.
Cheers, Saurabh
Hi Florian,
I am still working on this issue. Here's where I am at the moment.
In summary, this is happening because of a type cast which is leading to a null pointer dereference. Here are the details.
This assertion is failing https://github.com/llvm/llvm-project/blob/ ca5247bb1770a1dfa56b78303d99f1cc9a0a06ee/clang/include/clang/AST/Type.h#L677. And this is connected to how we are doing a static cast from a Stmt object to CompoundAssignOperator object here https://github.com/llvm/llvm-project/blob/ ca5247bb1770a1dfa56b78303d99f1cc9a0a06ee/clang/include/clang/AST/StmtVisitor. h#L76. On that StmtVisitor.h line, the type cast from S whose type is Stmt to CompoundAssignOperator is resulting in a CompoundAssignOperator like this.
(lldb) p ((CompoundAssignOperator *) S)->ComputationResultType (clang::QualType) $42 = { Value = (Value = 277297312) }
(lldb) p ((CompoundAssignOperator *) S)->ComputationLHSType (clang::QualType) $43 = { Value = (Value = 0) }
So it is assigning null QualType to ComputationLHSType attribute. My guess is C++ does casting by going through the type hierarchy which in this case is Stmt-> ValueStmt -> Expr -> BinaryOperator -> CompoundAssignOperator. I did some digging and I think the type information is preserved atleast till Stmt -> ... -> BinaryOperator. Here's what it looked like for me.
You are correct I think, the key issue is that we assign the wrong type to LHS (null instead of the matrix type of the binop result). The problem is that we do not properly set CompLHSTy for matrix expressions (see https://github.com/llvm/llvm-project/blob/ca5247bb1770a1dfa56b78303d99f1cc9a0a06ee/clang/lib/Sema/SemaExpr.cpp#L10518 and compare to what’s done for vector types above). I think if that is fixed, things should work out as expected.
Hi Florian,
I am still working on this issue. Here's where I am at the moment.
In summary, this is happening because of a type cast which is leading to a null pointer dereference. Here are the details.
This assertion is failing https://github.com/llvm/llvm-project/blob/ca5247bb1770a1dfa56b78303d99f1cc9a0a06ee/clang/include/clang/AST/Type.h#L677. And this is connected to how we are doing a static cast from a Stmt object to CompoundAssignOperator object here https://github.com/llvm/llvm-project/blob/ca5247bb1770a1dfa56b78303d99f1cc9a0a06ee/clang/include/clang/AST/StmtVisitor.h#L76. On that StmtVisitor.h line, the type cast from S whose type is Stmt to CompoundAssignOperator is resulting in a CompoundAssignOperator like this.
(lldb) p ((CompoundAssignOperator *) S)->ComputationResultType (clang::QualType) $42 = { Value = (Value = 277297312) }
(lldb) p ((CompoundAssignOperator *) S)->ComputationLHSType (clang::QualType) $43 = { Value = (Value = 0) }
So it is assigning null QualType to ComputationLHSType attribute. My guess is C++ does casting by going through the type hierarchy which in this case is Stmt-> ValueStmt -> Expr -> BinaryOperator -> CompoundAssignOperator. I did some digging and I think the type information is preserved atleast till Stmt -> ... -> BinaryOperator. Here's what it looked like for me.
(lldb) p ((Expr ) ((((BinaryOperator *) S)).SubExprs[0]))->TR (clang::QualType) $48 = { Value = (Value = 277297312) } (lldb) p ((Expr ) ((((BinaryOperator *) S)).SubExprs[1]))->TR (clang::QualType) $49 = { Value = (Value = 277297312) }
Somehow that last cast from BinaryOperator to CompoundAssignOperator is failing. So I think I should be able to solve this problem if I get this typecast right.
Do you think there's something straightforward that I am missing? Or do you think I am on a completely wrong track?
Thanks so much, Saurabh
I did see the comment on "TypeEvaluationKind" which has a TODO "should vectors maybe be split out into their own thing?". I guess matrices are probably scalar but would be great if you can also confirm it.
Many thanks, Saurabh
Hi Florian,
In the godbolt stack trace that you linked in your last message (thanks for that), on #10, it is calling "EmitAnyExpr" which in turn calls "EmitScalarExpr". I was looking at "EmitAnyExpr" in "clang/lib/CodeGen/CGExpr.cpp:212" and it is switching on three cases.
- TEK_Scalar
- TEK_Complex
- TEK_Aggregate
My question is whether the matrices are regarded as "scalar"? I am confused because I used to think of scalars as elementary data types like int types.
Thanks so much, Saurabh
Sounds like you are on the right track!
You could also take a look at a simple program with a matrix += operator, which crashes at the moment. That should give another point to start: https://godbolt.org/z/eM7x1q
I spent some time on it this week. With some debugging on a few gtests, I am starting to get how clang is generally organised and who calls what to compile some cxx text.
My plan now is to look at existing vector implementations and Florian's matrix patches and use these two as guide to to implement matrix ones compound assignments.
Thank you, Saurabh
Hi Florian,
I have started working on this, using this patch of yours as a guide https://reviews.llvm.org/rG6f6e91d19337315548f550479f94cbc0af93c8fe.
I am running a lit test using "python3 /tmp/build/bin/llvm-lit clang/test/CodeGen/matrix-type-operators.c ". However, I am not able to understand how these tests are written in the first place. I can vaguely understand for this particular file that we define three matrices of sizes 55, 23, and 9*3, and then we apply various operations on them. But I am not able to get how the comments are parsed and how are they generated/constructed.
Can you please point me to some documentation where I can learn how to write tests? I am not able to find it by myself unfortunately.
Thank you, Saurabh
Hey everyone,
Seems like this problem is not solved yet so I would like to give it a try. I have never contributed to LLVM and this is going to be my first issue.
I have gone through LLVM's developer policy. This bug is unassigned so I am assigning it to myself. Please let me know if it's something already actively worked upon and I will unassigned myself.
I would start by reading the snippet posted by Florian and go from there.
Sounds good to me, thanks for looking into this!
To add a bit to my previous comment, a good place to start adding tests would be https://github.com/llvm/llvm-project/blob/main/clang/test/CodeGen/matrix-type-operators.c
Let me know if you have any additional questions.
Hey everyone,
Seems like this problem is not solved yet so I would like to give it a try. I have never contributed to LLVM and this is going to be my first issue.
I have gone through LLVM's developer policy. This bug is unassigned so I am assigning it to myself. Please let me know if it's something already actively worked upon and I will unassigned myself.
I would start by reading the snippet posted by Florian and go from there.
Thanks, Saurabh
As
I'm making this as suitable for new contributors, because I think this should be fairly self-contained & I am happy to help anyone interested in picking this up.
I'm interested in working on this. What would you suggest as a good starting point?
That's great!
As a starting point, I think we need to add support for compound operators to Sema, similar to how it is done for the vector types (see https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/SemaExpr.cpp#L10388 and the matrix handling a few lines below).
Going from there with some test cases should uncover any other places that need changes.
I'm making this as suitable for new contributors, because I think this should be fairly self-contained & I am happy to help anyone interested in picking this up.
I'm interested in working on this. What would you suggest as a good starting point?
I'm making this as suitable for new contributors, because I think this should be fairly self-contained & I am happy to help anyone interested in picking this up.
assigned to @SaurabhJha