Skip to content

Commit ab70da0

Browse files
committed
Add basic loop fusion pass.
This patch adds a basic loop fusion pass. It will fuse loops that conform to the following 4 conditions: 1. Adjacent (no code between them) 2. Control flow equivalent (if one loop executes, the other loop executes) 3. Identical bounds (both loops iterate the same number of iterations) 4. No negative distance dependencies between the loop bodies. The pass does not make any changes to the IR to create opportunities for fusion. Instead, it checks if the necessary conditions are met and if so it fuses two loops together. The pass has not been added to the pass pipeline yet, and thus is not enabled by default. It can be run stand alone using the -loop-fusion option. Phabricator: https://reviews.llvm.org/D55851 llvm-svn: 358543
1 parent 64c3236 commit ab70da0

File tree

13 files changed

+2283
-0
lines changed

13 files changed

+2283
-0
lines changed

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ void initializeLoopDeletionLegacyPassPass(PassRegistry&);
219219
void initializeLoopDistributeLegacyPass(PassRegistry&);
220220
void initializeLoopExtractorPass(PassRegistry&);
221221
void initializeLoopGuardWideningLegacyPassPass(PassRegistry&);
222+
void initializeLoopFuseLegacyPass(PassRegistry&);
222223
void initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry&);
223224
void initializeLoopInfoWrapperPassPass(PassRegistry&);
224225
void initializeLoopInstSimplifyLegacyPassPass(PassRegistry&);

llvm/include/llvm/Transforms/Scalar.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ FunctionPass *createNaryReassociatePass();
458458
//
459459
FunctionPass *createLoopDistributePass();
460460

461+
//===----------------------------------------------------------------------===//
462+
//
463+
// LoopFuse - Fuse loops.
464+
//
465+
FunctionPass *createLoopFusePass();
466+
461467
//===----------------------------------------------------------------------===//
462468
//
463469
// LoopLoadElimination - Perform loop-aware load elimination.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- LoopFuse.h - Loop Fusion Pass ----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file implements the Loop Fusion pass.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TRANSFORMS_SCALAR_LOOPFUSE_H
15+
#define LLVM_TRANSFORMS_SCALAR_LOOPFUSE_H
16+
17+
#include "llvm/IR/PassManager.h"
18+
19+
namespace llvm {
20+
21+
class Function;
22+
23+
class LoopFusePass : public PassInfoMixin<LoopFusePass> {
24+
public:
25+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
26+
};
27+
28+
} // end namespace llvm
29+
30+
#endif // LLVM_TRANSFORMS_SCALAR_LOOPFUSE_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
123123
#include "llvm/Transforms/Scalar/LoopDeletion.h"
124124
#include "llvm/Transforms/Scalar/LoopDistribute.h"
125+
#include "llvm/Transforms/Scalar/LoopFuse.h"
125126
#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
126127
#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
127128
#include "llvm/Transforms/Scalar/LoopLoadElimination.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ FUNCTION_PASS("partially-inline-libcalls", PartiallyInlineLibCallsPass())
197197
FUNCTION_PASS("lcssa", LCSSAPass())
198198
FUNCTION_PASS("loop-data-prefetch", LoopDataPrefetchPass())
199199
FUNCTION_PASS("loop-load-elim", LoopLoadEliminationPass())
200+
FUNCTION_PASS("loop-fuse", LoopFusePass())
200201
FUNCTION_PASS("loop-distribute", LoopDistributePass())
201202
FUNCTION_PASS("loop-vectorize", LoopVectorizePass())
202203
FUNCTION_PASS("pgo-memop-opt", PGOMemOPSizeOpt())

llvm/lib/Transforms/Scalar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_llvm_library(LLVMScalarOpts
2828
LoopDeletion.cpp
2929
LoopDataPrefetch.cpp
3030
LoopDistribute.cpp
31+
LoopFuse.cpp
3132
LoopIdiomRecognize.cpp
3233
LoopInstSimplify.cpp
3334
LoopInterchange.cpp

0 commit comments

Comments
 (0)