[llvm-dev] Get constants of undefined types in IR

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Tue Sep 3 10:15:12 PDT 2019


Adding llvm-dev back.

On Tue, 3 Sep 2019 at 18:02, Iulia Stirb <iulia_s24 at yahoo.com> wrote:

> Thank very much you for your answer. Following the indications in your
> mail, I obtained one of the 16th elements as below:
>
> static cpu_set_t getCpuAffinityElement(cpu_set_t affinity, int index) {
> cpu_set_t mask;
> CPU_ZERO(&mask);
> for(int i = index * sizeof(unsigned long); i < (index + 1) * sizeof(
> unsigned long); i++) {
> CPU_SET(i,&mask);
> }
> CPU_AND(&affinity, &affinity, &mask);
> return affinity;
> }
>

That looks like a weird function, but it’s just within the realms of
plausible so I’ll assume you know what you’re doing.

>
The problem is that the element is of type cpu_set_t and cannot be
> converted to unsigned long. So, when I try to get the ConstantInt object
> like below, I get an error saying that I cannot pass a cpu_set_t parameter
> as second parameter of get method.
> ConstantInt * elem = ConstantInt::get(Type::getInt64Ty(I.getContext
> ()),affinityElement,false);
>

To use a real cpu_set_t from the host as part of a. ConstantInt like that
you’ll have to look at its actual definition and access something like
affinityElement.__bits[0]. The exact internals may vary with the header
you’re using, but you’re fundamentally trying to do something ABI specific
so you will have to get your hands dirty.


> What I need is to insert in IR a call that has a cpu_set_t parameter, and
> the parameter is already set (so I don't need to construct it).
>

An alternative might be to pass a pointer to the cpu_set_t you created
above into the Module somehow (a global might work). It would sidestep the
tricky annoying API for Constants in LLVM, and possibly even be a buffer
against the structure changing in future.

Cheers.

Tim.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190903/f05ced3c/attachment-0001.html>


More information about the llvm-dev mailing list