Skip to content

Instantly share code, notes, and snippets.

@matthijskooijman
Created July 11, 2019 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthijskooijman/c5ae86988ec44db529ab600accf67af9 to your computer and use it in GitHub Desktop.
Save matthijskooijman/c5ae86988ec44db529ab600accf67af9 to your computer and use it in GitHub Desktop.

Testcase:

$ cat log.ir 
declare double    @llvm.log.f64(double %Val)

define double @foo() {
start:
  %0 = call double @llvm.log.f64(double 1.024000e+03) #8
  %1 = call double @llvm.log.f64(double 2.000000e+00) #8
  %2 = fdiv double %0, %1
  ret double %2
}

define double @bar() {
start:
  %0 = call double @llvm.log.f64(double 1.024000e+03) #8
  ret double %0
}

define double @baz() {
start:
  %0 = call double @llvm.log.f64(double 2.000000e+00) #8
  ret double %0
}

With libllvm7 from Debian (correct):

$ opt-7 log.ir -S -instcombine
; ModuleID = 'log.ir'
source_filename = "log.ir"

; Function Attrs: nounwind readnone speculatable
declare double @llvm.log.f64(double) #0

define double @foo() {
start:
  ret double 1.000000e+01
}

define double @bar() {
start:
  ret double 0x401BB9D3BEB8C86B
}

define double @baz() {
start:
  ret double 0x3FE62E42FEFA39EF
}

attributes #0 = { nounwind readnone speculatable }

With libllvm7 from Raspbian (incorrect, everything becomes NaN):

$ opt-7 log.ir -S -instcombine
; ModuleID = 'log.ir'
source_filename = "log.ir"

; Function Attrs: nounwind readnone speculatable
declare double @llvm.log.f64(double) #0

define double @foo() {
start:
  ret double 0x7FF8000000000000
}


In the original 

define double @bar() {
start:
  ret double 0x7FF8000000000000
}

define double @baz() {
start:
  ret double 0x7FF8000000000000
}

attributes #0 = { nounwind readnone speculatable }

In the original print-after-all output, I also noticed that the EarlyCSE transformation evaluates log(1024) incorrectly, but I could not find which opt option to use to reproduce this.

*** IR Dump After SROA ***
; Function Attrs: inlinehint nounwind nonlazybind readnone uwtable
define internal fastcc double @"_ZN3std3f6421_$LT$impl$u20$f64$GT$3log17hca0d05b8ada2f919E"() unnamed_addr #1 personality i32 (i32, %"unwind::libunwind::_Unwind_Exception"*, %"unwind::libunwind::_Unwind_Context"*)* @rust_eh_personality {
start:
  %0 = call double @llvm.log.f64(double 1.024000e+03) #8
  %1 = call double @llvm.log.f64(double 2.000000e+00) #8
  %2 = fdiv double %0, %1
  ret double %2
}
*** IR Dump After Early CSE w/ MemorySSA ***
; Function Attrs: inlinehint nounwind nonlazybind readnone uwtable
define internal fastcc double @"_ZN3std3f6421_$LT$impl$u20$f64$GT$3log17hca0d05b8ada2f919E"() unnamed_addr #1 personality i32 (i32, %"unwind::libunwind::_Unwind_Exception"*, %"unwind::libunwind::_Unwind_Context"*)* @rust_eh_personality {
start:
  %0 = call double @llvm.log.f64(double 2.000000e+00) #8
  %1 = fdiv double 0x747BB550747BA440, %0
  ret double %1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment