Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
support memref::subview
Browse files Browse the repository at this point in the history
  • Loading branch information
makslevental committed Mar 11, 2024
1 parent 86f0a37 commit 04ddcda
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
14 changes: 12 additions & 2 deletions openhls/ir/memref.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from dataclasses import dataclass
from typing import Tuple

Expand Down Expand Up @@ -81,9 +82,18 @@ def reduce_add(self):
def reduce_max(self):
return ReduceMax(list(self.registers.flatten()))

def alias(self, other_memref):
def alias(self, other_memref, offsets=None, sizes=None, strides=None):
assert isinstance(other_memref, MemRef)
self.registers = other_memref.registers
if offsets is not None and sizes is not None and strides is not None:
subview = []
for o, si, st in zip(offsets, sizes, strides):
subview.append(slice(o, o + si, st))
print("subview", subview, file=sys.stderr)
print("before subview", self.registers.shape, file=sys.stderr)
self.registers = other_memref.registers[tuple(subview)]
print("aftier subview", self.registers.shape, file=sys.stderr)
else:
self.registers = other_memref.registers


class GlobalMemRef:
Expand Down
21 changes: 13 additions & 8 deletions openhls_translate/EmitHLSPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class ModuleEmitter : public OpenHLSEmitterBase {
void emitLoad(memref::LoadOp op);
void emitStore(memref::StoreOp op);
void emitMemCpy(memref::CopyOp op);
void emitMemSubview(memref::SubViewOp op);
void emitGlobal(memref::GlobalOp op);
void emitGetGlobal(memref::GetGlobalOp op);
void emitTensorStore(memref::TensorStoreOp op);
Expand Down Expand Up @@ -420,6 +421,7 @@ class StmtVisitor : public HLSVisitorBase<StmtVisitor, bool> {
bool visitOp(memref::StoreOp op) { return emitter.emitStore(op), true; }
bool visitOp(memref::DeallocOp op) { return true; }
bool visitOp(memref::CopyOp op) { return emitter.emitMemCpy(op), true; }
bool visitOp(memref::SubViewOp op) { return emitter.emitMemSubview(op), true; }
bool visitOp(memref::GlobalOp op) { return emitter.emitGlobal(op), true; }
bool visitOp(memref::GetGlobalOp op) {
return emitter.emitGetGlobal(op), true;
Expand Down Expand Up @@ -1169,20 +1171,23 @@ void ModuleEmitter::emitStore(memref::StoreOp op) {
}

void ModuleEmitter::emitMemCpy(memref::CopyOp op) {
// indent() << "memcpy(";
indent() << "";
// emitValue(op.target());
// os << " = ";
emitValue(op.target());
os << ".alias(";
emitValue(op.getSource());
os << ")";
// os << ", ";
os << "\n";
}

// auto type = op.target().getType().cast<MemRefType>();
// os << type.getNumElements() << " * sizeof(" << getTypeName(op.target())
// << "))";
// os << "\n";
void ModuleEmitter::emitMemSubview(memref::SubViewOp op) {
indent() << "";
emitValue(op.result());
os << ".alias(";
emitValue(op.getSource());
os << ", offsets=" << op.getStaticOffsets();
os << ", sizes=" << op.getStaticSizes();
os << ", strides=" << op.getStaticStrides();
os << ")";
os << "\n";
}

Expand Down
3 changes: 2 additions & 1 deletion openhls_translate/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class HLSVisitorBase {
// Memref-related statements.
memref::AllocOp, memref::AllocaOp, memref::LoadOp, memref::StoreOp,
memref::GlobalOp, memref::GetGlobalOp,
memref::DeallocOp, memref::CopyOp, memref::TensorStoreOp,
memref::DeallocOp, memref::CopyOp, memref::SubViewOp, memref::TensorStoreOp,
tensor::ReshapeOp, memref::ReshapeOp, memref::CollapseShapeOp,
memref::ExpandShapeOp, memref::ReinterpretCastOp,
bufferization::ToMemrefOp, bufferization::ToTensorOp,
Expand Down Expand Up @@ -132,6 +132,7 @@ class HLSVisitorBase {
HANDLE(memref::GetGlobalOp);
HANDLE(memref::DeallocOp);
HANDLE(memref::CopyOp);
HANDLE(memref::SubViewOp);
HANDLE(memref::TensorStoreOp);
HANDLE(tensor::ReshapeOp);
HANDLE(memref::ReshapeOp);
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
requires = [
"setuptools>=42",
"wheel",
"cmake==3.21",
"cmake>=3.24",
# MLIR build depends.
"ninja",
"numpy==1.23.1",
Expand Down
16 changes: 3 additions & 13 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ if [ ! -f "${OPENHLS_DIR}"/build/llvm/CMakeCache.txt ]; then
-DCMAKE_BUILD_TYPE=DEBUG \
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
-DLLVM_TARGETS_TO_BUILD=host \
-DPython3_FIND_VIRTUALENV=ONLY \
-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-S "${OPENHLS_DIR}"/externals/llvm-project/llvm \
-B "${OPENHLS_DIR}"/build/llvm
Expand Down Expand Up @@ -137,7 +138,7 @@ if [ ! -f "${OPENHLS_DIR}"/build/flopoco_converter/CMakeCache.txt ]; then
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
-DLLVM_TARGETS_TO_BUILD=host \
-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-S "${OPENHLS_DIR}"/flopoco_convert_ext \
-S "${OPENHLS_DIR}"/extensions/flopoco_convert_ext \
-B "${OPENHLS_DIR}"/build/flopoco_converter
fi

Expand All @@ -154,15 +155,4 @@ if [ ! -f "${OPENHLS_DIR}"/build/ghdl/bin/ghdl ]; then
mkdir -p "${OPENHLS_DIR}"/build/ghdl
tar -xvf ghdl-gha-ubuntu-20.04-llvm.tgz -C "${OPENHLS_DIR}"/build/ghdl
fi
fi


# TODO
#PYBIND11_DIR=${PREFIX}/lib/python3.10/site-packages/pybind11/share/cmake/
#PYBIND11_DIR=$(python -c "import pybind11; print(pybind11.get_cmake_dir())")
#-DPYTHON_LIBRARY="/Users/mlevental/miniforge3/envs/openhls/lib/libpython3.10.dylib" -DPYTHON_INCLUDE_DIR="/Users/mlevental/miniforge3/envs/openhls/include/python3.10" \

# -DPYTHON_INCLUDE_DIR="$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")" \
# -DPYTHON_LIBRARY="$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")" \

#-Dpybind11_DIR=/home/mlevental/miniconda3/envs/openhls/lib/python3.10/site-packages/pybind11/share/cmake/pybind11 -DPython_EXECUTABLE=/home/mlevental/miniconda3/envs/openhls/bin/python
fi
9 changes: 1 addition & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,6 @@ def build_torch_mlir(base_cmake_args):
)


def install_torch_mlir_from_wheel():
torch_mlir_wheel = get_latest_torch_mlir()
subprocess.check_call(
[sys.executable, "-m", "pip", "install", torch_mlir_wheel],
cwd=CWD,
)


def build_circt(base_cmake_args):
circt_dir = os.path.join(EXTERNALS, "circt")
circt_build_dir = os.path.join(ROOT_BUILD_DIR, "circt")
Expand Down Expand Up @@ -168,6 +160,7 @@ def build_openhls_translate(base_cmake_args):
f'-DMLIR_DIR={os.path.join(LLVM_BUILD_DIR, "lib", "cmake", "mlir")}',
f'-DLLVM_DIR={os.path.join(LLVM_BUILD_DIR, "lib", "cmake", "llvm")}',
"-DMLIR_ENABLE_BINDINGS_PYTHON=ON",
"-DLLVM_ENABLE_ABI_BREAKING_CHECKS=OFF"
f"-Dpybind11_DIR={pybind11.get_cmake_dir()}",
]
run_cmake(openhls_dir, cmake_args, openhls_build_dir, target="openhls_translate")
Expand Down

0 comments on commit 04ddcda

Please sign in to comment.