1

I am trying to set up llvm-mca to work with CMake. The official documentation gives simple commands to output the info such as.

$ clang foo.c -O2 -target x86_64-unknown-unknown -S -o - | llvm-mca -mcpu=btver2

This works great for simple files. However I am unsure how to expand on this and implement it for a practical project using CMake.

user6574932
  • 61
  • 1
  • 5

1 Answers1

0

Posting my answer for anyone else who comes across this problem.

#compile with optimizations on
target_compile_options(Executable PRIVATE -O3 -g)

#this will convert the passed file to assembly
add_custom_command(
        OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/assembly.s
        COMMAND ${CMAKE_CXX_COMPILER}
        ARGS ${CMAKE_CURRENT_SOURCE_DIR}/foo.cpp -O3 -S -o ${CMAKE_CURRENT_SOURCE_DIR}/assembly.s
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/foo.cpp
        COMMENT "Generating The Assembly File"
        VERBATIM
)

#this will convert the assembly analysis
add_custom_command(
        OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/analysis.txt
        COMMAND llvm-mca
        ARGS -mcpu=btver2 -timeline ${CMAKE_CURRENT_SOURCE_DIR}/assembly.s -o ${CMAKE_CURRENT_SOURCE_DIR}/analysis.txt
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/assembly.s
        COMMENT "Running LLVM MCA"
        VERBATIM
)

add_custom_target(run ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/assembly.s ${CMAKE_CURRENT_SOURCE_DIR}/analysis.txt)

Probably worth mentioning that I am by no means an expert, however LLVM MCA seems to be used for analysing a single file at a time. This means that a simple command (such as I pasted above when asking the question) is realistically sufficient.

Also an error 'error: no assembly instructions found'. Means optimizations probably removed all code between the START and END macros (happened to me when I made a simple program for testing).

user6574932
  • 61
  • 1
  • 5