Wednesday, January 30, 2013

Writing fast, processor-independent code using C++ and LLVM bitcode

Using inline assembly along with C/C++ code has long been the best choice for developers writing optimized code. However the approach had its shortcomings:

- the need to write assembly for each architecture
- the use of inline assembly hinders certain compiler optimizations (such as register allocation)

But thanks to LLVM it is now possible to write processor-independent assembly functions using its bitcode, and use its link-time optimizer to get the function expanded in-line into C/C++ code.

I have created an example that shows how to implement fast integer arithmetics (with overflow detection) in C++, which is available at github.com/kazuho/add_with_overflow.

It uses a bitcode-level intrinsic called "llvm.sadd.with.overflow.i32" (that gets inlined) to implement integer addition with overflow check.

With the example, the source code
if (! add_with_overflow(&ret, x, y))
gets compiled into
addl    8(%rsp), %esi
jno     LBB1_4

As can be seen, the generated code is highly optimized. Not only does it use the JNO instruction, the source operand of ADDL is placed on stack (which would be faster than on register since the value is never again being referred to). Such kind of an optimization has been impossible with inline assembly of GCC (that requires the arguments to be loaded on registers).

Since the output (after inline expansion) is a .s file (processor dependent assembly), it is possible to link the optimized code using other linkers as well.

Note: the work is based on Fast integer overflow detection - Xi Wang, and I would like to thank the author for his excellent work.

Note 2: Since the bitcode instructions might change in the future, it might be a good idea to limit the length of the functions written in bitcode as short as possible.

21 comments:

  1. This is great do you have a catologue if so I would love one to share with friends and family.
    ICC T20 World Cup 2016 Venues & Starting

    Date

    Australia vs India Prediction

    ReplyDelete
  2. This is the time doing a reversal and forward between banks while you apply, and could end up being very expensive by the day's end. On the off chance that you need to minimize time seeking and costs, then the most ideal approach to scan for this sort of advance is to look through the Internet. Payday Loans

    ReplyDelete

  3. tgjobsnews is the best job website for govt jobsBank Jobs

    ReplyDelete
  4. Webroot.com/safe is a protection software solution that communicates with the cloud avoiding the hassle to manage the signature updates to deploy. for office setup visit office.com/setup

    ReplyDelete
  5. Sample Assignment is a renowned for assignment help in Australia and has assisted thousands of international students with their academics. Our dedicated team of experts has been providing full-fledged assignments to students pursuing their courses at various colleges and universities, and found to be avidly googling "assignment help Sydney" across the continent. While Australia is among the most preferred destinations for individuals from around the world, Assignment Experts has also won the trust of a vast pool of students here. The Australian Assignment help, such as Sample Assignment, can be easily contacted via WhatsApp and Messenger too. With our 24-hour online academic assistance, any student can reach out to us whenever he or she is in the need of help of a subject expert.

    ReplyDelete
  6. flipkart icici bank offer,
    flipkart icici cash back,flipkart icici coupon code,flipkart icici credit card,flipkart icici discount coupon,flipkart icici netbanking offer,flipkart icici offer,

    icici bank card offer on flipkart,icici bank cashback offer on flipkart,icici bank credit card
    flipkart offer,icici bank credit card offer flipkart,icici bank credit card offers on flipkart,icici bank debit card offers on flipkart,icici bank flipkart offer,icici bank flipkart offer 2016,icici bank flipkart vouchers,icici bank offer for flipkart,icici bank offers flipkart,icici card offers on flipkart,icici cash back offer on flipkart,icici coupon code for flipkart,
    icici credit card cash back offer on flipkart,icici credit card coupon code flipkart,icici credit card discount on flipkart,icici credit card flipkart,icici credit card offers flipkart,icici credit card offers in flipkart,icici credit card offers on flipkart,icici debit card flipkart offer,icici debit card offers in flipkart,icici debit card offers on flipkart,icici discount on flipkart,icici flipkart discount,icici net banking offers on flipkart,icici offer for flipkart



    flipkart icici,
    flipkart icici debit card emi,flipkart icici offer,flipkart icici credit card offer,flipkart offers for icici credit card,flipkart icici debit card offer,flipkart emi on icici debit card,flipkart icici bank offer,flipkart icici offer 2018,flipkart icici emi,flipkart icici bank debit card emi,flipkart coupons for icici debit card,icici flipkart gift voucher,flipkart icici coupon,flipkart icici coupon code,flipkart offer with icici bank,flipkart icici cashback offer,flipkart icici bank,flipkart icici offer 2017,icici bank flipkart gift voucher,icici pockets flipkart

    ReplyDelete


  7. flipkart emi,
    flipkart debit card emi,
    flipkart emi for debit card,
    flipkart emi on debit card,
    flipkart emi with debit card,
    flipkart emi sbi debit card,
    flipkart no emi cost,
    flipkart emi,
    flipkart with emi,
    flipkart email id,
    flipkart emi mobile,
    flipkart emi without credit card,
    flipkart mobile emi on debit card,
    flipkart emi options,
    flipkart emi card,
    flipkart emi using debit card,
    flipkart emi bajaj finserv
    zestmoney flipkart emi,
    flipkart emi laptop,
    flipkart emi process,
    flipkart emi phone,
    flipkart no cost emi laptops,
    flipkart emi in debit card,
    flipkart emi debit card icici,
    flipkart emi on credit card,
    flipkart emi credit card,
    flipkart emi policy,
    flipkart emi rules,
    flipkart emi cancellation,
    flipkart 0 emi,
    flipkart emi eligibility check,
    flipkart easy emi on debit card,
    flipkart zero emi,
    flipkart emi procedure,
    flipkart emi pay,
    flipkart emi eligibility check sms,
    flipkart emi calculator,
    flipkart emi customer care number,
    flipkart emi process debit card,,
    flipkart give emi on debit card,
    flipkart emi sms,
    flipkart emi icici debit card,,
    flipkart no emi cost ,
    flipkart emi app,
    flipkart emi shopping,
    flipkart no cost emi led tv,
    flipkart easy emi option,
    flipkart emi loan,
    flipkart emi for mobile,
    flipkart emi service,
    flipkart emi with credit card,
    flipkart in emi,
    flipkart debit card emi laptop,
    flipkart cardless emi link,
    flipkart free emi,
    flipkart emi hdfc,
    flipkart emi credit,,
    flipkart emi hdfc debit card,
    flipkart emi message,
    flipkart emi information,
    flipkart emi contact number,
    flipkart emi msg,
    flipkart new emi scheme,
    flipkart emi on debit card hdfc,
    flipkart emi number,
    flipkart debit card emi kaise kare,
    flipkart emi on debit card axis bank,
    flipkart emi registration,
    flipkart emi on icici debit card,
    flipkart emi on debit card quora,
    flipkart emi on axis debit card,
    flipkart debit card emi eligibility icici,
    flipkart debit card emi yes bank,
    flipkart emi on debit card sms,
    flipkart emi bank list,
    flipkart emi process in hindi,
    flipkart emi return policy
    flipkart debit card emi youtube,
    flipkart pay later emi,
    flipkart hdfc emi offer,
    flipkart emi how to pay,
    flipkart hdfc debit card emi eligibility,
    flipkart debit card emi eligibility sbi,
    flipkart debit card emi 2018,
    flipkart emi sbi,
    flipkart emi conditions,
    flipkart emi review,
    flipkart emi payment date,
    flipkart emi helpline number,
    flipkart emi how it works,
    flipkart se emi kaise kare,
    flipkart emi quora,
    flipkart debit card emi quora,
    flipkart emi faq,
    flipkart emi interest rate,
    flipkart emi 60000,,
    flipkart emi without interest,
    flipkart no cost emi quora

    ,

    ReplyDelete