This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][Presburger] introduce SlowMPInt, an auto-resizing APInt for fully correct signed integer computations
ClosedPublic

Authored by arjunp on Apr 13 2022, 8:37 PM.

Details

Summary

The Presburger library currently uses int64_t throughout for its integers.
This runs the risk of silently producing incorrect results when overflows occur.
Fixing this issue requires some sort of multiprecision integer
that transparently supports aribtrary arithmetic computations.

The class SlowMPInt provides this functionality, and is intended to be used
as the slow path fallback for a more optimized upcoming class, MPInt, that optimizes
for the Presburger library's workloads.

Diff Detail

Event Timeline

arjunp created this revision.Apr 13 2022, 8:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2022, 8:37 PM
arjunp requested review of this revision.Apr 13 2022, 8:37 PM
arjunp updated this revision to Diff 422733.Apr 13 2022, 8:43 PM

Add some additional documentation about overflows.

grosser added inline comments.Apr 13 2022, 9:30 PM
mlir/lib/Analysis/Presburger/TPInt.cpp
65 ↗(On Diff #422733)

'runOpWithExpandOnOverflow' could be an alternative name.

140 ↗(On Diff #422733)

'the values is'

mismatch in numerus

arjunp updated this revision to Diff 422834.Apr 14 2022, 6:01 AM
  • Rename TPInt to MPInt, MultiPrecision Integer.
  • Move the implementations to the header to facilitate inlining
  • Address other comments
ftynse added inline comments.Apr 14 2022, 6:57 AM
mlir/include/mlir/Analysis/Presburger/MPInt.h
84 ↗(On Diff #422837)

Nit: prefer static_cast to C-style casts.
Nit: it's a bit weird to have an inline function (I know it's a mere compiler hint) that is intended to be passed around as a function pointer, which would typically preclude inlining.

88 ↗(On Diff #422837)
221 ↗(On Diff #422837)

Nit: why is this abbreviated while gcd is not?

Could you comment on estimated performance effects if enabled in affine constructs?

arjunp updated this revision to Diff 435555.EditedJun 9 2022, 8:07 AM
arjunp marked 3 inline comments as done.

This approach initially had a slowdown of 6x. Tuning APInt is not so easy. The new planned approach is to use this class as a slow path fallback, so this class has hence been renamed to SlowMPInt.

I will be sending another patch for the main integer class, MPInt, that will be used in Presburger. This will have an int64-based fast path and will fallback to SlowMPInt on overflow for multi-precision arithmetic support.

Since this class isn't intended to be used outside the upcoming MPInt class, I've moved it into a detail namespace.

Since this class is no longer performance-critical, I have moved the function implementations back into a .cpp file.

Under this new design, on my laptop, 10 runs of the unittests take 1.99s on MPInt vs. 1.27s on native int64_t. For comparison, before the perf tuning patches I sent in the last few weeks, the runtime on native int64_t was 2.19s.

Essentially, the perf tuning plus the switch to MPInt taken together actually improve the performance of the library as compared to the state from two weeks ago.

arjunp retitled this revision from [MLIR][Prebsurger] introduce TPInt, an auto-resizing APInt for fully correct signed integer computations to [MLIR][Prebsurger] introduce SlowMPInt, an auto-resizing APInt for fully correct signed integer computations.Jun 9 2022, 8:17 AM
arjunp edited the summary of this revision. (Show Details)
Groverkss retitled this revision from [MLIR][Prebsurger] introduce SlowMPInt, an auto-resizing APInt for fully correct signed integer computations to [MLIR][Presburger] introduce SlowMPInt, an auto-resizing APInt for fully correct signed integer computations.Jun 9 2022, 8:35 AM
rriddle added inline comments.Jun 9 2022, 8:44 AM
mlir/lib/Analysis/Presburger/SlowMPInt.cpp
14

Please switch to 'using namespace blah' instead.

arjunp added inline comments.Jun 9 2022, 10:50 AM
mlir/lib/Analysis/Presburger/SlowMPInt.cpp
14

This necessitates prefixing all free function definitions with detail::. Is that really preferred?

rriddle added inline comments.Jun 9 2022, 10:56 AM
mlir/lib/Analysis/Presburger/SlowMPInt.cpp
14

Yes, and prefer static functions over putting them in anonymous namespaces.

https://llvm.org/docs/CodingStandards.html#use-namespace-qualifiers-to-implement-previously-declared-functions

arjunp updated this revision to Diff 435618.Jun 9 2022, 11:14 AM
arjunp marked 2 inline comments as done.

Address River's comment

mlir/lib/Analysis/Presburger/SlowMPInt.cpp
14

Ah, I see, thanks.

Groverkss added inline comments.Jun 18 2022, 3:20 PM
mlir/include/mlir/Analysis/Presburger/SlowMPInt.h
2–3

Please make this fit in one line

15–16

I don't think you need to write "soon to be implemented".

33
33

Could you also mention that the width is kept as a power of 2?

74

Should this be: void print(raw_ostream &os) const ?

mlir/lib/Analysis/Presburger/SlowMPInt.cpp
2

Please update this.

176

Why this semi colon?

mlir/unittests/Analysis/Presburger/SlowMPIntTest.cpp
107–110

Can we have some more overflow tests?

arjunp updated this revision to Diff 438555.Jun 20 2022, 9:54 PM
arjunp marked 7 inline comments as done.

Address kunwar's comments

arjunp added inline comments.Jun 21 2022, 8:08 PM
mlir/include/mlir/Analysis/Presburger/SlowMPInt.h
15–16

I think it should be there until that class actually exists in the code base

LGTM. But I would wait for @ftynse's review.

mlir/include/mlir/Analysis/Presburger/SlowMPInt.h
15–16

Ig just add a TODO to change this when MPInt is implemented.

ftynse accepted this revision.Jun 22 2022, 8:49 AM
ftynse added inline comments.
mlir/include/mlir/Analysis/Presburger/SlowMPInt.h
15

Nit: i wouldn't put "soon-to-be-implemented" in the file-level comment; it will likely get forgotten when MPInt is actually implemented.

mlir/lib/Analysis/Presburger/SlowMPInt.cpp
171

Can this be llvm::function_ref<APInt (const APInt &, const APInt &)> or does it fail to match types?

This revision is now accepted and ready to land.Jun 22 2022, 8:49 AM
arjunp updated this revision to Diff 439059.Jun 22 2022, 9:31 AM

Address Alex's comments

arjunp marked 4 inline comments as done.Jun 22 2022, 9:32 AM
arjunp added inline comments.
mlir/include/mlir/Analysis/Presburger/SlowMPInt.h
15

Okay. It just seemed weird to me to refer to a class that does not exist, even temporarily

mlir/lib/Analysis/Presburger/SlowMPInt.cpp
171

Yes, it can. Good catch, thanks

This revision was landed with ongoing or failed builds.Jun 22 2022, 9:37 AM
This revision was automatically updated to reflect the committed changes.
arjunp marked 2 inline comments as done.