From 85dfe19b36ba6e9657612e072c9183ce168fdbbc Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 31 Jan 2022 16:33:56 -0800 Subject: [ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils D116542 adds EmbedBufferInModule which introduces a layer violation (https://llvm.org/docs/CodingStandards.html#library-layering). See 2d5f857a1eaf5f7a806d12953c79b96ed8952da8 for detail. EmbedBufferInModule does not use BitcodeWriter functionality and should be moved LLVMTransformsUtils. While here, change the function case to the prevailing convention. It seems that EmbedBufferInModule just follows the steps of EmbedBitcodeInModule. EmbedBitcodeInModule calls WriteBitcodeToFile but has IR update operations which ideally should be refactored to another library. Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D118666 --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 49 ------------------------------- 1 file changed, 49 deletions(-) (limited to 'llvm/lib/Bitcode/Writer/BitcodeWriter.cpp') diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 7ebe10e..eb4e09e 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -4973,52 +4973,3 @@ void llvm::EmbedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); NewUsed->setSection("llvm.metadata"); } - -static void appendToCompilerUsed(Module &M, ArrayRef Values) { - GlobalVariable *GV = M.getGlobalVariable("llvm.compiler.used"); - SmallPtrSet InitAsSet; - SmallVector Init; - if (GV) { - if (GV->hasInitializer()) { - auto *CA = cast(GV->getInitializer()); - for (auto &Op : CA->operands()) { - Constant *C = cast_or_null(Op); - if (InitAsSet.insert(C).second) - Init.push_back(C); - } - } - GV->eraseFromParent(); - } - - Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext()); - for (auto *V : Values) { - Constant *C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, Int8PtrTy); - if (InitAsSet.insert(C).second) - Init.push_back(C); - } - - if (Init.empty()) - return; - - ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size()); - GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage, - ConstantArray::get(ATy, Init), - "llvm.compiler.used"); - GV->setSection("llvm.metadata"); -} - -void llvm::EmbedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, - StringRef SectionName) { - ArrayRef ModuleData = - ArrayRef(Buf.getBufferStart(), Buf.getBufferSize()); - - // Embed the buffer into the module. - llvm::Constant *ModuleConstant = - llvm::ConstantDataArray::get(M.getContext(), ModuleData); - llvm::GlobalVariable *GV = new llvm::GlobalVariable( - M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage, - ModuleConstant, "llvm.embedded.object"); - GV->setSection(SectionName); - - appendToCompilerUsed(M, GV); -} -- cgit v1.1