Skip to content

[CodeGen][NPM] Port BranchRelaxation to NPM #130067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions llvm/include/llvm/CodeGen/BranchRelaxation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===- llvm/CodeGen/BranchRelaxation.h --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_BRANCHRELAXATION_H
#define LLVM_CODEGEN_BRANCHRELAXATION_H

#include "llvm/CodeGen/MachinePassManager.h"

namespace llvm {

class BranchRelaxationPass : public PassInfoMixin<BranchRelaxationPass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
static bool isRequired() { return true; }
};

} // namespace llvm

#endif // LLVM_CODEGEN_BRANCHRELAXATION_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void initializeBasicAAWrapperPassPass(PassRegistry &);
void initializeBlockFrequencyInfoWrapperPassPass(PassRegistry &);
void initializeBranchFolderLegacyPass(PassRegistry &);
void initializeBranchProbabilityInfoWrapperPassPass(PassRegistry &);
void initializeBranchRelaxationPass(PassRegistry &);
void initializeBranchRelaxationLegacyPass(PassRegistry &);
void initializeBreakCriticalEdgesPass(PassRegistry &);
void initializeBreakFalseDepsPass(PassRegistry &);
void initializeCanonicalizeFreezeInLoopsPass(PassRegistry &);
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
#ifndef MACHINE_FUNCTION_PASS
#define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS)
#endif
MACHINE_FUNCTION_PASS("branch-relaxation", BranchRelaxationPass())
MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
MACHINE_FUNCTION_PASS("detect-dead-lanes", DetectDeadLanesPass())
MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass())
Expand Down
32 changes: 25 additions & 7 deletions llvm/lib/CodeGen/BranchRelaxation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/BranchRelaxation.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/LivePhysRegs.h"
Expand Down Expand Up @@ -44,7 +45,7 @@ STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed");

namespace {

class BranchRelaxation : public MachineFunctionPass {
class BranchRelaxation {
/// BasicBlockInfo - Information about the offset and size of a single
/// basic block.
struct BasicBlockInfo {
Expand Down Expand Up @@ -115,23 +116,31 @@ class BranchRelaxation : public MachineFunctionPass {
void dumpBBs();
void verify();

public:
bool run(MachineFunction &MF);
};

class BranchRelaxationLegacy : public MachineFunctionPass {
public:
static char ID;

BranchRelaxation() : MachineFunctionPass(ID) {}
BranchRelaxationLegacy() : MachineFunctionPass(ID) {}

bool runOnMachineFunction(MachineFunction &MF) override;
bool runOnMachineFunction(MachineFunction &MF) override {
return BranchRelaxation().run(MF);
}

StringRef getPassName() const override { return BRANCH_RELAX_NAME; }
};

} // end anonymous namespace

char BranchRelaxation::ID = 0;
char BranchRelaxationLegacy::ID = 0;

char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
char &llvm::BranchRelaxationPassID = BranchRelaxationLegacy::ID;

INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
INITIALIZE_PASS(BranchRelaxationLegacy, DEBUG_TYPE, BRANCH_RELAX_NAME, false,
false)

/// verify - check BBOffsets, BBSizes, alignment of islands
void BranchRelaxation::verify() {
Expand Down Expand Up @@ -744,7 +753,16 @@ bool BranchRelaxation::relaxBranchInstructions() {
return Changed;
}

bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
PreservedAnalyses
BranchRelaxationPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
if (!BranchRelaxation().run(MF))
return PreservedAnalyses::all();

return getMachineFunctionPassPreservedAnalyses();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure more can be preserved, but it's so late it likely doesn't matter

}

bool BranchRelaxation::run(MachineFunction &mf) {
MF = &mf;

LLVM_DEBUG(dbgs() << "***** BranchRelaxation *****\n");
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeBasicBlockPathCloningPass(Registry);
initializeBasicBlockSectionsPass(Registry);
initializeBranchFolderLegacyPass(Registry);
initializeBranchRelaxationPass(Registry);
initializeBranchRelaxationLegacyPass(Registry);
initializeBreakFalseDepsPass(Registry);
initializeCallBrPreparePass(Registry);
initializeCFGuardLongjmpPass(Registry);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#include "llvm/CodeGen/AtomicExpand.h"
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
#include "llvm/CodeGen/BranchFoldingPass.h"
#include "llvm/CodeGen/BranchRelaxation.h"
#include "llvm/CodeGen/CallBrPrepare.h"
#include "llvm/CodeGen/CodeGenPrepare.h"
#include "llvm/CodeGen/ComplexDeinterleavingPass.h"
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "llvm/Analysis/KernelInfo.h"
#include "llvm/Analysis/UniformityAnalysis.h"
#include "llvm/CodeGen/AtomicExpand.h"
#include "llvm/CodeGen/BranchRelaxation.h"
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
Expand Down Expand Up @@ -2201,7 +2202,7 @@ void AMDGPUCodeGenPassBuilder::addPreEmitPass(AddMachinePass &addPass) const {
addPass(AMDGPUInsertDelayAluPass());
}

// TODO: addPass(BranchRelaxationPass());
addPass(BranchRelaxationPass());
}

bool AMDGPUCodeGenPassBuilder::isPassEnabled(const cl::opt<bool> &Opt,
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/AArch64/branch-relax-block-size.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# REQUIRES: asserts
# RUN: llc -mtriple=aarch64--linux-gnu -run-pass=branch-relaxation -debug-only=branch-relaxation %s -o /dev/null 2>&1 | FileCheck %s
# RUN: llc -mtriple=aarch64--linux-gnu -passes=branch-relaxation -debug-only=branch-relaxation %s -o /dev/null 2>&1 | FileCheck %s

# Ensure meta instructions (e.g. CFI_INSTRUCTION) don't contribute to the code
# size of a basic block.
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/AArch64/branch-relax-cross-section.mir
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass branch-relaxation -aarch64-b-offset-bits=64 -aarch64-tbz-offset-bits=9 -aarch64-cbz-offset-bits=9 %s -o - | FileCheck %s
# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass branch-relaxation -aarch64-tbz-offset-bits=9 -aarch64-cbz-offset-bits=9 %s -o - | FileCheck --check-prefix=INDIRECT %s

# RUN: llc -mtriple=aarch64-none-linux-gnu -passes branch-relaxation -aarch64-tbz-offset-bits=9 -aarch64-cbz-offset-bits=9 %s -o - | FileCheck --check-prefix=INDIRECT %s

--- |
declare i32 @bar()
declare i32 @baz()
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/AMDGPU/branch-relax-no-terminators.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -verify-machineinstrs -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a --amdgpu-s-branch-bits=5 -run-pass branch-relaxation %s -o - | FileCheck %s
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a --amdgpu-s-branch-bits=5 -passes=branch-relaxation %s -o - | FileCheck %s

---
name: branch_no_terminators
Expand Down