onnx / onnx-mlir

Representation and Reference Lowering of ONNX Models in MLIR Compiler Infrastructure
Apache License 2.0
767 stars 320 forks source link

How to pass clang options to be applied to generated object file? #2950

Closed jinchenglee closed 4 weeks ago

jinchenglee commented 1 month ago

For example, I can compile my binary using clang -target riscv64-unknown-elf -fno-builtin-printf -fno-lto -O2 -c example.c -o example.o, how may I pass down the additional options through onnx-mlir? This might be a dumb question as I'm new to LLVM toolsets.

I tried $ onnx-mlir -mtriple=riscv64-unknown-elf -mllvm="-fno-builtin-printf -fno-lto -O2" --EmitObj ./nn.onnx:

[1/6] Mon Sep 23 15:21:19 2024 (0s) Importing ONNX Model to MLIR Module from "nn.onnx"
[2/6] Mon Sep 23 15:21:19 2024 (0s) Compiling and Optimizing MLIR Module
[3/6] Mon Sep 23 15:21:19 2024 (0s) Translating MLIR Module to LLVM and Generating LLVM Optimized Bitcode
opt: Unknown command line argument '-fno-builtin-printf -fno-lto -O2'.  Try: 'opt --help'
opt: Did you mean '--rotation-prepare-for-lto'?
opt -O0 --mtriple=riscv64-unknown-elf --code-model small -fno-builtin-printf -fno-lto -O2 -o ./nn.bc ./nn.unoptimized.bc
Error message:
Program path: ~/git_repo/llvm-project/build/bin/opt
Command execution failed.
jinchenglee commented 1 month ago

Seems only a single option is allowed/supported.

$~/experimental/onnx-mlir-example$ onnx-mlir -mtriple=riscv64-unknown-elf -mllvm="--disable-simplify-libcalls -O2" --EmitObj ./nn.onnx
[1/6] Mon Sep 23 15:35:49 2024 (0s) Importing ONNX Model to MLIR Module from "nn.onnx"
[2/6] Mon Sep 23 15:35:49 2024 (0s) Compiling and Optimizing MLIR Module
[3/6] Mon Sep 23 15:35:49 2024 (0s) Translating MLIR Module to LLVM and Generating LLVM Optimized Bitcode
opt: Unknown command line argument '--disable-simplify-libcalls -O2'.  Try: 'opt --help'
opt: Did you mean '--disable-simplify-libcalls'?
opt -O0 --mtriple=riscv64-unknown-elf --code-model small --disable-simplify-libcalls -O2 -o ./nn.bc ./nn.unoptimized.bc
Error message:
Program path: ~/git_repo/llvm-project/build/bin/opt
Command execution failed.
$:~/experimental/onnx-mlir-example$ onnx-mlir -mtriple=riscv64-unknown-elf -mllvm="--disable-simplify-libcalls" --EmitObj ./nn.onnx
[1/6] Mon Sep 23 15:36:40 2024 (0s) Importing ONNX Model to MLIR Module from "nn.onnx"
[2/6] Mon Sep 23 15:36:40 2024 (0s) Compiling and Optimizing MLIR Module
[3/6] Mon Sep 23 15:36:40 2024 (0s) Translating MLIR Module to LLVM and Generating LLVM Optimized Bitcode
[4/6] Mon Sep 23 15:36:40 2024 (0s) Generating Object from LLVM Bitcode
[5/6] Mon Sep 23 15:36:40 2024 (0s) Compilation completed
AlexandreEichenberger commented 1 month ago

I have seen the system being fragile with multiple options in a string, not entirely sure why. One workaround is to add support for multiple instances of -mllvm=<single option>.

Do you want me to add this feature?

jinchenglee commented 1 month ago

I have seen the system being fragile with multiple options in a string, not entirely sure why. One workaround is to add support for multiple instances of -mllvm=<single option>.

Do you want me to add this feature?

That would be nice. Thanks.

chentong319 commented 1 month ago

The reason is that strings have to be passed separately in the argv[][] for the command. There is another small issue for the example here: -O0 is created by default. If -O2 is passed the mllvm, I got error for "multiple -O#". I am going create a PR to fix this issue of multiple options in mllvm.

AlexandreEichenberger commented 1 month ago

Thanks @chentong319

chentong319 commented 1 month ago

@jinchenglee Which step do you intend to use these options, opt or llc? The current implementation has another issue is that the -mllvm is used in both opt and llc. But they may support different options. I need to introduce new options for onnx-mlir.

jinchenglee commented 1 month ago

I'm just exploring and besides I'm pretty greenhorn about LLVM toolsets. It seems to me it would be better to separate them as the tools (llc and opt) are different from beginning?

chentong319 commented 1 month ago

@jinchenglee There are three options in onnx-mlir to pass options to opt and/or llc

  1. mllvm: only one is used. passed to both opt and llc
  2. Xopt: multiple is allowed. passed to only opt
  3. Xllc: multiple is allowed. passed to only llc

I merged a new PR to handle the issue with mllvm. I added example of how to use Xopt and Xllc. Hope this PR2955 solved your issue.

AlexandreEichenberger commented 4 weeks ago

@jinchenglee can we close this issue?

jinchenglee commented 4 weeks ago

@jinchenglee can we close this issue?

Sorry, I don't yet have time to try these yet. But for what this ticket is asking for, it's safe to close. If any issues, I or anyone can reopen it. Thanks.