[llvm] [SandboxVec] Boilerplate (PR #107431)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 5 10:01:18 PDT 2024


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/107431

This patch implements the new pass and registers it with the pass manager. For context, this is a vectorizer that operates on Sandbox IR, which is a transactional IR on top of LLVM IR.

>From cc0b60c13efdfa1bd1a6760c046727525d2d38a9 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Thu, 5 Sep 2024 09:09:45 -0700
Subject: [PATCH] [SandboxVec] Boilerplate

This patch implements the new pass and registers it with the pass manager.
For context, this is a vectorizer that operates on Sandbox IR, which is a
transactional IR on top of LLVM IR.
---
 .../Vectorize/SandboxVec/SandboxVectorizer.h  | 35 +++++++++++
 llvm/lib/Passes/CMakeLists.txt                |  1 +
 llvm/lib/Passes/PassBuilder.cpp               |  1 +
 llvm/lib/Passes/PassRegistry.def              |  1 +
 llvm/lib/Transforms/Vectorize/CMakeLists.txt  |  3 +
 .../Vectorize/SandboxVec/CMakeLists.txt       | 16 +++++
 .../SandboxVec/SandboxVectorizer.cpp          | 63 +++++++++++++++++++
 .../test/Transforms/SandboxVec/boilerplate.ll | 11 ++++
 8 files changed, 131 insertions(+)
 create mode 100644 llvm/include/llvm/Transforms/Vectorize/SandboxVec/SandboxVectorizer.h
 create mode 100644 llvm/lib/Transforms/Vectorize/SandboxVec/CMakeLists.txt
 create mode 100644 llvm/lib/Transforms/Vectorize/SandboxVec/SandboxVectorizer.cpp
 create mode 100644 llvm/test/Transforms/SandboxVec/boilerplate.ll

diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVec/SandboxVectorizer.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVec/SandboxVectorizer.h
new file mode 100644
index 00000000000000..0f7e43112d2d88
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVec/SandboxVectorizer.h
@@ -0,0 +1,35 @@
+//===- SandboxVectorizer.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_TRANSFORMS_VECTORIZE_SANDBOXVEC_SANDBOXVECTORIZER_H
+#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVEC_SANDBOXVECTORIZER_H
+
+#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/ScalarEvolution.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/SandboxIR/SandboxIR.h"
+
+namespace llvm {
+
+class SandboxVectorizerPass : public PassInfoMixin<SandboxVectorizerPass> {
+  DenseSet<sandboxir::Value *> Visited;
+  ScalarEvolution *SE = nullptr;
+  const DataLayout *DL = nullptr;
+  TargetTransformInfo *TTI = nullptr;
+  AliasAnalysis *AA = nullptr;
+
+public:
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
+
+  bool runImpl(Function &F);
+};
+
+} // namespace llvm
+
+#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVEC_SANDBOXVECTORIZER_H
diff --git a/llvm/lib/Passes/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt
index 6425f4934b2103..a90b196b0b0292 100644
--- a/llvm/lib/Passes/CMakeLists.txt
+++ b/llvm/lib/Passes/CMakeLists.txt
@@ -26,6 +26,7 @@ add_llvm_component_library(LLVMPasses
   InstCombine
   IRPrinter
   ObjCARC
+  SandboxVec
   Scalar
   Support
   Target
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index a22abed8051a11..80916b8a9502a0 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -321,6 +321,7 @@
 #include "llvm/Transforms/Vectorize/LoopIdiomVectorize.h"
 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
+#include "llvm/Transforms/Vectorize/SandboxVec/SandboxVectorizer.h"
 #include "llvm/Transforms/Vectorize/VectorCombine.h"
 #include <optional>
 
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index d6067089c6b5c1..a1324e81705669 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -453,6 +453,7 @@ FUNCTION_PASS("reassociate", ReassociatePass())
 FUNCTION_PASS("redundant-dbg-inst-elim", RedundantDbgInstEliminationPass())
 FUNCTION_PASS("reg2mem", RegToMemPass())
 FUNCTION_PASS("safe-stack", SafeStackPass(TM))
+FUNCTION_PASS("sandbox-vectorizer", SandboxVectorizerPass())
 FUNCTION_PASS("scalarize-masked-mem-intrin", ScalarizeMaskedMemIntrinPass())
 FUNCTION_PASS("scalarizer", ScalarizerPass())
 FUNCTION_PASS("sccp", SCCPPass())
diff --git a/llvm/lib/Transforms/Vectorize/CMakeLists.txt b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
index 5c88d94d96622d..cabddd7a8a8e0d 100644
--- a/llvm/lib/Transforms/Vectorize/CMakeLists.txt
+++ b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
@@ -1,3 +1,5 @@
+add_subdirectory(SandboxVec)
+
 add_llvm_component_library(LLVMVectorize
   LoadStoreVectorizer.cpp
   LoopIdiomVectorize.cpp
@@ -27,4 +29,5 @@ add_llvm_component_library(LLVMVectorize
   Core
   Support
   TransformUtils
+  SandboxVec
   )
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVec/CMakeLists.txt b/llvm/lib/Transforms/Vectorize/SandboxVec/CMakeLists.txt
new file mode 100644
index 00000000000000..ccf0c8c2d5da2d
--- /dev/null
+++ b/llvm/lib/Transforms/Vectorize/SandboxVec/CMakeLists.txt
@@ -0,0 +1,16 @@
+add_llvm_component_library(LLVMSandboxVec
+  SandboxVectorizer.cpp
+
+  ADDITIONAL_HEADER_DIRS
+  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms/Vectorize/SandboxVec
+
+  DEPENDS
+  intrinsics_gen
+
+  LINK_COMPONENTS
+  Analysis
+  Core
+  SandboxIR
+  Support
+  TransformUtils
+)
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVec/SandboxVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SandboxVec/SandboxVectorizer.cpp
new file mode 100644
index 00000000000000..d113f02d7fa978
--- /dev/null
+++ b/llvm/lib/Transforms/Vectorize/SandboxVec/SandboxVectorizer.cpp
@@ -0,0 +1,63 @@
+//===- SandboxVectorizer.cpp - Vectorizer based on Sandbox IR -------------===//
+//
+// 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/SandboxVec/SandboxVectorizer.h"
+#include "llvm/Support/CommandLine.h"
+
+using namespace llvm;
+
+#define SV_NAME "sandbox-vectorizer"
+#define DEBUG_TYPE "SBVec"
+
+cl::opt<bool>
+    SBVecDisable("sbvec-disable", cl::init(false), cl::Hidden,
+                 cl::desc("Disable the Sandbox Vectorization passes"));
+
+PreservedAnalyses SandboxVectorizerPass::run(Function &F,
+                                             FunctionAnalysisManager &AM) {
+  TTI = &AM.getResult<TargetIRAnalysis>(F);
+  SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
+  DL = &F.getParent()->getDataLayout();
+  AA = &AM.getResult<AAManager>(F);
+
+  bool Changed = runImpl(F);
+  if (!Changed)
+    return PreservedAnalyses::all();
+
+  PreservedAnalyses PA;
+  PA.preserveSet<CFGAnalyses>();
+  return PA;
+}
+
+bool SandboxVectorizerPass::runImpl(Function &F) {
+  if (SBVecDisable)
+    return false;
+
+  // If the target claims to have no vector registers don't attempt
+  // vectorization.
+  if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true))) {
+    LLVM_DEBUG(dbgs() << "SBVec: Target has no vector registers, abort.\n");
+    return false;
+  }
+
+  // Don't vectorize when the attribute NoImplicitFloat is used.
+  if (F.hasFnAttribute(Attribute::NoImplicitFloat))
+    return false;
+
+  sandboxir::Context Ctx(F.getContext());
+
+  LLVM_DEBUG(dbgs() << "SBVec: Analyzing blocks in " << F.getName() << ".\n");
+
+  // Create SandboxIR for `F`.
+  sandboxir::Function &SBF = *Ctx.createFunction(&F);
+
+  // TODO: Initialize SBVec Pass Manager
+  (void)SBF;
+
+  return false;
+}
diff --git a/llvm/test/Transforms/SandboxVec/boilerplate.ll b/llvm/test/Transforms/SandboxVec/boilerplate.ll
new file mode 100644
index 00000000000000..353659d41485fc
--- /dev/null
+++ b/llvm/test/Transforms/SandboxVec/boilerplate.ll
@@ -0,0 +1,11 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=sandbox-vectorizer %s -S | FileCheck %s
+
+; This test checks that the pass was registered with the pass manager.
+; TODO: Remove this test once actual tests land.
+define void @boilerplate() {
+; CHECK-LABEL: define void @boilerplate() {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}



More information about the llvm-commits mailing list