我正在编写一个MachineFunctionPass
针对 X86 架构的代码,这会导致修改后的llc
二进制文件。
为了测试我的修改版本,llc
我创建了一堆.c
程序,它们的 MIR 将由我的 pass 处理。
为了简洁起见,我已将包含源代码的目录直接添加到 LLVM 的源代码树中,特别是在$llvm_src_dir/lib/Target/X86/$examples_dir
: 然后我通过将add_subdirectory()
指令附加到$llvm_src_dir/lib/Target/X86/CMakeLists.txt
.
通过这种方式,我将能够直接从 LLVM 的构建目录构建所有内容。
现在:如何在我的指定$examples_dir/CMakeLists.txt
使用 LLVM 的 in-tree llc
?
源码树结构
这是源的目录结构。我省略了所有根目录的子目录,因为我只包含了“有趣的”。
LLVM 定义了llc
目标,tools/llc
而我的源在目录中的位置更深,如下图所示:
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
这就是我编辑CMakeLists.txt
目标架构的方式:
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
我已经尝试过的
我目前正在使用find_path()
查找llc
,但这需要llc
已经编译,因此CMakeLists.txt
如果我不llc
事先编译,我的示例将无法通过验证。
假设路径存在,我最终在我的中使用了一个指令add_custom_command()
,但在我看来这太hacky了。llc
CMakeLists.txt
基本上,我需要将目标添加llc
为我的目标的依赖项,然后使用llc
“路径将我的示例”.bc
文件编译成.s
.
有任何想法吗?
非常感谢!