Skip to content

Commit

Permalink
[DAP] Fix 'dap.fir' operation. (#432)
Browse files Browse the repository at this point in the history
* [DAP] Reformat CMakeLists.txt.

* [DAP] Debug 'dap.fir' operation.

Co-authorized-by: ShiHaoGao <[email protected]>
  • Loading branch information
taiqzheng authored Nov 27, 2024
1 parent b8f1ec9 commit 94f276d
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 77 deletions.
2 changes: 1 addition & 1 deletion examples/DAPDialect/FIRLowpass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main() {
printLogLabel();
std::cout << "Running FIR operation..." << std::endl;
const auto loadStart = std::chrono::high_resolution_clock::now();
dap::fir(&inputContainer, &kernel, &outputMemRef);
dap::FIR(&inputContainer, &kernel, &outputMemRef);
const auto loadEnd = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double, std::milli> loadTime =
loadEnd - loadStart;
Expand Down
6 changes: 3 additions & 3 deletions frontend/Interfaces/buddy/DAP/DSP/FIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace detail {
extern "C" {
// TODO: support both float and double.
void _mlir_ciface_buddy_fir(MemRef<float, 1> *inputBuddyConv1D,
MemRef<float, 1> *kernelBuddyConv1D,
MemRef<float, 1> *outputBuddyConv1D);
MemRef<float, 1> *kernelBuddyConv1D,
MemRef<float, 1> *outputBuddyConv1D);
}
} // namespace detail

Expand Down Expand Up @@ -67,7 +67,7 @@ void firLowpass(MemRef<T, N> &input, WINDOW_TYPE type, size_t len, T cutoff,
}

template <typename T, size_t N>
void fir(MemRef<float, N> *input, MemRef<T, N> *filter,
void FIR(MemRef<float, N> *input, MemRef<T, N> *filter,
MemRef<float, N> *output) {
if (N != 1)
assert(0 && "Only mono audio is supported for now.");
Expand Down
154 changes: 84 additions & 70 deletions frontend/Interfaces/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ elseif (HAVE_NEON)
set(SPLITING_SIZE 16)
endif ()

#-------------------------------------------------------------------------------
# Generate Buddy DIP Library: BuddyLibDIP
#-------------------------------------------------------------------------------

add_custom_command(OUTPUT DIP.o
COMMAND ${CMAKE_BINARY_DIR}/bin/buddy-opt ${CMAKE_CURRENT_SOURCE_DIR}/DIP.mlir
-lower-dip="DIP-strip-mining=${SPLITING_SIZE}"
Expand Down Expand Up @@ -37,87 +41,97 @@ SET_TARGET_PROPERTIES(BuddyLibDIP PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_DIRECTORY}
)

add_custom_command(OUTPUT DAP.o
COMMAND ${CMAKE_BINARY_DIR}/bin/buddy-opt ${CMAKE_CURRENT_SOURCE_DIR}/DAP.mlir
-lower-dap="DAP-vector-splitting=${SPLITING_SIZE}"
--convert-linalg-to-affine-loops
-arith-expand
-lower-affine
-convert-scf-to-cf
-convert-math-to-llvm
-convert-vector-to-llvm
-finalize-memref-to-llvm
-llvm-request-c-wrappers
-convert-func-to-llvm
-reconcile-unrealized-casts |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llc
-mtriple=${BUDDY_TARGET_TRIPLE}
-mattr=${BUDDY_OPT_ATTR}
--filetype=obj
-o ${CMAKE_CURRENT_BINARY_DIR}/DAP.o
#-------------------------------------------------------------------------------
# Generate Buddy DAP Library: BuddyLibDAP
#-------------------------------------------------------------------------------

add_custom_command(
OUTPUT DAP.o
COMMAND
${CMAKE_BINARY_DIR}/bin/buddy-opt ${CMAKE_CURRENT_SOURCE_DIR}/DAP.mlir
-lower-dap="DAP-vector-splitting=${SPLITING_SIZE}"
--convert-linalg-to-affine-loops
-arith-expand
-lower-affine
-convert-scf-to-cf
-convert-math-to-llvm
-convert-vector-to-llvm
-finalize-memref-to-llvm
-llvm-request-c-wrappers
-convert-func-to-llvm
-reconcile-unrealized-casts |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llc
-mtriple=${BUDDY_TARGET_TRIPLE}
-mattr=${BUDDY_OPT_ATTR}
--filetype=obj
-o ${CMAKE_CURRENT_BINARY_DIR}/DAP.o
DEPENDS mlir-translate llc buddy-opt
)
)

add_custom_command(OUTPUT DAP-extend.o
COMMAND ${CMAKE_BINARY_DIR}/bin/buddy-opt ${CMAKE_CURRENT_SOURCE_DIR}/DAP-extend.mlir
-extend-dap
-one-shot-bufferize
-convert-linalg-to-loops
-convert-scf-to-cf
-expand-strided-metadata
-lower-affine
-convert-vector-to-llvm
-memref-expand
-arith-expand
-convert-arith-to-llvm
-finalize-memref-to-llvm
-convert-math-to-llvm
-llvm-request-c-wrappers
-convert-func-to-llvm
-reconcile-unrealized-casts |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llc
-mtriple=${BUDDY_TARGET_TRIPLE}
-mattr=${BUDDY_OPT_ATTR}
-filetype=obj -relocation-model=pic
-o ${CMAKE_CURRENT_BINARY_DIR}/DAP-extend.o
add_custom_command(
OUTPUT DAP-extend.o
COMMAND
${CMAKE_BINARY_DIR}/bin/buddy-opt ${CMAKE_CURRENT_SOURCE_DIR}/DAP-extend.mlir
-extend-dap
-one-shot-bufferize
-convert-linalg-to-loops
-convert-scf-to-cf
-expand-strided-metadata
-lower-affine
-convert-vector-to-llvm
-memref-expand
-arith-expand
-convert-arith-to-llvm
-finalize-memref-to-llvm
-convert-math-to-llvm
-llvm-request-c-wrappers
-convert-func-to-llvm
-reconcile-unrealized-casts |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llc
-mtriple=${BUDDY_TARGET_TRIPLE}
-mattr=${BUDDY_OPT_ATTR}
-filetype=obj -relocation-model=pic
-o ${CMAKE_CURRENT_BINARY_DIR}/DAP-extend.o
DEPENDS mlir-translate llc buddy-opt
)
)

add_custom_command(OUTPUT DAPVectorization.o
COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/DAP.mlir |
sed 's/buddy_fir/buddy_fir_vectorization/' |
sed 's/buddy_iir/buddy_iir_vectorization/' |
sed 's/buddy_biquad/buddy_biquad_vectorization/' |
${CMAKE_BINARY_DIR}/bin/buddy-opt
-vectorize-dap
-convert-linalg-to-affine-loops
-arith-expand
-lower-affine
-convert-scf-to-cf
-convert-math-to-llvm
-convert-vector-to-llvm
-finalize-memref-to-llvm
-llvm-request-c-wrappers
-convert-func-to-llvm
-reconcile-unrealized-casts |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llc
-mtriple=${BUDDY_TARGET_TRIPLE}
-mattr=${BUDDY_OPT_ATTR}
-filetype=obj
-o ${CMAKE_CURRENT_BINARY_DIR}/DAPVectorization.o
add_custom_command(
OUTPUT DAPVectorization.o
COMMAND
cat ${CMAKE_CURRENT_SOURCE_DIR}/DAP.mlir |
sed -e 's/@buddy_fir/@buddy_fir_vectorization/'
-e 's/@buddy_iir/@buddy_iir_vectorization/'
-e 's/@buddy_biquad/@buddy_biquad_vectorization/' |
${CMAKE_BINARY_DIR}/bin/buddy-opt
-vectorize-dap
-convert-linalg-to-affine-loops
-arith-expand
-lower-affine
-convert-scf-to-cf
-convert-math-to-llvm
-convert-vector-to-llvm
-finalize-memref-to-llvm
-llvm-request-c-wrappers
-convert-func-to-llvm
-reconcile-unrealized-casts |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llc
-mtriple=${BUDDY_TARGET_TRIPLE}
-mattr=${BUDDY_OPT_ATTR}
-filetype=obj
-o ${CMAKE_CURRENT_BINARY_DIR}/DAPVectorization.o
DEPENDS mlir-translate llc buddy-opt
)
)

add_library(BuddyLibDAP STATIC
DAP.o
DAP-extend.o
DAPVectorization.o
)
)

SET_TARGET_PROPERTIES(BuddyLibDAP PROPERTIES
LINKER_LANGUAGE CXX
ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_DIRECTORY}
)
)
60 changes: 57 additions & 3 deletions midend/lib/Conversion/LowerDAP/LowerDAPPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,67 @@ class DAPFirLowering : public OpRewritePattern<dap::FirOp> {
LogicalResult matchAndRewrite(dap::FirOp op,
PatternRewriter &rewriter) const override {
auto loc = op->getLoc();
// auto ctx = op->getContext();
auto ctx = op->getContext();
Value input = op->getOperand(0);
Value kernel = op->getOperand(1);
Value output = op->getOperand(2);

rewriter.create<linalg::Conv1DOp>(loc, ValueRange{input, kernel},
ValueRange{output});
Value c0 = rewriter.create<arith::ConstantIndexOp>(loc, 0);
Value c1 = rewriter.create<arith::ConstantIndexOp>(loc, 1);
FloatType f32 = FloatType::getF32(ctx);
Value f0 =
rewriter.create<arith::ConstantFloatOp>(loc, APFloat(float(0.0)), f32);

Value kernelSize = rewriter.create<memref::DimOp>(loc, kernel, c0);
Value dataLen = rewriter.create<memref::DimOp>(loc, output, c0);

// Populate the FIR pipeline by padding the `input` with [`kernelSize`-1]
// zeros at the beginning. Compute only the padding section of the input
// data.
Value fillInLen = rewriter.create<arith::SubIOp>(loc, kernelSize, c1);
rewriter.create<scf::ForOp>(
loc, c0, fillInLen, c1, ValueRange{std::nullopt},
[&](OpBuilder &b, Location loc, Value iv_n, ValueRange iargs) {
Value upperBound = b.create<arith::AddIOp>(loc, iv_n, c1);
Value outFinal =
b.create<scf::ForOp>(
loc, c0, upperBound, c1, ValueRange{f0},
[&](OpBuilder &b, Location loc, Value iv_k,
ValueRange iargs) {
Value i = b.create<arith::SubIOp>(loc, iv_n, iv_k);
Value in = b.create<memref::LoadOp>(loc, input, i);
Value k = b.create<memref::LoadOp>(loc, kernel, iv_k);
Value mul = b.create<arith::MulFOp>(loc, in, k);
Value outNext =
b.create<arith::AddFOp>(loc, iargs[0], mul);
b.create<scf::YieldOp>(loc, outNext);
})
.getResult(0);
b.create<memref::StoreOp>(loc, outFinal, output, ValueRange{iv_n});
b.create<scf::YieldOp>(loc, std::nullopt);
});

// Compute the input data following the padding section.
rewriter.create<scf::ForOp>(
loc, fillInLen, dataLen, c1, ValueRange{std::nullopt},
[&](OpBuilder &b, Location loc, Value iv_n, ValueRange iargs) {
Value outFinal =
b.create<scf::ForOp>(
loc, c0, kernelSize, c1, ValueRange{f0},
[&](OpBuilder &b, Location loc, Value iv_k,
ValueRange iargs) {
Value i = b.create<arith::SubIOp>(loc, iv_n, iv_k);
Value in = b.create<memref::LoadOp>(loc, input, i);
Value k = b.create<memref::LoadOp>(loc, kernel, iv_k);
Value mul = b.create<arith::MulFOp>(loc, in, k);
Value outNext =
b.create<arith::AddFOp>(loc, iargs[0], mul);
b.create<scf::YieldOp>(loc, outNext);
})
.getResult(0);
b.create<memref::StoreOp>(loc, outFinal, output, ValueRange{iv_n});
b.create<scf::YieldOp>(loc, std::nullopt);
});

rewriter.eraseOp(op);
return success();
Expand Down

0 comments on commit 94f276d

Please sign in to comment.