[mlir][spirv] Serialize selection with separate header block
authorLei Zhang <antiagainst@google.com>
Mon, 13 Dec 2021 15:36:04 +0000 (10:36 -0500)
committerLei Zhang <antiagainst@google.com>
Mon, 13 Dec 2021 15:42:26 +0000 (10:42 -0500)
commit5e55a20119a31c19dd90a2e03329d21c291ea362
treefed27d59e7ce035156cd5185e16db78471dc8c35
parent9db8162820c49b2b9f17f3542b3cc2e05d8c00ea
[mlir][spirv] Serialize selection with separate header block

The previous "optimization" that tries to reuse existing block for
selection header block can be problematic for deserialization
because it effectively pulls in previous ops in the selection op's
enclosing block into the selection op's header. When deserializing,
those ops will be placed in the selection op's region. If any of
the previous ops has usage after the section op, it will break. That
is, the following IR cannot round trip:

```mlir
^bb:
  %def = ...
  spv.mlir.selection { ... }
  %use = spv.SomeOp %def
```

This commit removes the "optimization" to always create new blocks
for the selection header.

Along the way, also made error reporting better in deserialization
by turning asserts into proper errors and add check of uses outside
of sinked structured control flow region blocks.

Reviewed By: Hardcode84

Differential Revision: https://reviews.llvm.org/D115582
mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
mlir/lib/Target/SPIRV/Serialization/SerializeOps.cpp
mlir/test/Target/SPIRV/loop.mlir
mlir/test/Target/SPIRV/selection.mlir