[llvm-dev] How to let LLVM handle undefined behavior more gracefully?

Mehdi Amini via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 16 18:29:50 PDT 2016


> On Mar 16, 2016, at 6:16 PM, Zhao, Weiming via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hi,
> 
> There are cases where LLVM is able to detect some UB but clang is not.
> 
> For example,
> 
> unsigned int foo(unsigned int x) {
> 
>  int ret = 0;
>  for(int i = 0; i <= 32; ++i)
>    ret += x >> i;
>  return ret;
> 
> }
> 
> When the loop is unrolled, LLVM InstructionSimplify will catch it and
> return a UNDEF value.
> How can we let LLVM report some warning message to help developers
> correct the error?
> Or should we use similar behavior as GCC (e.g. x >> 32 returns 0)?
> 
> This can also saves compiler engineer's effort: users complain that it's
> a compiler bug because their code works with GCC or older version of
> LLVM (because the loop is not unrolled). And it's really hard to debug
> such UB in some large code base.

Isn't it catched by UBSAN?

-- 
Mehdi


> 
> Thanks,
> Weiming
> 
> -- 
> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
> 
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



More information about the llvm-dev mailing list