[BindingsPython] Directly use C++ API for ScaleHLS transform util bindings

This commit is contained in:
Hanchen Ye 2021-11-03 15:49:46 -05:00
parent 5fc73d0bb2
commit 07d027d3c5
11 changed files with 46 additions and 174 deletions

View File

@ -1,38 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Copyright 2020-2021 The ScaleHLS Authors.
//
//===----------------------------------------------------------------------===//
#ifndef SCALEHLS_C_TRANSFORMS_UTILS_H
#define SCALEHLS_C_TRANSFORMS_UTILS_H
#include "mlir-c/IR.h"
#ifdef __cplusplus
extern "C" {
#endif
struct MlirAffineLoopBand {
const MlirOperation *loopBegin;
const MlirOperation *loopEnd;
};
typedef struct MlirAffineLoopBand MlirAffineLoopBand;
MLIR_CAPI_EXPORTED bool mlirApplyAffineLoopPerfection(MlirAffineLoopBand band);
MLIR_CAPI_EXPORTED bool mlirApplyAffineLoopOrderOpt(MlirAffineLoopBand band);
MLIR_CAPI_EXPORTED bool mlirApplyRemoveVariableBound(MlirAffineLoopBand band);
MLIR_CAPI_EXPORTED bool mlirApplyLoopPipelining(MlirAffineLoopBand band,
unsigned pipelineLoc,
unsigned targetII);
MLIR_CAPI_EXPORTED bool mlirApplyLegalizeToHlscpp(MlirOperation op,
bool topFunc);
MLIR_CAPI_EXPORTED bool mlirApplyMemoryAccessOpt(MlirOperation op);
MLIR_CAPI_EXPORTED bool mlirApplyArrayPartition(MlirOperation op);
#ifdef __cplusplus
}
#endif
#endif // SCALEHLS_C_TRANSFORMS_UTILS_H

View File

@ -10,8 +10,6 @@ include(AddMLIRPython)
declare_mlir_python_sources(ScaleHLSBindingsPythonExtension) declare_mlir_python_sources(ScaleHLSBindingsPythonExtension)
set(_depends LLVMSupport)
declare_mlir_python_extension(ScaleHLSBindingsPythonExtension.Core declare_mlir_python_extension(ScaleHLSBindingsPythonExtension.Core
MODULE_NAME _scalehls MODULE_NAME _scalehls
ADD_TO_PARENT ScaleHLSBindingsPythonExtension ADD_TO_PARENT ScaleHLSBindingsPythonExtension
@ -19,10 +17,11 @@ declare_mlir_python_extension(ScaleHLSBindingsPythonExtension.Core
ScaleHLSModule.cpp ScaleHLSModule.cpp
EMBED_CAPI_LINK_LIBS EMBED_CAPI_LINK_LIBS
MLIRScaleHLSCAPIHLSCpp MLIRScaleHLSCAPIHLSCpp
MLIRScaleHLSCAPITransformsUtils
MLIRScaleHLSCAPIEmitHLSCpp MLIRScaleHLSCAPIEmitHLSCpp
PRIVATE_LINK_LIBS PRIVATE_LINK_LIBS
${_depends} LLVMSupport
MLIRScaleHLSConversion
MLIRScaleHLSTransforms
) )
################################################################################ ################################################################################

View File

@ -7,10 +7,9 @@
#include "mlir-c/Bindings/Python/Interop.h" #include "mlir-c/Bindings/Python/Interop.h"
#include "mlir/Bindings/Python/PybindAdaptors.h" #include "mlir/Bindings/Python/PybindAdaptors.h"
#include "mlir/CAPI/IR.h" #include "mlir/CAPI/IR.h"
#include "scalehls-c/Dialect/HLSCpp.h" #include "scalehls-c/EmitHLSCpp.h"
#include "scalehls-c/Transforms/Utils.h" #include "scalehls-c/HLSCpp.h"
#include "scalehls-c/Translation/EmitHLSCpp.h" #include "scalehls/Transforms/Utils.h"
#include "scalehls/Support/Utils.h"
#include "llvm-c/ErrorHandling.h" #include "llvm-c/ErrorHandling.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
@ -25,25 +24,20 @@ using namespace scalehls;
class PyAffineLoopBand { class PyAffineLoopBand {
public: public:
PyAffineLoopBand(AffineLoopBand &band) { PyAffineLoopBand(AffineLoopBand &band) : band(band) {}
for (auto loop : band)
impl.push_back(wrap(loop));
}
operator MlirAffineLoopBand() const { return get(); } AffineLoopBand &get() const { return band; }
MlirAffineLoopBand get() const { return {impl.begin(), impl.end()}; } size_t size() const { return band.size(); }
size_t size() const { return impl.size(); }
PyAffineLoopBand &dunderIter() { return *this; } PyAffineLoopBand &dunderIter() { return *this; }
MlirOperation dunderNext() { MlirOperation dunderNext() {
if (nextIndex >= impl.size()) if (nextIndex >= band.size())
throw py::stop_iteration(); throw py::stop_iteration();
return impl[nextIndex++]; return wrap(band[nextIndex++]);
} }
private: private:
llvm::SmallVector<MlirOperation, 6> impl; AffineLoopBand &band;
size_t nextIndex = 0; size_t nextIndex = 0;
}; };
@ -51,26 +45,21 @@ class PyAffineLoopBandList {
public: public:
PyAffineLoopBandList(MlirOperation op) { PyAffineLoopBandList(MlirOperation op) {
for (auto &region : unwrap(op)->getRegions()) for (auto &region : unwrap(op)->getRegions())
for (auto &block : region) { for (auto &block : region)
AffineLoopBands bands; getLoopBands(block, impl);
getLoopBands(block, bands);
for (auto band : bands)
impl.push_back(PyAffineLoopBand(band));
}
} }
size_t size() const { return impl.size(); } size_t size() const { return impl.size(); }
PyAffineLoopBandList &dunderIter() { return *this; } PyAffineLoopBandList &dunderIter() { return *this; }
PyAffineLoopBand dunderNext() { PyAffineLoopBand dunderNext() {
if (nextIndex >= impl.size()) if (nextIndex >= impl.size())
throw py::stop_iteration(); throw py::stop_iteration();
return impl[nextIndex++]; return PyAffineLoopBand(impl[nextIndex++]);
} }
private: private:
llvm::SmallVector<PyAffineLoopBand> impl; AffineLoopBands impl;
size_t nextIndex = 0; size_t nextIndex = 0;
}; };
@ -89,42 +78,46 @@ PYBIND11_MODULE(_scalehls, m) {
mlirDialectHandleLoadDialect(hlscpp, context); mlirDialectHandleLoadDialect(hlscpp, context);
}); });
m.def("apply_affine_loop_perfection", [](PyAffineLoopBand band) -> bool { m.def("apply_affine_loop_perfection", [](PyAffineLoopBand band) {
py::gil_scoped_release(); py::gil_scoped_release();
return mlirApplyAffineLoopPerfection(band.get()); return applyAffineLoopPerfection(band.get());
}); });
m.def("apply_affine_loop_order_opt", [](PyAffineLoopBand band) -> bool { m.def("apply_affine_loop_order_opt", [](PyAffineLoopBand band) {
py::gil_scoped_release(); py::gil_scoped_release();
return mlirApplyAffineLoopOrderOpt(band.get()); return applyAffineLoopOrderOpt(band.get());
}); });
m.def("apply_remove_variable_bound", [](PyAffineLoopBand band) -> bool { m.def("apply_remove_variable_bound", [](PyAffineLoopBand band) {
py::gil_scoped_release(); py::gil_scoped_release();
return mlirApplyRemoveVariableBound(band.get()); return applyRemoveVariableBound(band.get());
}); });
m.def("apply_legalize_to_hlscpp",
[](MlirOperation op, bool top_func) -> bool {
py::gil_scoped_release();
return mlirApplyLegalizeToHlscpp(op, top_func);
});
m.def("apply_loop_pipelining", m.def("apply_loop_pipelining",
[](PyAffineLoopBand band, unsigned pipelineLoc, [](PyAffineLoopBand band, unsigned pipeline_loc, unsigned target_ii) {
unsigned targetII) -> bool {
py::gil_scoped_release(); py::gil_scoped_release();
return mlirApplyLoopPipelining(band.get(), pipelineLoc, targetII); return applyLoopPipelining(band.get(), pipeline_loc, target_ii);
}); });
m.def("apply_memory_access_opt", [](MlirOperation op) -> bool { m.def("apply_legalize_to_hlscpp", [](MlirOperation op, bool top_func) {
py::gil_scoped_release(); py::gil_scoped_release();
return mlirApplyMemoryAccessOpt(op); if (auto func = dyn_cast<FuncOp>(unwrap(op)))
return applyLegalizeToHLSCpp(func, top_func);
return false;
}); });
m.def("apply_array_partition", [](MlirOperation op) -> bool { m.def("apply_memory_access_opt", [](MlirOperation op) {
py::gil_scoped_release(); py::gil_scoped_release();
return mlirApplyArrayPartition(op); if (auto func = dyn_cast<FuncOp>(unwrap(op)))
return applyMemoryAccessOpt(func);
return false;
});
m.def("apply_array_partition", [](MlirOperation op) {
py::gil_scoped_release();
if (auto func = dyn_cast<FuncOp>(unwrap(op)))
return applyArrayPartition(func);
return false;
}); });
m.def("emit_hlscpp", [](MlirModule mod, py::object fileObject) { m.def("emit_hlscpp", [](MlirModule mod, py::object fileObject) {

View File

@ -1,3 +1,2 @@
add_subdirectory(Dialect) add_subdirectory(Dialect)
add_subdirectory(Transforms)
add_subdirectory(Translation) add_subdirectory(Translation)

View File

@ -4,11 +4,11 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "scalehls-c/Dialect/HLSCpp.h" #include "scalehls-c/HLSCpp.h"
#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Registration.h" #include "mlir/CAPI/Registration.h"
#include "mlir/CAPI/Support.h"
#include "scalehls/Dialect/HLSCpp/HLSCpp.h" #include "scalehls/Dialect/HLSCpp/HLSCpp.h"
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(HLSCpp, hlscpp, using namespace mlir;
mlir::scalehls::hlscpp::HLSCppDialect) using namespace scalehls;
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(HLSCpp, hlscpp, hlscpp::HLSCppDialect)

View File

@ -1,11 +0,0 @@
add_mlir_public_c_api_library(MLIRScaleHLSCAPITransformsUtils
Utils.cpp
ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir-c
LINK_LIBS PUBLIC
MLIRCAPIIR
MLIRScaleHLSConversion
MLIRScaleHLSTransforms
)

View File

@ -1,68 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Copyright 2020-2021 The ScaleHLS Authors.
//
//===----------------------------------------------------------------------===//
#include "scalehls-c/Transforms/Utils.h"
#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Support.h"
#include "mlir/IR/BuiltinOps.h"
#include "scalehls/Transforms/Utils.h"
using namespace mlir;
using namespace scalehls;
static void unwrapBand(MlirAffineLoopBand band, AffineLoopBand &unwrappedBand) {
for (auto op = band.loopBegin; op != band.loopEnd; ++op) {
auto loop = dyn_cast<AffineForOp>(unwrap(*op));
assert(loop && "operation in loop band must be AffineForOp");
unwrappedBand.push_back(loop);
}
}
MLIR_CAPI_EXPORTED bool mlirApplyAffineLoopPerfection(MlirAffineLoopBand band) {
AffineLoopBand unwrappedBand;
unwrapBand(band, unwrappedBand);
return applyAffineLoopPerfection(unwrappedBand);
}
MLIR_CAPI_EXPORTED bool mlirApplyAffineLoopOrderOpt(MlirAffineLoopBand band) {
AffineLoopBand unwrappedBand;
unwrapBand(band, unwrappedBand);
return applyAffineLoopOrderOpt(unwrappedBand);
}
MLIR_CAPI_EXPORTED bool mlirApplyRemoveVariableBound(MlirAffineLoopBand band) {
AffineLoopBand unwrappedBand;
unwrapBand(band, unwrappedBand);
return applyRemoveVariableBound(unwrappedBand);
}
MLIR_CAPI_EXPORTED bool mlirApplyLoopPipelining(MlirAffineLoopBand band,
unsigned pipelineLoc,
unsigned targetII) {
AffineLoopBand unwrappedBand;
unwrapBand(band, unwrappedBand);
return applyLoopPipelining(unwrappedBand, pipelineLoc, targetII);
}
MLIR_CAPI_EXPORTED bool mlirApplyLegalizeToHlscpp(MlirOperation op,
bool topFunc) {
if (auto func = dyn_cast<FuncOp>(unwrap(op)))
return applyLegalizeToHLSCpp(func, topFunc);
return false;
}
MLIR_CAPI_EXPORTED bool mlirApplyMemoryAccessOpt(MlirOperation op) {
if (auto func = dyn_cast<FuncOp>(unwrap(op)))
return applyMemoryAccessOpt(func);
return false;
}
MLIR_CAPI_EXPORTED bool mlirApplyArrayPartition(MlirOperation op) {
if (auto func = dyn_cast<FuncOp>(unwrap(op)))
return applyArrayPartition(func);
return false;
}

View File

@ -4,13 +4,11 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "scalehls-c/Translation/EmitHLSCpp.h" #include "scalehls-c/EmitHLSCpp.h"
#include "mlir/CAPI/IR.h" #include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Support.h" #include "mlir/CAPI/Support.h"
#include "mlir/CAPI/Utils.h" #include "mlir/CAPI/Utils.h"
#include "scalehls/Translation/EmitHLSCpp.h" #include "scalehls/Translation/EmitHLSCpp.h"
#include "llvm/Support/raw_ostream.h"
using namespace mlir; using namespace mlir;
using namespace scalehls; using namespace scalehls;

View File

@ -48,7 +48,7 @@ def main():
bands = scalehls.LoopBandList(op) bands = scalehls.LoopBandList(op)
for band in bands: for band in bands:
scalehls.apply_affine_loop_perfection(band) scalehls.apply_affine_loop_perfection(band)
# scalehls.apply_affine_loop_order_opt(band) scalehls.apply_affine_loop_order_opt(band)
scalehls.apply_remove_variable_bound(band) scalehls.apply_remove_variable_bound(band)
scalehls.apply_loop_pipelining(band, band.size - 1, 3) # targetII scalehls.apply_loop_pipelining(band, band.size - 1, 3) # targetII
scalehls.apply_legalize_to_hlscpp(op.operation, True) # topFunc scalehls.apply_legalize_to_hlscpp(op.operation, True) # topFunc