Implement foo in terms of __builtin_foo fails

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Implement foo in terms of __builtin_foo fails

Joerg Sonnenberger via cfe-dev
Hello clang devs,

I'm implementing libm for a toolchain based on LLVM. There is instruction level support for some libm functions that I'd like to target via clang. The IR to machine code part is working so I think the next step is writing C that generates calls to the llvm intrinsics.

For example:
double sin(double x) { return __builtin_sin(x); }

I expected this to compile to:
define double @sin(double %x)  {
  %res = call double @llvm.sin.f64(double %x)
  ret double %res
}

Our back end recognises llvm.sin.f64 so this would compile into sensible instructions.

However, at -O0 clang generates a call to sin.

define double @sin(double %x) {
  %x.addr = alloca double, align 8
  store double %x, double* %x.addr, align 8
  %0 = load double, double* %x.addr, align 8
  %call = call double @sin(double %0)
  ret double %call
}

At higher optimisations, this recursive call is detected and optimised into:
define double @sin(double %x) {
entry:
  ret double undef
}

How can I write C that generates a call to the llvm.sin.f64 intrinsic?

Cheers,

Jon

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Implement foo in terms of __builtin_foo fails

Joerg Sonnenberger via cfe-dev
On Fri, Feb 23, 2018 at 05:03:46PM +0000, Jon Chesterfield via cfe-dev wrote:
> How can I write C that generates a call to the llvm.sin.f64 intrinsic?

Why do you want to force that? The library handling already does it when
the compilation flags match them up. I.e. most of the intrinsics are a
lot more restricted than the ISO C constraints.

Joerg
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Implement foo in terms of __builtin_foo fails

Joerg Sonnenberger via cfe-dev
I think this means I'm missing some compilation flags.

I'm not passing -ffast-math because treating floating point as associative etc is unattractive, but I'm willing to violate iso c handling of errorno. fast-math and denormal-fp-math are the only two flags I can find in the documentation. ffreestanding and fno-builtin don't appear to change the example. Where are the controlling flags listed?

Thanks!

Jon

On 23 Feb 2018 7:05 p.m., "Joerg Sonnenberger" <[hidden email]> wrote:
On Fri, Feb 23, 2018 at 05:03:46PM +0000, Jon Chesterfield via cfe-dev wrote:
> How can I write C that generates a call to the llvm.sin.f64 intrinsic?

Why do you want to force that? The library handling already does it when
the compilation flags match them up. I.e. most of the intrinsics are a
lot more restricted than the ISO C constraints.

Joerg

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Implement foo in terms of __builtin_foo fails

Joerg Sonnenberger via cfe-dev
I can now present this problem more clearly. I was missing flags fno-math-errno, fno-trapping-math, but clang is also missing some functionality.

I think that various clang builtins have the same semantics as llvm builtins with both of these math flags set, e.g. sin, exp. There's special handling for sqrt (including returning undef on negative inputs with no-nans-fp-math) in CGBuiltin, but most of libm is emitted as library calls.

SelectionDAGBuilder then matches some of the library calls and emits ISD nodes for them, e.g. it supports exp2 but misses exp. This is too late for implementing libm but otherwise OK.

I would like to add handling to CGBuiltin to lower more of the libm derived clang intrinsics to llvm intrinsics when appropriate fpmath flags are set.

AMDGPU duplicates the libm derived builtins, lowering them via SelectionDAG. This would also work for me but it seems a shame to ignore llvm.sin.f32 et al when they already exist. I can't find a way to target them from C without changing clang.

What does the list think of extending CGBuiltin vs adding more target specific nodes?

Cheers,

Jon

On 24 Feb 2018 05:41, "via cfe-dev" <[hidden email]> wrote:
Send cfe-dev mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of cfe-dev digest..."


Today's Topics:

   1. Re: Implement foo in terms of __builtin_foo fails
      (Jon Chesterfield via cfe-dev)
   2. Google Summer of Code 2018 (Réka Nikolett Kovács via cfe-dev)
   3. Re: GSoC 2018 (Devin Coughlin via cfe-dev)
   4. Re: Google Summer of Code 2018 (Artem Dergachev via cfe-dev)


----------------------------------------------------------------------

Message: 1
Date: Fri, 23 Feb 2018 20:45:01 +0000
From: Jon Chesterfield via cfe-dev <[hidden email]>
To: [hidden email]
Subject: Re: [cfe-dev] Implement foo in terms of __builtin_foo fails
Message-ID:
        <[hidden email]>
Content-Type: text/plain; charset="utf-8"

I think this means I'm missing some compilation flags.

I'm not passing -ffast-math because treating floating point as associative
etc is unattractive, but I'm willing to violate iso c handling of errorno.
fast-math and denormal-fp-math are the only two flags I can find in the
documentation. ffreestanding and fno-builtin don't appear to change the
example. Where are the controlling flags listed?

Thanks!

Jon

On 23 Feb 2018 7:05 p.m., "Joerg Sonnenberger" <[hidden email]> wrote:

On Fri, Feb 23, 2018 at 05:03:46PM +0000, Jon Chesterfield via cfe-dev
wrote:
> How can I write C that generates a call to the llvm.sin.f64 intrinsic?

Why do you want to force that? The library handling already does it when
the compilation flags match them up. I.e. most of the intrinsics are a
lot more restricted than the ISO C constraints.

Joerg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180223/37d29d67/attachment-0001.html>

------------------------------

Message: 2
Date: Sat, 24 Feb 2018 01:10:34 +0100
From: Réka Nikolett Kovács via cfe-dev <[hidden email]>
To: [hidden email]
Subject: [cfe-dev] Google Summer of Code 2018
Message-ID:
        <CAFdNtUc_hM=[hidden email]>
Content-Type: text/plain; charset="utf-8"

Dear All,

I'm Réka Kovács, a final-year M.S. student from Eötvös Loránd University,
Budapest, and I would love to work on a Clang SA-related GSoC project this
summer.

I've been working on static analysis for the past half a year and started
meddling in Clang by submitting a few patches:
- 3 Clang-Tidy checks [1][2][3],
- a Clang SA check [4],
- a diagnostic flag extension [5][6], and
- a tiny tweak in the core [7].

I'm currently studying constraint solving issues in symbolic execution as
part of a university project, and plan to continue with a PhD focusing on
Clang-related stuff.

I was initially most interested in the Z3 integration project, but I've
noticed that Mikhail has applied already. Creating a checker for dangling
string pointers would also be an interesting challenge, so I'd like to
express my enthusiasm for that project.

The main goal for me would be to get more comfortable with the inner
workings of the analyzer and learn as much along the process as possible.

I'm also open to any other suggestions, so please be so kind to share your
thoughts with me.

Thanks,
Réka


[1] bugprone-suspicious-memset-usage: https://reviews.llvm.org/D32700
[2] bugprone-undefined-memory-manipulation: https://reviews.llvm.org/D35051
[3] bugprone-integer-division: https://reviews.llvm.org/D35932
[4] alpha.cplusplus.DeleteWithNonVirtualDtor: <a href="https://reviews.llvm.org/ D35796" rel="noreferrer" target="_blank">https://reviews.llvm.org/
D35796
[5] -Wenum-compare: https://reviews.llvm.org/D36407
[6] -Wenum-compare-switch: https://reviews.llvm.org/D36526
[7] model unrepresentable left shifts: https://reviews.llvm.org/D41816
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180224/ee457111/attachment-0001.html>

------------------------------

Message: 3
Date: Fri, 23 Feb 2018 17:03:00 -0800
From: Devin Coughlin via cfe-dev <[hidden email]>
To: Mikhail Ramalho <[hidden email]>
Cc: Mikhail Ramalho via cfe-dev <[hidden email]>
Subject: Re: [cfe-dev] GSoC 2018
Message-ID: <[hidden email]>
Content-Type: text/plain; CHARSET=US-ASCII



> On Feb 23, 2018, at 9:29 AM, Mikhail Ramalho via cfe-dev <[hidden email]> wrote:
>
> I also have a question about the proposal. I understand that ideas about the project will be discussed in the mailing list. However, once that's settled and I write my first draft proposal, should I send it to the mailing list for discussion again or should I send it only to the mentor?

Please make sure to keep email discussions on the mailing list rather than just personal email. This is a topic that members of the community will be interested in and will have valuable feedback on.

Devin



------------------------------

Message: 4
Date: Fri, 23 Feb 2018 21:40:32 -0800
From: Artem Dergachev via cfe-dev <[hidden email]>
To: Réka Nikolett Kovács <[hidden email]>,
        [hidden email]
Subject: Re: [cfe-dev] Google Summer of Code 2018
Message-ID: <[hidden email]>
Content-Type: text/plain; charset=utf-8; format=flowed

Hey, welcome!

First of all, it's great that you let us know about your interest in the
Z3 integration project. It might be puzzling for us to come up with the
final arrangement, given how the project doesn't seem to be easy to
quantize for cooperative work, but a lot of things may change by the
time everything is settled, and your enthusiasm is an important piece of
the puzzle!

We didn't come up with other exciting project ideas so far, but the list
of projects is definitely not set in stone. I'd let you know if anything
shows up, and please feel free to share your ideas of how the analyzer
could be improved or what features you want it to have :) I guess i'd
explain the other project a little bit, for completeness.

The use-after-free-like checker for values managed by temporary objects
should be an easier and more straightforward project than Z3, but there
are quite a lot of unknowns here as well. Because internals of
std::string and other similar classes are too hard for the analyzer's
generic use-after-free checker to understand (partially because of the
lack of a good solver, but mostly due to how hard it is to track STL's
internal invariants, and how not all of the code is necessarily present
in the header), an API-specific checker seems to be necessary. The
original plan we've had in mind was to keep track of dangerous values
like str.c_str() in the program state (similarly to how
SimpleStreamChecker tracks file descriptors) and then see if any of them
are still present in memory at the end of the original value's lifetime
(similarly to how StackAddrEscape checker finds stack pointers at the
end of a function's stack frame).

With this description and your knowledge, you'd probably be able to
think of how the checker might be implemented (and if it's of interest
to you) - though also feel free to ask if you have any questions! The
unknowns here include how easy would it be to track scopes (for now we
only track function scopes, but if fairly old but recently reincarnated
patches [1] and [2] land any time soon, we may get a much better
granularity), how easy would it be to track objects when they are moved
or lifetime-extended by binding to references, which was a large problem
for other C++ object checkers, but we may work our way around it to some
extent (or do it properly, depending on my current work outlined in [3]
and in follow-up mails in February), and also how helpful inlining would
be (eg. would we be able to automagically support string_view-like
classes by inlining their methods?). So the checker would need an almost
indefinite amount of incremental improvements once the initial prototype
is done, some of which must be fairly curious and would certainly expose
you to some of the analyzer's internals.

[1] https://reviews.llvm.org/D16403
[2] https://reviews.llvm.org/D19979
[3] http://lists.llvm.org/pipermail/cfe-dev/2018-January/056691.html

On 23/02/2018 4:10 PM, Réka Nikolett Kovács via cfe-dev wrote:
> Dear All,
>
> I'm Réka Kovács, a final-year M.S. student from Eötvös Loránd
> University, Budapest, and I would love to work on a Clang SA-related
> GSoC project this summer.
>
> I've been working on static analysis for the past half a year and
> started meddling in Clang by submitting a few patches:
> - 3 Clang-Tidy checks [1][2][3],
> - a Clang SA check [4],
> - a diagnostic flag extension [5][6], and
> - a tiny tweak in the core [7].
>
> I'm currently studying constraint solving issues in symbolic execution
> as part of a university project, and plan to continue with a PhD
> focusing on Clang-related stuff.
>
> I was initially most interested in the Z3 integration project, but
> I've noticed that Mikhail has applied already. Creating a checker for
> dangling string pointers would also be an interesting challenge, so
> I'd like to express my enthusiasm for that project.
>
> The main goal for me would be to get more comfortable with the inner
> workings of the analyzer and learn as much along the process as possible.
>
> I'm also open to any other suggestions, so please be so kind to share
> your thoughts with me.
>
> Thanks,
> Réka
>
>
> [1] bugprone-suspicious-memset-usage:https://reviews.llvm.org/D32700
> <https://reviews.llvm.org/D32700>
> [2]
> bugprone-undefined-memory-manipulation:https://reviews.llvm.org/D35051
> <https://reviews.llvm.org/D35051>
> [3] bugprone-integer-division:https://reviews.llvm.org/D35932
> <https://reviews.llvm.org/D35932>
> [4]
> alpha.cplusplus.DeleteWithNonVirtualDtor:https://reviews.llvm.org/D35796
> <https://reviews.llvm.org/D35796>
> [5] -Wenum-compare:https://reviews.llvm.org/D36407
> <https://reviews.llvm.org/D36407>
> [6] -Wenum-compare-switch:https://reviews.llvm.org/D36526
> <https://reviews.llvm.org/D36526>
> [7] model unrepresentable left shifts:https://reviews.llvm.org/D41816
> <https://reviews.llvm.org/D41816>
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



------------------------------

Subject: Digest Footer

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


------------------------------

End of cfe-dev Digest, Vol 128, Issue 71
****************************************


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Implement foo in terms of __builtin_foo fails

Joerg Sonnenberger via cfe-dev
On Mon, Feb 26, 2018 at 04:08:50PM +0000, Jon Chesterfield via cfe-dev wrote:
> I can now present this problem more clearly. I was missing flags
> fno-math-errno, fno-trapping-math, but clang is also missing some
> functionality.

Depending on the amount of control you have over your toolchain, you can
disable errno-reporting completely. I consider that a historic mistake
in general and the standard quite allows that. -fno-trapping-math should
not be necessary in general, what do you need it for?

The other thing to keep in mind is that LLVM is still quite limited in
what it can do for the intrinsics beyond lowering to instructions. Given
that few CPUs have decent transcendental FP support, that isn't that
high on the priority list for most. I.e. the range reduction on x86 is
completely broken for the trigonometric functions etc.

Joerg
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev