How to run openmp in mlir?

I get an obj file by lowering mlir. This is my lower pipline.

@${MLIR_OPT} ./test.mlir \
		-pass-pipeline="builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | \
	${MLIR_OPT} \
		--arith-expand \
		--empty-tensor-to-alloc-tensor \
		--linalg-bufferize \
		--convert-linalg-to-affine-loops \
		--affine-loop-fusion \
		--affine-parallelize \
		--lower-affine \
		--convert-scf-to-openmp \
		--func-bufferize \
		--arith-bufferize \
		--tensor-bufferize \
		--finalizing-bufferize \
		--convert-vector-to-scf \
		--expand-strided-metadata \
		--convert-vector-to-llvm \
		--memref-expand \
		--arith-expand \
		--convert-arith-to-llvm \
		--finalize-memref-to-llvm \
		--convert-scf-to-cf \
		--convert-openmp-to-llvm \
		--convert-arith-to-llvm \
		--convert-math-to-llvm \
		--convert-math-to-libm \
		--llvm-request-c-wrappers \
		--convert-func-to-llvm \
		--reconcile-unrealized-casts | \
	${MLIR_TRANSLATE} \
		-mlir-to-llvmir | \
	${LLVM_AS} | \
	${LLC} -filetype=obj --relocation-model=pic ${OPT_FLAG} -o test.o

When I link this test.o, I get some errors.

LLVMDialectModule:(.text+0x3c738c): undefined reference to `__kmpc_global_thread_num'
/usr/bin/ld: LLVMDialectModule:(.text+0x3c73dc): undefined reference to `__kmpc_for_static_init_8u'
/usr/bin/ld: LLVMDialectModule:(.text+0x3c742d): undefined reference to `__kmpc_for_static_fini'
/usr/bin/ld: LLVMDialectModule:(.text+0x3c7439): undefined reference to `__kmpc_global_thread_num'
/usr/bin/ld: LLVMDialectModule:(.text+0x3c7447): undefined reference to `__kmpc_barrier'
/usr/bin/ld: LLVMDialectModule:(.text+0x3c747e): undefined reference to `__kmpc_global_thread_num'
/usr/bin/ld: LLVMDialectModule:(.text+0x3c756b): undefined reference to `__kmpc_fork_call'

My CMAKELists.txt is as follows.

add_executable(test test_run.cc llama.o)
find_package(OpenMP REQUIRED)
add_compile_options(${OpenMP_CXX_FLAGS})
target_link_libraries(test mlir_c_runner_utils OpenMP::OpenMP_CXX)

It seems lack some libraries to link. But I have no idea. Thank you for your help.

You have to link the OpenMP runtime library (libomp.a, libomp.so). llvm-project/openmp contains the OpenMP runtime. You can enable this project while building llvm.

Thank you. I tried to enable openmp when I build llvm. But I get this CMake Error.

CMake Error: install(EXPORT "LLVMExports" ...) includes target "omptarget" which requires target "omp" that is not in any export set.
CMake Error in cmake/modules/CMakeLists.txt:
  export called with target "omptarget" which requires target "omp" that is
  not in any export set.

I have solve it! In llvm17, it should be LLVM_ENABLE_RUNTIMES=“openmp”. Thank you for your help.

1 Like