From 94f257fbcafaa6be437e583efafad4f500966e4e Mon Sep 17 00:00:00 2001 From: Ruizhe Zhao Date: Thu, 21 Oct 2021 19:53:28 +0100 Subject: [PATCH 1/3] [AnnotateScop] new pass annotates scop.ignored --- include/polymer/CMakeLists.txt | 1 + include/polymer/Transforms/CMakeLists.txt | 3 ++ include/polymer/Transforms/Passes.h | 17 ++++++++++ include/polymer/Transforms/Passes.td | 21 ++++++++++++ lib/Transforms/AnnotateScop.cc | 34 +++++++++++++++++++ lib/Transforms/CMakeLists.txt | 3 +- lib/Transforms/PassDetail.h | 14 ++++++++ .../AnnotateScop/annotate-single.mlir | 12 +++++++ tools/polymer-opt/polymer-opt.cc | 2 ++ 9 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 include/polymer/Transforms/CMakeLists.txt create mode 100644 include/polymer/Transforms/Passes.h create mode 100644 include/polymer/Transforms/Passes.td create mode 100644 lib/Transforms/AnnotateScop.cc create mode 100644 lib/Transforms/PassDetail.h create mode 100644 test/polymer-opt/AnnotateScop/annotate-single.mlir diff --git a/include/polymer/CMakeLists.txt b/include/polymer/CMakeLists.txt index e69de29b..e31af326 100644 --- a/include/polymer/CMakeLists.txt +++ b/include/polymer/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(Transforms) diff --git a/include/polymer/Transforms/CMakeLists.txt b/include/polymer/Transforms/CMakeLists.txt new file mode 100644 index 00000000..21311d83 --- /dev/null +++ b/include/polymer/Transforms/CMakeLists.txt @@ -0,0 +1,3 @@ +set(LLVM_TARGET_DEFINITIONS Passes.td) +mlir_tablegen(Passes.h.inc -gen-pass-decls -name Transforms) +add_public_tablegen_target(PolymerTransformsIncGen) diff --git a/include/polymer/Transforms/Passes.h b/include/polymer/Transforms/Passes.h new file mode 100644 index 00000000..73c54bf9 --- /dev/null +++ b/include/polymer/Transforms/Passes.h @@ -0,0 +1,17 @@ +//===- Passes.h - Include Tblgen pass defs ------------C++-===// +#ifndef POLYMER_TRANSFORMS_PASSES_H +#define POLYMER_TRANSFORMS_PASSES_H + +#include "mlir/Pass/Pass.h" + +namespace polymer { + +std::unique_ptr> createAnnotateScopPass(); + +/// Generate the code for registering passes. +#define GEN_PASS_REGISTRATION +#include "polymer/Transforms/Passes.h.inc" + +} // namespace polymer + +#endif diff --git a/include/polymer/Transforms/Passes.td b/include/polymer/Transforms/Passes.td new file mode 100644 index 00000000..a69a0d5c --- /dev/null +++ b/include/polymer/Transforms/Passes.td @@ -0,0 +1,21 @@ +#ifndef POLYMER_TRANSFORMS_PASSES +#define POLYMER_TRANSFORMS_PASSES + +include "mlir/Pass/PassBase.td" +include "mlir/Rewrite/PassUtil.td" + +def AnnotateScop : FunctionPass<"annotate-scop"> { + let summary = "Annotate scop.ignore to unselected functions."; + let constructor = "polymer::createAnnotateScopPass()"; + + let options = [ + ListOption< + "includedFunctions", + "functions", + "std::string", + "A list of functions to be included.", + "llvm::cl::ZeroOrMore, llvm::cl::MiscFlags::CommaSeparated"> + ]; +} + +#endif diff --git a/lib/Transforms/AnnotateScop.cc b/lib/Transforms/AnnotateScop.cc new file mode 100644 index 00000000..f6b7cce9 --- /dev/null +++ b/lib/Transforms/AnnotateScop.cc @@ -0,0 +1,34 @@ +//===- AnnotateScop.cc --------------------------------------*- C++ -*-===// + +#include "PassDetail.h" + +#include "mlir/Dialect/Affine/IR/AffineOps.h" +#include "mlir/Dialect/MemRef/IR/MemRef.h" +#include "mlir/Dialect/SCF/SCF.h" +#include "mlir/Transforms/LoopUtils.h" +#include "mlir/Transforms/Passes.h" +#include "mlir/Transforms/RegionUtils.h" +#include "llvm/Support/Debug.h" + +using namespace mlir; +using namespace llvm; +using namespace polymer; + +namespace { +struct AnnotateScop : public polymer::AnnotateScopBase { + void runOnFunction() override { + FuncOp f = getOperation(); + OpBuilder b(f.getContext()); + + for (auto &name : includedFunctions) + if (name == f.getName()) + return; + + f->setAttr("scop.ignored", b.getUnitAttr()); + } // namespace +}; +} // namespace + +std::unique_ptr> polymer::createAnnotateScopPass() { + return std::make_unique(); +} diff --git a/lib/Transforms/CMakeLists.txt b/lib/Transforms/CMakeLists.txt index c28a7823..42599f3c 100644 --- a/lib/Transforms/CMakeLists.txt +++ b/lib/Transforms/CMakeLists.txt @@ -6,12 +6,13 @@ add_mlir_conversion_library(PolymerTransforms LoopAnnotate.cc LoopExtract.cc FoldSCFIf.cc + AnnotateScop.cc ADDITIONAL_HEADER_DIRS "${POLYMER_MAIN_INCLUDE_DIR}/polymer/Transforms" DEPENDS - ${POLYMER_TRANSFORM_DEPENDS} + PolymerTransformsIncGen LINK_LIBS PUBLIC MLIRAffine diff --git a/lib/Transforms/PassDetail.h b/lib/Transforms/PassDetail.h new file mode 100644 index 00000000..caeb81ed --- /dev/null +++ b/lib/Transforms/PassDetail.h @@ -0,0 +1,14 @@ +//===- PassDetail.h - Transforms Pass class details -------------*- C++ -*-===// + +#ifndef POLYMER_TRANSFORMS_PASSDETAIL_H_ +#define POLYMER_TRANSFORMS_PASSDETAIL_H_ + +#include "mlir/Pass/Pass.h" +#include "polymer/Transforms/Passes.h" + +namespace polymer { +#define GEN_PASS_CLASSES +#include "polymer/Transforms/Passes.h.inc" +} // namespace polymer + +#endif diff --git a/test/polymer-opt/AnnotateScop/annotate-single.mlir b/test/polymer-opt/AnnotateScop/annotate-single.mlir new file mode 100644 index 00000000..fe7e2a7d --- /dev/null +++ b/test/polymer-opt/AnnotateScop/annotate-single.mlir @@ -0,0 +1,12 @@ +// RUN: polymer-opt %s -annotate-scop="functions=foo" | FileCheck %s + +func @foo() { + return +} + +func @bar() { + return +} + +// CHECK: func @foo() { +// CHECK: func @bar() attributes {scop.ignored} { diff --git a/tools/polymer-opt/polymer-opt.cc b/tools/polymer-opt/polymer-opt.cc index c8fd1a9f..08060c66 100644 --- a/tools/polymer-opt/polymer-opt.cc +++ b/tools/polymer-opt/polymer-opt.cc @@ -9,6 +9,7 @@ #include "polymer/Transforms/FoldSCFIf.h" #include "polymer/Transforms/LoopAnnotate.h" #include "polymer/Transforms/LoopExtract.h" +#include "polymer/Transforms/Passes.h" #include "polymer/Transforms/PlutoTransform.h" #include "polymer/Transforms/Reg2Mem.h" #include "polymer/Transforms/ScopStmtOpt.h" @@ -61,6 +62,7 @@ int main(int argc, char *argv[]) { registerLoopAnnotatePasses(); registerLoopExtractPasses(); registerFoldSCFIfPass(); + registerAnnotateScopPass(); // Register any pass manager command line options. registerMLIRContextCLOptions(); From 6ff3ad5808d8b080fb6d792c81dbf7a260e16d48 Mon Sep 17 00:00:00 2001 From: Ruizhe Zhao Date: Thu, 21 Oct 2021 19:57:54 +0100 Subject: [PATCH 2/3] [Reg2Mem] understands scop.ignored --- lib/Transforms/Reg2Mem.cc | 3 +++ test/polymer-opt/Reg2Mem/ignored.mlir | 14 ++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 test/polymer-opt/Reg2Mem/ignored.mlir diff --git a/lib/Transforms/Reg2Mem.cc b/lib/Transforms/Reg2Mem.cc index 202c49f6..a7bceb5a 100644 --- a/lib/Transforms/Reg2Mem.cc +++ b/lib/Transforms/Reg2Mem.cc @@ -315,6 +315,9 @@ class RegToMemPass mlir::FuncOp f = getOperation(); auto builder = OpBuilder(f.getContext()); + if (f->hasAttr("scop.ignored")) + return; + separateAffineIfBlocks(f, builder); demoteRegisterToMemory(f, builder); } diff --git a/test/polymer-opt/Reg2Mem/ignored.mlir b/test/polymer-opt/Reg2Mem/ignored.mlir new file mode 100644 index 00000000..4fa47da7 --- /dev/null +++ b/test/polymer-opt/Reg2Mem/ignored.mlir @@ -0,0 +1,14 @@ +// RUN: polymer-opt %s -reg2mem | FileCheck %s +func @foo(%A: memref) attributes {scop.ignored} { + %0 = affine.load %A[0] : memref + affine.for %i = 1 to 30 { + affine.store %0, %A[%i] : memref + } + + return +} + +// CHECK: func @foo +// CHECK-NEXT: %[[v0:.*]] = affine.load +// CHECK-NEXT: affine.for %{{.*}} = 1 to 30 +// CHECK-NEXT: affine.store %[[v0]] From b6dd56b79b2cdb3350033275c141663ae789dc82 Mon Sep 17 00:00:00 2001 From: Ruizhe Zhao Date: Thu, 21 Oct 2021 20:00:46 +0100 Subject: [PATCH 3/3] [ExtractScopStmt/PlutoOpt] supports scop.ignored --- lib/Transforms/ExtractScopStmt.cc | 6 +++++- lib/Transforms/PlutoTransform.cc | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/ExtractScopStmt.cc b/lib/Transforms/ExtractScopStmt.cc index 3f0687db..175d98fb 100644 --- a/lib/Transforms/ExtractScopStmt.cc +++ b/lib/Transforms/ExtractScopStmt.cc @@ -510,7 +510,11 @@ class ExtractScopStmtPass OpBuilder b(m.getContext()); SmallVector funcs; - m.walk([&](mlir::FuncOp f) { funcs.push_back(f); }); + m.walk([&](mlir::FuncOp f) { + if (f->hasAttr("scop.ignored")) + return; + funcs.push_back(f); + }); unsigned numCallees = 0; for (mlir::FuncOp f : funcs) { diff --git a/lib/Transforms/PlutoTransform.cc b/lib/Transforms/PlutoTransform.cc index 0c9830aa..55d21723 100644 --- a/lib/Transforms/PlutoTransform.cc +++ b/lib/Transforms/PlutoTransform.cc @@ -156,7 +156,7 @@ class PlutoTransformPass llvm::DenseMap funcMap; m.walk([&](mlir::FuncOp f) { - if (!f->getAttr("scop.stmt")) + if (!f->getAttr("scop.stmt") && !f->hasAttr("scop.ignored")) funcOps.push_back(f); });