[llvm-dev] Adding a new instruction

476 views
Skip to first unread message

Ammar Naqvi via llvm-dev

unread,
Apr 17, 2016, 9:30:24 PM4/17/16
to llvm...@lists.llvm.org
Hello Everyone,

I'm trying to add a new instruction to LLVM; a binary operator named "increment" that adds 1 to its LHS value.

I've followed this http://llvm.org/docs/ExtendingLLVM.html and added my operator in the required places however, LLVM does not recognize the "increment" opcode instruction in the bitcode file; I get the error: expected instruction opcode. at the place of "increment".

Then i try doing a make from build directory to make sure my instruction is added to the necessary files however i get the error:

/home/test/llvm/include/llvm/IR/Instruction.def: In function ‘LLVMOpcode map_to_llvmopcode(int)’:
/home/test/llvm/lib/IR/Core.cpp:971:54: error: ‘LLVMincrement’ was not declared in this scope
 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
                                                      ^
/home/test/llvm/include/llvm/IR/Instruction.def:42:48: note: in expansion of macro ‘HANDLE_INST’
 #define HANDLE_BINARY_INST(num, opcode, Class) HANDLE_INST(num, opcode, Class)
                                                ^
/home/test/llvm/include/llvm/IR/Instruction.def:136:1: note: in expansion of macro ‘HANDLE_BINARY_INST’
 HANDLE_BINARY_INST(23, increment, BinaryOperator) . . .

I'm really stuck at this point and can not see the error through, any help is greatly appreciated!

Thanks! :)

Vladimir Miloserdov via llvm-dev

unread,
Apr 18, 2016, 5:11:31 AM4/18/16
to llvm...@lists.llvm.org
Hello Ammar,

Can you share the patch of changes in LLVM you have done already?

Regards,
Vladimir
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Ammar Naqvi via llvm-dev

unread,
Apr 18, 2016, 8:30:35 PM4/18/16
to Vladimir Miloserdov, llvm...@lists.llvm.org
Hey Vladmir,

Here's what and where i added :

1- llvm/include/llvm/IR/Instruction.def :
   Line 136 : HANDLE_BINARY_INST(23, increment , BinaryOperator)  also changed all numbers accordingly


2-   llvm/include/llvm/IR/Instructions.h:
   Line 699: 
   enum BinOp {
    /// *p = v
    Xchg,
    /// *p = old + v
    Add,
    /// *p = old + 1
    increment,

3-llvm/include/llvm/IR/InstVisitor.h:  NO Change, since binary operator visit is already defined

4 - llvm/lib/AsmParser/LLLexer.cpp:
     Line 717 :  INSTKEYWORD(add,   Add);  INSTKEYWORD(increment,   increment);  INSTKEYWORD(fadd,   FAdd);

5 - llvm/lib/AsmParser/LLParser.cpp
     Line 2865 : case lltok::kw_increment:
     Line 2887 : if (Opc == Instruction::Add || Opc == Instruction::increment || Opc == Instruction::Sub ||
     Line 2918 : case Instruction::myAdd:
     Line 4737 : case lltok::kw_increment:
     Line 5942 : case lltok::kw_increment: Operation = AtomicRMWInst::increment; break;

6 -  llvm/lib/Bitcode/Reader/BitcodeReader.cpp
      Line 748 :case bitc::BINOP_increment: return IsFP ? -1 : Instruction::increment;
      Line 782 : case bitc::RMW_increment: return AtomicRMWInst::increment;
      Line 2720 : Opc == Instruction::increment||
      Line 4113 : Opc == Instruction::increment||

7 - llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  Line 91 : case Instruction::increment:return bitc::BINOP_increment;
  Line 116 : case AtomicRMWInst::increment: return bitc::RMW_increment;
  
8 - llvm/lib/IR/Instruction.cpp:
  Line 211 : case increment: return "increment";
  Line 495 : Opcode == Add || Opcode == increment || Opcode == Mul;
  Line 522 : case increment:
 
 9 -  llvm/lib/IR/Instructions.cpp:
  Line 1939 : case increment:

I've made these changes following the official document here : http://llvm.org/docs/ExtendingLLVM.html

Please let me know if i'm doing something wrong or anything else needs to be changed.

ThankYou!

Ammar Naqvi via llvm-dev

unread,
Apr 19, 2016, 4:19:48 PM4/19/16
to Vladimir Miloserdov, llvm...@lists.llvm.org
So i fixed this error, i had to add LLVMincrement in core.h as a BINOP and a couple more fixes after this fix, however, after a successful make, LLVM still does not recognize my increment instruction and i get the error expected instruction opcode when i run with lli.

Please guide if anyone has added any instruction successfully to LLVM, does not necessarily have to be a binary op,i'll pick up from wherever we are, I can not find anything at all except the official link which doesn't seem very helpful! :( 

On 18 April 2016 at 02:10, Vladimir Miloserdov via llvm-dev <llvm...@lists.llvm.org> wrote:
Reply all
Reply to author
Forward
0 new messages