-
-
Notifications
You must be signed in to change notification settings - Fork 283
Fix #5116 - Skip the sematics checks of compiler-generated functions and template instances in dcompute #5131
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
Open
gulugulubing
wants to merge
2
commits into
ldc-developers:master
Choose a base branch
from
gulugulubing:issue5116
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -212,6 +212,11 @@ struct DComputeSemanticAnalyser : public StoppableVisitor { | |
| } | ||
| } | ||
| void visit(CallExp *e) override { | ||
| // Indirect calls via function pointers / delegates have no associated | ||
| // FuncDeclaration, so there is no module to check. | ||
| if (!e->f) | ||
| return; | ||
|
|
||
| // SynchronizedStatement is lowered to | ||
| // Critsec __critsec105; // 105 == line number | ||
| // _d_criticalenter(& __critsec105); <-- | ||
|
|
@@ -246,6 +251,16 @@ struct DComputeSemanticAnalyser : public StoppableVisitor { | |
| return; | ||
| } | ||
|
|
||
| // Skip compiler-generated struct support functions (e.g. __xopEquals, | ||
| // __xopCmp, postblit, destructor). Their bodies may reference non-@compute | ||
| // templates (such as __equals for static array comparison) that are outside | ||
| // of user control. They are only codegenerated if actually referenced by | ||
| // user code, at which point the codegen layer will report any issues. | ||
| if (fd->isGenerated()) { | ||
| stop = true; | ||
| return; | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just find this guard is redundant. |
||
| IF_LOG Logger::println("current function = %s", fd->toChars()); | ||
| currentFunction = fd; | ||
| } | ||
|
|
@@ -260,6 +275,20 @@ struct DComputeSemanticAnalyser : public StoppableVisitor { | |
| // as they contain unsupported global variables. | ||
| if (ti->tempdecl == Type::rtinfo || ti->tempdecl == Type::rtinfoImpl) { | ||
| stop = true; | ||
| return; | ||
| } | ||
|
|
||
| // Template instantiations for templates declared in non-@compute modules | ||
| // (e.g. __equals and isEqual from core.internal.array.equality) are | ||
| // created as a side effect of compiler-generated support functions. They | ||
| // contain calls back into their declaring (non-@compute) module, which | ||
| // would produce spurious errors. Skip them. | ||
| if (ti->tempdecl) { | ||
| Module *m = ti->tempdecl->getModule(); | ||
| if (m && hasComputeAttr(m) == DComputeCompileFor::hostOnly) { | ||
| stop = true; | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Regression test for issue #5116: defining a struct with a static array | ||
| // field in a @compute module previously caused a spurious semantic error | ||
| // ("can only call functions from other `@compute` modules") followed by a | ||
| // null-pointer dereference crash in DComputeSemanticAnalyser::visit(CallExp*). | ||
| // The crash happened because compiler-generated support functions (__xopEquals) | ||
| // triggered instantiation of __equals templates from core.internal.array.equality, | ||
| // whose body contains an indirect call through a function pointer (e->f == null). | ||
|
|
||
| // REQUIRES: target_NVPTX | ||
| // RUN: %ldc -mdcompute-targets=cuda-350 %s | ||
|
|
||
| @compute(CompileFor.deviceOnly) module tests.compilable.issue5116; | ||
| import ldc.dcompute; | ||
|
|
||
| private enum N = 16u; | ||
|
|
||
| struct S { | ||
| float[N] data; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
__equalsis a template which is specifically allowed, any non-template code it generates will still cause problems though.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.
Are you suggesting that __equals (and its generated code, like isEqual) should be included in
isNonComputeCallExpVaildsimilar to dcReflect, rather than using the module-based check in visit(TemplateInstance *)?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.
Or should we make __equals really work on GPU?
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.
well it should either fall back on default element-wise comparison, or use a user-supplied
opEqualsboth of which should be doable.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.
Sorry if I've been going in circles here--With the current patch (skipping the semantic check for these
generated template instances), __equals should actually be able to run on GPU.
Are you suggesting that I should implement the new lowering for == on GPU targets, so that == doesn't rely on __equals from the CPU runtime at all)?
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.
No, templates should already work across host/device (assuming that they are leaf level function or only call other templates. If
__equalsdoes something like callmemsetthen that obviously will not work.Why specifically does it not currently work? is it because of
memset.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.
No, it's not about
memsetThe problem isn't that __equals does anything GPU-incompatible — at first, I didn't quite figure it out either. The problem is how it gets instantiated. These template instances are added as members of the
@computemodule. ThendcomputeSemanticAnalysiswalks them and either:(cast(PureType)&isEqual)() inside __equalsbecause e->f is null for indirect calls.@computethendcomputeSemanticAnalysistriggersError: can only call functions from other @compute modules in @compute code.