I am writing a MachineFunctionPass
targeting the X86 architecture which results in a modified llc
binary.
In order to test my modified version of llc
I have created a bunch of .c
programs whose MIR will be handled by my pass.
For the sake of cleanliness, I have added the directory including the sources directly into LLVM's source tree, specifically in $llvm_src_dir/lib/Target/X86/$examples_dir
: I have then plugged it into LLVM build system by appending the add_subdirectory()
directive to $llvm_src_dir/lib/Target/X86/CMakeLists.txt
.
In this way, I will be able to build everything directly from LLVM's build directory.
Now: how do I specify in my $examples_dir/CMakeLists.txt
to use LLVM's in-tree llc
?
Source tree structure
This is the sources' directory structure. I have omitted all the root's children directories since I've only included the "interesting ones".
LLVM's defines the llc
target in tools/llc
while my sources live quite deeper in the directory as shown in the following tree:
llvm_src_dir
├── bindings
├── cmake
├── docs
├── examples
├── include
├── lib
└── Target
└── X86
/*
* My git repo is here. LLVM's and
* my MachineFunctionPass' files
* live here
*/
├── .git
├── CMakeLists.txt // This is LLVM's X86 CMakeLists.txt
└── examples
└── CMakeLists.txt // My CMakeLists.txt
├── projects
├── resources
├── runtimes
├── test
├── tools
└── llc
└── CMakeLists.txt // this is where LLVM's llc target is defined
├── unittests
└── utils
lib/Target/X86/CMakeLists.txt
This is how I edited the CMakeLists.txt
of the architecture I'm targeting:
set (CMAKE_CXX_STANDARD 14)
set(LLVM_TARGET_DEFINITIONS X86.td)
tablegen(LLVM X86GenAsmMatcher.inc -gen-asm-matcher)
tablegen(LLVM X86GenAsmWriter.inc -gen-asm-writer)
tablegen(LLVM X86GenAsmWriter1.inc -gen-asm-writer -asmwriternum=1)
tablegen(LLVM X86GenCallingConv.inc -gen-callingconv)
tablegen(LLVM X86GenDAGISel.inc -gen-dag-isel)
tablegen(LLVM X86GenDisassemblerTables.inc -gen-disassembler)
tablegen(LLVM X86GenEVEX2VEXTables.inc -gen-x86-EVEX2VEX-tables)
tablegen(LLVM X86GenFastISel.inc -gen-fast-isel)
tablegen(LLVM X86GenGlobalISel.inc -gen-global-isel)
tablegen(LLVM X86GenInstrInfo.inc -gen-instr-info)
tablegen(LLVM X86GenRegisterBank.inc -gen-register-bank)
tablegen(LLVM X86GenRegisterInfo.inc -gen-register-info)
tablegen(LLVM X86GenSubtargetInfo.inc -gen-subtarget)
if (X86_GEN_FOLD_TABLES)
tablegen(LLVM X86GenFoldTables.inc -gen-x86-fold-tables)
endif ()
add_public_tablegen_target(X86CommonTableGen)
set(MY_SOURCES
a.cpp
b.cpp
c.cpp
)
set(sources
ShadowCallStack.cpp
X86AsmPrinter.cpp
X86CallFrameOptimization.cpp
X86CallingConv.cpp
X86CallLowering.cpp
X86CmovConversion.cpp
X86DomainReassignment.cpp
X86ExpandPseudo.cpp
X86FastISel.cpp
X86FixupBWInsts.cpp
X86FixupLEAs.cpp
X86AvoidStoreForwardingBlocks.cpp
X86FixupSetCC.cpp
X86FlagsCopyLowering.cpp
X86FloatingPoint.cpp
X86FrameLowering.cpp
X86InstructionSelector.cpp
X86ISelDAGToDAG.cpp
X86ISelLowering.cpp
X86IndirectBranchTracking.cpp
X86InterleavedAccess.cpp
X86InstrFMA3Info.cpp
X86InstrFoldTables.cpp
X86InstrInfo.cpp
X86EvexToVex.cpp
X86LegalizerInfo.cpp
X86MCInstLower.cpp
X86MachineFunctionInfo.cpp
X86MacroFusion.cpp
X86OptimizeLEAs.cpp
X86PadShortFunction.cpp
X86RegisterBankInfo.cpp
X86RegisterInfo.cpp
X86RetpolineThunks.cpp
X86SelectionDAGInfo.cpp
X86ShuffleDecodeConstantPool.cpp
X86SpeculativeLoadHardening.cpp
X86Subtarget.cpp
X86TargetMachine.cpp
X86TargetObjectFile.cpp
X86TargetTransformInfo.cpp
X86VZeroUpper.cpp
X86WinAllocaExpander.cpp
X86WinEHState.cpp
${MY_SOURCES}
)
add_llvm_target(X86CodeGen ${sources})
add_subdirectory(AsmParser)
add_subdirectory(Disassembler)
add_subdirectory(InstPrinter)
add_subdirectory(MCTargetDesc)
add_subdirectory(TargetInfo)
add_subdirectory(Utils)
add_subdirectory(examples) // my examples directory
What I have already tried
I am currently using find_path()
to find llc
but this requires llc
to be already compiled and therefore my examples CMakeLists.txt
will fail the validation if I don't compile llc
beforehand.
Assuming the path exists, I finally use an add_custom_command()
directive to use llc
in my CMakeLists.txt
but this is way too hacky in my opinion.
Basically, I need to add the llc
target as a dependency for my targets and then use llc
's path to compile my examples' .bc
files into .s
.
Any ideas?
Thank you very much!