From 4a780aa13ee5e1c8268de54ef946200a270127b9 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Mon, 31 Jan 2022 18:58:35 -0500 Subject: [PATCH] [LLVM] Resolve layer violation in BitcodeWriter Summary: The changes introduced in D116542 added a dependency on TransformUtils to use the `appendToCompilerUsed` method. This created a circular dependency. This patch simply copies the needed function locally to remove the dependency. --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 34 ++++++++++++++++++++++++++++++- llvm/lib/Bitcode/Writer/CMakeLists.txt | 1 - 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index fafccb4..7ebe10e 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -69,7 +69,6 @@ #include "llvm/Support/MathExtras.h" #include "llvm/Support/SHA1.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Transforms/Utils/ModuleUtils.h" #include #include #include @@ -4975,6 +4974,39 @@ void llvm::EmbedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, 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 = diff --git a/llvm/lib/Bitcode/Writer/CMakeLists.txt b/llvm/lib/Bitcode/Writer/CMakeLists.txt index dcdb73b..36808c8 100644 --- a/llvm/lib/Bitcode/Writer/CMakeLists.txt +++ b/llvm/lib/Bitcode/Writer/CMakeLists.txt @@ -11,7 +11,6 @@ add_llvm_component_library(LLVMBitWriter Analysis Core MC - TransformUtils Object Support ) -- 2.7.4