When I cross compile the MLIR, llvm-min-tblgen: Exec format error

I’m cross-compiling the risc-v version of MLIR, in fact I know where the problem is.The reason for this problem is that I executed the risc-v version of llvm-min-tblgen, however my host is x86.I need to use the x86 version of llvm-min-tblgen.

// just like this
-DMLIR_TABLEGEN=$PWD/../../llvm/build/bin/mlir-tblgen \

But there is a problem.I didn’t find the relevant cmake variables to correspond to llvm-min-tblgen.I looked up the documentation and searched the entire project using vscode to no avail.

I think I can be more specific about this problem.You can see that llvm-min-tblgen is used here.Here I can’t use llvm-min-tblgen in the build-cross-mlir/bin/ directory, because it is the riscv version, now there is a problem how I can define cmake variables to let cmake use the llvm-min-tblgen I specified to build the project.Or is there another way to solve this problem here? I would be grateful if someone helps me.

build-cross-mlir/bin/llvm-min-tblgen -gen-attrs -I llvm/llvm/include/llvm/IR -I /build-cross-mlir/include -I llvm/llvm/include/llvm/IR/Attributes.td --write-if-changed -o include/llvm/IR/Attributes.inc -d include/llvm/IR/Attributes.inc.d
/bin/sh: 1: /home/sen/buddy-mlir/thirdparty/build-cross-mlir/bin/llvm-min-tblgen: Exec format error

@chapuni introduced this recently I believe and may be able to help?

When you cross-compile Clang/LLVM/LLD the build system detects this and builds native TableGen binaries automatically for you. Does MLIR not do this? That would surprise me, and would seem like a bit of an oversight.

Could you try:

> file /home/sen/buddy-mlir/thirdparty/build-cross-mlir/bin/llvm-min-tblgen

The error does not seem to be about MLIR part of the build, this is LLVM itself (llvm-min-tblgen binary, generating include/llvm/IR/Attributes.inc).
The issue may also be a non-standard CMake invocation, @linuxlonelyeagle it would be easier if you just provide your complete CMake invocation instead of providing partial fragments.

Could you use llvm-tblgen?
llvm-min-tblgen is assumed as build-only tool and is not installed.

Yes,but it will report an error: build-cross-mlir/bin/llvm-min-tblgen: Exec format error .

There is the complete CMake invocation.

 cmake -G Ninja ../../llvm/llvm \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_HOST_TRIPLE=riscv64-unknown-linux-gnu \
    -DLLVM_DEFAULT_TARGET_TRIPLE=riscv64-unknown-linux-gnu \
    -DCMAKE_C_COMPILER=$PWD/../build-local-clang/bin/clang \
    -DCMAKE_CXX_COMPILER=$PWD/../build-local-clang/bin/clang++ \
    -DCMAKE_C_FLAGS="--target=riscv64-unknown-linux-gnu --sysroot=$PWD/../build-riscv-gnu-toolchain/sysroot --gcc-toolchain=$PWD/../build-riscv-gnu-toolchain" \
    -DCMAKE_CXX_FLAGS="--target=riscv64-unknown-linux-gnu --sysroot=$PWD/../build-riscv-gnu-toolchain/sysroot --gcc-toolchain=$PWD/../build-riscv-gnu-toolchain" \
    -DMLIR_TABLEGEN=$PWD/../../llvm/build/bin/mlir-tblgen \
    -DLLVM_TABLEGEN=$PWD/../../llvm/build/bin/llvm-tblgen \
    -DMLIR_LINALG_ODS_YAML_GEN=$PWD/../../llvm/build/bin/mlir-linalg-ods-yaml-gen \
    -DMLIR_PDLL_TABLEGEN=$PWD/../../llvm/build/bin/mlir-pdll \

llvm-min-tblgen is actually installed, but what is actually installed is the risc-v version of llvm-min-tblgen, which makes it impossible to execute llvm-min-tblgen, I actually installed the x86 version of min-tblgen, but I wasn’t able to find the CMake variable to specify the use of my x86 version of llvm-min-tblgen.

I believe that when using -DCMAKE_CROSSCOMPILING=True, the host tablegen tools are supposed to be built for the host config automatically: that is you don’t need to passed these in. What happen if you don’t set these options?
(actually I’m not sure for mlir-tblgen, try to not unset the llvm-tblgen one only)

It’s very possible that these aren’t setup for handling CMAKE_CROSSCOMPILING, patch welcome :slight_smile: (but that’s unrelated to the llvm-min-tblgen issue).

By the way, are you at HEAD? Seems like what you see could be explained before this bug fix: add_tablegen: Quick fix to reflect LLVM_TABLEGEN to llvm-min-tblgen · llvm/llvm-project@95d4506 · GitHub (almost 3 weeks ago)


I’m using LLVM from a few weeks ago, I can’t remember exactly, but I don’t see the patch you showed me in the TableGen.cmake of the LLVM I’m using.I think that should be the problem, thank you.

Thank you again, I used the latest LLVM, which solved the problem. :blush:

1 Like