This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][Presburger] Add values to PresburgerSpace
ClosedPublic

Authored by Groverkss on Jun 8 2022, 2:30 PM.

Details

Summary

This patch allows attaching user information, called "values" to each
identifier. The values are used to carry information along with variables and
are also used to determine if two variables are identical.

This patch is part of a series of patches to allow attaching user information
with variables in Presburger library.

Diff Detail

Event Timeline

Groverkss created this revision.Jun 8 2022, 2:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 8 2022, 2:30 PM
Groverkss requested review of this revision.Jun 8 2022, 2:30 PM
ftynse requested changes to this revision.Jun 9 2022, 1:23 AM
ftynse added inline comments.
mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
69

Nit: I'm trying to think of a better name than "values" to avoid potential confusion with mlir::Value. Something generic like info or data.

70

Btw, is there any expectations that values are unique for all dimensions?

71

It is worth mentioning that ValueType should be connected to MLIR's TypeID mechanism.

72–73
163–174

These look like they should be private, we don't want the give the caller access without type safety.

199–200

I'd rather have this return some kDimNotFound constant.

201

Let's template this method too, no need to show users void * (and many value types that wrap pointers aren't implicitly convertible to pointers).

220

Should this also clear the list of values, just in case?

269–270

Have you considered alternative storage schemes that don't require using 4 bytes (because of alignment) on each object to store a flag? One could steal a bit from one of the ints above and provide accessor functions with bit manipulation, for example.

272

This should be #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS (https://llvm.org/docs/ProgrammersManual.html#abi-breaking-checks) because it changes the size of the object that may be passed around in function arguments.

278

We likely want SmallVector<void *, 0> so we don't stack-allocate values for 6 pointers when they are not used.

Also nit: don't prefix SmallVector with llvm::.

mlir/lib/Analysis/Presburger/PresburgerSpace.cpp
163

makeArrayRef(values).slice(getIdKindOffset(kind), getNumIdKind(kind)) looks more readable

This revision now requires changes to proceed.Jun 9 2022, 1:23 AM
Groverkss updated this revision to Diff 435491.Jun 9 2022, 4:00 AM
Groverkss marked 12 inline comments as done.

Address Alex's comments

Groverkss added inline comments.Jun 9 2022, 4:15 AM
mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
69

The idea I had was to call all dimensions/symbols/locals as "variables" and the values as the "identifiers" since they "identify" variables. But I will be fine with anything else also like info or data.

70

There are no such requirements in the space. But practically, they should be unique so that two variables can be distinguished in a single space.

71

AFAIU mlir::TypeID has a fallback in case the ValueType is not connected to mlir::TypeID and should work for all types. So it is not required to be connected to mlir::TypeID right?

199–200

Is something like using kDimNotFound = INT_MAX acceptable?

269–270

We could use a flags variable to store these flags, with each bit corresponding to a different flag. I'm not sure if the stealing bit approach is as scalable in case we have to add more flags.

272

The reason I see this flag is used is so that the user can have an ABI compatible build between with asserts and without asserts. If a user disables LLVM_ENABLE_ABI_BREAKING_CHECKS but keeps LLVM_ENABLE_ASSERTIONS enabled, it will fail to compile, since we use valueType in asserts. Should we also guard the asserts with this macro?

ftynse added inline comments.Jun 9 2022, 5:20 AM
mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
69

I am fine with any suggestion, avoiding potential confusion is the most important part.

70

Aren't dimensions distinguished by IdKind + position? This makes it sounds like values are required to distinguish dimensions, but it is not really the case, is it?

71

The fallback is based on names and doesn't guarantee actual uniqueness. It is only relevant in case of shared libraries so may be a bit of a stretch, but it's better to at least tell the user that MLIR's TypeID mechanism is being used so they know how to debug.

199–200

UINT_MAX probably, or static_cast<unsigned>(-1) sounds fine.

211

This now also needs LLVM_ENABLE_ABI_BREAKING_CHECKS since the variable definition is guarded by this flag...

269–270

Are you planning to add more flags? If so, how many?

In general, I would say we expect numDomain + numRange + numSymbols + numLocals <= UNIT_MAX, otherwise we would not be able to reason about the total dimensionality of the space. Under this assumption, we can safely use 2 bits (take one bit from two of the values above) and be sure we are not losing any representational capacity.

272

Yes, absoutely.

278

llvm:: is unnecessary

Groverkss updated this revision to Diff 435719.Jun 9 2022, 3:46 PM
Groverkss marked 6 inline comments as done.
  • Address most comments (2 remaining)
Groverkss added inline comments.Jun 9 2022, 3:46 PM
mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
70

Right, I think I should have been more clear. What I mean was, let's say we are comparing two spaces and we are looking to align an identifier at the i^th position of some kind in space1 to an identifier in space2 of the same kind. We need the identifiers in space2 to be unique, so we have only one matching identifier in space2. This is why, practically, you would want the identifiers to be unique.

ftynse added inline comments.Jun 10 2022, 12:15 AM
mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
70

Makes sense. I suggest documenting this in the class documentation and in the relevant functions + adding the assertion in the function that values are indeed unique. May also be worth adding a documentation hint to the functions that add dimensions (and set values to null) so the user doesn't forget to add the values later on (a stricter alternative is to take the value or a list thereof as an optional extra parameter).

Groverkss marked 3 inline comments as done.
  • Address remaining comments
Groverkss added inline comments.Jun 13 2022, 1:50 PM
mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
69

Changed it to` attachments` for now. I'll send a bigger patch after this for naming since it would affect the whole Presburger library (variables/identifiers).

269–270

There is no current plan to add any flags, however, if we ever decide to move to nested spaces, named spaces, etc. we could need more flags.

For now, I would like to avoid stealing a bit from these ints since I'm not sure of the impact of having to extract the sub-ranges of the ints every time we want the variable. I can put this as a TODO, and we can decide this after benchmarking with and without this change. Would that be acceptable?

ftynse accepted this revision.Jun 14 2022, 1:09 AM
ftynse added inline comments.
mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
269–270

There is no current plan to add any flags, however, if we ever decide to move to nested spaces, named spaces, etc. we could need more flags.

When we do, we can reconsider. This sounds rather distant in the future so sounds like premature "optimization".

For now, I would like to avoid stealing a bit from these ints since I'm not sure of the impact of having to extract the sub-ranges of the ints every time we want the variable. I can put this as a TODO, and we can decide this after benchmarking with and without this change. Would that be acceptable?

This will cost one trivial integer arithmetic operation (right shift or bitwise "and" assuming you are stealing the least significant bit), pretty much the cheapest operation imaginable. Orders of magnitude less than a memory access. We use this trick (via PointerIntPair), e.g., to represent the small result number in mlir::OpResult (most common mlir::Value) without any visible performance problems, among many other places in LLVM. But sure, if you want to benchmark, I am happy to see the numbers.

This revision is now accepted and ready to land.Jun 14 2022, 1:09 AM
This revision was landed with ongoing or failed builds.Jun 14 2022, 10:46 AM
This revision was automatically updated to reflect the committed changes.