<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2">>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></font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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. <font size="2">
<br>
</font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2"><span style="font-size:11pt"><br>
</span></font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2"><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></font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2"><span style="font-size:11pt"><br>
</span></font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2"><span style="font-size:11pt"></span></font>/// 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 <br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of Diogo Sampaio via llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Sent:</b> Wednesday, August 18, 2021 3:45 PM<br>
<b>To:</b> cfe-dev@lists.llvm.org <cfe-dev@lists.llvm.org>; llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [llvm-dev] Canonicalizing conditional loads</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<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">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><br>
</div>
</span></font></div>
</body>
</html>