This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Allow Affine scalar replacement to handle inner operations
ClosedPublic

Authored by wsmoses on Jun 28 2021, 1:04 PM.

Details

Summary

Affine scalar replacement (and other affine passes, though not fixed here) don't properly handle operations with nested regions. This patch fixes the pass and two affine utilities to function properly given a non-affine internal region.

This patch prevents the pass from throwing an internal compiler error when running on the added test case.

Diff Detail

Event Timeline

wsmoses created this revision.Jun 28 2021, 1:04 PM
wsmoses requested review of this revision.Jun 28 2021, 1:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2021, 1:04 PM

The relevant test looks broken

ayzhuang requested changes to this revision.Jun 30 2021, 11:04 AM
ayzhuang added inline comments.
mlir/lib/Dialect/Affine/Transforms/AffineScalarReplacement.cpp
155 ↗(On Diff #355006)

should be >. A loop nest of depth 2 can have dependence at 3 different levels. Here all the stores have dependence into the following load, at different depths. First store: depth 1, second store: depth 2, third store: depth 3:

affine.for %i0 = 0 to 9 {
    affine.for %i1 = 0 to 9 {
      %a0 = affine.apply affine_map<(d0, d1) -> (d0 + 1)> (%i0, %i1)
      affine.store %c7, %m[%a0, %i1] : memref<20x20xf32>
      %v0 = affine.load %m[%i0, %i1] : memref<20x20xf32>
    }
  }
  
  affine.for %i0 = 0 to 9 {
    affine.for %i1 = 0 to 9 {
      %a1 = affine.apply affine_map<(d0, d1) -> (d1 + 1)> (%i0, %i1)
      affine.store %c7, %m[%i0, %a1] : memref<20x20xf32>
      %v0 = affine.load %m[%i0, %i1] : memref<20x20xf32>
    }
  }
  
  affine.for %i0 = 0 to 9 {
    affine.for %i1 = 0 to 9 {
      affine.store %c7, %m[%i0, %i1] : memref<20x20xf32>
      %v0 = affine.load %m[%i0, %i1] : memref<20x20xf32>
    }
  }

You can take a look at addOrderingConstraints function in lib/Analysis/AffineAnalysis.cpp for better understanding.

This revision now requires changes to proceed.Jun 30 2021, 11:04 AM
wsmoses updated this revision to Diff 355633.Jun 30 2021, 11:08 AM

Fix depth inequality

wsmoses edited the summary of this revision. (Show Details)Jul 1 2021, 8:14 AM
ayzhuang accepted this revision.Jul 1 2021, 9:40 AM

Looks good, thanks.

This revision is now accepted and ready to land.Jul 1 2021, 9:40 AM