This allow lowering to support scf.for and scf.if with results. As right now spv region operations don't have return value the results are demoted to Function memory. We create one allocation per result right before the region and store the yield values in it. Then we can load back the value from allocation to be able to use the result.
Details
Diff Detail
Event Timeline
Cool, thanks!
mlir/lib/Conversion/GPUToSPIRV/ConvertGPUToSPIRV.cpp | ||
---|---|---|
45 | This comment needs to be updated. It would be nice to give a high-level description of what we are doing here regarding yield handling. | |
58 | llvm::to_vector<4>(operands) will work here? | |
142 | s/IfOp/scf ops/ | |
143 | There is no spv.if. :) | |
143 | s/doesn't have result/doesn't yield value/ | |
144 | s/ForOp/scf op/ | |
146 | s/Replace/replace/ | |
160 | I think you can directly pass in spirv::StorageClass::Function here. We should have a build method for that. | |
mlir/test/Conversion/GPUToSPIRV/if.mlir | ||
134 | Let's add a TODO here to mention this is missing proper VariablePointer capability. |
I realized my previous patch breaks seriazliation/deserialization since those require merge block to only have merge op in it. I rebased my change on top of D82914 and use the scf context to keep track of the VariableOp created during lowering of the control flow region. This allows adding store when lowering yield operation instead and solve the problem with merge block.
Sorry for the mess this cause in the review process.
mlir/include/mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h | ||
---|---|---|
26 ↗ | (On Diff #274651) | Can you please comment on what this class is, and its intended purpose? |
mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp | ||
23 ↗ | (On Diff #274651) | Please document what this is and what it is used for. |
25 ↗ | (On Diff #274651) | nit: Move these out of the mlir namespace and into the global namespace for the file. |
32 ↗ | (On Diff #274651) | nit: /// |
75 ↗ | (On Diff #274651) | nit: Please use /// for top-level comments. |
80 ↗ | (On Diff #274651) | Static methods should not be within namespaces. Please split this out of the anonymous namespace. Anonymous namespaces should not be used for functions. |
85 ↗ | (On Diff #274651) | nit: Please spell out auto here. |
88 ↗ | (On Diff #274651) | nit: Please spell out auto here. |
97 ↗ | (On Diff #274651) | nit: Please add a parameter comment here, /*...=*/nullptr |
134 ↗ | (On Diff #274651) | nit: Spell out auto here. |
144 ↗ | (On Diff #274651) | nit: Please cache the end iterator of for loops, and drop the int part of unsigned int. for (unsigned i = 1, e = ...; i != e; ++i) |
249 ↗ | (On Diff #274651) | nit: Drop trivial braces, and cache the end iterator. |
Nice! Thanks for this! I just have a few more nits. Please address River's comments.
mlir/include/mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h | ||
---|---|---|
26 ↗ | (On Diff #274651) | +1 |
mlir/lib/Conversion/GPUToSPIRV/ConvertGPUToSPIRV.cpp | ||
58 | Oh sorry misread. :) | |
mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp | ||
75 ↗ | (On Diff #274651) | "Replaces SCF op outputs with SPIR-V variable loads." |
78 ↗ | (On Diff #274651) | s/srf/SCF/ |
243 ↗ | (On Diff #274651) | If the region returns values, store ... |
mlir/lib/Conversion/GPUToSPIRV/ConvertGPUToSPIRV.cpp | ||
---|---|---|
160 | I think we have a build methods that is: static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type pointer, ::mlir::spirv::StorageClass storage_class, /*optional*/::mlir::Value initializer); |
This comment needs to be updated. It would be nice to give a high-level description of what we are doing here regarding yield handling.