[isl] Alternative to the `to_str()` method

8 views
Skip to first unread message

Riccardo Mori

unread,
Jun 11, 2021, 7:10:30 AMJun 11
to Polly Development
Hi everyone,
The official isl C++ interface doesn't export the method `to_str()` but instead has a `operator<<` so the only way to retrieve the string would be to define a function like this for each isl class:
```
std::string to_string(isl::aff &obj)
{
    std::ostringstream os;
    os << obj;
    return os.str();
}
```

Unfortunately by doing this we incurr in a unnecessary copy while storing the string in the ostringstream buffer.
In C++20 they added a way to avoid this overhead (https://en.cppreference.com/w/cpp/io/basic_ostringstream/str) by doing this:
```
return move(os).str();
```
Unfortunatelly the adoption of C++20 for llvm seems to be far away in the future, so we can either accept the overhead or stick with our own exported `to_str()` method and maybe trying to get it upstreamed.
I personally think that having a `to_str()` method would be handy, does anybody has a suggestion on how we should approach this difference?

Riccardo

Michael Kruse

unread,
Jun 11, 2021, 11:41:11 AMJun 11
to Riccardo Mori, Polly Development
Printing to string should not be in any hot code path, it is mostly used for debug output and exporting to a JSON file, we don't really need to optimize it. I think the higher priority would be to move-semantics for isl objects where we currently impose more cow-copies than necessary and used everywhere.

There are alternatives to to_str(). E.g.:
1. dump() / polly::dumpPw()
2. Use the C API to_str directly for glue code (such as to_string).  
4. isl_printer_print_* (not exposed by the C++ interface I think)  
5. ISLIOStream.h (well, it calls the to_str() method, but could be made to use the C interface instead)
6. stringFromIslObj (GICHelper.h, which AFAICS is exactly what you proposed)

Using the C interface for glue coe is fine, just for the main Polly logic it's nicer to not having to think (too much) about memory management.

Michael

--
Tardyzentrismus verboten!

Riccardo Mori

unread,
Jun 11, 2021, 1:34:56 PMJun 11
to Polly Development
Alright, In ISLIOStream.h I think we can rely on the official C++ interface by using the exported `operator<<`.
For all the rest I will overload stringFromIslObj (GICHelper.h) so that it will work with C++ objects like this: `std::string stringFromIslObj(isl::aff)` and internally will call the C interface.

Thank you,
Riccardo
Reply all
Reply to author
Forward
0 new messages