Skip to content

Conversation

@krishna2803
Copy link
Contributor

This PR implements the following macros for sched.h:

  • CPU_ZERO
  • CPU_ISSET
  • CPU_SET

Fixes #124642

@krishna2803
Copy link
Contributor Author

@jhuber6 @lntue could you please review this

#define LLVM_LIBC_TYPES_CPU_SET_T_H

#define __CPU_SETSIZE 1024
#define __NCPUBITS 8 * sizeof(unsigned long)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need parens?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in c94712f1

Signed-off-by: krishna2803 <[email protected]>
@krishna2803 krishna2803 requested a review from jhuber6 March 17, 2025 10:00
const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;

const unsigned long mask = 1UL << bit_position;
return (set->__mask[element_index] & mask) != 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the standard say when these pointers are null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

these macros are glibc extensions, the man page itself doesn't define a way to deal with null pointers. i found an implementation of those in glibc's code and surprisingly even that doesn't handle null pointers. ref:

#define __CPU_ISSET_S(cpu, setsize, cpusetp) \
  (__extension__							      \
   ({ size_t __cpu = (cpu);						      \
      __cpu / 8 < (setsize)						      \
      ? ((((const __cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]	      \
	  & __CPUMASK (__cpu))) != 0					      \
      : 0; }))

for implementing these macros (CPU_ZERO et al), i looked at our existing implementation for CPU_COUNT ref:

LLVM_LIBC_FUNCTION(int, __sched_getcpucount,
(size_t cpuset_size, const cpu_set_t *mask)) {
int result = 0;
for (size_t i = 0; i < cpuset_size / sizeof(long); ++i) {
result += __builtin_popcountl(mask->__mask[i]);
}
return result;
}

and even that doesn't handle null pointers!


should i implement a nullptr guard for these?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, can you add null check with https://github.com/llvm/llvm-project/blob/main/libc/src/__support/macros/null_check.h for these functions in this PR? Thanks,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! 522c6697

@krishna2803 krishna2803 requested a review from lntue March 19, 2025 10:50

#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Update the cmake dependency of these targets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! 9b0d165e

@lntue lntue merged commit bda87e0 into llvm:main Mar 19, 2025
12 of 15 checks passed
@krishna2803 krishna2803 deleted the implement-sched-macros branch March 21, 2025 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc][sched] missing macros CPU_ZERO, CPU_ISSET, and CPU_SET

4 participants