[llvm] [CodeLayout] Size-aware machine block placement (PR #109711)

Kyungwoo Lee via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 12:23:25 PDT 2024


================
@@ -3545,15 +3559,19 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
     }
   }
 
-  // Apply a post-processing optimizing block placement.
-  if (MF.size() >= 3 && EnableExtTspBlockPlacement &&
-      (ApplyExtTspWithoutProfile || MF.getFunction().hasProfileData()) &&
-      MF.size() <= ExtTspBlockPlacementMaxBlocks) {
-    // Find a new placement and modify the layout of the blocks in the function.
-    applyExtTsp();
-
-    // Re-create CFG chain so that we can optimizeBranches and alignBlocks.
-    createCFGChainExtTsp();
+  // Apply a post-processing optimizing block placement:
+  // - find a new placement and modify the layout of the blocks in the function;
+  // - re-create CFG chains so that we can optimizeBranches and alignBlocks.
+  if (MF.size() >= 3) {
+    if (EnableExtTspBlockPlacement &&
+        (ApplyExtTspWithoutProfile || MF.getFunction().hasProfileData()) &&
+        MF.size() <= ExtTspBlockPlacementMaxBlocks) {
----------------
kyulee-com wrote:

It looks like this flag is used to avoid a large function that impacts the build time when running ExtTsp. Can it be a concern when applying for size?
I see we're only interested in `MF.size() >= 3` in this check, but it's also checked from `UseExtTspForSize`. I would say these size constraints should apply for both size and perf case
Similarly, can we set `UseExtTspForPerf` early like below so that we can simplify this part?
```
bool UseExtTspForPerf = false;
bool UseExtTspForSize = false;
bool isWithinSizeRange = (MF.size() >= 3) && (MF.size() <= ExtTspBlockPlacementMaxBlocks);
if (isWithinSizeRange) {
    UseExtTspForPerf = EnableExtTspBlockPlacement &&
                       (ApplyExtTspWithoutProfile || MF.getFunction().hasProfileData());
    UseExtTspForSize = !UseExtTspForPerf && OptForSize && ApplyExtTspForSize;
}
```

Then, this part will be like:
```
if (UseExtTspForPerf || UseExtTspForSize) {
  assert(!(UseExtTspForPerf && UseExtTspForSize) && "Both UseExtTspForPerf and UseExtTspForSize can't be set");
  applyExtTsp(/*OptForSize=*/UseExtTspForSize);
  createCFGChainExtTsp();
}
```

https://github.com/llvm/llvm-project/pull/109711


More information about the llvm-commits mailing list