Skip to content

[libc]: Clean up unnecessary function pointers in scanf #121215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 20, 2025

Conversation

vinay-deshmukh
Copy link
Contributor

@vinay-deshmukh vinay-deshmukh commented Dec 27, 2024

Resolves #115394

  1. Move definitions of cross-platform getc ungetc to reader.h.
  2. Remove function pointer members to define them once per platform in .h
  3. Built in overlay mode in macOS m1
  4. Remove reader.cpp as it's empty now

Also, full build doesn't yet build on macos m1 AFAIK

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

@llvmbot
Copy link
Member

llvmbot commented Dec 27, 2024

@llvm/pr-subscribers-libc

Author: Vinay Deshmukh (vinay-deshmukh)

Changes

Resolves #115394

  1. Move definition of Reader::getc from .h to .cpp similar to where Reader::ungetc is defined.
  2. Remove function pointer members to define them once per platform in .cpp
  3. Tried to test in "overlay mode": via ninja libc check-libc - https://libc.llvm.org/overlay_mode.html#id3 but the build fails locally due to the issue mentioned in this issue has appeared for me: [libc] macOS build issues since (v)asprintf patch #102145

Also, full build doesn't yet build on macos m1 AFAIK


Full diff: https://github.com/llvm/llvm-project/pull/121215.diff

3 Files Affected:

  • (modified) libc/src/stdio/scanf_core/reader.cpp (+64-1)
  • (modified) libc/src/stdio/scanf_core/reader.h (+3-23)
  • (modified) libc/src/stdio/scanf_core/vfscanf_internal.h (+1-30)
diff --git a/libc/src/stdio/scanf_core/reader.cpp b/libc/src/stdio/scanf_core/reader.cpp
index ec1f5c098dc7a6..ccfcd220e19c21 100644
--- a/libc/src/stdio/scanf_core/reader.cpp
+++ b/libc/src/stdio/scanf_core/reader.cpp
@@ -6,13 +6,75 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/types/FILE.h"
 #include "src/stdio/scanf_core/reader.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/File/file.h"
+
 #include <stddef.h>
 
+
+
+
 namespace LIBC_NAMESPACE_DECL {
 namespace scanf_core {
 
+namespace internal {
+
+#if defined(LIBC_TARGET_ARCH_IS_GPU)
+// The GPU build provides FILE access through the host operating system's
+// library. So here we simply use the public entrypoints like in the SYSTEM_FILE
+// interface. Entrypoints should normally not call others, this is an exception.
+// FIXME: We do not acquire any locks here, so this is not thread safe.
+LIBC_INLINE int getc(void *f) {
+  return LIBC_NAMESPACE::getc(reinterpret_cast<::FILE *>(f));
+}
+
+LIBC_INLINE void ungetc(int c, void *f) {
+  LIBC_NAMESPACE::ungetc(c, reinterpret_cast<::FILE *>(f));
+}
+
+#elif !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
+
+LIBC_INLINE int getc(void *f) {
+  unsigned char c;
+  auto result =
+      reinterpret_cast<LIBC_NAMESPACE::File *>(f)->read_unlocked(&c, 1);
+  size_t r = result.value;
+  if (result.has_error() || r != 1)
+    return '\0';
+
+  return c;
+}
+
+LIBC_INLINE void ungetc(int c, void *f) {
+  reinterpret_cast<LIBC_NAMESPACE::File *>(f)->ungetc_unlocked(c);
+}
+
+#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
+
+// Since ungetc_unlocked isn't always available, we don't acquire the lock for
+// system files.
+LIBC_INLINE int getc(void *f) { return ::getc(reinterpret_cast<::FILE *>(f)); }
+
+LIBC_INLINE void ungetc(int c, void *f) {
+  ::ungetc(c, reinterpret_cast<::FILE *>(f));
+}
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
+
+} // namespace internal
+
+char Reader::getc() {
+  ++cur_chars_read;
+  if (rb != nullptr) {
+    char output = rb->buffer[rb->buff_cur];
+    ++(rb->buff_cur);
+    return output;
+  }
+  // This should reset the buffer if applicable.
+  return static_cast<char>(internal::getc(input_stream));
+}
+
 void Reader::ungetc(char c) {
   --cur_chars_read;
   if (rb != nullptr && rb->buff_cur > 0) {
@@ -23,7 +85,8 @@ void Reader::ungetc(char c) {
     --(rb->buff_cur);
     return;
   }
-  stream_ungetc(static_cast<int>(c), input_stream);
+  internal::ungetc(static_cast<int>(c), input_stream);
 }
+
 } // namespace scanf_core
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/scanf_core/reader.h b/libc/src/stdio/scanf_core/reader.h
index f984fd93789109..e29ea6ea7d8917 100644
--- a/libc/src/stdio/scanf_core/reader.h
+++ b/libc/src/stdio/scanf_core/reader.h
@@ -16,9 +16,6 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace scanf_core {
 
-using StreamGetc = int (*)(void *);
-using StreamUngetc = void (*)(int, void *);
-
 // This is intended to be either a raw string or a buffer syncronized with the
 // file's internal buffer.
 struct ReadBuffer {
@@ -29,38 +26,21 @@ struct ReadBuffer {
 
 class Reader {
   ReadBuffer *rb;
-
   void *input_stream = nullptr;
-
-  // TODO: Remove these unnecessary function pointers
-  StreamGetc stream_getc = nullptr;
-  StreamUngetc stream_ungetc = nullptr;
-
   size_t cur_chars_read = 0;
 
 public:
   // TODO: Set buff_len with a proper constant
   LIBC_INLINE Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}
 
-  LIBC_INLINE Reader(void *stream, StreamGetc stream_getc_in,
-                     StreamUngetc stream_ungetc_in,
+  LIBC_INLINE Reader(void *stream,
                      ReadBuffer *stream_buffer = nullptr)
-      : rb(stream_buffer), input_stream(stream), stream_getc(stream_getc_in),
-        stream_ungetc(stream_ungetc_in) {}
+      : rb(stream_buffer), input_stream(stream){}
 
   // This returns the next character from the input and advances it by one
   // character. When it hits the end of the string or file it returns '\0' to
   // signal to stop parsing.
-  LIBC_INLINE char getc() {
-    ++cur_chars_read;
-    if (rb != nullptr) {
-      char output = rb->buffer[rb->buff_cur];
-      ++(rb->buff_cur);
-      return output;
-    }
-    // This should reset the buffer if applicable.
-    return static_cast<char>(stream_getc(input_stream));
-  }
+  char getc(); 
 
   // This moves the input back by one character, placing c into the buffer if
   // this is a file reader, else c is ignored.
diff --git a/libc/src/stdio/scanf_core/vfscanf_internal.h b/libc/src/stdio/scanf_core/vfscanf_internal.h
index 67126431fcded5..84d074711b8fb6 100644
--- a/libc/src/stdio/scanf_core/vfscanf_internal.h
+++ b/libc/src/stdio/scanf_core/vfscanf_internal.h
@@ -38,14 +38,6 @@ LIBC_INLINE void flockfile(::FILE *) { return; }
 
 LIBC_INLINE void funlockfile(::FILE *) { return; }
 
-LIBC_INLINE int getc(void *f) {
-  return LIBC_NAMESPACE::getc(reinterpret_cast<::FILE *>(f));
-}
-
-LIBC_INLINE void ungetc(int c, void *f) {
-  LIBC_NAMESPACE::ungetc(c, reinterpret_cast<::FILE *>(f));
-}
-
 LIBC_INLINE int ferror_unlocked(::FILE *f) { return LIBC_NAMESPACE::ferror(f); }
 
 #elif !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
@@ -58,21 +50,6 @@ LIBC_INLINE void funlockfile(FILE *f) {
   reinterpret_cast<LIBC_NAMESPACE::File *>(f)->unlock();
 }
 
-LIBC_INLINE int getc(void *f) {
-  unsigned char c;
-  auto result =
-      reinterpret_cast<LIBC_NAMESPACE::File *>(f)->read_unlocked(&c, 1);
-  size_t r = result.value;
-  if (result.has_error() || r != 1)
-    return '\0';
-
-  return c;
-}
-
-LIBC_INLINE void ungetc(int c, void *f) {
-  reinterpret_cast<LIBC_NAMESPACE::File *>(f)->ungetc_unlocked(c);
-}
-
 LIBC_INLINE int ferror_unlocked(FILE *f) {
   return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->error_unlocked();
 }
@@ -85,12 +62,6 @@ LIBC_INLINE void flockfile(::FILE *) { return; }
 
 LIBC_INLINE void funlockfile(::FILE *) { return; }
 
-LIBC_INLINE int getc(void *f) { return ::getc(reinterpret_cast<::FILE *>(f)); }
-
-LIBC_INLINE void ungetc(int c, void *f) {
-  ::ungetc(c, reinterpret_cast<::FILE *>(f));
-}
-
 LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror(f); }
 
 #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
@@ -103,7 +74,7 @@ LIBC_INLINE int vfscanf_internal(::FILE *__restrict stream,
                                  const char *__restrict format,
                                  internal::ArgList &args) {
   internal::flockfile(stream);
-  scanf_core::Reader reader(stream, &internal::getc, internal::ungetc);
+  scanf_core::Reader reader(stream);
   int retval = scanf_core::scanf_main(&reader, format, args);
   if (retval == 0 && internal::ferror_unlocked(stream))
     retval = EOF;

Copy link

github-actions bot commented Dec 27, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

From:
https://github.com/llvm/llvm-project/actions/runs/12518682283/job/34921546556?pr=121215
Error was:
```
/usr/bin/ld: libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/./reader.cpp.o: in function `__llvm_libc_20_0_0_git::scanf_core::Reader::getc()':
reader.cpp:(.text._ZN22__llvm_libc_20_0_0_git10scanf_core6Reader4getcEv+0x38): undefined reference to `__llvm_libc_20_0_0_git::File::read_unlocked(void*, unsigned long)'
/usr/bin/ld: libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/./reader.cpp.o: in function `__llvm_libc_20_0_0_git::scanf_core::Reader::ungetc(char)':
reader.cpp:(.text._ZN22__llvm_libc_20_0_0_git10scanf_core6Reader6ungetcEc+0x2c): undefined reference to `__llvm_libc_20_0_0_git::File::ungetc_unlocked(int)'
/usr/bin/ld: libc/test/src/stdio/libc.test.src.stdio.sscanf_test.__unit__.__build__: hidden symbol `_ZN22__llvm_libc_20_0_0_git4File13read_unlockedEPvm' isn't defined
/usr/bin/ld: final link failed: bad value
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```
@vinay-deshmukh
Copy link
Contributor Author

Ubuntu error - linker error for read_unlocked ungetc_unlocked
usr/bin/ld: libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/./reader.cpp.o: in function `__llvm_libc_20_0_0_git::scanf_core::Reader::getc()':
reader.cpp:(.text._ZN22__llvm_libc_20_0_0_git10scanf_core6Reader4getcEv+0x38): undefined reference to `__llvm_libc_20_0_0_git::File::read_unlocked(void*, unsigned long)'
/usr/bin/ld: libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/./reader.cpp.o: in function `__llvm_libc_20_0_0_git::scanf_core::Reader::ungetc(char)':
reader.cpp:(.text._ZN22__llvm_libc_20_0_0_git10scanf_core6Reader6ungetcEc+0x2c): undefined reference to `__llvm_libc_20_0_0_git::File::ungetc_unlocked(int)'
/usr/bin/ld: libc/test/src/stdio/libc.test.src.stdio.vsscanf_test.__unit__.__build__: hidden symbol `_ZN22__llvm_libc_20_0_0_git4File13read_unlockedEPvm' isn't defined
/usr/bin/ld: final link failed: bad value
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Windows error - macro substitution issue
[252/1389] Building CXX object libc\src\stdio\scanf_core\CMakeFiles\libc.src.stdio.scanf_core.reader.dir\reader.cpp.obj
FAILED: libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/reader.cpp.obj 
sccache C:\PROGRA~1\LLVM\bin\clang-cl.exe  /nologo -TP -DLIBC_NAMESPACE=__llvm_libc_20_0_0_git -ID:\a\llvm-project\llvm-project\libc -imsvcD:\a\llvm-project\llvm-project\build\libc\include /DWIN32 /D_WINDOWS   /Zc:inline /Zc:__cplusplus /Oi /Brepro /bigobj /permissive- -Werror=unguarded-availability-new /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O1 /Ob1 /DNDEBUG -std:c++17 -MD -Z7 -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_ADD_NULL_CHECKS /EHs-c- /GR- /showIncludes /Folibc\src\stdio\scanf_core\CMakeFiles\libc.src.stdio.scanf_core.reader.dir\reader.cpp.obj /Fdlibc\src\stdio\scanf_core\CMakeFiles\libc.src.stdio.scanf_core.reader.dir\ -c -- D:\a\llvm-project\llvm-project\libc\src\stdio\scanf_core\reader.cpp
In file included from D:\a\llvm-project\llvm-project\libc\src\stdio\scanf_core\reader.cpp:10:
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(51,28): error: use of undeclared identifier 'off_t'
   51 |   using SeekFunc = ErrorOr<off_t>(File *, off_t, int);
      |                            ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(51,43): error: unknown type name 'off_t'
   51 |   using SeekFunc = ErrorOr<off_t>(File *, off_t, int);
      |                                           ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(87,3): error: unknown type name 'SeekFunc'
   87 |   SeekFunc *platform_seek;
      |   ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(90,3): error: unknown type name 'Mutex'
   90 |   Mutex mutex;
      |   ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(156,47): error: unknown type name 'SeekFunc'
  156 |   constexpr File(WriteFunc *wf, ReadFunc *rf, SeekFunc *sf, CloseFunc *cf,
      |                                               ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(186,21): error: unknown type name 'off_t'
  186 |   ErrorOr<int> seek(off_t offset, int whence);
      |                     ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(188,11): error: use of undeclared identifier 'off_t'
  188 |   ErrorOr<off_t> tell();
      |           ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(314,14): error: expected parameter declarator
  314 | extern File *stdin;
      |              ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.[261](https://github.com/llvm/llvm-project/actions/runs/12527517475/job/34941171823?pr=121215#step:10:262)00.0\ucrt\corecrt_wstdio.h(36,33): note: expanded from macro 'stdin'
   36 | #define stdin  (__acrt_iob_func(0))
      |                                 ^
In file included from D:\a\llvm-project\llvm-project\libc\src\stdio\scanf_core\reader.cpp:10:
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(314,14): error: expected ')'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(36,33): note: expanded from macro 'stdin'
   36 | #define stdin  (__acrt_iob_func(0))
      |                                 ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(314,14): note: to match this '('
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(36,32): note: expanded from macro 'stdin'
   36 | #define stdin  (__acrt_iob_func(0))
      |                                ^
In file included from D:\a\llvm-project\llvm-project\libc\src\stdio\scanf_core\reader.cpp:10:
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(315,14): error: expected parameter declarator
  315 | extern File *stdout;
      |              ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(37,33): note: expanded from macro 'stdout'
   37 | #define stdout (__acrt_iob_func(1))
      |                                 ^
In file included from D:\a\llvm-project\llvm-project\libc\src\stdio\scanf_core\reader.cpp:10:
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(315,14): error: expected ')'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(37,33): note: expanded from macro 'stdout'
   37 | #define stdout (__acrt_iob_func(1))
      |                                 ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(315,14): note: to match this '('
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(37,32): note: expanded from macro 'stdout'
   37 | #define stdout (__acrt_iob_func(1))
      |                                ^
In file included from D:\a\llvm-project\llvm-project\libc\src\stdio\scanf_core\reader.cpp:10:
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(316,14): error: expected parameter declarator
  316 | extern File *stderr;
      |              ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(38,33): note: expanded from macro 'stderr'
   38 | #define stderr (__acrt_iob_func(2))
      |                                 ^
In file included from D:\a\llvm-project\llvm-project\libc\src\stdio\scanf_core\reader.cpp:10:
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(316,14): error: expected ')'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(38,33): note: expanded from macro 'stderr'
   38 | #define stderr (__acrt_iob_func(2))
      |                                 ^
D:\a\llvm-project\llvm-project\libc\src/__support/File/file.h(316,14): note: to match this '('
C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt\corecrt_wstdio.h(38,32): note: expanded from macro 'stderr'
   38 | #define stderr (__acrt_iob_func(2))
      |                                ^
13 errors generated.
Macos Error - macro substitution issue
libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/reader.cpp.o
FAILED: libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/reader.cpp.o 
sccache /usr/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_20_0_0_git -I/Users/runner/work/llvm-project/llvm-project/libc -isystem /Users/runner/work/llvm-project/llvm-project/build/libc/include -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -Os -DNDEBUG -std=gnu++17 -arch arm64 -isysroot /Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_ADD_NULL_CHECKS -fpie -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -MD -MT libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/reader.cpp.o -MF libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/reader.cpp.o.d -o libc/src/stdio/scanf_core/CMakeFiles/libc.src.stdio.scanf_core.reader.dir/reader.cpp.o -c /Users/runner/work/llvm-project/llvm-project/libc/src/stdio/scanf_core/reader.cpp
In file included from /Users/runner/work/llvm-project/llvm-project/libc/src/stdio/scanf_core/reader.cpp:10:
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:90:3: error: unknown type name 'Mutex'
  Mutex mutex;
  ^
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:260:8: error: expected member name or ';' after declaration specifiers
  void clearerr_unlocked() { err = false; }
  ~~~~ ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:397:30: note: expanded from macro 'clearerr_unlocked'
#define clearerr_unlocked(p)    __sclearerr(p)
                                ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:285:26: note: expanded from macro '__sclearerr'
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
                          ^
In file included from /Users/runner/work/llvm-project/llvm-project/libc/src/stdio/scanf_core/reader.cpp:10:
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:260:8: error: expected ')'
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:397:30: note: expanded from macro 'clearerr_unlocked'
#define clearerr_unlocked(p)    __sclearerr(p)
                                ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:285:26: note: expanded from macro '__sclearerr'
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
                          ^
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:260:8: note: to match this '('
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:397:30: note: expanded from macro 'clearerr_unlocked'
#define clearerr_unlocked(p)    __sclearerr(p)
                                ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:285:25: note: expanded from macro '__sclearerr'
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
                         ^
In file included from /Users/runner/work/llvm-project/llvm-project/libc/src/stdio/scanf_core/reader.cpp:10:
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:260:8: error: unknown type name '_flags'
  void clearerr_unlocked() { err = false; }
       ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:397:30: note: expanded from macro 'clearerr_unlocked'
#define clearerr_unlocked(p)    __sclearerr(p)
                                ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:285:37: note: expanded from macro '__sclearerr'
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
                                     ^
In file included from /Users/runner/work/llvm-project/llvm-project/libc/src/stdio/scanf_core/reader.cpp:10:
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:[260](https://github.com/llvm/llvm-project/actions/runs/12527517475/job/34941171892?pr=121215#step:10:261):8: error: a type specifier is required for all declarations
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:397:30: note: expanded from macro 'clearerr_unlocked'
#define clearerr_unlocked(p)    __sclearerr(p)
                                ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:285:34: note: expanded from macro '__sclearerr'
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
                                  ^
In file included from /Users/runner/work/llvm-project/llvm-project/libc/src/stdio/scanf_core/reader.cpp:10:
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:260:8: error: expected ')'
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:397:30: note: expanded from macro 'clearerr_unlocked'
#define clearerr_unlocked(p)    __sclearerr(p)
                                ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:285:44: note: expanded from macro '__sclearerr'
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
                                            ^
/Users/runner/work/llvm-project/llvm-project/libc/src/__support/File/file.h:260:8: note: to match this '('
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:397:30: note: expanded from macro 'clearerr_unlocked'
#define clearerr_unlocked(p)    __sclearerr(p)
                                ^
/Applications/Xcode_15.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/stdio.h:285:31: note: expanded from macro '__sclearerr'
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
                               ^
6 errors generated.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good start, there's a couple things to clean up, but with the suggested changes it build on linux for me.

@@ -62,6 +62,7 @@ add_object_library(
reader.h
DEPENDS
libc.src.__support.macros.attributes
libc.hdr.types.FILE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the dependency on libc.src.__support.File.file, as well as the flag controlling if it's using the system file. You'll also need to set up proper handling for the GPU like what vfscanf_internal has.

Suggested change
libc.hdr.types.FILE
libc.src.__support.File.file
libc.hdr.types.FILE
${use_system_file}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to be getting an error like (after adding the above change):

  The dependency target "libc.src.__support.File.file" of target
  "libc.src.stdio.scanf_core.reader" does not exist.

is that expected on a M1 mac?

using the build commands from:#115394 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, that's because we only support overlay mode on mac, and overlay mode doesn't have FILE. You'll need to add logic to only add libc.src.__support.File.file to the list of DEPENDS if LLVM_LIBC_FULL_BUILD is true.

Copy link
Contributor Author

@vinay-deshmukh vinay-deshmukh Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like what vfscanf_internal has.

So, I think there's a bug / typo in the CMake call there.

Instead of:

  add_header_library(
    vfscanf_internal
    HDRS
      vfscanf_internal.h
    DEPENDS
      .reader
      .scanf_main
      libc.include.stdio
      libc.src.__support.File.file
      libc.src.__support.arg_list
    ${use_system_file}
  )

it should be:

  add_header_library(
    vfscanf_internal
    HDRS
      vfscanf_internal.h
    DEPENDS
      .reader
      .scanf_main
      libc.include.stdio
      libc.src.__support.File.file
      libc.src.__support.arg_list
    COMPILE_OPTIONS
      ${use_system_file}
  )

I think is what caused my build to have the bugs with macOS' system stdio.h macros

Link to code:

libc.src.__support.arg_list
${use_system_file}
)

I can add the COMPILE_OPTIONS in this PR if that's oK?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The COMPILE_OPTIONS shouldn't be necessary, because use_system_file already contains it, see: https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/CMakeLists.txt#L30

Comment on lines 65 to 74
char Reader::getc() {
++cur_chars_read;
if (rb != nullptr) {
char output = rb->buffer[rb->buff_cur];
++(rb->buff_cur);
return output;
}
// This should reset the buffer if applicable.
return static_cast<char>(internal::getc(input_stream));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this being in the header was intentional. That way the fast path (reading directly from the buffer) gets properly inlined into the readers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, will move it back to header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok to move the ungetc implementation to the header as well?

The reason is, now that the internal::getc is compiled based on the platform depending on which macro is defined, if we keep getc in .h but ungetc in .cpp that macro if/else chain will need to exist in two places over just one if it gets moved to .h

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving ungetc into the header should be fine.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to be OOO for the next week, so you will either need to wait or ask someone else to finish this review. I think it should be good to land once you do the CMake fix and address the comments, just make sure to test it before landing.

Comment on lines 65 to 74
char Reader::getc() {
++cur_chars_read;
if (rb != nullptr) {
char output = rb->buffer[rb->buff_cur];
++(rb->buff_cur);
return output;
}
// This should reset the buffer if applicable.
return static_cast<char>(internal::getc(input_stream));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving ungetc into the header should be fine.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one nit. Thanks for doing the work on this. Do you need me to merge it for you or do you have commit access?

@vinay-deshmukh
Copy link
Contributor Author

LGTM with one nit. Thanks for doing the work on this. Do you need me to merge it for you or do you have commit access?

No problem. Fixed the nit as well. Will need you to merge it. Thanks!

@michaelrj-google michaelrj-google merged commit 00f5aaf into llvm:main Feb 20, 2025
11 of 13 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Feb 20, 2025

LLVM Buildbot has detected a new failure on builder openmp-offload-libc-amdgpu-runtime running on omp-vega20-1 while building libc at step 5 "compile-openmp".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/13359

Here is the relevant piece of the build log for the reference
Step 5 (compile-openmp) failure: build (failure)
...
0.685 [395/34/271] Building CXX object libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_first_leading_one_ul.dir/stdc_first_leading_one_ul.cpp.o
0.685 [394/34/272] Building CXX object libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_first_leading_one_ull.dir/stdc_first_leading_one_ull.cpp.o
0.686 [393/34/273] Building CXX object libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_first_trailing_zero_uc.dir/stdc_first_trailing_zero_uc.cpp.o
0.686 [392/34/274] Building CXX object libc/src/stdio/gpu/CMakeFiles/libc.src.stdio.gpu.stdin.dir/stdin.cpp.o
0.686 [391/34/275] Building CXX object libc/src/stdio/gpu/CMakeFiles/libc.src.stdio.gpu.stdout.dir/stdout.cpp.o
0.687 [390/34/276] Building CXX object libc/src/stdio/gpu/CMakeFiles/libc.src.stdio.gpu.stderr.dir/stderr.cpp.o
0.688 [389/34/277] Building CXX object libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_first_trailing_zero_us.dir/stdc_first_trailing_zero_us.cpp.o
0.689 [388/34/278] Building CXX object libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_first_trailing_zero_ul.dir/stdc_first_trailing_zero_ul.cpp.o
0.689 [387/34/279] Building CXX object libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_first_trailing_zero_ui.dir/stdc_first_trailing_zero_ui.cpp.o
0.690 [386/34/280] Building CXX object libc/src/stdio/CMakeFiles/libc.src.stdio.scanf.dir/scanf.cpp.o
FAILED: libc/src/stdio/CMakeFiles/libc.src.stdio.scanf.dir/scanf.cpp.o 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang++ --target=amdgcn-amd-amdhsa -DLIBC_NAMESPACE=__llvm_libc_21_0_0_git -D__LIBC_USE_FLOAT16_CONVERSION -I/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc -isystem /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/include/amdgcn-amd-amdhsa -O3 -DNDEBUG --target=amdgcn-amd-amdhsa -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_ADD_NULL_CHECKS "-DLIBC_MATH=(LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)" -fpie -DLIBC_FULL_BUILD -nostdlibinc -ffixed-point -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wdeprecated -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -nogpulib -fvisibility=hidden -fconvergent-functions -flto -Wno-multi-gpu -Xclang -mcode-object-version=none -DLIBC_COPT_PUBLIC_PACKAGING -UNDEBUG -DLIBC_COPT_SCANF_DISABLE_FLOAT -DLIBC_COPT_SCANF_DISABLE_INDEX_MODE -MD -MT libc/src/stdio/CMakeFiles/libc.src.stdio.scanf.dir/scanf.cpp.o -MF libc/src/stdio/CMakeFiles/libc.src.stdio.scanf.dir/scanf.cpp.o.d -o libc/src/stdio/CMakeFiles/libc.src.stdio.scanf.dir/scanf.cpp.o -c /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf.cpp
In file included from /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf.cpp:14:
In file included from /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf_core/vfscanf_internal.h:16:
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf_core/reader.h:38:10: error: no member named 'getc' in namespace '__llvm_libc_21_0_0_git'; did you mean simply 'getc'?
   38 |   return LIBC_NAMESPACE::getc(reinterpret_cast<::FILE *>(f));
      |          ^~~~~~~~~~~~~~~~~~~~
      |          getc
<command line>:1:24: note: expanded from macro 'LIBC_NAMESPACE'
    1 | #define LIBC_NAMESPACE __llvm_libc_21_0_0_git
      |                        ^
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf_core/reader.h:37:17: note: 'getc' declared here
   37 | LIBC_INLINE int getc(void *f) {
      |                 ^
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf_core/reader.h:42:3: error: no member named 'ungetc' in namespace '__llvm_libc_21_0_0_git'; did you mean simply 'ungetc'?
   42 |   LIBC_NAMESPACE::ungetc(c, reinterpret_cast<::FILE *>(f));
      |   ^~~~~~~~~~~~~~~~~~~~~~
      |   ungetc
<command line>:1:24: note: expanded from macro 'LIBC_NAMESPACE'
    1 | #define LIBC_NAMESPACE __llvm_libc_21_0_0_git
      |                        ^
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf_core/reader.h:41:18: note: 'ungetc' declared here
   41 | LIBC_INLINE void ungetc(int c, void *f) {
      |                  ^
2 errors generated.
0.692 [386/33/281] Generating /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/include/amdgcn-amd-amdhsa/llvm-libc-decls/math.h
0.693 [386/32/282] Building CXX object libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_first_leading_one_uc.dir/stdc_first_leading_one_uc.cpp.o
0.696 [386/31/283] Building CXX object libc/src/stdio/CMakeFiles/libc.src.stdio.fscanf.dir/fscanf.cpp.o
FAILED: libc/src/stdio/CMakeFiles/libc.src.stdio.fscanf.dir/fscanf.cpp.o 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang++ --target=amdgcn-amd-amdhsa -DLIBC_NAMESPACE=__llvm_libc_21_0_0_git -D__LIBC_USE_FLOAT16_CONVERSION -I/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc -isystem /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/include/amdgcn-amd-amdhsa -O3 -DNDEBUG --target=amdgcn-amd-amdhsa -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_ADD_NULL_CHECKS "-DLIBC_MATH=(LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)" -fpie -DLIBC_FULL_BUILD -nostdlibinc -ffixed-point -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wdeprecated -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wstrict-prototypes -Wthread-safety -Wglobal-constructors -nogpulib -fvisibility=hidden -fconvergent-functions -flto -Wno-multi-gpu -Xclang -mcode-object-version=none -DLIBC_COPT_PUBLIC_PACKAGING -UNDEBUG -DLIBC_COPT_SCANF_DISABLE_FLOAT -DLIBC_COPT_SCANF_DISABLE_INDEX_MODE -MD -MT libc/src/stdio/CMakeFiles/libc.src.stdio.fscanf.dir/fscanf.cpp.o -MF libc/src/stdio/CMakeFiles/libc.src.stdio.fscanf.dir/fscanf.cpp.o.d -o libc/src/stdio/CMakeFiles/libc.src.stdio.fscanf.dir/fscanf.cpp.o -c /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/fscanf.cpp
In file included from /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/fscanf.cpp:14:
In file included from /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf_core/vfscanf_internal.h:16:
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/libc/src/stdio/scanf_core/reader.h:38:10: error: no member named 'getc' in namespace '__llvm_libc_21_0_0_git'; did you mean simply 'getc'?
   38 |   return LIBC_NAMESPACE::getc(reinterpret_cast<::FILE *>(f));
      |          ^~~~~~~~~~~~~~~~~~~~
      |          getc
<command line>:1:24: note: expanded from macro 'LIBC_NAMESPACE'
    1 | #define LIBC_NAMESPACE __llvm_libc_21_0_0_git
      |                        ^

@michaelrj-google
Copy link
Contributor

Looks like the issue is that the GPU-specific includes didn't get moved over. I'll make a PR to fix it.

michaelrj-google added a commit to michaelrj-google/llvm-project that referenced this pull request Feb 20, 2025
In llvm#121215 the reader was reorganized and the definitions of the
internal getc and ungetc functions were moved, but the includes that the
GPU builder depends on were not. This patch moves the includes to the
correct new place.
@jhuber6
Copy link
Contributor

jhuber6 commented Feb 20, 2025

Oh yeah I remember needing to do something stupid for the GPU scanf. I think it calls the C entrypoints which is kind of a no-no but I couldn't think of any other way to do it at the time.

michaelrj-google added a commit that referenced this pull request Feb 20, 2025
In #121215 the reader was reorganized and the definitions of the
internal getc and ungetc functions were moved, but the includes that the
GPU builder depends on were not. This patch moves the includes to the
correct new place.
@michaelrj-google
Copy link
Contributor

Yeah, I figure we can clean that up in a followup.

@jhuber6
Copy link
Contributor

jhuber6 commented Feb 20, 2025

I think it was due to the lack of an ungetc_unlocked in the system libc.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Feb 20, 2025

LLVM Buildbot has detected a new failure on builder fuchsia-x86_64-linux running on fuchsia-debian-64-us-central1-a-1 while building libc at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/12915

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/fuchsia-linux.py ...' (failure)
...
Call Stack (most recent call first):
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCFlagRules.cmake:158 (create_object_library)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCFlagRules.cmake:158 (cmake_language)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCFlagRules.cmake:251 (expand_flags_for_target)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCObjectRules.cmake:107 (add_target_with_flags)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/src/stdio/scanf_core/CMakeLists.txt:78 (add_object_library)


-- Generating done (3.4s)
CMake Generate step failed.  Build files cannot be regenerated correctly.
FAILED: runtimes/runtimes-armv8m.main-none-eabi-stamps/runtimes-armv8m.main-none-eabi-configure /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-stamps/runtimes-armv8m.main-none-eabi-configure 
cd /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-bins && /usr/bin/cmake --no-warn-unused-cli -DCMAKE_C_COMPILER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/clang -DCMAKE_CXX_COMPILER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/clang++ -DCMAKE_ASM_COMPILER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/clang -DCMAKE_LINKER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/ld.lld -DCMAKE_AR=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-ar -DCMAKE_RANLIB=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-ranlib -DCMAKE_NM=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-nm -DCMAKE_OBJDUMP=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-objdump -DCMAKE_OBJCOPY=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-objcopy -DCMAKE_STRIP=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-strip -DCMAKE_READELF=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-readelf -DCMAKE_C_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_CXX_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_Fortran_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_ASM_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_INSTALL_PREFIX=/usr/local -DLLVM_BINARY_DIR=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d -DLLVM_CONFIG_PATH=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/bin/llvm-config -DLLVM_ENABLE_WERROR=OFF -DLLVM_HOST_TRIPLE=x86_64-unknown-linux-gnu -DLLVM_HAVE_LINK_VERSION_SCRIPT=1 -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=OFF -DLLVM_USE_RELATIVE_PATHS_IN_FILES=ON -DLLVM_LIT_ARGS=-sv -DLLVM_SOURCE_PREFIX= -DPACKAGE_VERSION=21.0.0git -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=/usr/bin/ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DCOMPILER_RT_BUILD_BUILTINS=OFF -DLLVM_INCLUDE_TESTS=ON -DLLVM_ENABLE_PROJECTS_USED=ON -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON -DCMAKE_C_COMPILER_WORKS=ON -DCMAKE_CXX_COMPILER_WORKS=ON -DCMAKE_Fortran_COMPILER_WORKS=ON -DCMAKE_ASM_COMPILER_WORKS=ON -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON -DLLVM_RUNTIMES_TARGET=armv8m.main-none-eabi -DHAVE_LLVM_LIT=ON -DCLANG_RESOURCE_DIR= -DLLVM_DEFAULT_TARGET_TRIPLE=armv8m.main-none-eabi "-DLLVM_ENABLE_RUNTIMES=compiler-rt;libcxx;libcxxabi;libunwind" -DLLVM_USE_LINKER=lld "-DCMAKE_ASM_FLAGS=--target=armv8m.main-none-eabi -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1 -mthumb -mfloat-abi=softfp -march=armv8m.main+fp+dsp -mcpu=cortex-m33" -DCMAKE_BUILD_TYPE=MinSizeRel "-DCMAKE_CXX_FLAGS=--target=armv8m.main-none-eabi -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1 -mthumb -mfloat-abi=softfp -march=armv8m.main+fp+dsp -mcpu=cortex-m33" "-DCMAKE_C_FLAGS=--target=armv8m.main-none-eabi -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1 -mthumb -mfloat-abi=softfp -march=armv8m.main+fp+dsp -mcpu=cortex-m33" -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld -DCMAKE_MODULE_LINKER_FLAGS=-fuse-ld=lld -DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld -DCMAKE_SYSROOT= -DCMAKE_SYSTEM_NAME=Generic -DCMAKE_SYSTEM_PROCESSOR=arm -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY -DLIBCXX_ABI_VERSION=2 -DLIBCXX_CXX_ABI=none -DLIBCXX_ENABLE_EXCEPTIONS=OFF -DLIBCXX_ENABLE_FILESYSTEM=OFF -DLIBCXX_ENABLE_LOCALIZATION=OFF -DLIBCXX_ENABLE_MONOTONIC_CLOCK=OFF -DLIBCXX_ENABLE_RANDOM_DEVICE=OFF -DLIBCXX_ENABLE_RTTI=OFF -DLIBCXX_ENABLE_SHARED=OFF -DLI
SERTIONS=OFF -DLLVM_ENABLE_FATLTO=OFF -DLLVM_ENABLE_LTO=OFF "-DLLVM_ENABLE_RUNTIMES=libc;libcxx" -DLLVM_INCLUDE_TESTS=OFF -DLLVM_LIBC_FULL_BUILD=ON -GNinja -C/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/projects/runtimes-armv8m.main-none-eabi/tmp/runtimes-armv8m.main-none-eabi-cache-Release.cmake -S /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/runtimes/../../runtimes -B /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-bins && /usr/bin/cmake -E touch /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-stamps/runtimes-armv8m.main-none-eabi-configure
ninja: build stopped: subcommand failed.
['ninja', '-C', '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d', 'toolchain-distribution'] exited with return code 1.
@@@STEP_FAILURE@@@
@@@BUILD_STEP check@@@
Running: ninja -C /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d check-llvm check-clang check-lld
ninja: Entering directory `/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d'
[1/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/HardwareUnits/HardwareUnit.cpp.o
[2/1372] Linking CXX static library lib/libLLVMTableGenBasic.a
[3/1372] Linking CXX static library lib/libLLVMTableGenCommon.a
[4/1372] Creating export file for LTO
[5/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/HardwareUnits/RetireControlUnit.cpp.o
[6/1372] Building CXX object lib/Frontend/OpenACC/CMakeFiles/LLVMFrontendOpenACC.dir/ACC.cpp.o
[7/1372] Building CXX object lib/LineEditor/CMakeFiles/LLVMLineEditor.dir/LineEditor.cpp.o
[8/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/CodeEmitter.cpp.o
[9/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/Support.cpp.o
[10/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/Instruction.cpp.o
[11/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/HWEventListener.cpp.o
[12/1372] Building CXX object tools/lto/CMakeFiles/LTO.dir/LTODisassembler.cpp.o
[13/1372] Linking CXX static library lib/libLLVMFrontendOpenACC.a
[14/1372] Linking CXX static library lib/libLLVMLineEditor.a
[15/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/HardwareUnits/LSUnit.cpp.o
[16/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/Stages/MicroOpQueueStage.cpp.o
[17/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/Stages/Stage.cpp.o
[18/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/IncrementalSourceMgr.cpp.o
[19/1372] Building C object tools/llvm-c-test/CMakeFiles/llvm-c-test.dir/attributes.c.o
[20/1372] Building C object tools/llvm-c-test/CMakeFiles/llvm-c-test.dir/diagnostic.c.o
[21/1372] Building C object tools/llvm-c-test/CMakeFiles/llvm-c-test.dir/calc.c.o
[22/1372] Building CXX object lib/Telemetry/CMakeFiles/LLVMTelemetry.dir/Telemetry.cpp.o
[23/1372] Building C object tools/llvm-c-test/CMakeFiles/llvm-c-test.dir/debuginfo.c.o
[24/1372] Building C object tools/llvm-c-test/CMakeFiles/llvm-c-test.dir/helpers.c.o
[25/1372] Building CXX object lib/CodeGen/MIRParser/CMakeFiles/LLVMMIRParser.dir/MILexer.cpp.o
[26/1372] Linking CXX static library lib/libLLVMTelemetry.a
[27/1372] Building C object tools/llvm-c-test/CMakeFiles/llvm-c-test.dir/main.c.o
[28/1372] Building C object tools/llvm-c-test/CMakeFiles/llvm-c-test.dir/disassemble.c.o
[29/1372] Building CXX object lib/FuzzMutate/CMakeFiles/LLVMFuzzerCLI.dir/FuzzerCLI.cpp.o
[30/1372] Building CXX object lib/MCA/CMakeFiles/LLVMMCA.dir/Pipeline.cpp.o
Step 6 (build) failure: build (failure)
...
  The dependency target "libc.src.stdio.scanf_core.reader" of target
  "libc.src.stdio.scanf_core.converter" does not exist.
Call Stack (most recent call first):
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCFlagRules.cmake:158 (create_object_library)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCFlagRules.cmake:158 (cmake_language)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCFlagRules.cmake:251 (expand_flags_for_target)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/cmake/modules/LLVMLibCObjectRules.cmake:107 (add_target_with_flags)
  /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/libc/src/stdio/scanf_core/CMakeLists.txt:78 (add_object_library)
-- Generating done (3.4s)
CMake Generate step failed.  Build files cannot be regenerated correctly.
FAILED: runtimes/runtimes-armv8m.main-none-eabi-stamps/runtimes-armv8m.main-none-eabi-configure /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-stamps/runtimes-armv8m.main-none-eabi-configure 
cd /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-bins && /usr/bin/cmake --no-warn-unused-cli -DCMAKE_C_COMPILER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/clang -DCMAKE_CXX_COMPILER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/clang++ -DCMAKE_ASM_COMPILER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/clang -DCMAKE_LINKER=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/ld.lld -DCMAKE_AR=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-ar -DCMAKE_RANLIB=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-ranlib -DCMAKE_NM=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-nm -DCMAKE_OBJDUMP=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-objdump -DCMAKE_OBJCOPY=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-objcopy -DCMAKE_STRIP=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-strip -DCMAKE_READELF=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/./bin/llvm-readelf -DCMAKE_C_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_CXX_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_Fortran_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_ASM_COMPILER_TARGET=armv8m.main-none-eabi -DCMAKE_INSTALL_PREFIX=/usr/local -DLLVM_BINARY_DIR=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d -DLLVM_CONFIG_PATH=/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/bin/llvm-config -DLLVM_ENABLE_WERROR=OFF -DLLVM_HOST_TRIPLE=x86_64-unknown-linux-gnu -DLLVM_HAVE_LINK_VERSION_SCRIPT=1 -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=OFF -DLLVM_USE_RELATIVE_PATHS_IN_FILES=ON -DLLVM_LIT_ARGS=-sv -DLLVM_SOURCE_PREFIX= -DPACKAGE_VERSION=21.0.0git -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=/usr/bin/ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DCOMPILER_RT_BUILD_BUILTINS=OFF -DLLVM_INCLUDE_TESTS=ON -DLLVM_ENABLE_PROJECTS_USED=ON -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON -DCMAKE_C_COMPILER_WORKS=ON -DCMAKE_CXX_COMPILER_WORKS=ON -DCMAKE_Fortran_COMPILER_WORKS=ON -DCMAKE_ASM_COMPILER_WORKS=ON -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON -DLLVM_RUNTIMES_TARGET=armv8m.main-none-eabi -DHAVE_LLVM_LIT=ON -DCLANG_RESOURCE_DIR= -DLLVM_DEFAULT_TARGET_TRIPLE=armv8m.main-none-eabi "-DLLVM_ENABLE_RUNTIMES=compiler-rt;libcxx;libcxxabi;libunwind" -DLLVM_USE_LINKER=lld "-DCMAKE_ASM_FLAGS=--target=armv8m.main-none-eabi -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1 -mthumb -mfloat-abi=softfp -march=armv8m.main+fp+dsp -mcpu=cortex-m33" -DCMAKE_BUILD_TYPE=MinSizeRel "-DCMAKE_CXX_FLAGS=--target=armv8m.main-none-eabi -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1 -mthumb -mfloat-abi=softfp -march=armv8m.main+fp+dsp -mcpu=cortex-m33" "-DCMAKE_C_FLAGS=--target=armv8m.main-none-eabi -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" -D_LIBCPP_PRINT=1 -mthumb -mfloat-abi=softfp -march=armv8m.main+fp+dsp -mcpu=cortex-m33" -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld -DCMAKE_MODULE_LINKER_FLAGS=-fuse-ld=lld -DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld -DCMAKE_SYSROOT= -DCMAKE_SYSTEM_NAME=Generic -DCMAKE_SYSTEM_PROCESSOR=arm -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY -DLIBCXX_ABI_VERSION=2 -DLIBCXX_CXX_ABI=none -DLIBCXX_ENABLE_EXCEPTIONS=OFF -DLIBCXX_ENABLE_FILESYSTEM=OFF -DLIBCXX_ENABLE_LOCALIZATION=OFF -DLIBCXX_ENABLE_MONOTONIC_CLOCK=OFF -DLIBCXX_ENABLE_RANDOM_DEVICE=OFF -DLIBCXX_ENABLE_RTTI=OFF -DLIBCXX_ENABLE_SHARED=OFF -DLI
SERTIONS=OFF -DLLVM_ENABLE_FATLTO=OFF -DLLVM_ENABLE_LTO=OFF "-DLLVM_ENABLE_RUNTIMES=libc;libcxx" -DLLVM_INCLUDE_TESTS=OFF -DLLVM_LIBC_FULL_BUILD=ON -GNinja -C/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/projects/runtimes-armv8m.main-none-eabi/tmp/runtimes-armv8m.main-none-eabi-cache-Release.cmake -S /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/runtimes/../../runtimes -B /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-bins && /usr/bin/cmake -E touch /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d/runtimes/runtimes-armv8m.main-none-eabi-stamps/runtimes-armv8m.main-none-eabi-configure
ninja: build stopped: subcommand failed.
['ninja', '-C', '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-k7_u_c1d', 'toolchain-distribution'] exited with return code 1.

@michaelrj-google
Copy link
Contributor

Ah, due to the cmake conditions, scanf isn't working for targets without FILE. Give me a sec and I'll fix this.

@Caslyn
Copy link
Contributor

Caslyn commented Feb 20, 2025

Ah, due to the cmake conditions, scanf isn't working for targets without FILE. Give me a sec and I'll fix this.

Just started looking into this. Thanks @michaelrj-google! Let me know if I can assist or review.

@michaelrj-google
Copy link
Contributor

Much appreciated, I'll send you a review once I have something ready.

Right now it looks like there's two main issues: the reader target in scanf_core isn't being defined on targets without FILE, and once I fix that I run into linker errors with the reader_test and some other scanf internals, since link-time dependencies aren't properly propagated through header libraries (defined with add_header_library in the cmake).

michaelrj-google added a commit to michaelrj-google/llvm-project that referenced this pull request Feb 20, 2025
Another followup fix to llvm#121215

The new cmake wouldn't define the readerat all if the target wasn't GPU
or didn't have a definition of FILE. This patch rewrites the cmake to be
more general.

As a followup, I'd like to make `use_system_file` consistent between
/test and /src. Currently in /src it includes the `COMPILE_OPTIONS` and
in /test it does not.
michaelrj-google added a commit that referenced this pull request Feb 20, 2025
Another followup fix to #121215

The new cmake wouldn't define the readerat all if the target wasn't GPU
or didn't have a definition of FILE. This patch rewrites the cmake to be
more general.

As a followup, I'd like to make `use_system_file` consistent between
/test and /src. Currently in /src it includes the `COMPILE_OPTIONS` and
in /test it does not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc] Clean up unnecessary function pointers in scanf
6 participants