This is an archive of the discontinued LLVM Phabricator instance.

[mlir][spirv] Do not truncate i/f64 -> i/f32 in SPIRVConversion
ClosedPublic

Authored by kuhar on Oct 31 2022, 2:49 PM.

Details

Summary

This truncation can be unexpected and break program behavior.
Dedicated emulation passes should be used instead.

Also rename pass options to "emulate-lt-32-bit-scalar-types".

Fixes: https://github.com/llvm/llvm-project/issues/57917

Diff Detail

Event Timeline

kuhar created this revision.Oct 31 2022, 2:49 PM
kuhar requested review of this revision.Oct 31 2022, 2:49 PM
kuhar edited the summary of this revision. (Show Details)Oct 31 2022, 2:50 PM
antiagainst added inline comments.Nov 1 2022, 8:46 AM
mlir/test/Conversion/ArithToSPIRV/arith-to-spirv.mlir
1522

This is for 16 bit?

1567

This is for index and bool?

1868

This is for f16?

mlir/test/Conversion/FuncToSPIRV/types-to-spirv.mlir
48

This is for i8 and i16?

kuhar added inline comments.Nov 1 2022, 8:54 AM
mlir/test/Conversion/ArithToSPIRV/arith-to-spirv.mlir
1522

I tried to remove the related duplicated tests cases here and the remaining ones in https://reviews.llvm.org/D137166

Let me know if I removed too much by accident

1567

see above

1868

see above

mlir/test/Conversion/FuncToSPIRV/types-to-spirv.mlir
48

see above

Hardcode84 added inline comments.
mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
227

Can we get a separate option for this (set to False by default), instead of just disabling it?

kuhar added inline comments.Nov 3 2022, 9:23 PM
mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
227

Do we have a use case for this? If yes, I can add a new option like .emulateGT32BitScalarTypes, but this will make testing more difficult

Hardcode84 added inline comments.Nov 4 2022, 2:41 AM
mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
227

It looked useful but I'm ok for it to be removed if it is too much problem to keep it around.,

Also, after the second look it seem it will produce plainly wrong code for memrefs passes to kernel from outside, as element size changes.

antiagainst added inline comments.Nov 4 2022, 9:23 AM
mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
227

Yes. This is actually dangerous path, even behind an option. For cases where we really need larger bitwidth, the lowering would be just plainly wrong. So we'd like to remove it to avoid subtle bugs. For proper larger bitwidth emulation, Jakub has been builing a more proper and principled solution (you can search his recent commits); you can feel free to use that if needed.

mlir/test/Conversion/ArithToSPIRV/arith-to-spirv.mlir
1522

Verified yes this is duplicated.

1567

Verified yes this is duplicated.

1868

Verified yes this is duplicated.

mlir/test/Conversion/FuncToSPIRV/types-to-spirv.mlir
48

This one looks is not duplicated. So we should keep it.

antiagainst accepted this revision.Nov 4 2022, 9:28 AM

LGTM, but please double check the test in types-to-spirv.mlir

This revision is now accepted and ready to land.Nov 4 2022, 9:28 AM
Hardcode84 added inline comments.Nov 4 2022, 9:58 AM
mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
227

Just FYI, I was experimenting with proper f64 truncation passes in our project https://github.com/intel/mlir-extensions/pull/438

It probably can be pulled upstream after some fixes in memref dialect.

Thanks

antiagainst added inline comments.Nov 4 2022, 11:48 AM
mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
227

Oh that's cool to know! Jakub's change just handles integers. FP is more tricky; thanks for working on that!

kuhar updated this revision to Diff 473308.Nov 4 2022, 12:09 PM
kuhar marked 2 inline comments as done.

Add accidentally deleted tests. Rebase.

This revision was landed with ongoing or failed builds.Nov 4 2022, 12:11 PM
This revision was automatically updated to reflect the committed changes.