<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div style="font-size: 12pt; font-family: arial, helvetica, sans-serif; color: rgb(0, 0, 0);"><div>

<div id="OLK_SRC_BODY_SECTION">
<div id="OLK_SRC_BODY_SECTION">
<blockquote style="margin: 0 0 0 .8em; border-left: 1px #ccc solid; padding-left: 1em;"><br>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"><span style="font-size: small;">>I<span style="font-size: 11pt;">s there a way to tell clang to always generate the code as the first function, or perhaps a llvm pass that converts the conditional branch/load into a select or hoists the load before/after the branch, also using a select?</span></span></div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"> </div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;">Have you tried enabling either the EarlyIfConverter pass (for selects/conditional-moves) or EarlyIfPredicator pass (for predicated instructions) in the LLVM backend? If you are writing a downstream target, you will need to implement a few TargetInstrInfo APIs to get either pass up and running. Some in-tree backends use the former pass (e.g., AArch64, PPC). I use the latter in a downstream backend with reasonable results.</div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"> </div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;">O we don't have them active. Will start from these, many thanks!</div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"> <span style="font-size: small;"> <br></span></div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"><span style="font-size: small;"><span style="font-size: 11pt;"> </span></span></div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"><span style="font-size: small;"><span style="font-size: 11pt;">There is also an IR-level speculator in SimplifyCFG that may catch some of your opportunities (see caveats):<br></span></span></div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"><span style="font-size: small;"><span style="font-size: 11pt;"> </span></span></div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;">/// Speculate a conditional basic block flattening the CFG.
<div>///</div>
<div>/// Note that this is a very risky transform currently. Speculating</div>
<div>/// instructions like this is most often not desirable. Instead, there is an MI</div>
<div>/// pass which can do it with full awareness of the resource constraints.</div>
<div>/// However, some cases are "obvious" and we should do directly. An example of</div>
<div>/// this is speculating a single, reasonably cheap instruction.</div>
<div>///</div>
<div>/// There is only one distinct advantage to flattening the CFG at the IR level:</div>
<div>/// it makes very common but simplistic optimizations such as are common in</div>
<div>/// instcombine and the DAG combiner more powerful by removing CFG edges and</div>
<div>/// modeling their effects with easier to reason about SSA value graphs.</div>
<div>///</div>
<div>///</div>
<div>/// An illustration of this transform is turning this IR:</div>
<div>/// \code</div>
<div>///   BB:</div>
<div>///     %cmp = icmp ult %x, %y</div>
<div>///     br i1 %cmp, label %EndBB, label %ThenBB</div>
<div>///   ThenBB:</div>
<div>///     %sub = sub %x, %y</div>
<div>///     br label BB2</div>
<div>///   EndBB:</div>
<div>///     %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ]</div>
<div>///     ...</div>
<div>/// \endcode</div>
<div>///</div>
<div>/// Into this IR:</div>
<div>/// \code</div>
<div>///   BB:</div>
<div>///     %cmp = icmp ult %x, %y</div>
<div>///     %sub = sub %x, %y</div>
<div>///     %cond = select i1 %cmp, 0, %sub</div>
<div>///     ...</div>
<div>/// \endcode</div>
<div>///</div>
<div>/// \returns true if the conditional block is removed.</div>
<div>bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,</div>
                                            const TargetTransformInfo </div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;"> </div>
<div style="font-family: 'calibri' , 'arial' , 'helvetica' , sans-serif; font-size: 12pt; color: #000000;">This also seems interesting to our target. Many thanks x2 ! =)</div>
<div id="appendonsend"></div>
<p id="fb_identificator"></p>
<p> </p>
<div id="content_out_dsampaio_kalray.eu"></div><hr style="display: inline-block; width: 98%;">
<div id="divRplyFwdMsg" dir="ltr"><span style="font-size: 11pt; color: #000000; font-family: Calibri, sans-serif;"><strong>From:</strong> llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of Diogo Sampaio via llvm-dev <llvm-dev@lists.llvm.org><br><strong>Sent:</strong> Wednesday, August 18, 2021 3:45 PM<br><strong>To:</strong> cfe-dev@lists.llvm.org <cfe-dev@lists.llvm.org>; llvm-dev <llvm-dev@lists.llvm.org><br><strong>Subject:</strong> [llvm-dev] Canonicalizing conditional loads</span>
<div> </div>
</div>
<div class="BodyFragment">
<div class="PlainText">External email: Use caution opening links or attachments<br><br><br>Hi,<br>our target machine has conditional (predicated) load instructions. However it is hard to generate at dag-select<br>as clang emits conditional branches to avoid performing the load in most cases.<br>e.g.:https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgodbolt.org%2Fz%2FohKTnqeqz&amp;data=04%7C01%7Cjeckhardt%40nvidia.com%7C6b309028a3f646dc312c08d962892a54%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637649163524599564%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=f9eSwLPRl7pZ1%2FzXAL5i%2B8dzPkwifT9Fk6FvIS7X6WA%3D&amp;reserved=0<br>All these functions perform the same thing, but only the first one does not add a conditional branch instruction.<br><br>Is there a way to tell clang to always generate the code as the first function, or perhaps a llvm pass that converts<br>the conditional branch/load into a select or hoists the load before/after the branch, also using a select?<br><br>If not, would anyone else be interested in such thing? Any ideas for which would be the best solution?<br><br><br>Regards,<br>Diogo Sampaio<br><br><br><br><br>_______________________________________________<br>LLVM Developers mailing list<br>llvm-dev@lists.llvm.org<br><a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.llvm.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fllvm-dev&amp;data=04%7C01%7Cjeckhardt%40nvidia.com%7C6b309028a3f646dc312c08d962892a54%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637649163524599564%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=Qdl1GdBO9QXdAGG6cQu6kvKRD2WELiVHa9eMIQ4Ubeo%3D&amp;reserved=0" target="_blank" rel="noopener noreferrer">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.llvm.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fllvm-dev&amp;data=04%7C01%7Cjeckhardt%40nvidia.com%7C6b309028a3f646dc312c08d962892a54%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637649163524599564%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=Qdl1GdBO9QXdAGG6cQu6kvKRD2WELiVHa9eMIQ4Ubeo%3D&amp;reserved=0</a></div>
</div>
</blockquote>
</div>
</div></div></div></body></html>