Re: [llvm-dev] How static casts work between Stmt and its inheritors in clang?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How static casts work between Stmt and its inheritors in clang?

Manas via cfe-dev
Hi,


On Mar 2, 2021, at 20:10, Saurabh Jha via llvm-dev <[hidden email]> wrote:

Hi LLVM people,


Llvm-dev is usually used to discuss issues related to llvm itself. As this email is related to Clang internals, cfe-dev would be more suitable (added as CC).

I am new to clang/llvm and have been hacking on it for about three weeks now.  I am trying to implement compound assignment operators, +=, -=, and *=, for matrices. Here are the bug details.


Thank you very much for taking a look at the issue!

I have a lit test that tries to do a "a += b" for matrices a and b. Here's its trace (courtesy Florian Hahn). Clang fails on this assertion. And this is because when we do a static cast from a Stmt instance to a CompoundAssignOperator here, we are not assigning the correct QualType to the LHS. Concretely, here's what I found in my debugging.

# printing out S
(lldb) p S
(std::__add_pointer_helper<clang::Stmt, true>::type) $0 = 0x0000000011621f70

# cast S to CompoundAssignOperator
(lldb) p ((CompoundAssignOperator *) S)
(clang::CompoundAssignOperator *) $1 = 0x0000000011621f70

# access ComputationLHSType attribute of casted S. The QualType is NULL.
(lldb) p ((CompoundAssignOperator *) S)->ComputationLHSType
(clang::QualType) $2 = {
  Value = (Value = 0)
}

# access ComputationResultType attribute of casted S.
(lldb) p ((CompoundAssignOperator *) S)->ComputationResultType
(clang::QualType) $3 = {
  Value = (Value = 291641504)
}

So I think it's working correctly for ComputationResultType but is somehow assigning null to ComputationLHSType's QualType.Value. This is interesting because if I try to cast S to something like BinaryOperator and cast its operands to Expr, the types are coming out correctly.

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.

Cheers,
Florian


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How static casts work between Stmt and its inheritors in clang?

Manas via cfe-dev
Hi Florian,

Thank you for the suggestions. I am able to get += and -= work for matrices and should be able to create a patch for review soon, after I am done writing lit tests.

Cheers,
Saurabh

On Wed, Mar 3, 2021 at 10:30 AM Florian Hahn <[hidden email]> wrote:
Hi,


On Mar 2, 2021, at 20:10, Saurabh Jha via llvm-dev <[hidden email]> wrote:

Hi LLVM people,


Llvm-dev is usually used to discuss issues related to llvm itself. As this email is related to Clang internals, cfe-dev would be more suitable (added as CC).

I am new to clang/llvm and have been hacking on it for about three weeks now.  I am trying to implement compound assignment operators, +=, -=, and *=, for matrices. Here are the bug details.


Thank you very much for taking a look at the issue!

I have a lit test that tries to do a "a += b" for matrices a and b. Here's its trace (courtesy Florian Hahn). Clang fails on this assertion. And this is because when we do a static cast from a Stmt instance to a CompoundAssignOperator here, we are not assigning the correct QualType to the LHS. Concretely, here's what I found in my debugging.

# printing out S
(lldb) p S
(std::__add_pointer_helper<clang::Stmt, true>::type) $0 = 0x0000000011621f70

# cast S to CompoundAssignOperator
(lldb) p ((CompoundAssignOperator *) S)
(clang::CompoundAssignOperator *) $1 = 0x0000000011621f70

# access ComputationLHSType attribute of casted S. The QualType is NULL.
(lldb) p ((CompoundAssignOperator *) S)->ComputationLHSType
(clang::QualType) $2 = {
  Value = (Value = 0)
}

# access ComputationResultType attribute of casted S.
(lldb) p ((CompoundAssignOperator *) S)->ComputationResultType
(clang::QualType) $3 = {
  Value = (Value = 291641504)
}

So I think it's working correctly for ComputationResultType but is somehow assigning null to ComputationLHSType's QualType.Value. This is interesting because if I try to cast S to something like BinaryOperator and cast its operands to Expr, the types are coming out correctly.

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.

Cheers,
Florian


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How static casts work between Stmt and its inheritors in clang?

Manas via cfe-dev


> On Mar 5, 2021, at 09:55, Saurabh Jha <[hidden email]> wrote:
>
> Thank you for the suggestions. I am able to get += and -= work for matrices and should be able to create a patch for review soon, after I am done writing lit tests.

Sounds great, thanks!


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev