This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Fix bug in partial dialect conversion
ClosedPublic

Authored by vinograd47 on Aug 21 2021, 12:17 AM.

Details

Summary

The discussion on forum:
https://llvm.discourse.group/t/bug-in-partial-dialect-conversion/4115

The applyPartialConversion didn't handle the operations, that were
marked as illegal inside dynamic legality callback.
Instead of reporting error, if such operation was not converted to legal set,
the method just added it to unconvertedSet in the same way as unknown operations.

This patch fixes that and handle dynamically illegal operations as well.

The patch includes 2 fixes for existing passes:

  • tensor-bufferize - explicitly mark std.return as legal.
  • convert-parallel-loops-to-gpu - ugly fix with marking visited operations to avoid recursive legality checks.

Diff Detail

Event Timeline

vinograd47 created this revision.Aug 21 2021, 12:17 AM
vinograd47 requested review of this revision.Aug 21 2021, 12:18 AM
bondhugula added inline comments.Aug 21 2021, 6:39 AM
mlir/include/mlir/Dialect/GPU/ParallelLoopMapper.h
45 ↗(On Diff #367946)

Do you need to expose this? Just make it local to SCFToGPU.cpp.

mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
574

rewriter.getUnitAttr()

Applied review remarks.

vinograd47 marked an inline comment as done.Aug 23 2021, 1:02 AM

@bondhugula I've fixed your remarks.

rriddle requested changes to this revision.Aug 23 2021, 1:54 AM

Thanks for the patch!

mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
573

Can you describe the logic behind this? It feels a bit sketchy.

mlir/lib/Transforms/Utils/DialectConversion.cpp
1547

I don't think this function name needs to change.

mlir/test/Transforms/test-legalizer.mlir
302–311

Please format the other tests like this one.

mlir/test/lib/Dialect/Test/TestOps.td
1326

UnregisteredOp is a confusing name for an operation that is registered. I'd suggest using a different/existing operation, or name this something else.

This revision now requires changes to proceed.Aug 23 2021, 1:54 AM

Fixed review remarks

vinograd47 marked 2 inline comments as done.Aug 23 2021, 8:09 AM
vinograd47 added inline comments.
mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
573

Added extra comments to the kVisitedAttrName constant declaration with logic explanation.

mlir/test/lib/Dialect/Test/TestOps.td
1326

Replaced it with std.constant.

vinograd47 marked an inline comment as done.

Fix review remarks

vinograd47 marked 2 inline comments as done.Aug 23 2021, 8:17 AM

@rriddle I've fixed your remarks

rriddle accepted this revision.Aug 23 2021, 12:47 PM

Approval for the general bits, but I'd like sign off from someone in the GPU space (e.g. @ftynse or @herhut ) as they would be the ones following up on (and possibly addressing in some other form in the future) the SCFToGPU.cpp parallel loop marker fix.

This revision is now accepted and ready to land.Aug 23 2021, 12:47 PM

@ftynse @herhut Please take a look into GPU related part.

ftynse added a comment.Sep 7 2021, 6:08 AM

Sorry for the delay: once approved by someone, the diff doesn't show in the review chain anymore, don't hesitate to ping me on other channels if necessary.

Are there cases (e.g., tests) where we expect the parallel loops with unsupported mapping to persist in the IR or are we always erroring out? In the latter case, just keeping the unlowered op as illegal would still error out but with a different message (conversion failed), we can just update the message in tests in that case.

mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
42

There's a second criterion, right?

50

Nit: consider adding a note, maybe a TODO, that a cleaner solution would require factoring out the "matching" logic from the pattern and its callees into a separate function that can be called from both the pattern and the op legality check.

Rebased and extend NOTE/TODO comment

vinograd47 marked 2 inline comments as done.Sep 20 2021, 12:17 AM

Are there cases (e.g., tests) where we expect the parallel loops with unsupported mapping to persist in the IR or are we always erroring out? In the latter case, just keeping the unlowered op as illegal would still error out but with a different message (conversion failed), we can just update the message in tests in that case.

Yes, there are such cases and they started to fail after my original fixes to dialect conversion engine. That's the reason why I've added such workaround to the SCFToGPU logic.

I've updated the comment in the pass with TODO note.

mlir/lib/Conversion/SCFToGPU/SCFToGPU.cpp
42

There's a second criterion, right?

42

There was only one criteria in the legality callback. The more advanced check is done in rewriter.

This revision was automatically updated to reflect the committed changes.
vinograd47 marked an inline comment as done.

It looks like there are some integration tests which have not been adapted to this change, e.g.

mlir/test/Integration/Dialect/Linalg/CPU/test-collapse-tensor.mlir

When running cmake, you can specify -DMLIR_INCLUDE_INTEGRATION_TESTS=ON to get them included, and then run the check-mlir target.
I think this change should be temporarily reverted to make the tests succeed again.

There are more tests that are failing:

Failed Tests (8):
  MLIR :: Integration/Dialect/Linalg/CPU/test-collapse-tensor.mlir
  MLIR :: Integration/Dialect/Linalg/CPU/test-elementwise.mlir
  MLIR :: Integration/Dialect/Linalg/CPU/test-expand-tensor.mlir
  MLIR :: Integration/Dialect/Linalg/CPU/test-padtensor.mlir
  MLIR :: Integration/Dialect/Linalg/CPU/test-subtensor-insert-multiple-uses.mlir
  MLIR :: Integration/Dialect/Linalg/CPU/test-subtensor-insert.mlir
  MLIR :: Integration/Dialect/Linalg/CPU/test-tensor-e2e.mlir
  MLIR :: Integration/Dialect/Linalg/CPU/test-tensor-matmul.mlir

@akuegel Yes, I'm currently investigating them.

@akuegel @ftynse I've just pushed a fix to upstream.