Translate ^^ operator to druntime hook#22831
Conversation
|
Thanks for your pull request and interest in making D better, @dkorpel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#22831" |
|
I had to double check what ldc with optimizations would do with float abc(float a) {
return a ^^ 0.5;
}It will optimize it, doing it in the frontend is far too early. |
|
@rikkimax You got that backwards |
Which part? |
|
ldc shares dmd's frontend, which does the transformation. LDC's backend is not allowed to rewrite pow(x, 0.5) to sqrt(x) because that's not strictly equivalent. |
|
Okay, yes llvm isn't doing the rewrite, IR with optimizations turned off is already sqrt. |
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@dkorpel are you offering to write an ibm-real implementation of |
|
What is that and why would it be needed? |
alias real = double[2];https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#Double-double_arithmetic Without support, building druntime would regress on POWER targets. |
|
The goal here is to move code from one package to another. Why would that break existing support or require new support? Looks like a separate thing to me. |
|
These power targets can't build std.math, moving that out of phobos means they won't be able to build druntime - ergo can't bootstrap the compiler. |
| // TODO: move implementation to druntime instead of Phobos. | ||
| auto _d_sqrt(T)(T x) | ||
| { | ||
| import std.math : sqrt; |
There was a problem hiding this comment.
This is an intrinsic, use core.math.sqrt instead.
There was a problem hiding this comment.
The goal of this PR is to keep behavior equivalent to what it was before, just moving the logic to druntime so at least users of custom druntime can already swap in their own implementation. I plan on breaking the Phobos connection in subsequent PRs.
| // TODO: move implementation to druntime instead of Phobos. | ||
| auto _d_pow(Base, Exp)(Base base, Exp exp) | ||
| { | ||
| import std.math : pow; |
There was a problem hiding this comment.
Could test the import in case this is a port that doesn't have phobos included?
static if (__traits(compiles, { import std.math : pow; }))
There was a problem hiding this comment.
No need, because the import is inside a template
There was a problem hiding this comment.
Then why go to all the trouble of putting in a friendly error in the compiler then.
The current runtime does not support the ^^ operator, or the runtime is corrupt.
There was a problem hiding this comment.
That's the standard verifyHookExist procedure.
There was a problem hiding this comment.
Which succeeds because _d_pow exists, then it later fails as std.math doesn't.
There was a problem hiding this comment.
Test.
[Environment64]
#DFLAGS=-I%@P%/../../../../druntime/import -I%@P%/../../../../../phobos -L-L%@P%/../../../../../phobos/generated/linux/release/64 -L--export-dynamic -fPIC
DFLAGS=-I%@P%/../../../../druntime/import -L-L%@P%/../../../../../phobos/generated/linux/release/64 -L--export-dynamic -fPIC
Before:
pow.d(3): Error: `a ^^ b` requires `std.math` for `^^` operators
auto c = a ^^ b;
^
After:
./generated/linux/release/64/../../../../druntime/import/object.d(4761): Error: unable to read module `math`
import std.math : pow;
^
./generated/linux/release/64/../../../../druntime/import/object.d(4761): Expected 'std/math.d' or 'std/math/package.d' in one of the following import paths:
import std.math : pow;
^
import path[0] = ./generated/linux/release/64/../../../../druntime/import
There was a problem hiding this comment.
Yes, that's to be expected
There was a problem hiding this comment.
Then why shy away from friendly error messages then? :-)
static assert(__traits(compiles, { import std.math : pow; }),
"The current runtime does not support the ^^ operator");
return imported!"std.math".pow(base, exp);->
./generated/linux/release/64/../../../../druntime/import/object.d(4761): Error: static assert: "The current runtime does not support the ^^ operator"
static assert(__traits(compiles, { import std.math : pow; }),
^
pow.d(3): instantiated from here: `_d_pow!(double, double)`
auto c = a ^^ b;
^
There was a problem hiding this comment.
Multiple reasons
- __traits(compiles) is a hack
- It's better to improve compiler error messages in general than trying to 'catch' them and replacing them manually
- The error message is not part of normal usage, but advanced usage in unsupported environments
- The static assert removes information from the compiler's error message (import paths)
- The whole std import is to be removed any way
The unnormalized path is pretty long and pushes the message to the right, I think that would make a good improvement to start with in general.
If they couldn't build std.math, then they couldn't use ^^ either, so if the implementation in druntime gets versioned out for those targets it will work like before |
Except it won't as someone's going to add |
|
Do downstreams just pull changes from upstream without testing? |
|
This needs change log entry, this will affect custom runtimes. |
First step to fixing #22670
It doesn't move the implementation like #14297 does, but it moves the hard-coded std import to one that can be overridden in custom druntime.