Skip to content
Open
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
80 changes: 80 additions & 0 deletions mlir/include/mlir/Pass/PipelineBase.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//===-- PipelineBase.td - Base pipeline definition file -----*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file contains definitions for defining pipeline registration and other
// mechanisms.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_PASS_PIPELINEBASE
#define MLIR_PASS_PIPELINEBASE

include "mlir/Pass/PassBase.td"
include "mlir/IR/Utils.td"

//===----------------------------------------------------------------------===//
// Pipeline Elements
//===----------------------------------------------------------------------===//

// Base class for all pipeline elements
class PipelineElement;

// Reference to a pass with options
class PassElement<string passName> : PipelineElement {
// The pass to instantiate (must match registered pass argument)
string pass = passName;

// Options to pass to this pass instance as a list of strings
// Format: ["option_name", "option_value", "option_name", "option_value", ...]
list<string> options = [];
}

// Nested pipeline for a different operation type
class NestedElement<string targetOp> : PipelineElement {
// The operation type this nested pipeline targets
string operation = targetOp;

// Sequence of elements in this nested pipeline
list<PipelineElement> elements = [];
}

// Reference to a pipeline with options
class PipelineRef<string pipelineName> : PipelineElement {
// The pipeline to instantiate
string pipeline = pipelineName;

// Options to pass to the pipeline as a list of strings
list<string> options = [];
}

//===----------------------------------------------------------------------===//
// Pipeline Definitions
//===----------------------------------------------------------------------===//

// Main pipeline definition class
class Pipeline<string pipelineArg, string targetOp = ""> {
// Command line argument for the pipeline
string argument = pipelineArg;

// Target operation type (empty means any/module-level)
string operation = targetOp;

// Short summary of the pipeline
string summary = "";

// Detailed description
string description = "";

// Pipeline-level options
list<Option> options = [];

// Sequence of pipeline elements
list<PipelineElement> elements = [];
}

#endif // MLIR_PASS_PIPELINEBASE
102 changes: 102 additions & 0 deletions mlir/include/mlir/TableGen/Pipeline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//===- Pipeline.h - TableGen Pipeline Record Wrapper ----------*- 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
//
//===----------------------------------------------------------------------===//
//
// This file contains the Pipeline class, which is a wrapper around a TableGen
// Record representing a Pipeline.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TABLEGEN_PIPELINE_H_
#define MLIR_TABLEGEN_PIPELINE_H_

#include "mlir/TableGen/Pass.h"
#include "llvm/ADT/StringRef.h"
#include <vector>

namespace llvm {
class DagInit;
class Init;
class ListInit;
class Record;
} // namespace llvm

namespace mlir {
namespace tblgen {

/// Wrapper class around a TableGen Pipeline record.
class Pipeline {
public:
explicit Pipeline(const llvm::Record *def);

/// Get the command line argument for this pipeline.
llvm::StringRef getArgument() const;

/// Get the target operation for this pipeline.
llvm::StringRef getOperation() const;

/// Get the summary for this pipeline.
llvm::StringRef getSummary() const;

/// Get the description for this pipeline.
llvm::StringRef getDescription() const;

/// Get the pipeline options.
std::vector<PassOption> getOptions() const;

/// Get the pipeline elements as a list.
const llvm::ListInit *getElements() const;

/// Get the underlying TableGen record.
const llvm::Record *getDef() const { return def; }

private:
/// The TableGen record this pipeline is constructed from.
const llvm::Record *def;
};

/// Wrapper class around a TableGen PipelineElement record.
class PipelineElement {
public:
explicit PipelineElement(const llvm::Init *init);

/// Check if this is a pass element.
bool isPassElement() const;

/// Check if this is a nested element.
bool isNestedElement() const;

/// Check if this is a pipeline reference element.
bool isPipelineElement() const;

/// Get the pass name (for PassElement).
llvm::StringRef getPassName() const;

/// Get the target operation (for NestedElement).
llvm::StringRef getOperation() const;

/// Get the pipeline name (for PipelineElement).
llvm::StringRef getPipelineName() const;

/// Get the nested elements (for NestedElement).
const llvm::ListInit *getElements() const;

/// Get the options list.
const llvm::ListInit *getOptions() const;

/// Get the underlying TableGen record.
const llvm::Record *getDef() const { return def; }

private:
/// The TableGen record this element is constructed from.
const llvm::Record *def;
};

} // namespace tblgen
} // namespace mlir

#endif // MLIR_TABLEGEN_PIPELINE_H_
1 change: 1 addition & 0 deletions mlir/lib/TableGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ llvm_add_library(MLIRTableGen STATIC
Operator.cpp
Pass.cpp
Pattern.cpp
Pipeline.cpp
Predicate.cpp
Property.cpp
Region.cpp
Expand Down
101 changes: 101 additions & 0 deletions mlir/lib/TableGen/Pipeline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//===- Pipeline.cpp - TableGen Pipeline Record Wrapper ------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file contains the Pipeline class implementation.
//
//===----------------------------------------------------------------------===//

#include "mlir/TableGen/Pipeline.h"
#include "llvm/TableGen/Record.h"

using namespace llvm;

using namespace mlir::tblgen;

//===----------------------------------------------------------------------===//
// Pipeline
//===----------------------------------------------------------------------===//

Pipeline::Pipeline(const llvm::Record *def) : def(def) {
assert(def->isSubClassOf("Pipeline") &&
"must be subclass of 'Pipeline'");
}

llvm::StringRef Pipeline::getArgument() const {
return def->getValueAsString("argument");
}

llvm::StringRef Pipeline::getOperation() const {
return def->getValueAsString("operation");
}

llvm::StringRef Pipeline::getSummary() const {
return def->getValueAsString("summary");
}

llvm::StringRef Pipeline::getDescription() const {
return def->getValueAsString("description");
}

std::vector<PassOption> Pipeline::getOptions() const {
auto *listInit = def->getValueAsListInit("options");
std::vector<PassOption> options;
for (const auto *optionInit : *listInit) {
auto *optionDef = cast<DefInit>(optionInit)->getDef();
options.emplace_back(optionDef);
}
return options;
}

const llvm::ListInit *Pipeline::getElements() const {
return def->getValueAsListInit("elements");
}

//===----------------------------------------------------------------------===//
// PipelineElement
//===----------------------------------------------------------------------===//

PipelineElement::PipelineElement(const llvm::Init *init) {
def = cast<DefInit>(init)->getDef();
}

bool PipelineElement::isPassElement() const {
return def->isSubClassOf("PassElement");
}

bool PipelineElement::isNestedElement() const {
return def->isSubClassOf("NestedElement");
}

bool PipelineElement::isPipelineElement() const {
return def->isSubClassOf("PipelineRef");
}

llvm::StringRef PipelineElement::getPassName() const {
assert(isPassElement() && "not a PassElement");
return def->getValueAsString("pass");
}

llvm::StringRef PipelineElement::getOperation() const {
assert(isNestedElement() && "not a NestedElement");
return def->getValueAsString("operation");
}

llvm::StringRef PipelineElement::getPipelineName() const {
assert(isPipelineElement() && "not a PipelineElement");
return def->getValueAsString("pipeline");
}

const llvm::ListInit *PipelineElement::getElements() const {
assert(isNestedElement() && "not a NestedElement");
return def->getValueAsListInit("elements");
}

const llvm::ListInit *PipelineElement::getOptions() const {
return def->getValueAsListInit("options");
}
29 changes: 29 additions & 0 deletions mlir/test/mlir-tblgen/gen-pipeline-decls.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: mlir-tblgen -gen-pipeline-decls -I %S/../../include -name Test %s | FileCheck %s

include "mlir/Pass/PipelineBase.td"

// CHECK: namespace mlir {

def SimpleOptimize : Pipeline<"simple-opt", "func.func"> {
let summary = "Simple optimization pipeline";
let description = "Basic canonicalization and CSE";

let options = [
Option<"level", "level", "int", "1", "Optimization level">
];

let elements = [
PassElement<"canonicalize">,
PassElement<"cse">
];
}

// CHECK: struct SimpleOptimizeOptions {
// CHECK: int level = 1;
// CHECK: };

// CHECK: void buildSimpleOptimizePipeline(::mlir::OpPassManager &pm, const SimpleOptimizeOptions &options);

// CHECK: void registerTestPipelines();

// CHECK: } // namespace mlir
32 changes: 32 additions & 0 deletions mlir/test/mlir-tblgen/gen-pipeline-doc.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: mlir-tblgen -gen-pipeline-doc -I %S/../../include %s | FileCheck %s

include "mlir/Pass/PipelineBase.td"

def OptimizePipeline : Pipeline<"optimize", "builtin.module"> {
let summary = "Full optimization pipeline";
let description = [{
Comprehensive optimization including canonicalization, inlining, and cleanup.
}];

let options = [
Option<"enableInlining", "enable-inlining", "bool", "true", "Enable inlining">
];

let elements = [
PassElement<"canonicalize">,
PassElement<"inline">,
PassElement<"symbol-dce">
];
}

// CHECK: # Generated Pipeline Documentation

// CHECK: ## optimize

// CHECK: Full optimization pipeline

// CHECK: Comprehensive optimization including canonicalization, inlining, and cleanup.

// CHECK: ### Options

// CHECK: - `--enable-inlining`: Enable inlining
Loading