Skip to content

[SandboxVec] Add region-from-bbs helper pass #130153

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 1 commit into from
Mar 8, 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
1 change: 1 addition & 0 deletions llvm/include/llvm/SandboxIR/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Region {
void remove(Instruction *I);
friend class Context; // The callbacks need to call add() and remove().
friend class RegionInternalsAttorney; // For unit tests.
friend class RegionsFromBBs; // For add().

/// Set \p I as the \p Idx'th element in the auxiliary vector.
/// NOTE: This is for internal use, it does not set the metadata.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===- RegionsFromBBs.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
//
//===----------------------------------------------------------------------===//
//
// A SandboxIR function pass that builds one region per BB and then runs a
// pipeline of region passes on them. This is useful to test region passes in
// isolation without relying on the output of other vectorizer components.
//

#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_REGIONSFROMBBS_H
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_REGIONSFROMBBS_H

#include "llvm/ADT/StringRef.h"
#include "llvm/SandboxIR/Pass.h"
#include "llvm/SandboxIR/PassManager.h"

namespace llvm::sandboxir {

class RegionsFromBBs final : public FunctionPass {
// The PM containing the pipeline of region passes.
RegionPassManager RPM;

public:
RegionsFromBBs(StringRef Pipeline);
bool runOnFunction(Function &F, const Analyses &A) final;
void printPipeline(raw_ostream &OS) const final {
OS << getName() << "\n";
RPM.printPipeline(OS);
}
};

} // namespace llvm::sandboxir

#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_REGIONSFROMBBS_H
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Vectorize/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_llvm_component_library(LLVMVectorize
SandboxVectorizer/Interval.cpp
SandboxVectorizer/Legality.cpp
SandboxVectorizer/Passes/BottomUpVec.cpp
SandboxVectorizer/Passes/RegionsFromBBs.cpp
SandboxVectorizer/Passes/RegionsFromMetadata.cpp
SandboxVectorizer/Passes/SeedCollection.cpp
SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ REGION_PASS("bottom-up-vec", ::llvm::sandboxir::BottomUpVec)
#endif

FUNCTION_PASS_WITH_PARAMS("seed-collection", ::llvm::sandboxir::SeedCollection)
FUNCTION_PASS_WITH_PARAMS("regions-from-bbs", ::llvm::sandboxir::RegionsFromBBs)
FUNCTION_PASS_WITH_PARAMS("regions-from-metadata", ::llvm::sandboxir::RegionsFromMetadata)

#undef FUNCTION_PASS_WITH_PARAMS
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===- RegionsFromBBs.cpp - A helper to test RegionPasses -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/RegionsFromBBs.h"
#include "llvm/SandboxIR/Function.h"
#include "llvm/SandboxIR/Region.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerPassBuilder.h"

namespace llvm::sandboxir {

RegionsFromBBs::RegionsFromBBs(StringRef Pipeline)
: FunctionPass("regions-from-bbs"),
RPM("rpm", Pipeline, SandboxVectorizerPassBuilder::createRegionPass) {}

bool RegionsFromBBs::runOnFunction(Function &F, const Analyses &A) {
SmallVector<std::unique_ptr<Region>, 16> Regions;
// Create a region for each BB.
for (BasicBlock &BB : F) {
Regions.push_back(std::make_unique<Region>(F.getContext(), A.getTTI()));
auto &RgnPtr = Regions.back();
for (Instruction &I : BB)
RgnPtr->add(&I);
}
// For each region run the region pass pipeline.
for (auto &RgnPtr : Regions)
RPM.runOnRegion(*RgnPtr, A);
return false;
}

} // namespace llvm::sandboxir
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCount.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/RegionsFromBBs.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/RegionsFromMetadata.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/SeedCollection.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.h"
Expand Down
31 changes: 31 additions & 0 deletions llvm/test/Transforms/SandboxVectorizer/regions-from-bbs.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt --passes=sandbox-vectorizer -sbvec-passes='regions-from-bbs<null>' %s -S | FileCheck %s

define void @foo(i8 %v) {
; CHECK-LABEL: define void @foo(
; CHECK-SAME: i8 [[V:%.*]]) {
; CHECK-NEXT: [[BBA:.*:]]
; CHECK-NEXT: [[ADD0:%.*]] = add i8 [[V]], 0, !sandboxvec [[META0:![0-9]+]]
; CHECK-NEXT: br label %[[BBB:.*]], !sandboxvec [[META0]]
; CHECK: [[BBB]]:
; CHECK-NEXT: [[ADD1:%.*]] = add i8 [[V]], 1, !sandboxvec [[META1:![0-9]+]]
; CHECK-NEXT: br label %[[BBC:.*]], !sandboxvec [[META1]]
; CHECK: [[BBC]]:
; CHECK-NEXT: ret void, !sandboxvec [[META2:![0-9]+]]
;
bbA:
%add0 = add i8 %v, 0
br label %bbB

bbB:
%add1 = add i8 %v, 1
br label %bbC

bbC:
ret void
}
;.
; CHECK: [[META0]] = distinct !{!"sandboxregion"}
; CHECK: [[META1]] = distinct !{!"sandboxregion"}
; CHECK: [[META2]] = distinct !{!"sandboxregion"}
;.
Loading