2020-05-07 13:44:12 +08:00
|
|
|
//===- MlirIr.h - MLIR IR Bindings ----------------------------------------===//
|
2020-05-01 10:23:18 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-06-19 09:02:39 +08:00
|
|
|
#ifndef NPCOMP_PYTHON_MLIR_IR_H
|
|
|
|
#define NPCOMP_PYTHON_MLIR_IR_H
|
2020-05-01 10:23:18 +08:00
|
|
|
|
2020-05-07 13:44:12 +08:00
|
|
|
#include "PybindUtils.h"
|
2020-05-01 10:23:18 +08:00
|
|
|
|
|
|
|
#include "mlir/IR/Block.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
2020-06-07 12:24:28 +08:00
|
|
|
#include "mlir/IR/Identifier.h"
|
2020-05-01 10:23:18 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
|
|
|
#include "mlir/IR/Module.h"
|
|
|
|
#include "mlir/IR/Operation.h"
|
|
|
|
#include "mlir/IR/Region.h"
|
2020-05-02 09:44:06 +08:00
|
|
|
#include "mlir/IR/SymbolTable.h"
|
2020-05-01 10:23:18 +08:00
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
|
|
|
|
struct PyContext;
|
|
|
|
|
2020-05-02 01:16:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template <typename ListTy, typename ItemWrapperTy> class PyIpListWrapper {
|
|
|
|
public:
|
|
|
|
using ThisTy = PyIpListWrapper<ListTy, ItemWrapperTy>;
|
|
|
|
static void bind(py::module m, const char *className);
|
|
|
|
PyIpListWrapper(ListTy &list) : list(list) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ListTy &list;
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Wrapper types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-05-01 10:23:18 +08:00
|
|
|
/// Wrapper around an Operation*.
|
|
|
|
struct PyBaseOperation {
|
|
|
|
virtual ~PyBaseOperation();
|
|
|
|
static void bind(py::module m);
|
|
|
|
virtual Operation *getOperation() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper around Module, capturing a PyContext reference.
|
|
|
|
struct PyModuleOp : PyBaseOperation {
|
|
|
|
PyModuleOp(std::shared_ptr<PyContext> context, ModuleOp moduleOp)
|
2020-05-02 09:44:06 +08:00
|
|
|
: context(context), moduleOp(moduleOp) {
|
|
|
|
assert(moduleOp);
|
|
|
|
}
|
2020-05-02 01:16:19 +08:00
|
|
|
~PyModuleOp();
|
2020-05-01 10:23:18 +08:00
|
|
|
static void bind(py::module m);
|
|
|
|
Operation *getOperation() override;
|
|
|
|
std::string toAsm(bool enableDebugInfo, bool prettyForm,
|
|
|
|
int64_t largeElementLimit);
|
|
|
|
|
|
|
|
std::shared_ptr<PyContext> context;
|
|
|
|
ModuleOp moduleOp;
|
|
|
|
};
|
|
|
|
|
2020-05-02 01:16:19 +08:00
|
|
|
/// Wrapper around an Operation*.
|
|
|
|
struct PyOperationRef : PyBaseOperation {
|
2020-05-02 09:44:06 +08:00
|
|
|
PyOperationRef(Operation *operation) : operation(operation) {
|
|
|
|
assert(operation);
|
|
|
|
}
|
2020-05-02 01:16:19 +08:00
|
|
|
PyOperationRef(Operation &operation) : operation(&operation) {}
|
|
|
|
~PyOperationRef();
|
|
|
|
static void bind(py::module m);
|
|
|
|
Operation *getOperation() override;
|
|
|
|
|
|
|
|
Operation *operation;
|
|
|
|
};
|
|
|
|
|
2020-05-02 09:44:06 +08:00
|
|
|
/// Wrapper around SymbolTable.
|
|
|
|
struct PySymbolTable {
|
|
|
|
PySymbolTable(SymbolTable &symbolTable) : symbolTable(symbolTable) {}
|
|
|
|
static void bind(py::module m);
|
|
|
|
SymbolTable &symbolTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper around Value.
|
|
|
|
struct PyValue {
|
|
|
|
PyValue(Value value) : value(value) { assert(value); }
|
|
|
|
static void bind(py::module m);
|
|
|
|
operator Value() { return value; }
|
|
|
|
Value value;
|
|
|
|
};
|
|
|
|
|
2020-06-07 12:24:28 +08:00
|
|
|
/// Wrapper around Identifier.
|
|
|
|
struct PyIdentifier {
|
|
|
|
PyIdentifier(Identifier identifier) : identifier(identifier) {}
|
|
|
|
static void bind(py::module m);
|
|
|
|
Identifier identifier;
|
|
|
|
};
|
|
|
|
|
2020-05-02 09:44:06 +08:00
|
|
|
/// Wrapper around Attribute.
|
|
|
|
struct PyAttribute {
|
|
|
|
PyAttribute(Attribute attr) : attr(attr) { assert(attr); }
|
|
|
|
static void bind(py::module m);
|
|
|
|
Attribute attr;
|
|
|
|
};
|
|
|
|
|
2020-05-01 10:23:18 +08:00
|
|
|
/// Wrapper around MLIRContext.
|
|
|
|
struct PyContext : std::enable_shared_from_this<PyContext> {
|
2020-08-28 06:09:10 +08:00
|
|
|
PyContext();
|
2020-05-01 10:23:18 +08:00
|
|
|
static void bind(py::module m);
|
|
|
|
PyModuleOp parseAsm(const std::string &asm_text);
|
|
|
|
MLIRContext context;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper around a Block&.
|
|
|
|
struct PyBlockRef {
|
|
|
|
PyBlockRef(Block &block) : block(block) {}
|
|
|
|
static void bind(py::module m);
|
|
|
|
Block █
|
|
|
|
};
|
|
|
|
|
2020-05-02 01:16:19 +08:00
|
|
|
/// Wrapper around a Region&.
|
|
|
|
struct PyRegionRef {
|
|
|
|
PyRegionRef(Region ®ion) : region(region) {}
|
|
|
|
static void bind(py::module m);
|
|
|
|
Region ®ion;
|
|
|
|
};
|
|
|
|
|
2020-05-02 09:44:06 +08:00
|
|
|
struct PyType {
|
|
|
|
PyType() = default;
|
|
|
|
PyType(Type type) : type(type) {}
|
|
|
|
static void bind(py::module m);
|
|
|
|
operator Type() { return type; }
|
|
|
|
Type type;
|
|
|
|
};
|
|
|
|
|
2020-05-01 10:23:18 +08:00
|
|
|
/// Wrapper around an OpBuilder reference.
|
|
|
|
/// This class is inherently dangerous because it does not track ownership
|
|
|
|
/// of IR objects that it may be operating on and incorrect usage can cause
|
|
|
|
/// memory access errors, just as it can in C++. It is intended for use by
|
|
|
|
/// higher level constructs that are specifically coded to satisfy object
|
|
|
|
/// lifetime needs.
|
|
|
|
class PyBaseOpBuilder {
|
|
|
|
public:
|
|
|
|
virtual ~PyBaseOpBuilder();
|
|
|
|
static void bind(py::module m);
|
2020-05-02 09:44:06 +08:00
|
|
|
virtual OpBuilder &getBuilder(bool requirePosition = false) = 0;
|
2020-06-07 12:24:28 +08:00
|
|
|
MLIRContext *getContext() { return getBuilder(false).getContext(); }
|
|
|
|
|
|
|
|
// For convenience, we track the current location at the builder level
|
|
|
|
// to avoid lots of parameter passing.
|
|
|
|
void setCurrentLoc(Location loc) { currentLoc = loc; }
|
|
|
|
Location getCurrentLoc() {
|
|
|
|
if (currentLoc) {
|
|
|
|
return Location(currentLoc);
|
|
|
|
} else {
|
|
|
|
return UnknownLoc::get(getBuilder(false).getContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LocationAttr currentLoc;
|
2020-05-01 10:23:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper around an instance of an OpBuilder.
|
|
|
|
class PyOpBuilder : public PyBaseOpBuilder {
|
|
|
|
public:
|
|
|
|
PyOpBuilder(PyContext &context) : builder(&context.context) {}
|
|
|
|
~PyOpBuilder() override;
|
|
|
|
static void bind(py::module m);
|
2020-05-02 09:44:06 +08:00
|
|
|
OpBuilder &getBuilder(bool requirePosition = false) override;
|
2020-05-01 10:23:18 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
OpBuilder builder;
|
|
|
|
};
|
|
|
|
|
2020-05-07 09:24:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Custom types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Helper for creating (possibly dialect specific) IR objects. This class
|
|
|
|
/// is intended to be subclassed on the Python side (possibly with multiple
|
|
|
|
/// inheritance) to provide Python level APIs for custom dialects. The base
|
|
|
|
/// class contains helpers for std types and ops.
|
|
|
|
class PyDialectHelper {
|
|
|
|
public:
|
2020-06-09 07:10:51 +08:00
|
|
|
PyDialectHelper(PyContext &context, PyOpBuilder &builder)
|
2020-06-14 05:53:54 +08:00
|
|
|
: context(context), pyOpBuilder(builder) {}
|
2020-05-07 09:24:51 +08:00
|
|
|
static void bind(py::module m);
|
2020-06-14 05:53:54 +08:00
|
|
|
MLIRContext *getContext() { return pyOpBuilder.getContext(); }
|
|
|
|
|
2020-05-07 09:24:51 +08:00
|
|
|
protected:
|
2020-06-09 07:10:51 +08:00
|
|
|
PyContext &context;
|
|
|
|
PyOpBuilder &pyOpBuilder;
|
2020-05-07 09:24:51 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 10:23:18 +08:00
|
|
|
} // namespace mlir
|
|
|
|
|
2020-06-19 09:02:39 +08:00
|
|
|
#endif // NPCOMP_PYTHON_MLIR_IR_H
|