buddy-compiler / buddy-benchmark

Benchmark Framework for Buddy Projects
Apache License 2.0
44 stars 33 forks source link

OpenMP need a different linking way #88

Closed EllisLambda closed 10 months ago

EllisLambda commented 10 months ago

Is your feature request related to a problem? Please describe. I tried to use these CMake code for the BatchMatMulBroadcast.mlir in my PR to compile static library. -fopenmp could only be used in clang and I have ruled out the possibility that the problem arises from using clang instead of llc.

function(build_batch_matmul_broadcast_omp step)
  add_custom_command(OUTPUT batch-matmul-broadcast-${step}-omp.o
    COMMAND cat ${BUDDY_SOURCE_DIR}/benchmarks/OpOptimization/MatMul/BatchMatMulBroadcast.mlir |
          sed 's/batch_matmul_broadcast_STEP_PLACEHOLDER/batch_matmul_broadcast_STEP_PLACEHOLDER_omp/g' |
          sed 's/STEP_PLACEHOLDER/${step}/g' |
          ${BUDDY_MLIR_BUILD_DIR}/bin/buddy-opt
            -batchmatmul-optimize="step-placeholder=${step}"
            -expand-strided-metadata
            -affine-super-vectorize
            -lower-affine
            -convert-scf-to-openmp
            -convert-vector-to-llvm
            -finalize-memref-to-llvm
            -convert-scf-to-cf
            -convert-linalg-to-llvm
            -convert-openmp-to-llvm
            -llvm-request-c-wrappers
            -convert-func-to-llvm
            -reconcile-unrealized-casts | 
          ${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
          clang -c -x ir -O3 --target=${BUDDY_OPT_TRIPLE} -fopenmp
            -o ${BUDDY_BINARY_DIR}/../benchmarks/OpOptimization/MatMul/batch-matmul-broadcast-${step}-omp.o -
  )
  add_library(BatchMatMulBroadcast${step}OMP STATIC batch-matmul-broadcast-${step}-omp.o)
  set_target_properties(BatchMatMulBroadcast${step}OMP PROPERTIES LINKER_LANGUAGE CXX)
endfunction()

Under normal circumstances, OpenMP requires dynamic library to start. I originally wanted to try statically linking the OpenMP library, gcc has libgomp.a and the compiler can do that, but I found that the Clang/LLVM official discourages this approach. nm -n shows that the entry name of the library is named batch_matmul_broadcast_64_omp..omp_par, not starts with _mlir_ciface_, also the C++ program cannot pass the Memref parameters to the MLIR function, I guess it should be called by the OpenMP dynamic library, but I can't find the way to use it.

Describe the solution you'd like Discuss various possibilities to solve this problem. Perhaps correctly utilizing OpenMP::OpenMP_CXX in CMake is a good idea.

xlinsist commented 10 months ago

We had better dive into this issue after PR https://github.com/buddy-compiler/buddy-benchmark/pull/73 and https://github.com/buddy-compiler/buddy-mlir/pull/187 are merged, since this issue depends on the previous PRs, but they haven't been fully settled. Other reviewers may find it hard to understand the background hided in a downstream file.

EllisLambda commented 10 months ago

We had better dive into this issue after PR #73 and buddy-compiler/buddy-mlir#187 are merged, since this issue depends on the previous PRs, but they haven't been fully settled. Other reviewers may find it hard to understand the background hided in a downstream file.

The problem had been solved.