This is an archive of the discontinued LLVM Phabricator instance.

support for HIP non hostcall printf

Authored by vikramRH on Nov 25 2022, 3:26 AM.



The patch essentially ports the existing non-hostcall printf support in OpenCL to HIP (to be controlled via new option "-fdelayed-printf"), with following changes

  1. Code refactoring -> we now use API's "getConstantStringInfo()" to extract constant string contents at compile time.
  2. Support to print non-const null terminated strings, required in HIP context. This is achieved as follows
    • calculate string size using a function "getStrlenWithNull()" and reserve the space in printf buffer using __printf_alloc() (as was the case in OpenCL, but number of bytes allocated could be dynamic now)
    • copy the string contents to buffer using previously calculated size and the pointer to string (a memcpy intrinsic is generated here)

Diff Detail

Event Timeline

vikramRH created this revision.Nov 25 2022, 3:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 25 2022, 3:26 AM
vikramRH requested review of this revision.Nov 25 2022, 3:26 AM
vikramRH planned changes to this revision.Nov 27 2022, 8:38 PM
arsenm requested changes to this revision.Nov 28 2022, 7:21 AM

I have a few questions. First, why surface this to users? If we really need to, I don't think this is the right flag name/design. A named argument to some kind of printf lowering flag would be better. Second, I thought we were moving towards hostcall printf, not away from it.


Typo 'onf'


Help text reads weird


Missing clang side tests


Why not share these? They should be in the same place. This should also be a separate change from any flag changes


auto *


Why would the block be missing a terminator here?


Really should try hard to avoid introducing ptrtoint. Do you really need to do a pointer subtract?


Don't need llvm::


Broken for opaque pointers?


Why isn't this using the IRBuilder?


Why do we have raw new and strcpy here? We have better string classes


Don't need these separate prefixes


Should use generated checks


Use named values in tests

There were some issues reported where it was found that pcie atomics are not available in some environments, as a result hostcall and HIP-Printf were also not available. This Patch is intended to provide an alternative in such scenarios while hostcall based implementation remains the primary option.

After discussions with Sameer, I am convinced that it is better to do this transformation at clang CodeGen phase rather than a opt pass (similar to current hostcall based implementation),
Hence this patch needs rework and I shall include the review comments in the updated patch. (I agree on the option too, I shall think of a option name with a named argument)

vikramRH abandoned this revision.Feb 10 2023, 5:24 AM

A new implentation is under discussion