[llvm] [SandboxVec] Add BottomUpVec test flag to build regions from metadata. (PR #111904)
Jorge Gorbe Moya via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 10 16:50:57 PDT 2024
https://github.com/slackito updated https://github.com/llvm/llvm-project/pull/111904
>From 61453a016f2a1b956a01281e52860147274eb788 Mon Sep 17 00:00:00 2001
From: Jorge Gorbe Moya <jgorbe at google.com>
Date: Thu, 10 Oct 2024 12:31:49 -0700
Subject: [PATCH 1/2] [SandboxVec] Add BottomUpVec test flag to build regions
from metadata.
This allows us to write lit tests for region passes where regions are
specified by metadata in textual IR. See added test file for an example.
---
.../Passes/PrintInstructionCountPass.h | 24 +++++++++++++++++++
.../SandboxVectorizer/Passes/BottomUpVec.cpp | 15 ++++++++++++
.../SandboxVectorizer/Passes/PassRegistry.def | 1 +
.../regions-from-metadata.ll | 16 +++++++++++++
4 files changed, 56 insertions(+)
create mode 100644 llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h
create mode 100644 llvm/test/Transforms/SandboxVectorizer/regions-from-metadata.ll
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h
new file mode 100644
index 00000000000000..d8cfdcb672ccce
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h
@@ -0,0 +1,24 @@
+#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H
+#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H
+
+#include "llvm/SandboxIR/Pass.h"
+
+namespace llvm::sandboxir {
+
+class Region;
+
+/// A Region pass that prints the instruction count for the region to stdout.
+/// Used to test -sbvec-passes while we don't have any actual optimization
+/// passes.
+class PrintInstructionCountPass final : public RegionPass {
+public:
+ PrintInstructionCountPass() : RegionPass("null") {}
+ bool runOnRegion(Region &R) final {
+ outs() << "InstructionCount: " << std::distance(R.begin(), R.end()) << "\n";
+ return false;
+ }
+};
+
+} // namespace llvm::sandboxir
+
+#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp
index 77198f932a3ec0..7743f5c50ff913 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp
@@ -10,8 +10,10 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/SandboxIR/Function.h"
#include "llvm/SandboxIR/Instruction.h"
+#include "llvm/SandboxIR/Region.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
+#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h"
namespace llvm::sandboxir {
@@ -19,6 +21,11 @@ static cl::opt<bool>
PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
cl::desc("Prints the pass pipeline and returns."));
+static cl::opt<bool> UseRegionsFromMetadata(
+ "sbvec-use-regions-from-metadata", cl::init(false), cl::Hidden,
+ cl::desc("Skips bottom-up vectorization, builds regions from metadata "
+ "already present in the IR and runs the region pass pipeline."));
+
/// A magic string for the default pass pipeline.
static const char *DefaultPipelineMagicStr = "*";
@@ -86,6 +93,14 @@ bool BottomUpVec::runOnFunction(Function &F) {
RPM.printPipeline(outs());
return false;
}
+ if (UseRegionsFromMetadata) {
+ SmallVector<std::unique_ptr<Region>> Regions =
+ Region::createRegionsFromMD(F);
+ for (auto &R : Regions) {
+ RPM.runOnRegion(*R);
+ }
+ return false;
+ }
Change = false;
// TODO: Start from innermost BBs first
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/PassRegistry.def b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/PassRegistry.def
index bbb0dcba1ea516..c0ad1710d06b91 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/PassRegistry.def
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/PassRegistry.def
@@ -18,5 +18,6 @@
#endif
REGION_PASS("null", NullPass())
+REGION_PASS("print-instruction-count", PrintInstructionCountPass())
#undef REGION_PASS
diff --git a/llvm/test/Transforms/SandboxVectorizer/regions-from-metadata.ll b/llvm/test/Transforms/SandboxVectorizer/regions-from-metadata.ll
new file mode 100644
index 00000000000000..3874876996f8c0
--- /dev/null
+++ b/llvm/test/Transforms/SandboxVectorizer/regions-from-metadata.ll
@@ -0,0 +1,16 @@
+; RUN: opt -disable-output --passes=sandbox-vectorizer \
+; RUN: -sbvec-passes=print-instruction-count \
+; RUN: -sbvec-use-regions-from-metadata %s | FileCheck %s
+
+define i8 @foo(i8 %v0, i8 %v1) {
+ %t0 = add i8 %v0, 1, !sandboxvec !0
+ %t1 = add i8 %t0, %v1, !sandboxvec !1
+ %t2 = add i8 %t1, %v1, !sandboxvec !1
+ ret i8 %t2
+}
+
+!0 = distinct !{!"sandboxregion"}
+!1 = distinct !{!"sandboxregion"}
+
+; CHECK: InstructionCount: 1
+; CHECK: InstructionCount: 2
>From 10b12d8b052902ea40b792cb125ab27ac352db28 Mon Sep 17 00:00:00 2001
From: Jorge Gorbe Moya <jgorbe at google.com>
Date: Thu, 10 Oct 2024 16:48:37 -0700
Subject: [PATCH 2/2] Move pass pipeline and region creation logic to the main
SandboxVectorizer class.
---
.../SandboxVectorizer/Passes/BottomUpVec.h | 5 +-
.../Passes/PrintInstructionCountPass.h | 3 +-
.../SandboxVectorizer/SandboxVectorizer.h | 5 ++
.../SandboxVectorizer/Passes/BottomUpVec.cpp | 51 +--------------
.../SandboxVectorizer/SandboxVectorizer.cpp | 64 ++++++++++++++++++-
5 files changed, 72 insertions(+), 56 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h
index 02abdf0a1ef0df..9ad61dbc03572f 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h
@@ -29,10 +29,11 @@ class BottomUpVec final : public FunctionPass {
void tryVectorize(ArrayRef<Value *> Seeds);
// The PM containing the pipeline of region passes.
- RegionPassManager RPM;
+ // TODO: Remove maybe_unused once we build regions to run passes on.
+ [[maybe_unused]] RegionPassManager *RPM;
public:
- BottomUpVec();
+ BottomUpVec(RegionPassManager *RPM);
bool runOnFunction(Function &F) final;
};
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h
index d8cfdcb672ccce..c4e97caf11aeb7 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h
@@ -2,11 +2,10 @@
#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_PRINTINSTRUCTIONCOUNTPASS_H
#include "llvm/SandboxIR/Pass.h"
+#include "llvm/SandboxIR/Region.h"
namespace llvm::sandboxir {
-class Region;
-
/// A Region pass that prints the instruction count for the region to stdout.
/// Used to test -sbvec-passes while we don't have any actual optimization
/// passes.
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.h
index b7cb418c00326a..ad11046db43b27 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.h
@@ -20,6 +20,11 @@ class TargetTransformInfo;
class SandboxVectorizerPass : public PassInfoMixin<SandboxVectorizerPass> {
TargetTransformInfo *TTI = nullptr;
+ // A pipeline of region passes. Typically, this will be run by BottomUpVec on
+ // each region it creates, but it can also be run on regions created from
+ // IR metadata in tests.
+ sandboxir::RegionPassManager RPM;
+
// The main vectorizer pass.
sandboxir::BottomUpVec BottomUpVecPass;
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp
index 7743f5c50ff913..ba1dc8e8cbbf6d 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp
@@ -12,45 +12,11 @@
#include "llvm/SandboxIR/Instruction.h"
#include "llvm/SandboxIR/Region.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
-#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h"
namespace llvm::sandboxir {
-static cl::opt<bool>
- PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
- cl::desc("Prints the pass pipeline and returns."));
-
-static cl::opt<bool> UseRegionsFromMetadata(
- "sbvec-use-regions-from-metadata", cl::init(false), cl::Hidden,
- cl::desc("Skips bottom-up vectorization, builds regions from metadata "
- "already present in the IR and runs the region pass pipeline."));
-
-/// A magic string for the default pass pipeline.
-static const char *DefaultPipelineMagicStr = "*";
-
-static cl::opt<std::string> UserDefinedPassPipeline(
- "sbvec-passes", cl::init(DefaultPipelineMagicStr), cl::Hidden,
- cl::desc("Comma-separated list of vectorizer passes. If not set "
- "we run the predefined pipeline."));
-
-static std::unique_ptr<RegionPass> createRegionPass(StringRef Name) {
-#define REGION_PASS(NAME, CREATE_PASS) \
- if (Name == NAME) \
- return std::make_unique<decltype(CREATE_PASS)>(CREATE_PASS);
-#include "PassRegistry.def"
- return nullptr;
-}
-
-BottomUpVec::BottomUpVec() : FunctionPass("bottom-up-vec"), RPM("rpm") {
- // Create a pipeline to be run on each Region created by BottomUpVec.
- if (UserDefinedPassPipeline == DefaultPipelineMagicStr) {
- // TODO: Add default passes to RPM.
- } else {
- // Create the user-defined pipeline.
- RPM.setPassPipeline(UserDefinedPassPipeline, createRegionPass);
- }
-}
+BottomUpVec::BottomUpVec(RegionPassManager *RPM)
+ : FunctionPass("bottom-up-vec"), RPM(RPM) {}
// TODO: This is a temporary function that returns some seeds.
// Replace this with SeedCollector's function when it lands.
@@ -89,19 +55,6 @@ void BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl) {
void BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) { vectorizeRec(Bndl); }
bool BottomUpVec::runOnFunction(Function &F) {
- if (PrintPassPipeline) {
- RPM.printPipeline(outs());
- return false;
- }
- if (UseRegionsFromMetadata) {
- SmallVector<std::unique_ptr<Region>> Regions =
- Region::createRegionsFromMD(F);
- for (auto &R : Regions) {
- RPM.runOnRegion(*R);
- }
- return false;
- }
-
Change = false;
// TODO: Start from innermost BBs first
for (auto &BB : F) {
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp
index ba4899cc624e99..60867207155f50 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp
@@ -10,13 +10,55 @@
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/SandboxIR/Constant.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h"
+#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/NullPass.h"
+#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/PrintInstructionCountPass.h"
-using namespace llvm;
+using namespace llvm::sandboxir;
+
+namespace llvm {
#define SV_NAME "sandbox-vectorizer"
#define DEBUG_TYPE SV_NAME
-SandboxVectorizerPass::SandboxVectorizerPass() = default;
+static cl::opt<bool>
+ PrintPassPipeline("sbvec-print-pass-pipeline", cl::init(false), cl::Hidden,
+ cl::desc("Prints the pass pipeline and returns."));
+
+/// A magic string for the default pass pipeline.
+static const char *DefaultPipelineMagicStr = "*";
+
+static cl::opt<std::string> UserDefinedPassPipeline(
+ "sbvec-passes", cl::init(DefaultPipelineMagicStr), cl::Hidden,
+ cl::desc("Comma-separated list of vectorizer passes. If not set "
+ "we run the predefined pipeline."));
+
+static cl::opt<bool> UseRegionsFromMetadata(
+ "sbvec-use-regions-from-metadata", cl::init(false), cl::Hidden,
+ cl::desc("Skips bottom-up vectorization, builds regions from metadata "
+ "already present in the IR and runs the region pass pipeline."));
+
+static std::unique_ptr<sandboxir::RegionPass> createRegionPass(StringRef Name) {
+#define REGION_PASS(NAME, CREATE_PASS) \
+ if (Name == NAME) \
+ return std::make_unique<decltype(CREATE_PASS)>(CREATE_PASS);
+#include "Passes/PassRegistry.def"
+ return nullptr;
+}
+
+sandboxir::RegionPassManager createRegionPassManager() {
+ sandboxir::RegionPassManager RPM("rpm");
+ // Create a pipeline to be run on each Region created by BottomUpVec.
+ if (UserDefinedPassPipeline == DefaultPipelineMagicStr) {
+ // TODO: Add default passes to RPM.
+ } else {
+ // Create the user-defined pipeline.
+ RPM.setPassPipeline(UserDefinedPassPipeline, createRegionPass);
+ }
+ return RPM;
+}
+
+SandboxVectorizerPass::SandboxVectorizerPass()
+ : RPM(createRegionPassManager()), BottomUpVecPass(&RPM) {}
SandboxVectorizerPass::SandboxVectorizerPass(SandboxVectorizerPass &&) =
default;
@@ -37,6 +79,11 @@ PreservedAnalyses SandboxVectorizerPass::run(Function &F,
}
bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
+ if (PrintPassPipeline) {
+ RPM.printPipeline(outs());
+ return false;
+ }
+
// If the target claims to have no vector registers early return.
if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
LLVM_DEBUG(dbgs() << "SBVec: Target has no vector registers, return.\n");
@@ -52,5 +99,16 @@ bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
// Create SandboxIR for LLVMF and run BottomUpVec on it.
sandboxir::Context Ctx(LLVMF.getContext());
sandboxir::Function &F = *Ctx.createFunction(&LLVMF);
- return BottomUpVecPass.runOnFunction(F);
+ if (UseRegionsFromMetadata) {
+ SmallVector<std::unique_ptr<sandboxir::Region>> Regions =
+ sandboxir::Region::createRegionsFromMD(F);
+ for (auto &R : Regions) {
+ RPM.runOnRegion(*R);
+ }
+ return false;
+ } else {
+ return BottomUpVecPass.runOnFunction(F);
+ }
}
+
+} // namespace llvm
More information about the llvm-commits
mailing list