Thread overview
LDC2 with -fxray-instrument
Jan 15, 2019
Márcio Martins
Jan 15, 2019
Johan Engelen
Jan 16, 2019
Márcio Martins
Jan 16, 2019
Johan Engelen
Jan 16, 2019
Johan Engelen
Jan 17, 2019
Johan Engelen
Jan 21, 2019
Márcio Martins
Jan 21, 2019
Johan Engelen
January 15, 2019
Anyone has any idea how to build with LDC and -fxray-instrument?
I am running LDC 1.13 on Linux (x64)

The XRay section is in the binary, and the compiler-rt is linked in, but when I run the binary with XRAY_OPTIONS="patch_premain=true verbosity=1" in the environment, and I get nothing. No XRay logging or terminal output.
January 15, 2019
On Tuesday, 15 January 2019 at 14:32:03 UTC, Márcio Martins wrote:
> Anyone has any idea how to build with LDC and -fxray-instrument?
> I am running LDC 1.13 on Linux (x64)
>
> The XRay section is in the binary, and the compiler-rt is linked in, but when I run the binary with XRAY_OPTIONS="patch_premain=true verbosity=1" in the environment, and I get nothing. No XRay logging or terminal output.

Great that you are trying this! I added it to LDC (trivial), but I have not tested it because I develop on macOS (bad excuse). Back than at least, XRay did not function on macOS.

What platform are you on?

-Johan

January 16, 2019
On Tuesday, 15 January 2019 at 22:51:15 UTC, Johan Engelen wrote:
> What platform are you on?

Linux x64

January 16, 2019
On Wednesday, 16 January 2019 at 17:36:31 UTC, Márcio Martins wrote:
> On Tuesday, 15 January 2019 at 22:51:15 UTC, Johan Engelen wrote:
>> What platform are you on?
>
> Linux x64

OK, so that should work. What is your testcase? Try with `-fxray-instruction-threshold=1` to also instrument small functions.
Have you gotten things to work with C++ code?

-Johan

January 16, 2019
On Wednesday, 16 January 2019 at 22:10:14 UTC, Johan Engelen wrote:
> On Wednesday, 16 January 2019 at 17:36:31 UTC, Márcio Martins wrote:
>> On Tuesday, 15 January 2019 at 22:51:15 UTC, Johan Engelen wrote:
>>> What platform are you on?
>>
>> Linux x64
>
> OK, so that should work. What is your testcase? Try with `-fxray-instruction-threshold=1` to also instrument small functions.

I'm trying now and am having the same problem. I'm trying to figure out how to fix LDC so that it works.

work in progress!

-Johan

January 17, 2019
On Wednesday, 16 January 2019 at 23:29:45 UTC, Johan Engelen wrote:
> On Wednesday, 16 January 2019 at 22:10:14 UTC, Johan Engelen wrote:
>> On Wednesday, 16 January 2019 at 17:36:31 UTC, Márcio Martins wrote:
>>> On Tuesday, 15 January 2019 at 22:51:15 UTC, Johan Engelen wrote:
>>>> What platform are you on?
>>>
>>> Linux x64
>>
>> OK, so that should work. What is your testcase? Try with `-fxray-instruction-threshold=1` to also instrument small functions.
>
> I'm trying now and am having the same problem. I'm trying to figure out how to fix LDC so that it works.
>
> work in progress!
>
> -Johan

OK, got it :-) LLVM 7 changed things a little, so it's broken with LDC 1.13  [*].

For now, you can use LDC 1.12 (LLVM 6). You also have to add `-L-lstdc++` as compiler flag. So with LDC 1.12, Linux, and this commandline things work for me:
```
ldc2 -fxray-instrument -fxray-instruction-threshold=1 xraytest.d -L-lstdc++
XRAY_OPTIONS="patch_premain=true xray_mode=xray-basic verbosity=1" xraytest
```

Happy testing,
  Johan

[*] XRay was split into several libraries, and we only copy+link with one of them. You can make things work by downloading the libs and linking with them.
January 21, 2019
On Thursday, 17 January 2019 at 00:11:10 UTC, Johan Engelen wrote:
> On Wednesday, 16 January 2019 at 23:29:45 UTC, Johan Engelen wrote:
>> On Wednesday, 16 January 2019 at 22:10:14 UTC, Johan Engelen wrote:
>>> On Wednesday, 16 January 2019 at 17:36:31 UTC, Márcio Martins wrote:
>>>> On Tuesday, 15 January 2019 at 22:51:15 UTC, Johan Engelen wrote:
>>>>> What platform are you on?
>>>>
>>>> Linux x64
>>>
>>> OK, so that should work. What is your testcase? Try with `-fxray-instruction-threshold=1` to also instrument small functions.
>>
>> I'm trying now and am having the same problem. I'm trying to figure out how to fix LDC so that it works.
>>
>> work in progress!
>>
>> -Johan
>
> OK, got it :-) LLVM 7 changed things a little, so it's broken with LDC 1.13  [*].
>
> For now, you can use LDC 1.12 (LLVM 6). You also have to add `-L-lstdc++` as compiler flag. So with LDC 1.12, Linux, and this commandline things work for me:
> ```
> ldc2 -fxray-instrument -fxray-instruction-threshold=1 xraytest.d -L-lstdc++
> XRAY_OPTIONS="patch_premain=true xray_mode=xray-basic verbosity=1" xraytest
> ```
>
> Happy testing,
>   Johan
>
> [*] XRay was split into several libraries, and we only copy+link with one of them. You can make things work by downloading the libs and linking with them.

Great! Does it mean it will be fixed in next LDC release? :)

I also played a bit with -finstrument-functions and it works fine for tools like uftrace... However, if you define your own __cyg_profile_func_* functions, it won't work (or be useful), because there is no way to disable instrumenting on these __cyg_profile_func_* functions, so you can imagine what happens. I tried @llvmAttr("no_instrument_function"), supposedly the GCC compatible way to disable instrumentation on these, but it doesn't make a difference.

There are few LLVM settings that will make this useful:
- https://reviews.llvm.org/D40276
- https://reviews.llvm.org/D39331 (which enables instrumenting after inlining, which is more useful in my use case)
- https://reviews.llvm.org/D37622 (is has not been merged yet, but necessary, to be able to conveniently call library functions from within the hooks)
January 21, 2019
On Monday, 21 January 2019 at 14:36:15 UTC, Márcio Martins wrote:
> On Thursday, 17 January 2019 at 00:11:10 UTC, Johan Engelen wrote:
>>
>> OK, got it :-) LLVM 7 changed things a little, so it's broken with LDC 1.13  [*].
>>
>> For now, you can use LDC 1.12 (LLVM 6). You also have to add `-L-lstdc++` as compiler flag. So with LDC 1.12, Linux, and this commandline things work for me:
>> ```
>> ldc2 -fxray-instrument -fxray-instruction-threshold=1 xraytest.d -L-lstdc++
>> XRAY_OPTIONS="patch_premain=true xray_mode=xray-basic verbosity=1" xraytest
>> ```
>>
>> Happy testing,
>>   Johan
>>
>> [*] XRay was split into several libraries, and we only copy+link with one of them. You can make things work by downloading the libs and linking with them.
>
> Great! Does it mean it will be fixed in next LDC release? :)

Yes: https://github.com/ldc-developers/ldc/pull/2965

> I also played a bit with -finstrument-functions and it works fine for tools like uftrace... However, if you define your own __cyg_profile_func_* functions, it won't work (or be useful), because there is no way to disable instrumenting on these __cyg_profile_func_* functions, so you can imagine what happens. I tried @llvmAttr("no_instrument_function"), supposedly the GCC compatible way to disable instrumentation on these, but it doesn't make a difference.

You need:  pragma(LDC_profile_instr, false), e.g.:
```
pragma(LDC_profile_instr, false)
void fun () { ... }
``

> There are few LLVM settings that will make this useful:
> - https://reviews.llvm.org/D40276
> - https://reviews.llvm.org/D39331 (which enables instrumenting after inlining, which is more useful in my use case)
> - https://reviews.llvm.org/D37622 (is has not been merged yet, but necessary, to be able to conveniently call library functions from within the hooks)

These are not too hard to implement in LDC. Whoever is interested in working on it, here are some pointers in addition to the LLVM links:
https://github.com/ldc-developers/ldc/issues/2466
https://github.com/ldc-developers/ldc/pull/1845/files

-Johan