-
Notifications
You must be signed in to change notification settings - Fork 801
[SPIR][SimplifyCFG] Do not generate select insructions with sampler type #4682
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
In SPIR-V neither image nor sampler types cannot go through phi and select instructions. Lowering of phi nodes and select instructions with image/sampler result type by SPIR-V translator tool is very complicated in common case, so the transformation is disabled for image/sampler types in the pass which does it.
|
/summary:run |
|
I was wondering if that is something which we can upstream to |
I think it make sense to check with if such difference in OpenCL and SPIR-V specifications is intended. @bashbaug, @mkinsner, do know the motivation to put an addition restriction on SPIR-V format for image/sampler types? Alternative solution would be setting |
|
Interesting, we were discussing a similar issue internally a few weeks ago and I believe any differences are unintentional. The OpenCL C spec has a bunch of restrictions around images (see item (b) specifically):
I think these will also disallow using images and samplers for a select or phi. For the old SPIR spec, we have:
So, samplers are restricted in SPIR also. I didn't find similar text for images but I suspect this was just an omission. |
|
Will this break conditional selection of an image accessor in SYCL? Currently, nothing in the SYCL spec disallows code like this: |
No, It shouldn't break this. Such SYCL will be represented in the IR with another set of instructions that doesn't use phi and select with image/sampler result, but it still be valid and functional code. EDIT: I looked at the code snippet a bit more carefully, and I think this is yet another case where SYCL is less restrictive than OpenCL (another example I can remember is that sampler/image struct fields are disallowed in OpenCL and basically our image accessor or SYCL sampler class just break this restriction). Unoptimized, this code is invalid in OpenCL just because image accessors contain field of image opaque type inside, in case some optimizations are enabled - this will likely also break the restriction that we are discussing here, i.e. images can be used only as arguments to builtins. This is not connected with this patch though, since with it or without, such valid SYCL code cannot be valid in OpenCL, I think. |
I think it may negatively affect performance for non -image/-sampler types, so I changed behavior only for restricted types.
I wasn't sure about this. The original code I was looking at was valid from this point of view, the thing is that it somehow was transformed to code that is explicitly disallowed by SPIR-V spec looked for me like some kind of implementation detail which is out of scope of OpenCL spec.
The phrase "Any other operation involving these i32 values is implementation defined." looks a bit suspicious here. "Implementation defined" doesn't sound a bit different from "disallowed". I case I'm overreacting, should I prepare review for this patch in LLORG then? |
@intel/dpcpp-specification-reviewers, do you have any ideas how image/sampler restrictions can be implemented for SYCL image/sampler classes? I don't see an easy way for C++ classes to implement such restrictions and diagnose "invalid" image/sampler usages at compile time. |
As @bashbaug mentioned, we've been talking about this a little, though we have no resolution currently. One idea was that we could change the definition of
This seems like something that must be fixed. The only way to pass an "image" opaque type into a SYCL kernel is via an accessor. Perhaps we need some special handling for this argument type which decomposes the |
This already happens for kernel arguments. I'm not sure that changing of user's functions signature is a good idea though. |
Are you proposing that it should be legal to capture an image accessor as a kernel argument, but it should not be legal to pass an image accessor as a parameter to an internal function inside the kernel? For example: I think that would be a huge limitation to the language. Essentially, it means that kernels operating on images have to be written as one single function. That seems unrealistically limiting. |
No, I agree that this is too restrictive. I personally think that some of OpenCL rules are too restrictive as well. Probably they were designed for old devices or smth like that, because in SYCL we break some of OpenCL rules once we started supporting image accessors and sampler objects and everything still works. But, my original concern was also about assignment operation between two image accessors. I'm not sure there is an OpenCL/SPIRV built-in function doing something like this for images, so I think we may end up with code breaking the rules as well. |
This relates to the idea I mentioned above. If the "image_accessor" type really contained a pointer to the OpenCL image, then this wouldn't be a problem. Assigning one accessor to another would just assign one pointer to another, and this would not violate any SPIRV rules. However, as I said, it's not clear whether it's valid to even have a pointer to an OpenCL image.
Yes, I think this is a problem. We cannot get into a situation where legal SYCL programs fail when we disable optimizations. If correctness relies on some compiler transformation, we need to ensure that the transformation always happens, regardless of whether optimizations are enabled. |
It seems pointers to OpenCL images are not allowed (even clang complains about it - https://godbolt.org/z/KefjKMzqf ). OpenCL C spec says:
Same thing is mentioned for samplers. |
And this is what's really relevant for SYCL/DPC++. The OpenCL restrictions aren't directly relevant because SYCL code isn't compiled to OpenCL source, it's compiled to SPIR-V. |
I expect The OpenCL™ SPIR-V Environment Specification to apply OpenCL restrictions to SPIR-V. |
Being honest, I've failed to find some explicit restriction on having pointers to image/sampler (like I saw in OpenCL spec) in this spec either. But I suspect this is a bug in the OpenCL™ SPIR-V Environment Specification, or we will face some restrictions like "value of this type cannot be produced by this instruction. In addition, I think even if we will use a pointer to image inside image accessors, I don't think that OpenCL will allow us kernel argument that is a pointer to an image, so likely we will receive an image object, then wrap it with a pointer and here probably face some SPIR-V limitation. It sounds weird for me if this is somehow allowed, because it looks like sampler/image cannot go through phi/select instructions because this way it is not traceable, but it is allowed to go be referenced through a pointer. BTW, I somehow triggered this discussion, but I think it moves away from the purpose of this patch. The way how we store sampler/image objects in SYCL classes implementation doesn't lift SPIR-V restriction on having this types go through phi/select instructions. I even used OpenCL code to produce the test (because IR turns out much simpler) - https://godbolt.org/z/e1WGhEvYs , that means the problem exists there as well. So, can we proceed with this patch? |
Summarizing an offline discussion with @gmlueck : The SPIR-V spec (note the SPIR-V spec, not the OpenCL SPIR-V environment spec) is a bit vague what can and cannot be done with images and samplers. Images and samplers are defined to be "opaque types" in SPIR-V and have limitations that do not apply to "concrete types" like integers and floats and pointers but it's not easy to untangle exactly what the limitations are. If we convince ourselves that the restrictions from the OpenCL C spec do not exist in the SPIR-V spec then I agree they should be documented in the OpenCL SPIR-V environment spec. I believe the intent was to not add new image capablities going through SPIR-V than were available via OpenCL C. Please note that these restrictions can be relaxed via extensions if this functionality is needed for SYCL 2020. |
|
Can we allow using images/sampler types in phi/select instructions when running SPIR-V in Level Zero execution environment? |
No, I don't think it will. There are ways we could make this work but they are not enabled currently.
I consider this to be another variant of the extension caveat described above. Because this will not work today it would either need a Level Zero extension or a new Level Zero version that explicitly allows it. This all assumes that these operations are allowed in SPIR-V, otherwise we need a SPIR-V extension as well. |
|
@Fznamznon, FYI - this PR can be superseded by #5283. |
|
Issue of this PR might get back if we return simplifycfg in the future. |
That's what are tests for. |
|
I don't think that trying to fix the problem with samplers by fixing or disabling LLVM passes is a good idea. It is quick and simple, but who knows maybe other passes will expose the same problem. IMO, It is better to fix it by adding some requirements for SYCL spec and analyzing which SPIR-V spec requirements we can relax/clarify for modern hardware. |
In SPIR-V neither image nor sampler types cannot go through phi and
select instructions. Lowering of phi nodes and select instructions with
image/sampler result type by SPIR-V translator tool is very complicated
in common case, so the transformation is disabled for image/sampler
types in the pass which does it.