This is an archive of the discontinued LLVM Phabricator instance.

[mlir][spirv] Fold noop `BitcastsOp`s
ClosedPublic

Authored by kuhar on Nov 4 2022, 12:47 PM.

Diff Detail

Event Timeline

kuhar created this revision.Nov 4 2022, 12:47 PM
kuhar requested review of this revision.Nov 4 2022, 12:47 PM
mravishankar accepted this revision.Nov 4 2022, 12:49 PM
This revision is now accepted and ready to land.Nov 4 2022, 12:49 PM
Hardcode84 added a comment.EditedNov 4 2022, 12:52 PM

Should we also update verifier to allow bitcasts to have same src and dst type? Is this allowed in spirv?

upd: original issue was failing with "result type must be different from operand type" but I cannot even find where it is checked.

kuhar added a comment.Nov 4 2022, 12:54 PM

Should we also update verifier to allow bitcasts to have same src and dst type? Is this allowed in spirv?

It's not allowed: https://registry.khronos.org/SPIR-V/specs/1.2/SPIRV.html#OpBitcast

Operand must have a type of OpTypePointer, or a scalar or vector of numerical-type. It must be a different type than Result Type.

Hardcode84 added a comment.EditedNov 4 2022, 1:01 PM

I'm ok to merge it as is to fix the bug, but we are basically abusing the fact the folder will run immediately after pattern, 'fixing' invalid IR that was produced.

mravishankar requested changes to this revision.Nov 4 2022, 1:31 PM

I'm ok to merge it as is to fix the bug, but we are basically abusing the fact the folder will run immediately after pattern, 'fixing' invalid IR that was produced.

Good point. Stamped without thinking through this. Maybe we can just change the folder to look for

%0 = spirv.Bitcast %a : <typea> to <typeb>
%1 = spirv.Bitcast %b : <typeb> to <typec>

and then fold to either

%0 = spirv.BItcast %a : <typea> to <typec>

or just

%a

if <typea> == <typeb>.

Marking as request changes for now.

This revision now requires changes to proceed.Nov 4 2022, 1:31 PM
kuhar updated this revision to Diff 473335.Nov 4 2022, 2:07 PM

Replace the canonicalizer with the folder to avoid generating invalid bitcasts.

kuhar updated this revision to Diff 473338.Nov 4 2022, 2:16 PM

Add early return

antiagainst accepted this revision.Nov 4 2022, 2:24 PM
mravishankar accepted this revision.Nov 4 2022, 2:29 PM
This revision is now accepted and ready to land.Nov 4 2022, 2:29 PM
Hardcode84 added inline comments.Nov 4 2022, 2:29 PM
mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp
125

It is only checks for limited depth, you can do smth like this to check into arbitrary deoth

Value current = getOperand();
while (auto parent = current.getDefiningOp<BitcastOp>()) {
  auto parentSource = parent.getOperand();
  if (parentSource.getType() == getType())
    return parentSource;

  current = parentSource;
}
Hardcode84 added inline comments.Nov 4 2022, 2:31 PM
mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp
125

Wait, nevermind, it will still check for arbitrary depth after multiple iterations, right?

kuhar marked 2 inline comments as done.Nov 4 2022, 2:32 PM
kuhar added inline comments.
mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.cpp
125

It works with longer chains because the innermost argument is always pushed through

Hardcode84 accepted this revision.Nov 4 2022, 2:33 PM
kuhar updated this revision to Diff 473343.Nov 4 2022, 2:35 PM
kuhar marked an inline comment as done.

Update test to demonstrate that long chains of bitcasts are also folded

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