Page MenuHomePhabricator

[OpenMP] Lower printf to __llvm_omp_vprintf

Authored by JonChesterfield on Oct 27 2021, 5:55 PM.



Extension of D112504. Lower amdgpu printf to __llvm_omp_vprintf
which takes the same const char*, void* arguments as cuda vprintf and also
passes the size of the void* alloca which will be needed by a non-stub
implementation of __llvm_omp_vprintf for amdgpu.

This removes the amdgpu link error on any printf in a target region in favour
of silently compiling code that doesn't print anything to stdout.

Diff Detail

Event Timeline

JonChesterfield requested review of this revision.Oct 27 2021, 5:55 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptOct 27 2021, 5:55 PM

Makes more sense with D112227 landed, but probably worthwhile just for the old runtime as it removes the link failure from all the tests that use printf.


This packArgsIntoNVPTXFormatBuffer helper could/should be factored out as a first patch to make the minimal change to EmitNVPTXDevicePrintfCallExpr clearer in the diff


the ## could be rolled into some other change, it makes PRINTF("no args") work, at which point we probably don't need/want #define PRINT()

351 ↗(On Diff #382868)

Possibly not a good thing to have in the interface

  • rebase to not require new runtime, stubs now return error

New runtime failing CI suggests it may take some iteration to land, so reworked this to make sense with or without it.

Doesn't make much difference to nvptx - printf still ultimately resolves to the same vprintf function as before - but a lot of tests that were previously disabled for amdgpu because references to printf were a linker error will now build and run, some of them successfully.


this should be -1, for 'error'

jdoerfert accepted this revision.Nov 8 2021, 7:26 AM

LG, one nit.


Suggestion: I feel this could be in a helper to avoid the duplication with the nvptx version. All but the extra argument is the same, no?


Namespace above should include _OMP, be anonymous or the functions should be static.

This revision is now accepted and ready to land.Nov 8 2021, 7:26 AM

Thanks for the review! Couple of helpers to split out and the rebase on main is messy, retesting now


Nice spot, yes - the tails of the two functions can fold.

// We don't know how to emit non-scalar varargs.

the block starting with that occurs three times in the files as of this diff, probably also worth splitting out


Sure, will patch. I thought the DeviceRTL had an internalize call as part of the build process which cuts out all the internal symbols but I might be imagining that.

  • rebase, static functions
  • helper for nonscalars

Updated. Left Builder.CreateCall alone as I've failed to think of a clean way to handle the sometimes present size argument. Tests pass locally and are a credible guess at what will pass the CI bot, but may need to roll back if there's a difference between local and CI

  • fold nvptx and openmp printf into one call
  • Drop ReturnValue argument

Folded EmitOpenMPDevicePrintfCallExpr and EmitNVPTXDevicePrintfCallExpr into asserts plus one line call into a common function. Leaving amdgpu alone in this patch as it's doing something odd with getScalarVal and I don't want to run this through the internal CI stack.

jdoerfert accepted this revision.Nov 8 2021, 10:26 AM
This revision was landed with ongoing or failed builds.Nov 8 2021, 10:38 AM
This revision was automatically updated to reflect the committed changes.


looks like this commit might be causing some failures on PowerPC buildbots, would you be able to take a quick look? Failing test case is Clang :: OpenMP/nvptx_target_printf_codegen.c.

Buildbot link:


JonChesterfield added a comment.EditedNov 8 2021, 12:30 PM

This passed the openmp buildbot but failed some others, e.g.

error: CHECK-64-NEXT: expected string not found in input
// CHECK-64-NEXT: [[TMP6:%.*]] = call i32 @vprintf(i8* [[TMP1]], i8* [[TMP5]])

Reverted while looking into this

Hi @Conanap! Sorry I didn't catch this quicker, you should be green on the next build

thanks for reverting!

JonChesterfield reopened this revision.Nov 8 2021, 1:12 PM
This revision is now accepted and ready to land.Nov 8 2021, 1:12 PM
JonChesterfield updated this revision to Diff 385614.EditedNov 8 2021, 1:15 PM
  • ./llvm/utils/ clang/test/OpenMP/nvptx_target_printf_codegen.c --opt=$HOME/llvm-install/bin/opt

^ that didn't work, updating the diff with the result while trying to remember/derive how this update script is meant to be used

Command Output (stderr):
$HOME/llvm-project/clang/test/OpenMP/nvptx_target_printf_codegen.c:49:20: error: CHECK-64-LABEL: expected string not found in input
// CHECK-64-LABEL: define {{[^@]+}}@{{__omp_offloading_[0-9a-z]+_[0-9a-z]+}}_CheckSimple_l13_worker
<stdin>:1:1: note: scanning from here
; ModuleID = '$HOME/llvm-project/clang/test/OpenMP/nvptx_target_printf_codegen.c'
<stdin>:21:1: note: possible intended match here
define weak void @__omp_offloading_10303_5e366c_CheckSimple_l13() #0 {

I think what has happened is the codegen test case is expecting a _worker subfunction which, after this change, has been inlined. Also, that isn't doing what I hoped, e.g. the calls in the test are still to @vprintf and not to @__llvm_oimp_printf.

It's not obviously a problem that the inlining decision has changed though I don't see why this patch would change it. It is a problem that the update script hasn't changed the print name. Deleting everything other than the RUN: statements and re-executing it doesn't update the print function name, though executing the RUN statement in the shell does:
%4 = call i32 @__llvm_omp_vprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str2, i64 0, i64 0), i8* %3, i32 4)

  • regen test file using the better './llvm/utils/ --llvm-bin=/home/amd/llvm-install/bin clang/test/OpenMP/nvptx_target_printf_codegen.c', now passing
JonChesterfield edited the summary of this revision. (Show Details)Nov 10 2021, 7:30 AM
This revision was landed with ongoing or failed builds.Nov 10 2021, 7:31 AM
This revision was automatically updated to reflect the committed changes.