This is an archive of the discontinued LLVM Phabricator instance.

[libc] Rework the file handling for the GPU
ClosedPublic

Authored by jhuber6 on Aug 8 2023, 12:03 PM.

Details

Summary

The GPU has much tighter requirements for handling IO functions.
Previously we attempted to define the GPU as one of the platform files.
Using a common interface allowed us to easily define these functions
without much extra work. However, it became more clear that this was a
poor fit for the GPU. The file interface uses function pointers, which
prevented inlining and caused bad perfromance and resource usage on the
GPU. Further, using an actual FILE type rather than referring to it as
a host stub prevented us from usin files coming from the host on the GPU
device.

After talking with @sivachandra, the approach now is to simply define
GPU specific versions of the functions we intend to support. Also, we
are ignoring errno for the time being as it is unlikely we will ever
care about supporting it fully.

Diff Detail

Event Timeline

jhuber6 created this revision.Aug 8 2023, 12:03 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 8 2023, 12:03 PM
jhuber6 requested review of this revision.Aug 8 2023, 12:03 PM
jhuber6 updated this revision to Diff 548643.Aug 9 2023, 9:07 AM

Fix namespace not being applied to the struct in handle entrypoints.

sivachandra added inline comments.Aug 9 2023, 11:00 AM
libc/config/gpu/entrypoints.txt
88

I suppose you want to add these back in a future patch?

libc/src/__support/File/CMakeLists.txt
1–2

In a different patch, you can may be get rid of the GPU mutex also?

libc/src/stdio/CMakeLists.txt
37

This can be confusing or misleading. For example, if GPU libc does not use the buffered file abstraction, why do we have these dependencies listed here? I think a structuring like this might be more natural and explicit:

# All all entrypoints with generic implementations will be listed with a "generic_" prefix in the name.
# For example:
add_entrypoint_object(
  generic_fclose
  ...
)

# The helper function chooses the OS specialization if available, else falls back to the generic version.
function(add_stdio_entrypoint_object name)
  if(TARGET libc.src.stdio.${LIBC_TARGET_OS}.${name})
    add_entrypoint_object(
      ${name}
      ALIAS
      DEPENDS
        .${LIBC_TARGET_OS}.${name}
    )
  elif(TARGET libc.src.stdio.generic_${name})
    add_entrypoint_object(
      ${name}
      ALIAS
      DEPENDS
        .generic_${entrypoint}
    )
endfunction(add_stdio_entrypoint_object)

add_stdio_entrypoint_object(fclose)
...
397

These should be listed - the actual global vars come from the deps.

jhuber6 added inline comments.Aug 9 2023, 11:05 AM
libc/config/gpu/entrypoints.txt
88

Yes, wanted to keep this patch smaller.

libc/src/__support/File/CMakeLists.txt
1–2

We also use that for atexit.

libc/src/stdio/CMakeLists.txt
37

Alright, I can do that and then only change the files that are overridden currently.

397

So we'll need an if(LIBC_TARGET_ARCHITECTURE_IS_GPU) version since the file dependency isn't built.

sivachandra added inline comments.Aug 9 2023, 11:10 AM
libc/src/stdio/CMakeLists.txt
397

No, I mean't for the generic flavors.

add_entrypoint_object(
  generic_stdin
  ...
  DEPENDS
    libc.src.__support.File.file
    libc.src.__support.File.platform_file
)

...

add_stdio_entyrpoint_object(stdin)
sivachandra accepted this revision.Aug 9 2023, 11:43 AM
This revision is now accepted and ready to land.Aug 9 2023, 11:43 AM
sivachandra added inline comments.Aug 9 2023, 11:48 AM
libc/src/stdio/gpu/stderr.cpp
13

Missed this: Do you need these ifdef conditionals now?

jhuber6 updated this revision to Diff 548718.Aug 9 2023, 11:48 AM

Remove ifdefs

libc/src/stdio/gpu/stderr.cpp
13

You beat me fixing it by a few seconds.

This revision was automatically updated to reflect the committed changes.
sivachandra added inline comments.Aug 9 2023, 12:46 PM
libc/src/stdio/CMakeLists.txt
16

This line should be fixed to get the bots to green I think.

jhuber6 added inline comments.Aug 9 2023, 12:48 PM
libc/src/stdio/CMakeLists.txt
16

Yep, already pushed a fix. Sorry about that, copy paste strikes again.

libc/src/__support/File/file.cpp