This is an archive of the discontinued LLVM Phabricator instance.

[LLVM] Do not speculate convergent calls
AbandonedPublic

Authored by sameerds on Jun 21 2023, 6:03 AM.

Details

Summary

This is a rebase of D85604 by Nicolai Haehnle <nicolai.haehnle@amd.com>. The
other changes in the original review have already been committed at different
points in time.

Diff Detail

Event Timeline

sameerds created this revision.Jun 21 2023, 6:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2023, 6:03 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
sameerds requested review of this revision.Jun 21 2023, 6:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2023, 6:03 AM
arsenm added inline comments.Jun 21 2023, 6:15 AM
llvm/lib/Analysis/ValueTracking.cpp
6006–6012

Should go through the CallBase rather than separately checking the callee and call site attributes. Also you're missing invokes

llvm/test/Transforms/SimplifyCFG/attr-convergent.ll
46

Add invoke test

nikic added a subscriber: nikic.Jun 21 2023, 6:34 AM

Shouldn't this be covered by the speculatable attribute? A convergent function shouldn't be marked speculatable.

Shouldn't this be covered by the speculatable attribute? A convergent function shouldn't be marked speculatable.

It depends. In the future we could contextually speculate convergent instructions (e.g. if the conditions reaching the point are uniform)

nikic added a comment.EditedJun 21 2023, 6:39 AM

Shouldn't this be covered by the speculatable attribute? A convergent function shouldn't be marked speculatable.

It depends. In the future we could contextually speculate convergent instructions (e.g. if the conditions reaching the point are uniform)

I still think that would not allow you to put speculatable on the function. A speculatable function has to be speculatable unconditionally. Absense of the speculatable attribute does not imply that the call cannot be speculated, just that we don't know that it is always safe to speculate.

It depends. In the future we could contextually speculate convergent instructions (e.g. if the conditions reaching the point are uniform)

I still think that would not allow you to put speculatable on the function. A speculatable function has to be speculatable unconditionally. Absense of the speculatable attribute does not imply that the call cannot be speculated, just that we don't know that it is always safe to speculate.

From a single-threaded point of view, a speculatable function is speculatable unconditionally, because the attribute is only reporting that certain side-effects will not happen. But whether it can be speculated across a divergent branch is decided by whether it is marked convergent. These two things are orthogonal. If a pass wants to speculate a convergent function across a uniform branch (or later even across a divergent branch if permitted by convergence tokesn), then this check for isConvergent will be combined by additional information passed from outside to isSafeToSpeculativelyExecute().

Theoretically, it is plausible that some function may be non-convergent and speculatable, but based on the environment (e.g., HIP or Vulcan or OpenCL), a call to such a function may be convergent. I don't actually have an example of such a thing right now, though it was considered at one point.

arsenm requested changes to this revision.Sep 12 2023, 4:30 AM

Missing invoke/callbr by going through CallInst

This revision now requires changes to proceed.Sep 12 2023, 4:30 AM
sameerds abandoned this revision.Sep 18 2023, 10:58 PM

Abandoning. I agree with relying on attribute speculatable. If we figure out the conditions that make a convergent call speculatable, then at that time, we should use the appropriate attribute to record that fact. On the other hand, if speculation of a convergent call is dependent on the surrounding control flow, then we will probably have to do it as a separate optimization.

llvm/lib/Analysis/ValueTracking.cpp
6006–6012

Just below, all the other "call-like" instructions are simply assumed to be not safe for speculation.