Skip to content

Add support for setting and querying fast math flags. #383

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

Merged
merged 2 commits into from
Feb 1, 2024
Merged

Conversation

maleadt
Copy link
Collaborator

@maleadt maleadt commented Jan 31, 2024

As requested by @chriselrod

Backport llvm/llvm-project#75123 to LLVMExtra, and add a Julian API:

  • fast_math(::Instruction) returning a named tuple
  • fast_math!(::Instruction; flags...) specifying flags as kwargs as kwarg

I contemplated using symbols instead (fast_math!(inst, :contract)), but with kwargs its easier to pass multiple flags.

@maleadt
Copy link
Collaborator Author

maleadt commented Jan 31, 2024

Demo:

@dispose ctx=Context() begin
    # set-up
    mod = LLVM.Module("my_module")

    param_types = [LLVM.FloatType()]
    ret_type = LLVM.FloatType()
    fun_type = LLVM.FunctionType(ret_type, param_types)
    fun = LLVM.Function(mod, "add_sub", fun_type)

    # generate IR
    @dispose builder=IRBuilder() begin
        entry = BasicBlock(fun, "entry")
        position!(builder, entry)

        # add and substract 42
        a = fadd!(builder, parameters(fun)[1], LLVM.ConstantFP(Float32(42.)), "a")
        fast_math!(a; all=true)
        b = fsub!(builder, a, LLVM.ConstantFP(Float32(42.)), "b")
        fast_math!(b; all=true)

        ret!(builder, b)
    end
    verify(mod)
    display(mod)

    println("#"^92)
    println()

    # optimize IR
    host_triple = triple()
    host_t = Target(triple=host_triple)
    @dispose tm=TargetMachine(host_t, host_triple) pb=PassBuilder(tm) begin
        NewPMModulePassManager(pb) do mpm
            parse!(pb, mpm, "default<O3>")
            run!(mpm, mod, tm)
        end
    end
    verify(mod)
    display(mod)
end
; ModuleID = 'my_module'
source_filename = "my_module"

define float @add_sub(float %0) {
entry:
  %a = fadd fast float %0, 4.200000e+01
  %b = fsub fast float %a, 4.200000e+01
  ret float %b
}

############################################################################################

; ModuleID = 'my_module'
source_filename = "my_module"

; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn
define float @add_sub(float returned %0) local_unnamed_addr #0 {
entry:
  ret float %0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind readnone willreturn }

@maleadt maleadt merged commit cead54d into master Feb 1, 2024
@maleadt maleadt deleted the tb/fast_math branch February 1, 2024 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant