[llvm-dev] Get constants of undefined types in IR
Tim Northover via llvm-dev
llvm-dev at lists.llvm.org
Sun Sep 1 23:41:47 PDT 2019
Hi Iulia,
On Sat, 31 Aug 2019 at 14:55, Iulia Stirb via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> I would like to obtain a constant that is initialized with a value of type cpu_set_t, a type which is defined by Pthreads. The problem is that a variable of this type cannot be cast to an int value, even in C source code. I tried get methods from Constant or ConstantInt classes, but with no result.
cpu_set_t appears to be a Linux-specific concept, which actually makes
this slightly easier because it would otherwise be highly platform
specific. This version looks reasonable:
https://android.googlesource.com/platform/bionic/+/f664034/libc/include/sched.h.
It's not 100% canonical since it's outside the kernel, but has the
advantage that all he details are in a single file.
That file tells you a couple of things:
1. The underlying type is a struct containing an array of "unsigned
long" sufficient for 32 or 1024 CPUs depending on the host.
2. The __CPU_ELT and __CPU_MASK defines tell you how that array
corresponds to each particular numbered CPU (i.e. which element and
which bit of that element you'd toggle to set CPU N).
>From that it's fairly easy to check that Clang turns the type into
something like "{ [16 x i64] }" and so that's what you'll have to
construct piece by piece.
1. Work out the contents of each integer element according to those
rules and get them with a ConstantInt::get.
2. Put them together into an array with ConstantArray::get
3. Put that array into a struct with ConstantStruct::get.
Cheers.
Tim.
More information about the llvm-dev
mailing list