Ignore:
Timestamp:
Aug 29, 2014, 4:47:16 PM (4 years ago)
Author:
linmengl
Message:

generate llvm.uadd.with.overflow. add 'make check' target to run greptest.py.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/llvm_gen.cpp

    r4089 r4096  
    551551    // to call->  b.CreateCall(mFunc_print_register, unicode_category);
    552552
     553    // Type Definitions for llvm.uadd.with.overflow.i128
     554    std::vector<Type*>StructTy_0_fields;
     555    StructTy_0_fields.push_back(IntegerType::get(mMod->getContext(), 128));
     556    StructTy_0_fields.push_back(IntegerType::get(mMod->getContext(), 1));
     557    StructType *StructTy_0 = StructType::get(mMod->getContext(), StructTy_0_fields, /*isPacked=*/false);
     558
     559    std::vector<Type*>FuncTy_1_args;
     560    FuncTy_1_args.push_back(IntegerType::get(mMod->getContext(), 128));
     561    FuncTy_1_args.push_back(IntegerType::get(mMod->getContext(), 128));
     562    FunctionType* FuncTy_1 = FunctionType::get(
     563                                              /*Result=*/StructTy_0,
     564                                              /*Params=*/FuncTy_1_args,
     565                                              /*isVarArg=*/false);
     566
     567    mFunc_llvm_uadd_with_overflow_i128 = mMod->getFunction("llvm.uadd.with.overflow.i128");
     568    if (!mFunc_llvm_uadd_with_overflow_i128) {
     569        mFunc_llvm_uadd_with_overflow_i128 = Function::Create(
     570          /*Type=*/FuncTy_1,
     571          /*Linkage=*/GlobalValue::ExternalLinkage,
     572          /*Name=*/"llvm.uadd.with.overflow.i128", mMod); // (external, no body)
     573        mFunc_llvm_uadd_with_overflow_i128->setCallingConv(CallingConv::C);
     574    }
     575    AttributeSet mFunc_llvm_uadd_with_overflow_i128_PAL;
     576    {
     577        SmallVector<AttributeSet, 4> Attrs;
     578        AttributeSet PAS;
     579        {
     580          AttrBuilder B;
     581          B.addAttribute(Attribute::NoUnwind);
     582          B.addAttribute(Attribute::ReadNone);
     583          PAS = AttributeSet::get(mMod->getContext(), ~0U, B);
     584        }
     585
     586        Attrs.push_back(PAS);
     587        mFunc_llvm_uadd_with_overflow_i128_PAL = AttributeSet::get(mMod->getContext(), Attrs);
     588    }
     589    mFunc_llvm_uadd_with_overflow_i128->setAttributes(mFunc_llvm_uadd_with_overflow_i128_PAL);
     590
     591    //Starts on process_block
    553592    SmallVector<AttributeSet, 4> Attrs;
    554593    AttributeSet PAS;
     
    11091148}
    11101149
     1150SumWithOverflowPack LLVM_Generator::genUaddOverflow(Value* int128_e1, Value* int128_e2) {
     1151    std::vector<Value*> struct_res_params;
     1152    struct_res_params.push_back(int128_e1);
     1153    struct_res_params.push_back(int128_e2);
     1154    CallInst* struct_res = CallInst::Create(mFunc_llvm_uadd_with_overflow_i128, struct_res_params, "uadd_overflow_res", mBasicBlock);
     1155    struct_res->setCallingConv(CallingConv::C);
     1156    struct_res->setTailCall(false);
     1157    AttributeSet struct_res_PAL;
     1158    struct_res->setAttributes(struct_res_PAL);
     1159
     1160    SumWithOverflowPack ret;
     1161
     1162    std::vector<unsigned> int128_sum_indices;
     1163    int128_sum_indices.push_back(0);
     1164    ret.sum = ExtractValueInst::Create(struct_res, int128_sum_indices, "sum", mBasicBlock);
     1165
     1166    std::vector<unsigned> int1_obit_indices;
     1167    int1_obit_indices.push_back(1);
     1168    ret.obit = ExtractValueInst::Create(struct_res, int1_obit_indices, "obit", mBasicBlock);
     1169
     1170    return ret;
     1171}
     1172
     1173
    11111174Value* LLVM_Generator::genAddWithCarry(Value* e1, Value* e2) {
    11121175    IRBuilder<> b(mBasicBlock);
     
    11181181    Value* carryq_value = genCarryInLoad(mptr_carry_q, this_carry_idx);
    11191182
    1120     Value* carrygen = b.CreateAnd(e1, e2, "carrygen");
    1121     Value* carryprop = b.CreateOr(e1, e2, "carryprop");
    1122     Value* digitsum = b.CreateAdd(e1, e2, "digitsum");
    1123     Value* partial = b.CreateAdd(digitsum, carryq_value, "partial");
    1124     Value* digitcarry = b.CreateOr(carrygen, b.CreateAnd(carryprop, genNot(partial)));
    1125     Value* mid_carry_in = genShiftLeft64(b.CreateLShr(digitcarry, 63), "mid_carry_in");
    1126 
    1127     Value* sum = b.CreateAdd(partial, mid_carry_in, "sum");
    1128     Value* carry_out = genShiftRight127(b.CreateOr(carrygen, b.CreateAnd(carryprop, genNot(sum))), "carry_out");
    1129 
    1130     //CarryQ - carry out:
     1183    //new code chunk, use llvm.uadd.with.overflow.i128
     1184    CastInst* int128_e1 = new BitCastInst(e1, IntegerType::get(mMod->getContext(), 128), "e1_128", mBasicBlock);
     1185    CastInst* int128_e2 = new BitCastInst(e2, IntegerType::get(mMod->getContext(), 128), "e2_128", mBasicBlock);
     1186    CastInst* int128_carryq_value = new BitCastInst(carryq_value, IntegerType::get(mMod->getContext(), 128), "carryq_128", mBasicBlock);
     1187
     1188    SumWithOverflowPack sumpack0, sumpack1;
     1189
     1190    sumpack0 = genUaddOverflow(int128_e1, int128_e2);
     1191    sumpack1 = genUaddOverflow(sumpack0.sum, int128_carryq_value);
     1192
     1193    Value* obit = b.CreateOr(sumpack0.obit, sumpack1.obit, "carry_bit");
     1194    Value* ret_sum = b.CreateBitCast(sumpack1.sum, m64x2Vect, "ret_sum");
     1195
     1196    /*obit is the i1 carryout, zero extend and insert it into a v2i64 vector.*/
     1197    ConstantAggregateZero* const_packed_5 = ConstantAggregateZero::get(m64x2Vect);
     1198    ConstantInt* const_int32_6 = ConstantInt::get(mMod->getContext(), APInt(32, StringRef("0"), 10));
     1199    CastInst* int64_o0 = new ZExtInst(obit, IntegerType::get(mMod->getContext(), 64), "o0", mBasicBlock);
     1200    InsertElementInst* carry_out = InsertElementInst::Create(const_packed_5, int64_o0, const_int32_6, "carry_out", mBasicBlock);
     1201
    11311202    Value* void_1 = genCarryOutStore(carry_out, mptr_carry_q, this_carry_idx);
    11321203
    1133     return sum;
     1204    return ret_sum;
     1205    //new code chunk ends
     1206
     1207    //Old code chunk, calculate carry through logical ops
     1208    //Value* carrygen = b.CreateAnd(e1, e2, "carrygen");
     1209    //Value* carryprop = b.CreateOr(e1, e2, "carryprop");
     1210    //Value* digitsum = b.CreateAdd(e1, e2, "digitsum");
     1211    //Value* partial = b.CreateAdd(digitsum, carryq_value, "partial");
     1212    //Value* digitcarry = b.CreateOr(carrygen, b.CreateAnd(carryprop, genNot(partial)));
     1213    //Value* mid_carry_in = genShiftLeft64(b.CreateLShr(digitcarry, 63), "mid_carry_in");
     1214
     1215    //Value* sum = b.CreateAdd(partial, mid_carry_in, "sum");
     1216    //Value* carry_out = genShiftRight127(b.CreateOr(carrygen, b.CreateAnd(carryprop, genNot(sum))), "carry_out");
     1217    //Value* void_1 = genCarryOutStore(carry_out, mptr_carry_q, this_carry_idx);
     1218
     1219    //return sum;
     1220    //Old code chunk ends
    11341221}
    11351222
Note: See TracChangeset for help on using the changeset viewer.