[PATCH] D130757: Sub-optimal placement of loop exit blocks
Rajesh Thakur via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 29 04:33:06 PDT 2022
rajkrthakur created this revision.
rajkrthakur added reviewers: momchil.velikov, greened, efriedma.
Herald added subscribers: zzheng, hiraditya.
Herald added a project: All.
rajkrthakur requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This draft patch assigns probabilistic metadata to the branch for the required block placement
This resolves https://github.com/llvm/llvm-project/issues/50529
TODO - Add unit tests.
https://reviews.llvm.org/D130757
Files:
llvm/lib/Transforms/Utils/LoopUnroll.cpp
Index: llvm/lib/Transforms/Utils/LoopUnroll.cpp
===================================================================
--- llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -36,6 +36,7 @@
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
+#include "llvm-c/Core.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DiagnosticInfo.h"
@@ -385,6 +386,25 @@
// A conditional branch which exits the loop, which can be optimized to an
// unconditional branch in the unrolled loop in some cases.
bool LatchIsExiting = L->isLoopExiting(LatchBlock);
+
+ if (LatchBI && (LatchBI->isConditional() && LatchIsExiting)) {
+ // Insert block frequency for exit latch. This rearranges the basic blocks
+ // neatly.
+ LLVMMetadataRef branch_weights =
+ LLVMMDStringInContext2(LLVMGetGlobalContext(), "branch_weights", 14);
+ LLVMMetadataRef weight1 =
+ LLVMValueAsMetadata(LLVMConstInt(LLVMIntType(32), 20, 0));
+ LLVMMetadataRef weight2 =
+ LLVMValueAsMetadata(LLVMConstInt(LLVMIntType(32), 80, 0));
+ LLVMMetadataRef mds[] = {branch_weights, weight1, weight2};
+ LLVMMetadataRef metadata =
+ LLVMMDNodeInContext2(LLVMGetGlobalContext(), mds, 3);
+ LLVMValueRef metadata_value =
+ LLVMMetadataAsValue(LLVMGetGlobalContext(), metadata);
+
+ LLVMSetMetadata(wrap(LatchBI), LLVMGetMDKindID("prof", 4), metadata_value);
+ }
+
if (!LatchBI || (LatchBI->isConditional() && !LatchIsExiting)) {
LLVM_DEBUG(
dbgs() << "Can't unroll; a conditional latch must exit the loop");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130757.448577.patch
Type: text/x-patch
Size: 1655 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220729/19174ddb/attachment.bin>
More information about the llvm-commits
mailing list