This is an archive of the discontinued LLVM Phabricator instance.

[mlir][sparse] Moved a conditional from the RT library to the generated MLIR.
ClosedPublic

Authored by wrengr on Sep 20 2021, 6:15 PM.

Details

Summary

When generating code to add an element to SparseTensorCOO (e.g., when doing dense=>sparse conversion), we used to check for nonzero values on the runtime side, whereas now we generate MLIR code to do that check.

Diff Detail

Event Timeline

wrengr created this revision.Sep 20 2021, 6:15 PM
wrengr requested review of this revision.Sep 20 2021, 6:15 PM

A bunch of integration tests are currently failing. I'll look into those and post an update tomorrow (or later this week).

aartbik added inline comments.Sep 21 2021, 9:22 AM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp
182

this seems the right decision right, so no TODO required?

188

note that there is a rewriter.getZeroAttr(vtp) utility that works for both FP and INT (doing the same under water). Perhaps you can simply start with

Value zero = rewriter.getZeroAttr(vtp);
if (fp)
if(int)
etc. to make the CMP builders fit on one line for readability?

189

Note that LLVM style (and thus clang-tidy) prefers

if (x)

return...

if (y)

return ...

<anything else>

instead of the else-s

193

so no else, but just unreachable

As for the tests, I have several CHECK tests that verify the IR is as we expect for the conversion, e.g.

func @sparse_convert_1d(%arg0: tensor<?xi32>) -> tensor<?xi32, #SparseVector> {

%0 = sparse_tensor.convert %arg0 : tensor<?xi32> to tensor<?xi32, #SparseVector>
return %0 : tensor<?xi32, #SparseVector>

}

In this case, you will need to add the "if val != 0" to the pattern in the CHECKs.

wrengr updated this revision to Diff 374045.Sep 21 2021, 3:00 PM
wrengr marked 4 inline comments as done.

simplified getting zeroAttr(). And resolving clang-tidy.

mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp
182

I was just wanting confirmation, so sgtm.

188

Nice! I thought something like getZeroAttr ought to exist, but I just couldn't seem to find it.

wrengr updated this revision to Diff 374085.Sep 21 2021, 6:26 PM

Marking the newly generated comparison ops as legal.

aartbik added inline comments.Sep 21 2021, 9:47 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp
1 ↗(On Diff #374085)

Ah, good catch. Can you also fix the spares -> sparse while you are on this line ;-)

aartbik added inline comments.Sep 22 2021, 9:35 AM
mlir/lib/ExecutionEngine/SparseUtils.cpp
554–555

please rebase to main. you will most likely get conflicts on this file (due to the ABI fixes that just went in), but it will be extremely easy to resolve since you just need to find the two lines where you deleted this return ;-)

wrengr updated this revision to Diff 374311.Sep 22 2021, 11:49 AM
wrengr marked an inline comment as done.

typo fix

wrengr marked an inline comment as done.Sep 22 2021, 11:52 AM

rebase

Is this rebased with a "pulled" mainline? I don't seen the memref ABI fixes in this revision yet?
No big deal, but better to rebase now then have failed merge due to conflicts later ;-)

Pretty sure I pulled before rebasing, but I guess it didn't take for some reason.

wrengr updated this revision to Diff 374412.Sep 22 2021, 6:22 PM

re-rebased

aartbik accepted this revision.Sep 22 2021, 6:51 PM
This revision is now accepted and ready to land.Sep 22 2021, 6:51 PM