qir-alliance / qat

QIR compiler tools and optimization passes for targeting QIR to different hardware backends
https://qir-alliance.github.io/qat/
MIT License
26 stars 14 forks source link

Setting error flag on division by zero #107

Closed troelsfr closed 2 years ago

troelsfr commented 2 years ago

This PR introduces a global error variable which is set if the program execution experience a division by zero. For instance,

entry:
  %y = alloca i64, align 8
  %x = alloca i64, align 8
  store i64 0, i64* %x, align 4
  store i64 20, i64* %y, align 4
  %0 = load i64, i64* %x, align 4
  %1 = load i64, i64* %y, align 4
  %2 = sdiv i64 %0, %1
  call void @print_i64(i64 %2)

is transformed into

entry:
  %y = alloca i64, align 8
  %x = alloca i64, align 8
  store i64 0, i64* %x, align 4
  store i64 20, i64* %y, align 4
  %0 = load i64, i64* %x, align 4
  %1 = load i64, i64* %y, align 4
  %2 = icmp eq i64 %1, 0
  br i1 %2, label %if_denominator_is_zero, label %after_zero_check

if_denominator_is_zero:                           ; preds = %entry
  %3 = load i64, i64* @__qir__error_code, align 4
  %4 = icmp eq i64 %3, 0
  br i1 %4, label %if_ecc_not_set, label %ecc_set_finally

if_ecc_not_set:                                   ; preds = %if_denominator_is_zero
  store i64 1338, i64* @__qir__error_code, align 4
  br label %ecc_set_finally

ecc_set_finally:                                  ; preds = %if_denominator_is_zero, %if_ecc_not_set
  br label %after_zero_check

after_zero_check:                                 ; preds = %entry, %ecc_set_finally
  %5 = sdiv i64 %0, %1
  call void @print_i64(i64 %5)