This is an archive of the discontinued LLVM Phabricator instance.

[Core] Add Twine support for StringAttr and Identifier. NFC.
ClosedPublic

Authored by lattner on Jun 5 2021, 11:38 AM.

Details

Summary

This is both more efficient and more ergonomic than going
through an std::string, e.g. when using llvm::utostr and
in string concat cases.

Diff Detail

Event Timeline

lattner created this revision.Jun 5 2021, 11:38 AM
lattner requested review of this revision.Jun 5 2021, 11:38 AM

What about just having the twine format given StringRef converts to twine implicitly and there is the getSingleStringRef method that should avoid extra overhead?

I'm open to that, only two downsides I can think of:

  1. absolutely tiny additional runtime overhead, slight codebloat for making the temporary twine. I'm not concerned about this.
  1. additional header depedency for everything that uses these methods, they'd have to include twine.h. This may already be happening implicitly though.

I'm open to that, only two downsides I can think of:

  1. absolutely tiny additional runtime overhead, slight codebloat for making the temporary twine. I'm not concerned about this.
  1. additional header depedency for everything that uses these methods, they'd have to include twine.h. This may already be happening implicitly though.

BuiltinAttributes.h already includes Twine.h transitively, Identifier.h doesn't. Though I would suspect that many uses of Identifier.h also bring in Twine.h through some other means.

lattner updated this revision to Diff 350500.Jun 7 2021, 11:18 PM

Simplify the API for Identifier::get and StringAttr::get to always take Twines.

River pointed out that Twine is effectively pervasive already, so we might as well
just use it.

lattner updated this revision to Diff 350501.Jun 7 2021, 11:19 PM
lattner edited the summary of this revision. (Show Details)

tweak commit message.

rriddle accepted this revision.Jun 8 2021, 1:16 AM
rriddle added inline comments.
mlir/lib/IR/MLIRContext.cpp
758

nit: Spell out auto here.

This revision is now accepted and ready to land.Jun 8 2021, 1:16 AM
lattner updated this revision to Diff 350634.Jun 8 2021, 9:46 AM
lattner marked an inline comment as done.

spell out auto

This revision was landed with ongoing or failed builds.Jun 8 2021, 9:47 AM
This revision was automatically updated to reflect the committed changes.
mehdi_amini added inline comments.Jul 16 2021, 12:53 AM
mlir/lib/IR/MLIRContext.cpp
758

I'm a bit puzzled by this change: this is pessimizing all the clients code that have a string already formed and were able to just pass a StringRef.

I feel that Twine is justified when an API may not have to materialize the string, otherwise in most cases you better hoist everything in the caller and keep the StringRef case fast (especially when it is the common case)

lattner added inline comments.Jul 16 2021, 3:04 PM
mlir/lib/IR/MLIRContext.cpp
758

What is your concern here? If you pass in something stringref compatible "toStringRef" will do the right thing, it doesn't add a copy of the data into tempStr unless needed.

mehdi_amini added inline comments.Jul 19 2021, 9:34 AM
mlir/lib/IR/MLIRContext.cpp
758

Ohhhh I didn't know about this toStringRef behavior! I thought the copy always happened...

lattner added inline comments.Jul 19 2021, 10:30 AM
mlir/lib/IR/MLIRContext.cpp
758

Nope! The vector is only there in case it is necessary, it isn't filled in if not.