This is an archive of the discontinued LLVM Phabricator instance.

[mlir][emitc] Add literal constant.
ClosedPublic

Authored by jpienaar on May 11 2023, 5:10 AM.

Details

Summary

A literal constant is not emitted as a variable but rather printed inline. The
form used is same as the Attribute emission form.

Currently this reuses the emit attribute method wherever name is queried. This
works for the use cases under consideration but could fail in cases where the
emission should be deferred. Addressing that requires some refactoring.

Diff Detail

Event Timeline

jpienaar created this revision.May 11 2023, 5:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2023, 5:10 AM
jpienaar requested review of this revision.May 11 2023, 5:10 AM

Could you also add tests to test/Dialect/EmitC/ops.mlir and maybe also to test/Dialect/EmitC/invalid_ops.mlir?

mlir/lib/Dialect/EmitC/IR/EmitC.cpp
206

constant op should be literal op here.

mlir/lib/Target/Cpp/TranslateToCpp.cpp
778

I think this would need to emit a cast expression like the printFloat function below (at least when called from a LiteralOp). Otherwise the exact type of the literal gets lost. The compiler might for example chose an unexpected function overload if a literal is used as an operand of an emitc.call op.

simon-camp added inline comments.May 11 2023, 6:45 AM
mlir/lib/Dialect/EmitC/IR/EmitC.cpp
84

We should disallow LiteralOps too here.

207

Wouldn't the AllTypesMatch constraint catch the type mismatch? The value can't be an OpaqueAttr anyhow here, because it is not a TypedAttr.

jpienaar marked 3 inline comments as done.May 13 2023, 6:26 AM
jpienaar added inline comments.
mlir/lib/Target/Cpp/TranslateToCpp.cpp
778

I started doing this and then wondered if I should go the opposite way: allow literal to take a string and return a different type. Make it just be a literal, have the lowering dictate the form and the type is to conform with the MLIR type system only. Then one could do things like

%20 = emitc.literal "M_PI" : f32

(no automatic casting or the like)

jpienaar updated this revision to Diff 539197.Jul 11 2023, 10:54 AM

Switch to string literal varient

jpienaar updated this revision to Diff 543597.Jul 24 2023, 9:36 AM

Update comment and test

marbre accepted this revision.Jul 27 2023, 6:42 AM

Thanks for updating the PR @jpienaar.

mlir/lib/Dialect/EmitC/IR/EmitC.cpp
207

Any comment on this?

This revision is now accepted and ready to land.Jul 27 2023, 6:42 AM
jpienaar added inline comments.Jul 27 2023, 7:22 AM
mlir/lib/Dialect/EmitC/IR/EmitC.cpp
207

Oh I removed this, but it would have yes. The attribute is now pure string and doesn't have a type associated beyond the op type to verify.

This revision was automatically updated to reflect the committed changes.