This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add "memref::MemRefDialect" as dependentDialects for GpuToLLVMConversionPass
ClosedPublic

Authored by python3kgae on Jan 17 2023, 8:22 PM.

Details

Summary

For https://github.com/llvm/llvm-project/issues/60070.
The issue is caused by memref.store is not registed.
Registe it by add "memref::MemRefDialect" as dependetDialects for GpuToLLVMConsersionPass.

Diff Detail

Event Timeline

python3kgae created this revision.Jan 17 2023, 8:22 PM
python3kgae requested review of this revision.Jan 17 2023, 8:22 PM

Do you know why we are generating ops from the memref dialect in gpu to llvm conversion in the first place?

python3kgae added a comment.EditedJan 18 2023, 6:47 AM

Do you know why we are generating ops from the memref dialect in gpu to llvm conversion in the first place?

It happens in VectorStoreToMemrefStoreLowering::matchAndRewrite https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp#L2203 where rewriter.replaceOpWithNewOp<memref::StoreOp> is called.

It is added by populateVectorToLLVMConversionPatterns in GpuToLLVMConversionPass::runOnOperation.

But windows build seems go different pattern and not have the dependency. :( So the windows build behavior is the correct path and gpu to llvm conversion don't need memref dialect?

Both VectorStoreToMemrefStoreLowering and VectorLoadStoreConversion<mlir::vector::StoreOp, mlir::vector::StoreOpAdaptor> are added to legalize vector.store.
On windows, VectorLoadStoreConversion has higher benefit, so it does not crash.
And on linux, VectorStoreToMemrefStoreLowering has higher benefit, and memref.store is not resisted, so it will crash.

If patterns added for vector.store are expected to be selected, memref.store should be registered.

Still not sure why windows and linux has different benefit for VectorStoreToMemrefStoreLowering and VectorLoadStoreConversion<mlir::vector::StoreOp, mlir::vector::StoreOpAdaptor>. Should I create an issue for the difference?

Does one of the patterns actually has a higher benefit value? Or do they have the default value and one just happens to be selected instead of the other? In the latter case, it sounds like there is some non-determinism involved (e.g., something stored in a hashmap) that is exposed by platform differences.

Does one of the patterns actually has a higher benefit value? Or do they have the default value and one just happens to be selected instead of the other? In the latter case, it sounds like there is some non-determinism involved (e.g., something stored in a hashmap) that is exposed by platform differences.

The difference is caused by llvm::array_pod_sort used in

unsigned OperationLegalizer::applyCostModelToPatterns(
       unsigned generatedOpDepth = computeOpLegalizationDepth(
           generatedOp, minOpPatternDepth, legalizerPatterns);
       depth = std::max(depth, generatedOpDepth + 1);
     }

If change it to std::stable_sort, both windows and linux will select VectorStoreToMemrefStoreLowering and crash.

Create https://reviews.llvm.org/D142110 for the non-determinism between windows and linux.

It happens in VectorStoreToMemrefStoreLowering::matchAndRewrite https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp#L2203 where rewriter.replaceOpWithNewOp<memref::StoreOp> is called.

I suppose the same problem is possible with just -convert-vector-to-llvm. This is what happens with uncontrolled addition of patterns :(

ftynse accepted this revision.Jan 20 2023, 6:09 AM
This revision is now accepted and ready to land.Jan 20 2023, 6:09 AM

rebase to get stable_sort fix