This is an archive of the discontinued LLVM Phabricator instance.

[LTO] Account for overriding lib calls via the alias attribute
ClosedPublic

Authored by wristow on Oct 3 2018, 10:55 AM.

Details

Summary

Given a library call that is represented as an llvm intrinsic call, but later transformed to an actual call, if an overriding definition of that library routine is provided indirectly via an alias, verify that LTO does not eliminate the definition.

This is a proposed fix for PR38547.

Diff Detail

Repository
rL LLVM

Event Timeline

wristow created this revision.Oct 3 2018, 10:55 AM

I'll respond more on the bug (especially since I can't reproduce the actual link time error), but I think the right way to fix these types of issues is to compile with one of -ffreestanding, -fno-builtin, or -fno-builtin-log (or whatever is being overridden), so that clang doesn't translate the call to an intrinsic. Clang translates to the intrinsic assuming it is calling the builtin, which means it can assume specific semantics for the call, and there is nothing stopping the compiler from translating that intrinsic call to e.g. an inline expansion of the function (that currently happens e.g. for memcpy and some other builtins).

I'll respond more on the bug (especially since I can't reproduce the actual link time error), ...

Thanks for that. I've responded more thoroughly in the bug, but a short note here to clarify.

The reason the link-time error wasn't reproducible appears to be a new LTO API vs old LTO API issue (as you suggested in the PR). Also, I agree that your point about -fno-builtin etc. means clang can use the intrinsics, and thus the user cannot depend on the overriding happening.

So in a sense, it comes down to whether we want to make the old LTO API behave like the new one does for situations like this. If the answer is no, then I'll abandon this patch. If the answer is yes, then this appears to work, but maybe there is a better way (e.g., a way for the old API to be driven to make the link pass).

tejohnson accepted this revision.Oct 10 2018, 2:53 PM

As discussed on bug, this is already fixed in new LTO API so best to be consistent, regardless of whether users should build with -fno-builtins.

Lgtm

This revision is now accepted and ready to land.Oct 10 2018, 2:53 PM
This revision was automatically updated to reflect the committed changes.

The check lines are:

; Check that the IR contains the overriding definition of the library routine
; in the IR after LTO:
; CHECK_IR: define internal float @logf(float [[X:%.*]])
; CHECK_IR-NEXT:   [[TMP:%.*]] = fadd float [[X]], [[X]]
; CHECK_IR-NEXT:   ret float [[TMP]]

However, the function argument uses an implicit label %0, causing failure of LTO/X86/libcall-overridden-via-alias.ll

; Function Attrs: norecurse nounwind readnone
define internal float @logf(float) #2 {
  %2 = fadd float %0, %0
  ret float %2
}

However, the function argument uses an implicit label %0, causing failure of LTO/X86/libcall-overridden-via-alias.ll

; Function Attrs: norecurse nounwind readnone
define internal float @logf(float) #2 {
  %2 = fadd float %0, %0
  ret float %2
}

I'm not seeing the implicit %0 in local runs, so I'm not seeing this failure. And searching on the bots I couldn't find it either. What I see in my local runs is an explicit parameter %x:

; Function Attrs: norecurse nounwind readnone
define internal float @logf(float %x) #2 {
  %add = fadd float %x, %x
  ret float %add
}

I can change the test to just look for an fadd followed by the ret (and so just not verify that the fadd is adding the formal arg to itself, which isn't critical), but I'm puzzled as to how this is happening. Can you let me know which bot is failing?

You need to do a release build without assertion. Local names like parameters are dropped from IR to save space/memory.
I think you just need to check the parameter name or the implementation in your test case.

Hi Warren, I'm also hitting the same error in our internal test with
optimized build. Would you mind relaxing the checks as you described?
Thanks!

Thanks for explaining, and sorry for the trouble!
Test updated at r344286.