[LLVMdev] how to use "new instruction()"
Nick Lewycky
nicholas at mxc.ca
Fri Apr 17 12:28:36 PDT 2015
zhi chen wrote:
> Thanks Nick, that's something what I am trying to implement as the
> following. But it seems I still only get the constant value not the
> instruction. Could you please go over the following instruction and see
> what wrong with it? Thanks for your time again.
>
> Value *vecVal = NULL;
> IRBuilder<> builder(&*pInst);
> Type *vecTy = VectorType::get(Type::getDoubleTy(ctxt), 2);
> Value *emptyVec = UndefValue::get(vecTy);
> Type* u32Ty = Type::getInt32Ty(currF->getContext());
> Value *index0 = ConstantInt::get(u32Ty, 0);
> Value *index1 = ConstantInt::get(u32Ty, 1);
>
> Instruction *InsertVal = InsertElementInst::Create(emptyVec, oprnd,
> index0, "insert");
This makes you:
%insert = insertelement <2 x double> undef, double %oprnd, i32 0
So far so good.
> InsertVal = InsertElementInst::Create(emptyVec, oprnd, index1,
> "insert");
This makes you:
%insert1 = insertelement <2 x double> undef, double %oprnd, i32 1
Not what you wanted. You meant to create:
%insert1 = insertelement <2 x double> %insert, double %oprnd, i32 1
by calling
InsertVal = InsertElementInst::Create(InsertVal, oprnd, index1, "insert");
> vecVal = builder.CreateFAdd(emptyVec, emptyVec, "");
This makes you:
%0 = fadd <2 x double> undef, undef
which constant folds away into a non-instruction. You wanted to sum
vecVal = builder.CreateFAdd(InsertVal, [...], "");
where the [...] is because you haven't yet written the code to create
the second vector (%5) yet.
Nick
>
> Best,
> Zhi
>
> On Fri, Apr 17, 2015 at 12:17 PM, Nick Lewycky <nicholas at mxc.ca
> <mailto:nicholas at mxc.ca>> wrote:
>
> zhi chen wrote:
>
> I got it. Thanks, Nick. So, it is back to the previous problem. If I
> have the following instruction:
>
> %3 = fadd double %1, double %2
>
> I want to change it into
> %6 = fadd <2 x double> %4, double %5
>
> where %4 = <double %1, double %1>, %5 = <double %2, double %2>,
> how can
> I do this?
>
>
> %4 = <double %1, double %1> isn't valid syntax, the way you would do
> it is:
>
> %tmp4 = insertelement <2 x double> undef, double %1, i32 0
> %4 = insertelement <2 x double> %A, double %1, i32 1
>
> and similarly for %5, then you create the fadd of the two of them.
>
> Nick
>
>
> Thanks,
> Best
>
>
> On Fri, Apr 17, 2015 at 1:56 AM, Nick Lewycky <nicholas at mxc.ca
> <mailto:nicholas at mxc.ca>
> <mailto:nicholas at mxc.ca <mailto:nicholas at mxc.ca>>> wrote:
>
> zhi chen wrote:
>
> It seems that the problem was because I used
> builder.CreateFAdd to
> create a <2 x double> vectortype FADD instruction. It
> works if I
> use it
> to create the scalar version FADD. I want to have an
> instruction
> like:
> *%2 = fadd <2 x double> undef, <2 x double> undef. *The
> following is the
> way I used to create the vectorized FADD instruction:
>
> //pInst is a double type instruction
>
> Type *vecTy = VectorType::get(pInst->getType(), 2);
> Value *emptyVec = UndefValue::get(vecTy);
> IRBuilder<> builder(&*pInst);
> Value *dupVal = builder.CreateFAdd(emptyVec, emptyVec,
> instName);
> std::cout << " dupVal " << *dupVal << "\n";
>
> It outputs: dupVal <2 x double> <double fadd (double
> undef, double
> undef), double fadd (double undef, double undef)>
>
> If I dyn_cast the dupVal to instruction type (dupInst)
> and print
> dupInst, it outputs: "dupInst printing a <null> value"
> But if I use Instruction *dupInst = (Instruction *)
> dupVal and
> print it,
> I'll get:
> dupInst <2 x double> <double fadd (double undef, double
> undef),
> double
> fadd (double undef, double undef)>
>
> It seems that if simply fails to generate the
> vectorized FADD
> instruction. Anything wrong with my code?
>
>
> IRBuilder gave you back a constant instead of an
> Instruction. This
> is why it returns a Value*. For a simple example, if you
> ask it to
> create "add i32 1, 2" it will not return an add
> instruction, it will
> instead return "i32 3" which is a ConstantInt.
>
> In your case, it returned to you a ConstantExpr whose
> getOpcode()
> shows Instruction::FAdd, and getOperand(0) and
> getOperand(1) show
> 'undef', but it is not an instruction. "Undef" is treated as a
> constant, so an fadd between two constants gets you a constant
> instead of an instruction.
>
> Usually it won't matter, just build the function top down
> and don't
> look at whether you're getting an Instruction* or Constant* and
> everything will be fine.
>
> Nick
>
>
> Best,
> Zhi
>
>
>
>
>
> On Thu, Apr 16, 2015 at 11:55 PM, zhi chen
> <zchenhn at gmail.com <mailto:zchenhn at gmail.com>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>>>> wrote:
>
> Yes. I was using this. It seems the produced
> instruction is not
> correct. There are probably some other problems. I
> need to
> recheck
> it. Thanks for your help, Daniel.
>
> Best,
> Zhi
>
> On Thu, Apr 16, 2015 at 11:40 PM, Daniel Berlin
> <dberlin at dberlin.org <mailto:dberlin at dberlin.org>
> <mailto:dberlin at dberlin.org <mailto:dberlin at dberlin.org>>
> <mailto:dberlin at dberlin.org <mailto:dberlin at dberlin.org>
> <mailto:dberlin at dberlin.org <mailto:dberlin at dberlin.org>>>> wrote:
>
> Value * is the instruction.
>
> use dyn_cast<Instruction> to get to it.
>
>
> On Thu, Apr 16, 2015 at 11:39 PM zhi chen
> <zchenhn at gmail.com <mailto:zchenhn at gmail.com>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>>>> wrote:
>
> But IRBuilder.CreateXYZ only returns a "VALUE"
> type. Can I
> get the instruction created by it? For
> example,
>
> IRBuilder<> builder(&*pinst);
> Value *val = builder.CreateFAdd(LV, RV, "");
>
> How can I get the fadd instruction created
> by builder?
>
> On Thu, Apr 16, 2015 at 8:52 PM, zhi chen
> <zchenhn at gmail.com <mailto:zchenhn at gmail.com>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>
> <mailto:zchenhn at gmail.com <mailto:zchenhn at gmail.com>>>> wrote:
>
> Yes. That's what I was the solution in
> my mind.
> But I
> just wanted to know if there was a
> generic way
> to save
> some code...
>
> On Thu, Apr 16, 2015 at 8:32 PM, Tim
> Northover
> <t.p.northover at gmail.com <mailto:t.p.northover at gmail.com>
> <mailto:t.p.northover at gmail.com <mailto:t.p.northover at gmail.com>>
> <mailto:t.p.northover at gmail.com <mailto:t.p.northover at gmail.com>
> <mailto:t.p.northover at gmail.com
> <mailto:t.p.northover at gmail.com>>>> wrote:
>
> > I understand that I can detect the operation
> first, and use "create" to
> > create for each of them. But I don't if there is a
> generic way to do this
> > because if might be add/sub/mul... operations.
>
> I don't think there is.
> Realistically, just
> blindly
> replacing
> instructions with vector
> equivalents is
> only going
> to work in a few
> cases anyway. You're probably best to
> intentionally
> detect those cases
> and call the correct CreateXYZ
> function.
>
> Cheers.
>
> Tim.
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>
> <mailto:LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>>
> <mailto:LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>
> <mailto:LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>>>
> http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>
> <mailto:LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>>
> http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
>
>
>
More information about the llvm-dev
mailing list