[llvm-dev] How to insert vector type input parameter for function in C/C++ API?

Michael Choi via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 10 16:54:52 PDT 2017


FYI.

I am referencing attached implementation for my project.

Thanks,
Michael

On Mon, Apr 10, 2017 at 4:34 PM, Michael Choi <choimichael103 at gmail.com>
wrote:

> Hi Craig,
>
> Thank you for your help!
>
> As you know from my first mail , I want to generrate LLVM-IR code (TARGET)
> by C/C++ APIs for below SOURCE source code (AVX2 code).
>
> I am using LLVM 3.8.x
>
> I can't find useful examples from online. So far, I just done following
> 'Done: Part-1' which gives me this,
> = LLVM-IR code from 'Done: Part-1' ==============================
> ===========
> define i32 @add1(i32 %AnArg) {
> EntryBlock:
>   %0 = add i32 1, %AnArg        // Line 1: From C/C++ APIs below
>   ret i32 %0                             // Line 2: From C/C++ APIs below
> }
> ===================================================================
> = Done: Part-1 ========================================================
>   Function *Add1F =
>     cast<Function>(M->getOrInsertFunction("add1",
>                                           VectorType::get(Type::
> getInt32Ty(Context),4),
>                                           VectorType::get(Type::
> getInt32Ty(Context),4),
>                                           nullptr));
>
>   Value *One = builder.getInt32(1);
>
>   // Get pointers to the integer argument of the add1 function...
>   assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an
> arg
>   Argument *ArgX = &*Add1F->arg_begin();          // Get the arg
>   ArgX->setName("AnArg");                         // Give it a nice
> symbolic name for fun.
>
>   // Create the add instruction, inserting it into the end of BB.
>   Value *Add = builder.CreateAdd(One, ArgX);
>
>   // Create the return instruction and add it to the basic block
>   builder.CreateRet(Add);
> =================================================================
>
> QUESTION:
> 1. How to prepare the vector values?
>     : Above 'Value *One = builder.getInt32(1);' for single value
>     : I want to get  vector value which has 4 value. How to do this?
> 2. How to prepare vector values for multiple vector type parameters?
>     : Ex, Parameters __m256i a, __m256i b for sum(__m256i a, __m256i b)
> function
> 3. If you have any example code (C/C++ APIs implementation) for "__m256i
> sum(__m256i a, __m256i b)", please let me know.
>
>
>
> FYI:
>
> ////////////// B E L O W //////////////////////////////
> ////////////////////////////////
> SOURCE:
> #include "immintrin.h"
> __m256i sum(__m256i a, __m256i b) {
>     return a+b;
> }
>
> TARGET:
> michael at michael-Precision-Tower-3420:~/Year_2017/work_DEMO$ cat
> avx2_add2.ll
> ; ModuleID = 'avx2_add2.c'
> target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
> target triple = "x86_64-unknown-windows-cygnus"
>
> ; Function Attrs: nounwind
> define <4 x i64> @sum(<4 x i64> %a, <4 x i64> %b) #0 {
>   %1 = alloca <4 x i64>, align 32
>   %2 = alloca <4 x i64>, align 32
>   store <4 x i64> %a, <4 x i64>* %1, align 32
>   store <4 x i64> %b, <4 x i64>* %2, align 32
>   %3 = load <4 x i64>, <4 x i64>* %1, align 32
>   %4 = load <4 x i64>, <4 x i64>* %2, align 32
>   %5 = add <4 x i64> %3, %4
>   ret <4 x i64> %5
> }
>
> attributes #0 = { nounwind "disable-tail-calls"="false"
> "less-precise-fpmad"="false" "no-frame-pointer-elim"="false"
> "no-infs-fp-math"="false" "no-nans-fp-math"="false"
> "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2"
> "unsafe-fp-math"="false" "use-soft-float"="false" }
>
> !llvm.ident = !{!0}
>
> !0 = !{!"clang version 3.8.1 (tags/RELEASE_381/final)"}
>
> Thanks,
> Michael
>
> On Fri, Apr 7, 2017 at 6:16 PM, Michael Choi <choimichael103 at gmail.com>
> wrote:
>
>> Fantastic! It's working! Thank you so much Craig!!!
>>
>>
>> On Fri, Apr 7, 2017 at 6:05 PM, Craig Topper <craig.topper at gmail.com>
>> wrote:
>>
>>> It should be VectorType::get(Type::getInt32Ty(Context),4).  You need
>>> the word "get" after VectorType::
>>>
>>> ~Craig
>>>
>>> On Fri, Apr 7, 2017 at 6:01 PM, Michael Choi <choimichael103 at gmail.com>
>>> wrote:
>>>
>>>> Thank you so much Craig!
>>>>
>>>> I tried it. But still complaining. Here is the error message during
>>>> compilation.
>>>>
>>>> HowToUseJIT_SIMD_FuncProto.cpp:94:55: error: expected unqualified-id
>>>>
>>>> VectorType::(Type::getInt32Ty(Context),4),
>>>>
>>>>
>>>>
>>>> THIS IS MY CODE:
>>>>   LLVMContext Context;
>>>>
>>>>   std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
>>>>   Module *M = Owner.get();
>>>>
>>>>   Function *Add1F =
>>>>     cast<Function>(M->getOrInsertFunction("add1",
>>>>
>>>> VectorType::(Type::getInt32Ty(Context),4),
>>>>
>>>> VectorType::(Type::getInt32Ty(Context),4),
>>>>
>>>>
>>>> //Type::getInt32Ty(Context),            //This is working fine
>>>>
>>>> //Type::getInt32Ty(Context),            //This is working fine
>>>>
>>>>                                           nullptr));
>>>>
>>>>
>>>> Any idea?
>>>> Please let me know what is wrong in my code.
>>>>
>>>> Thanks,
>>>> Michael
>>>>
>>>> On Fri, Apr 7, 2017 at 5:36 PM, Craig Topper <craig.topper at gmail.com>
>>>> wrote:
>>>>
>>>>> To create a vector type you can call VectorType::get(<scalar element
>>>>> type>, <num of elements>) and pass the output of that to the Type argument
>>>>> when creating instructions. To get the scalar element type you can use the
>>>>> get*Ty methods in IRBuilder or the Type::get*Ty methods.
>>>>>
>>>>> ~Craig
>>>>>
>>>>> On Fri, Apr 7, 2017 at 5:20 PM, Michael Choi via llvm-dev <
>>>>> llvm-dev at lists.llvm.org> wrote:
>>>>>
>>>>>> I am working on AVX2 code generation by LLVM framework.
>>>>>>
>>>>>> I want to generate LLVM-IR code for the following code by C/C++ API
>>>>>> from LLVM framework. I am using LLVM3.8.
>>>>>> Basically, I want to generate TARGET (Refer to below) LLVM-IR code
>>>>>> for SOURCE function by C/C++ API.
>>>>>> As you see below, the AVX2 data type is __m256i which is vector type.
>>>>>> How can I indicate vector type (function return type, input parameters) for
>>>>>> IRBuiler by C/C++ APIs?
>>>>>>
>>>>>> I don't see any example online and please let me know if anybody has
>>>>>> examples.
>>>>>>
>>>>>>
>>>>>>
>>>>>> SOURCE:
>>>>>> #include "immintrin.h"
>>>>>> __m256i sum(__m256i a, __m256i b) {
>>>>>>     return a+b;
>>>>>> }
>>>>>>
>>>>>> TARGET:
>>>>>> michael at michael-Precision-Tower-3420:~/Year_2017/work_DEMO$ cat
>>>>>> avx2_add2.ll
>>>>>> ; ModuleID = 'avx2_add2.c'
>>>>>> target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
>>>>>> target triple = "x86_64-unknown-windows-cygnus"
>>>>>>
>>>>>> ; Function Attrs: nounwind
>>>>>> define <4 x i64> @sum(<4 x i64> %a, <4 x i64> %b) #0 {
>>>>>>   %1 = alloca <4 x i64>, align 32
>>>>>>   %2 = alloca <4 x i64>, align 32
>>>>>>   store <4 x i64> %a, <4 x i64>* %1, align 32
>>>>>>   store <4 x i64> %b, <4 x i64>* %2, align 32
>>>>>>   %3 = load <4 x i64>, <4 x i64>* %1, align 32
>>>>>>   %4 = load <4 x i64>, <4 x i64>* %2, align 32
>>>>>>   %5 = add <4 x i64> %3, %4
>>>>>>   ret <4 x i64> %5
>>>>>> }
>>>>>>
>>>>>> attributes #0 = { nounwind "disable-tail-calls"="false"
>>>>>> "less-precise-fpmad"="false" "no-frame-pointer-elim"="false"
>>>>>> "no-infs-fp-math"="false" "no-nans-fp-math"="false"
>>>>>> "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2"
>>>>>> "unsafe-fp-math"="false" "use-soft-float"="false" }
>>>>>>
>>>>>> !llvm.ident = !{!0}
>>>>>>
>>>>>> !0 = !{!"clang version 3.8.1 (tags/RELEASE_381/final)"}
>>>>>>
>>>>>> Thanks,
>>>>>> Michael
>>>>>>
>>>>>> _______________________________________________
>>>>>> LLVM Developers mailing list
>>>>>> llvm-dev at lists.llvm.org
>>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170410/16fd535f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HowToUseJIT.cpp
Type: text/x-c++src
Size: 4674 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170410/16fd535f/attachment.cpp>


More information about the llvm-dev mailing list