Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: use new freeze intrinsics to avoid poison UB #38977

Merged
merged 1 commit into from
Dec 24, 2020
Merged

Conversation

vtjnash
Copy link
Sponsor Member

@vtjnash vtjnash commented Dec 23, 2020

LLVM would likely be required to insert this anyways to make hoisting
legal (converting branches to and/or), so might as well add it
ourselves, so that users can legally write that code directly.

Replaces #38735

julia> f() = Ref(Inf)[] < typemax(Clong)
f (generic function with 1 method)

julia> @code_llvm optimize=false f()
@ REPL[1]:1 within `f'
define i8 @julia_f_186() {
top:
  %0 = call {}*** @julia.ptls_states()
  %1 = bitcast {}*** %0 to {}**
  %2 = getelementptr inbounds {}*, {}** %1, i64 4
  %3 = bitcast {}** %2 to i64**
  %4 = load i64*, i64** %3, align 8
; ┌ @ float.jl:416 within `<'
; │┌ @ float.jl:239 within `unsafe_trunc'
    %5 = freeze i64 poison
; │└
; │ @ float.jl:416 within `<' @ int.jl:83
   %6 = icmp slt i64 %5, 9223372036854775807
; │ @ float.jl:416 within `<'
; │┌ @ bool.jl:36 within `&'
    %7 = zext i1 %6 to i8
    %8 = and i8 0, %7
    %9 = trunc i8 %8 to i1
; │└
; │┌ @ bool.jl:37 within `|'
    %10 = zext i1 %9 to i8
    %11 = or i8 0, %10
    %12 = trunc i8 %11 to i1
; └└
  %13 = zext i1 %12 to i8
  ret i8 %13
}

julia> @code_llvm  f()
;  @ REPL[1]:1 within `f'
define i8 @julia_f_221() {
top:
  ret i8 0
}

(otherwise, the llvm::IRBuilder<> constant folds this to ret i8 poison)

LLVM would likely be required to insert this anyways to make hoisting
legal (converting branches to and/or), so might as well add it
ourselves, so that users can legally write that code directly.
@vchuravy vchuravy added the compiler:codegen Generation of LLVM IR and native code label Dec 24, 2020
@Keno Keno merged commit d6a655b into master Dec 24, 2020
@Keno Keno deleted the jn/llvm-freeze branch December 24, 2020 05:29
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
LLVM would likely be required to insert this anyways to make hoisting
legal (converting branches to and/or), so might as well add it
ourselves, so that users can legally write that code directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:codegen Generation of LLVM IR and native code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants