This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add `ub` dialect and `poison` op.
ClosedPublic

Authored by Hardcode84 on Jun 30 2023, 11:56 AM.

Diff Detail

Event Timeline

Hardcode84 created this revision.Jun 30 2023, 11:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2023, 11:56 AM
Hardcode84 requested review of this revision.Jun 30 2023, 11:56 AM
tschuett added inline comments.
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
19 ↗(On Diff #536349)

Func dialect?

fix comment

Hardcode84 marked an inline comment as done.Jun 30 2023, 12:05 PM
Hardcode84 added inline comments.
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
19 ↗(On Diff #536349)

Copypaste )

Hardcode84 marked an inline comment as done.Jun 30 2023, 12:09 PM
kuhar added a comment.Jun 30 2023, 1:38 PM

What happened with the idea of having a poison attribute? I think this would be required for poison constants.

mlir/include/mlir/Dialect/CMakeLists.txt
2–4

Could you land this sorting change as a separate NFC commit? This seems uncontroversial and provides clear value IMO, but would be worth separating from this PR.

36

Can we follow the existing capitalization rules and call this UB?

mlir/include/mlir/Dialect/Ub/IR/UbOps.td
27 ↗(On Diff #536353)

Do we want this op to be constant-like? I guess that's not possible without a new attribute.

35 ↗(On Diff #536353)

What are the semantics of 'composite' poison types? E.g., vector<4xi32>, struct<...>, tensor<?x...>.

Are they TBD, defined by their dialect, or something else?

Hardcode84 added inline comments.Jun 30 2023, 2:47 PM
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
27 ↗(On Diff #536353)

With constant-like, rewriters will merge multiple poison into one, am I right? We should probably do this.

35 ↗(On Diff #536353)

I think, it should be up to the specific dialect/lowering how to interpret poison for its types.

Hardcode84 added inline comments.Jun 30 2023, 2:55 PM
mlir/include/mlir/Dialect/CMakeLists.txt
2–4
Matt added a subscriber: Matt.Jun 30 2023, 4:32 PM
kuhar added inline comments.Jul 1 2023, 2:27 PM
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
27 ↗(On Diff #536353)

I think declaring it as 'Pure' should be sufficient for that and handled by CSE.

But with this definition, it's impossible to create composite poison constants. For example, for vector<2xi32> whose only element 1 is poison, we'd have to first create a constant and then insert a poison. If we do this another way round and first poison the whole vector and, as-is, we do not know what happens if we insert a non-poison element. I think this story needs to be fleshed out before landing anything.

I can see temporary solution without poison attributes, but we should at least agree on the semantics.

35 ↗(On Diff #536353)

I think that for builtin types, the interpretation is universal. For scalars poison is an additional possible value. For composite types this is less obvious and should be carefully explained and agreed on.

Hardcode84 added inline comments.Jul 1 2023, 3:29 PM
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
35 ↗(On Diff #536353)

I think, current approach in llvm is to create whole poison vector and unpoison individual elements via insertElement, but I can't find any proper documentation on this.

I suggest to not introduce any special handling for aggregate/composite types in ub dialect (And I also think 'composite' term itself in not very well defined in MLIR, are memrefs composite or not).

If some dialect need some special handling for poison, it should introduce its own ops/attributes and refuse to lower ub.poison for its types.

kuhar added inline comments.Jul 1 2023, 4:38 PM
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
35 ↗(On Diff #536353)

I think, current approach in llvm is to create whole poison vector and unpoison individual elements via insertElement, but I can't find any proper documentation on this.

LLVM allows you to create vector constants with only some elements being poison, some example:
https://github.com/llvm/llvm-project/blob/15ef9b26adeb8c9dd98228fc26757966d8355986/llvm/test/Transforms/InstCombine/insert-extract-shuffle-inseltpoison.ll#L47

<2 x i32> <i32 0, i32 poison>

If some dialect need some special handling for poison, it should introduce its own ops/attributes and refuse to lower ub.poison for its types.

I'm not sure we agree on this. I think the end goal for poison would be to be an equally valid value as other ones.

Why wouldn't this be a property of types? Ops can decide which values to accept and produce, but I think of types being orthogonal to that. For example you could have an my.even_constant n : i32 op that only returns even constants, and fails to verify for odd numbers, but this does mean that 5 should now be considered a 'special' integer value.

(And I also think 'composite' term itself in not very well defined in MLIR, are memrefs composite or not).

I understand wanting to implement this by incrementally increasing the scope, but don't understand what the proposed strategy is. Could you briefly describe why we shouldn't do something like Karl's prototype with an attribute for poison values? It seems odd to me to have an op that is conceptually a constant without being an actual constant.

Hardcode84 added inline comments.Jul 3 2023, 1:52 PM
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
35 ↗(On Diff #536353)

Could you briefly describe why we shouldn't do something like Karl's prototype with an attribute for poison values? It seems odd to me to have an op that is conceptually a constant without being an actual constant.

For my usecase (control flow transformations) I need to create poisons of arbitrary type. If we decide to use arith.constant with poison attribute, I will have to hardcode arith dialect (and probably all dialects with custom types) dependency into my pass.

We can approach it with type interfaces (createPoison on type), but it will introduce dependency from builtin types to arith dialect.

Hardcode84 added inline comments.Jul 3 2023, 3:17 PM
mlir/include/mlir/Dialect/Ub/IR/UbOps.td
35 ↗(On Diff #536353)

After some more thought:
I think I can add poison op and poison attr to ub dialect, but without any composite semantics (always treat as fully poisoned), dialects can potentially introduce derived attributes to handle composites (and will have to do dyn_cast to check if it's base (fully-poisoned) or derived (partially))

Hardcode84 updated this revision to Diff 537372.Jul 5 2023, 8:45 AM

rebase, review comments, add poison attr

kuhar added a comment.Jul 12 2023, 1:36 PM

Thanks for pushing this forward, I think it's the right first step

mlir/include/mlir/Dialect/UB/IR/UBOps.td
57

Is this the full syntax? Where is the type?

mlir/test/Dialect/UB/ops.mlir
10

Since the poison op can return any type, should we add a couple of more types than just i32? I'm thinking vector/tensor/complex

@kuhar so what's your opinion on typed vs typeless PoisonAttr (from discussion thread)

rebase, fix asm format, more tests

Hardcode84 marked an inline comment as done.Jul 13 2023, 6:35 AM
Hardcode84 added inline comments.
mlir/include/mlir/Dialect/UB/IR/UBOps.td
57

FIxed.
Also, slightly changed asm format, because I was getting strange parsing errors with comma as separator

kuhar added a comment.Jul 13 2023, 6:39 AM

@kuhar so what's your opinion on typed vs typeless PoisonAttr (from discussion thread)

I think we will need more poison attributes in the future, but for all or nothing poison a single untyped attribute should work fine. And if we do need a typed full poison, this doesn't seem too hard to add to me. I'd probably start with a typed one from the beginning, following Karl's prototype, but I'm not the one doing the work so I don't get to complain about details like this.

kuhar accepted this revision.Jul 13 2023, 6:41 AM

Please try to get at least one more approval before submitting. I'd like to make sure that we have enough consensus in the community to move forward.

This revision is now accepted and ready to land.Jul 13 2023, 6:41 AM

add PoisonAttrInterface

fix namespace

move interface to separate file

fix license header

fix more license headers

This revision was automatically updated to reflect the committed changes.