-
Notifications
You must be signed in to change notification settings - Fork 787
[SYCL] Store expression as ConstantExpr in Semantic Attribute #3169
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
Conversation
Currently , the behavior is inconsistent for FPGA attributes. We store the ConstantExpr for some SYCL/FPGA attributes (not all). This patch is part 1 of making it consistent. It modfies addIntelSYCLSingleArgFunctionAttr() to save ConstantExpr. This saves ConstantExpr for all attributes that calls this function. A follow-up patch will address the remaining attributes. Signed-off-by: Elizabeth Andrews <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look reasonable, but there are a bit more gains to be had from the change. For instance, in:
llvm/clang/lib/CodeGen/CodeGenFunction.cpp
Line 726 in 4fed824
Optional<llvm::APSInt> ArgVal = |
You can start to take advantage of the fact that we don't need to recompute the constant expression -- we can just pull it back out of the attribute and get the value.
Do you think those sort of changes should be part of this patch, or should they be handled in a separate patch? My thinking is they'd be reasonable in this patch, but you might have other plans.
one comment here: SYCLIntelSchedulerTargetFmaxMhzAttr currently uses AddOneConstantValueAttr() instead of addIntelSYCLSingleArgFunctionAttr(), So this attribute already supports ConstantExpr. Please remove this attribute from the PR description. |
There is some inconsistency with |
Signed-off-by: Elizabeth Andrews <[email protected]>
Signed-off-by: Elizabeth Andrews <[email protected]>
I'm working on this. The change I made in CodeGen caused compiler to crash for tests. The system I work on has been very slow today. So I'm building on another system to debug the crash. |
Signed-off-by: Elizabeth Andrews <[email protected]>
This is done now. The following code (in CodeGen) in my original patch (not pushed to Github) crashed when handling an attribute which used common Sema handler for openCL and C++ attribute.
When the ConstantExpr is generated, openCL attributes creates it with So I changed the code to use |
clang/include/clang/Sema/Sema.h
Outdated
if (!XDimExpr->isValueDependent() && !YDimExpr->isValueDependent() && | ||
!ZDimExpr->isValueDependent()) { | ||
|
||
if (!handleMaxWorkSizeAttrExpr(*this, CI, XDimExpr)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be more clear if we make the pattern:
XDimExpr = checkMaxWorkSizeAttrExpr(*this, CI, XDimExpr);
YDimExpr = checkMaxWorkSizeAttrExpr(*this, CI, YDimExpr);
ZDimExpr = checkMaxWorkSizeAttrExpr(*this, CI, ZDimExpr);
if (!XDimExpr || !YDimExpr || !ZDimExpr)
return;
to make it super obvious that we're intentionally replacing the expression we pulled from the parsed attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
assert(ArgVal.hasValue() && "Not an integer constant expression"); | ||
|
||
Expr::EvalResult Result; | ||
assert(A->getValue()->EvaluateAsInt(Result, getContext()) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Result
will always be uninitialized when assertions are disabled. I think a better approach would be:
const auto *CE = dyn_cast<ConstantExpr>(A->getValue());
assert(CE && "Not an integer constant expression");
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
This same comment applies in all the cases.
As an aside: if our CI pipeline doesn't notice the failure caused by the use of assert
here, we may be missing some CI coverage for a release without asserts build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh thanks for catching that! I did not think of that. Will make the change now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed you suggested getResultAsAPSInt()
. This crashes for the same reason under discussion here - #3169 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
What the heck is going on with that code in SemaExpr.cpp? It's figured out that it has a constant expression and it knows what
@erichkeane, do you have thoughts on this? |
Woah, yeah, that is pretty strange. I'm guessing this causes us to have to re-evaluate it later, but I don't know what the symptoms of that would be. |
The impetus behind this patch is that I would like the semantic attribute to store a So I guess the question really boils down to: what should we do about it? I think it might be interesting to try making the change to SemaExpr.cpp I suggested above to see if anything breaks or not (independent of this patch). If nothing breaks, perhaps that would be a patch worth floating by the community to see if Richard accepts it or explains what we've misunderstood? An alternative is to use |
I'm pretty sure EvaluateAsInt does basically everything EvaluateAsIntegerConstant anyway, I think the idea of deciding that it is a ConstantExpr and storing that is a good idea. Short term, we could just try setting the ConstantExpr's value to the result we find as a 'fixup' (ConstantExpr::SetResult), but I think changing the SemaExpr version is definitely worth an attempt. |
I was mistaken in my earlier comment when I mentioned I'll look into Erich's suggestion of using |
@Ralender can you look at this, since you have worked on the constant evaluator... |
This did not work. The
Where ArgVal is of type APSInt. Is this what you meant @erichkeane or am I doing something wrong? |
No, thats what I meant... I hadn't realized that ConstantExpr used trailing storage to determine what is being stored, so the only way to do this would be to create a new ConstantExpr, or to try out @AaronBallman 's suggestion above of changing SemaExpr.cpp. |
FWIW, I am trying my approach on a community build right now. I'll report back whether it causes lit tests to fail or not when I have results. |
I get the following failures when testing on Windows with an MSVC 2019-built community clang with my suggested changes:
The AST dumping tests are failing because some |
Yeah, I'd imagine ASTDump tests would fail, so that makes a lot of sense to me. I think that generally confirms to me this is the 'right' thing to do. |
Likewise. So the question is: do we try to make that change upstream, wait for it to trickle down, and use it here? Or do we make the change here and try to upstream later (will that cause later merge conflicts for ourselves)? Or do we make the change here and not try to upstream? |
I'd say upstream it (as review-after-commit/NFC) and apply it here as well, the conflict should be easy enough to fix. |
Okay, that seems reasonable enough to me (though I'd probably get rsmith to review the change pre-commit rather than go with post-commit review just because this does seem like a surprising oversight for a very common case). @elizabethandrews -- would you like me to do the upstream work or would you like to do it? (I'm fine either way.) |
of interest, here is the patch that added that code: llvm/llvm-project@dea9d57 It appears the intent of this is to do exactly what we want, except it missed here:
There were no comments initially about this one, so it may have just fallen by the wayside. |
Good catch! That makes me feel more comfortable with post-commit review, thanks @erichkeane! |
We definitely should upstream the change since this is not a SYCL only change. However, I haven't contributed to llvm/clang enough to feel comfortable doing a post-commit review. If that's the route we're taking, I would appreciate if you could submit the change. Otherwise I can submit a patch which undergoes the normal review process upstream. I also personally feel we should submit this PR as is (after reverting CodeGen changes and implementing review comments) and make the CodeGen changes after the upstream patch is pulled to intel/llvm. Otherwise this PR is going to have a bunch of unrelated test changes (unrelated to Intel FPGA, which is what this PR is about). I can file a tracker to make sure we don't forget to do this. Is that alright with you @erichkeane @AaronBallman ? |
Signed-off-by: Elizabeth Andrews <[email protected]>
Signed-off-by: Elizabeth Andrews <[email protected]>
Okay, I'm happy to work on it.
I was thinking that if we wanted to do it both up and downstream, we'd apply the const expr changes separately (and before) these attribute changes. So they'd be logically separate commits. However, I'm fine any which way we want to go about it. |
Ok. That works for me. Thanks! |
Signed-off-by: Elizabeth Andrews <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @elizabethandrews for the refactoring work with work_group_size attributes and saving ConstantExpr for all function attributes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't an area that I am familiar with, and I trust @AaronBallman's and @erichkeane's reviews of the code. I am approving this.
…3169) Currently , the behavior is inconsistent for FPGA attributes. We evaluate and store the ConstantExpr for some SYCL/FPGA attributes , and discard it for others. This patch is part 1 of making the behavior consistent. This patch saves the ConstantExpr for the following attributes. The ConstantExpr is then extracted from the Sematic attributes and used in CodeGen instead of reevaluating it. SYCLIntelNoGlobalWorkOffsetAttr, SYCLIntelMaxGlobalWorkDimAttr, SYCLIntelNumSimdWorkItemsAttr , IntelReqdSubGroupSizeAttr , SYCLIntelSchedulerTargetFmaxMhzAttr , ReqdWorkGroupSizeAttr, IntelReqdSubGroupSizeAttr, SYCLIntelMaxWorkGroupSizeAttr A consequence of this change is a change in diagnostic when the attribute argument is not an integer constant expression. The new diagnostic is an existing llvm-project diagnostic. A follow-up patch(s) will address the remaining attributes. Signed-off-by: Elizabeth Andrews [email protected]
Currently , the behavior is inconsistent for FPGA attributes. We evaluate and store the ConstantExpr for some SYCL/FPGA attributes , and discard it for others. This patch is part 1 of making the behavior consistent. This patch saves the ConstantExpr for the following attributes. The ConstantExpr is then extracted from the Sematic attributes and used in CodeGen instead of reevaluating it.
SYCLIntelNoGlobalWorkOffsetAttr,
SYCLIntelMaxGlobalWorkDimAttr,
SYCLIntelNumSimdWorkItemsAttr ,
IntelReqdSubGroupSizeAttr ,
SYCLIntelSchedulerTargetFmaxMhzAttr ,
ReqdWorkGroupSizeAttr,
IntelReqdSubGroupSizeAttr,
SYCLIntelMaxWorkGroupSizeAttr
A consequence of this change is a change in diagnostic when the attribute argument is not an integer constant expression. The new diagnostic is an existing llvm-project diagnostic.
A follow-up patch(s) will address the remaining attributes.
Signed-off-by: Elizabeth Andrews [email protected]