Skip to content

Implements the @nonans decorator to enable some LLVM vectorizations #31862

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ macro _propagate_inbounds_meta()
return Expr(:meta, :inline, :propagate_inbounds)
end

# Alternative to `@nonans`.
macro _nonans_meta()
return Expr(:meta, :nonans)
end

"""
convert(T, x)

Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ export
@nospecialize,
@specialize,
@polly,
@nonans,

@assert,
@__dot__,
Expand Down
17 changes: 17 additions & 0 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ macro polly(ex)
esc(isa(ex, Expr) ? pushmeta!(ex, :polly) : ex)
end

"""
@nonans

Allows the compiler to assume no NaNs on floating-point operations.

```julia
@nonans function mathfunc(x)
#=
Function Definition
=#
end
```
"""
macro nonans(ex)
esc(isa(ex, Expr) ? pushmeta!(ex, :nonans) : ex)
end

## some macro utilities ##

function pushmeta!(ex::Expr, sym::Symbol, args::Any...)
Expand Down
2 changes: 2 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jl_sym_t *throw_undef_if_not_sym; jl_sym_t *getfield_undefref_sym;
jl_sym_t *gc_preserve_begin_sym; jl_sym_t *gc_preserve_end_sym;
jl_sym_t *escape_sym;
jl_sym_t *aliasscope_sym; jl_sym_t *popaliasscope_sym;
jl_sym_t *nonans_sym;

static uint8_t flisp_system_image[] = {
#include <julia_flisp.boot.inc>
Expand Down Expand Up @@ -367,6 +368,7 @@ void jl_init_frontend(void)
do_sym = jl_symbol("do");
aliasscope_sym = jl_symbol("aliasscope");
popaliasscope_sym = jl_symbol("popaliasscope");
nonans_sym = jl_symbol("nonans");
}

JL_DLLEXPORT void jl_lisp_prompt(void)
Expand Down
4 changes: 4 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5618,6 +5618,10 @@ static std::unique_ptr<Module> emit_function(
f->addFnAttr(Attribute::NoInline);
}

if (jl_has_meta(stmts, nonans_sym)) {
f->addFnAttr("no-nans-fp-math", "true");
}

if (returninfo.cc == jl_returninfo_t::Union) {
f->addAttribute(1, Attribute::getWithDereferenceableBytes(jl_LLVMContext, returninfo.union_bytes));
f->addAttribute(1, Attribute::getWithAlignment(jl_LLVMContext, returninfo.union_align));
Expand Down
1 change: 1 addition & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ extern jl_sym_t *throw_undef_if_not_sym; extern jl_sym_t *getfield_undefref_sym;
extern jl_sym_t *gc_preserve_begin_sym; extern jl_sym_t *gc_preserve_end_sym;
extern jl_sym_t *failed_sym; extern jl_sym_t *done_sym; extern jl_sym_t *runnable_sym;
extern jl_sym_t *escape_sym;
extern jl_sym_t *nonans_sym;

struct _jl_sysimg_fptrs_t;

Expand Down
21 changes: 21 additions & 0 deletions test/llvmpasses/nonans.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# RUN: julia --startup-file=no %s %t && llvm-link -S %t/* -o %t/module.ll
# RUN: cat %t/module.ll | FileCheck %s

## Notes:
# This script uses the `emit` function (defined llvmpasses.jl) to emit either
# optimized or unoptimized LLVM IR. Each function is emitted individually and
# `llvm-link` is used to create a single module that can be passed to opt.
# The order in which files are emitted and linked is important since `lit` will
# process the test cases in order.

include(joinpath("..", "testhelpers", "llvmpasses.jl"))

# CHECK-LABEL: @julia_minimum_nonans
@nonans function minimum_nonans(itr)
return reduce(itr) do a, b ifelse(a < b, a, b) end
end

# CHECK: attributes #{{[0-9]+}} = {{{[a-z "=]*}}"no-nans-fp-math"="true"{{[a-z "=]*}}}
emit(minimum_nonans, Vector{Float64})