|
| 1 | +//===- llvm/unittest/Linker/LinkModulesTest.cpp - IRBuilder tests ---------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#include "llvm/MCLinker/MCLinker.h" |
| 9 | +#include "llvm/MCLinker/MCPipeline.h" |
| 10 | +#include "llvm/ModuleSplitter/ModuleSplitter.h" |
| 11 | + |
| 12 | +#include "llvm/ADT/STLExtras.h" |
| 13 | +#include "llvm/Analysis/MemoryLocation.h" |
| 14 | +#include "llvm/Analysis/OptimizationRemarkEmitter.h" |
| 15 | +#include "llvm/AsmParser/Parser.h" |
| 16 | +#include "llvm/CodeGen/AsmPrinter.h" |
| 17 | +#include "llvm/CodeGen/MachineModuleInfo.h" |
| 18 | +#include "llvm/CodeGen/SelectionDAG.h" |
| 19 | +#include "llvm/CodeGen/TargetLowering.h" |
| 20 | +#include "llvm/CodeGen/TargetPassConfig.h" |
| 21 | +#include "llvm/IR/BasicBlock.h" |
| 22 | +#include "llvm/IR/DataLayout.h" |
| 23 | +#include "llvm/IR/Function.h" |
| 24 | +#include "llvm/IR/IRBuilder.h" |
| 25 | +#include "llvm/IR/LegacyPassManager.h" |
| 26 | +#include "llvm/IR/MDBuilder.h" |
| 27 | +#include "llvm/IR/Module.h" |
| 28 | +#include "llvm/MC/MCStreamer.h" |
| 29 | +#include "llvm/MC/TargetRegistry.h" |
| 30 | +#include "llvm/Support/KnownBits.h" |
| 31 | +#include "llvm/Support/SourceMgr.h" |
| 32 | +#include "llvm/Support/TargetSelect.h" |
| 33 | +#include "llvm/Target/TargetLoweringObjectFile.h" |
| 34 | +#include "llvm/Target/TargetMachine.h" |
| 35 | +#include "llvm/Testing/Support/Error.h" |
| 36 | +#include "gmock/gmock.h" |
| 37 | +#include "gtest/gtest.h" |
| 38 | + |
| 39 | +using namespace llvm; |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +class MCLinkerTest : public testing::Test { |
| 44 | +protected: |
| 45 | + static void SetUpTestCase() { |
| 46 | + LLVMInitializeX86TargetInfo(); |
| 47 | + LLVMInitializeX86TargetMC(); |
| 48 | + LLVMInitializeX86Target(); |
| 49 | + LLVMInitializeX86AsmPrinter(); |
| 50 | + } |
| 51 | + |
| 52 | + // Get TargetMachine. |
| 53 | + std::unique_ptr<TargetMachine> getTargetMachine() { |
| 54 | + // Get target triple for X86_64 |
| 55 | + Triple TargetTriple("x86_64--"); |
| 56 | + std::string Error; |
| 57 | + const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error); |
| 58 | + if (!T) |
| 59 | + return nullptr; |
| 60 | + |
| 61 | + TargetOptions Options; |
| 62 | + return std::unique_ptr<TargetMachine>(T->createTargetMachine( |
| 63 | + TargetTriple, "", "", Options, Reloc::Model::PIC_, {}, |
| 64 | + CodeGenOptLevel::Default)); |
| 65 | + } |
| 66 | + |
| 67 | + std::unique_ptr<MCContext> getMCContext(TargetMachine &TM) { |
| 68 | + Triple TargetTriple("x86_64--"); |
| 69 | + std::unique_ptr<MCContext> Ctx( |
| 70 | + new MCContext(TargetTriple, TM.getMCAsmInfo(), TM.getMCRegisterInfo(), |
| 71 | + TM.getMCSubtargetInfo())); |
| 72 | + |
| 73 | + Ctx->setObjectFileInfo(TM.getObjFileLowering()); |
| 74 | + TM.getObjFileLowering()->Initialize(*Ctx, TM); |
| 75 | + Ctx->setObjectFileInfo(TM.getObjFileLowering()); |
| 76 | + return Ctx; |
| 77 | + } |
| 78 | + |
| 79 | + MachineModuleInfoWrapperPass *getMMIWP(TargetMachine &TM, |
| 80 | + MCContext &ExternMC) { |
| 81 | + return new MachineModuleInfoWrapperPass(&TM, &ExternMC); |
| 82 | + } |
| 83 | + |
| 84 | + void SetUp() override { |
| 85 | + // Module to compile. |
| 86 | + const char *FooStr = R""""( |
| 87 | + define void @foo() { |
| 88 | + call void @baz() |
| 89 | + ret void |
| 90 | + } |
| 91 | +
|
| 92 | + define void @baz() { |
| 93 | + ret void |
| 94 | + } |
| 95 | +
|
| 96 | + define void @bar() { |
| 97 | + call void @baz() |
| 98 | + ret void |
| 99 | + } |
| 100 | +
|
| 101 | + define void @boo() { |
| 102 | + ret void |
| 103 | + } |
| 104 | + )""""; |
| 105 | + StringRef AssemblyF(FooStr); |
| 106 | + |
| 107 | + TM = getTargetMachine(); |
| 108 | + |
| 109 | + if (!TM) |
| 110 | + GTEST_SKIP(); |
| 111 | + |
| 112 | + // Parse the module. |
| 113 | + Expected<bool> MResult = M.create( |
| 114 | + [&](llvm::LLVMContext &Context) -> Expected<std::unique_ptr<Module>> { |
| 115 | + SMDiagnostic SMError; |
| 116 | + std::unique_ptr<Module> M = |
| 117 | + parseAssemblyString(AssemblyF, SMError, Context); |
| 118 | + if (!M) { |
| 119 | + return make_error<StringError>("could not load LLVM file", |
| 120 | + inconvertibleErrorCode()); |
| 121 | + } |
| 122 | + return M; |
| 123 | + }); |
| 124 | + |
| 125 | + ASSERT_FALSE((!MResult)); |
| 126 | + |
| 127 | + M->setDataLayout(TM->createDataLayout()); |
| 128 | + } |
| 129 | + |
| 130 | + LLVMModuleAndContext M; |
| 131 | + std::unique_ptr<TargetMachine> TM; |
| 132 | +}; |
| 133 | + |
| 134 | +TEST_F(MCLinkerTest, SplitModuleCompilerMCLink) { |
| 135 | + |
| 136 | + SymbolAndMCInfo SMCInfo; |
| 137 | + bool Failed = false; |
| 138 | + |
| 139 | + auto OutputLambda = |
| 140 | + [&](llvm::unique_function<LLVMModuleAndContext()> ProduceModule, |
| 141 | + std::optional<int64_t> Idx, unsigned NumFunctionsBase) mutable { |
| 142 | + LLVMModuleAndContext SubModule = ProduceModule(); |
| 143 | + std::unique_ptr<TargetMachine> TM = getTargetMachine(); |
| 144 | + std::unique_ptr<MCContext> MCCtx = getMCContext(*TM); |
| 145 | + MachineModuleInfoWrapperPass *MMIWP = getMMIWP(*TM, *MCCtx); |
| 146 | + |
| 147 | + legacy::PassManager PassMgr; |
| 148 | + mclinker::addPassesToEmitMC(*TM, PassMgr, true, MMIWP, |
| 149 | + NumFunctionsBase); |
| 150 | + if (!PassMgr.run(*SubModule)) |
| 151 | + Failed = true; |
| 152 | + |
| 153 | + SMCInfo.McInfos.emplace_back(std::make_unique<MCInfo>( |
| 154 | + std::make_unique<MachineModuleInfo>(std::move(MMIWP->getMMI())), |
| 155 | + std::move(SubModule), std::move(TM), std::move(MCCtx), Idx)); |
| 156 | + }; |
| 157 | + |
| 158 | + splitPerFunction(std::move(M), OutputLambda, SMCInfo.SymbolLinkageTypes, 0); |
| 159 | + |
| 160 | + std::unique_ptr<TargetMachine> TMMCLink = getTargetMachine(); |
| 161 | + SmallVector<SymbolAndMCInfo *> SMCInfos{&SMCInfo}; |
| 162 | + llvm::StringMap<llvm::GlobalValue::LinkageTypes> SymbolLinkageTypes; |
| 163 | + |
| 164 | + MCLinker Linker(SMCInfos, *TMMCLink, SymbolLinkageTypes); |
| 165 | + |
| 166 | + Expected<std::unique_ptr<WritableMemoryBuffer>> LinkResult = |
| 167 | + Linker.linkAndPrint("SplitModuleCompilerMCLink", |
| 168 | + llvm::CodeGenFileType::AssemblyFile, true); |
| 169 | + |
| 170 | + ASSERT_FALSE((!LinkResult)); |
| 171 | + llvm::dbgs() << "Size: " << (*LinkResult)->getBufferSize() << "\n"; |
| 172 | + |
| 173 | + llvm::dbgs() << StringRef((*LinkResult)->getBufferStart()) << "\n"; |
| 174 | +} |
| 175 | + |
| 176 | +} // end anonymous namespace |
0 commit comments