-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir][spirv] Split conditional basic blocks during deserialization #127639
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
Merged
Merged
Changes from all commits
Commits
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
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
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
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
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,60 @@ | ||
| ; RUN: %if spirv-tools %{ spirv-as --target-env spv1.0 %s -o - | mlir-translate --deserialize-spirv - -o - | FileCheck %s %} | ||
|
|
||
| ; COM: The purpose of this test is to check that a variable (in this case %color) that | ||
| ; COM: is defined before a selection region and used both in the selection region and | ||
| ; COM: after it, is not sunk into that selection region by the deserializer. If the | ||
| ; COM: variable is sunk, then it cannot be accessed outside the region and causes | ||
| ; COM: control-flow structurization to fail. | ||
|
|
||
| ; CHECK: spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> { | ||
| ; CHECK: spirv.func @main() "None" { | ||
| ; CHECK: spirv.Variable : !spirv.ptr<vector<4xf32>, Function> | ||
| ; CHECK: spirv.mlir.selection { | ||
| ; CHECK-NEXT: spirv.BranchConditional {{.*}}, ^[[bb:.+]], ^[[bb:.+]] | ||
| ; CHECK-NEXT: ^[[bb:.+]] | ||
| ; CHECK: spirv.Branch ^[[bb:.+]] | ||
| ; CHECK-NEXT: ^[[bb:.+]]: | ||
| ; CHECK-NEXT: spirv.mlir.merge | ||
| ; CHECK-NEXT: } | ||
| ; CHECK: spirv.Return | ||
| ; CHECK-NEXT: } | ||
| ; CHECK: } | ||
|
|
||
| OpCapability Shader | ||
| %2 = OpExtInstImport "GLSL.std.450" | ||
| OpMemoryModel Logical GLSL450 | ||
| OpEntryPoint Fragment %main "main" %colorOut | ||
| OpExecutionMode %main OriginUpperLeft | ||
| OpDecorate %colorOut Location 0 | ||
| %void = OpTypeVoid | ||
| %4 = OpTypeFunction %void | ||
| %float = OpTypeFloat 32 | ||
| %v4float = OpTypeVector %float 4 | ||
| %fun_v4float = OpTypePointer Function %v4float | ||
| %float_1 = OpConstant %float 1 | ||
| %float_0 = OpConstant %float 0 | ||
| %13 = OpConstantComposite %v4float %float_1 %float_0 %float_0 %float_1 | ||
| %out_v4float = OpTypePointer Output %v4float | ||
| %colorOut = OpVariable %out_v4float Output | ||
| %uint = OpTypeInt 32 0 | ||
| %uint_0 = OpConstant %uint 0 | ||
| %out_float = OpTypePointer Output %float | ||
| %bool = OpTypeBool | ||
| %25 = OpConstantComposite %v4float %float_1 %float_1 %float_0 %float_1 | ||
| %main = OpFunction %void None %4 | ||
| %6 = OpLabel | ||
| %color = OpVariable %fun_v4float Function | ||
| OpStore %color %13 | ||
| %19 = OpAccessChain %out_float %colorOut %uint_0 | ||
| %20 = OpLoad %float %19 | ||
| %22 = OpFOrdEqual %bool %20 %float_1 | ||
| OpSelectionMerge %24 None | ||
| OpBranchConditional %22 %23 %24 | ||
| %23 = OpLabel | ||
| OpStore %color %25 | ||
| OpBranch %24 | ||
| %24 = OpLabel | ||
| %26 = OpLoad %v4float %color | ||
| OpStore %colorOut %26 | ||
| OpReturn | ||
| OpFunctionEnd |
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 |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| ".test", | ||
| ".pdll", | ||
| ".c", | ||
| ".spv", | ||
| ] | ||
|
|
||
| # test_source_root: The root path where tests are located. | ||
|
|
||
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.
Thank you for following up on this objective! :)
Just a bit concerned about the added overhead here and in mlir/test/Target/SPIRV/loop.mlir. Is this creating extra unused branches?
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 is a good point, but I don't think it'll be much of an issue outside running
--test-spirv-roundtripthat is only used for testing. The splitting only happens in the deserializer, so it won't affect any lowering that use SPIR-V, etc. Then when it comes to deserialization, it shouldn't be an issue when going directly from SPIR-V generated from outside MLIR (GLSL, etc.). I would expect the code to always have something meaningful to split without introducing superfluous blocks.Now why do we get extra blocks with the roundtrip? I serialized the MLIR code from the test you commented and got (trimmed):
After serializing we are getting an extra block (%13) that would be unlikely to be present in non-MLIR generated SPIR-V, as OpSeletionMerge would be part of the predeceasing block. Actually, if you think about this is something my patch do, it isolates OpSeletionMerge and OpBranchConditional, so it shows my approach in deserialization matches how serializer works. Now because the block is already split, deserializng it again does further splitting creating superfluous blocks.
So, yes extra blocks are possible, but I think that would only happen if the input SPIR-V is already split and I don't think that would happen often with the upstream code. But even if it does happen, this only introduces some direct branches, which I believe are easy to optimise somewhere down the line - just collapse blocks together.
Hope that makes sense!
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.
Really appreciate the rigor, fwiw my entire workflow relies on doing spirv roundtrip.
Basically I am perf sensitive, and want to do spirv instrumentation & optimization passes at MLIR level before going back to spirv for deployment & this currently works very well.
Tested your PR with my use case, and figured I'd throw out a demo of what happens after roundtrip (easier to read as glsl):
Starting point:
Roundtrip from #123371 [My draft]:
Roundtrip from this PR:
To my naiive understanding, this still seems concerning, but eager to defer to your or @kuhar 's judgement on the matter -- perhaps I need to perform a loop detection pass if your version gets merged(?) or perhaps my perceived worry is actually just insignificant.
Uh oh!
There was an error while loading. Please reload this page.
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 for sharing it. I am a bit confused, because I didn't intend to affect loops, so I started looking into a simpler example and it seems to me that the loop continue block (https://mlir.llvm.org/docs/Dialects/SPIR-V/#loop) gets split into two. It wasn't my intention:
(I incorrectly called it a loop header here)
Let me investigate what's happing first and then I'll come back to you. There is no point in engaging into a deeper discussion when the problem may lie in an incorrect implementation :)
EDIT: Actually, this code may be doing what it intended (as header can have 2 outgoing edges) and the comment is correct, and I may need to handle continue blocks as well. Anyway, I need to re-think what I have done here.