Skip to content

Commit 4a780aa

Browse files
committed
[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.
1 parent 7a836ba commit 4a780aa

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#include "llvm/Support/MathExtras.h"
7070
#include "llvm/Support/SHA1.h"
7171
#include "llvm/Support/raw_ostream.h"
72-
#include "llvm/Transforms/Utils/ModuleUtils.h"
7372
#include <algorithm>
7473
#include <cassert>
7574
#include <cstddef>
@@ -4975,6 +4974,39 @@ void llvm::EmbedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
49754974
NewUsed->setSection("llvm.metadata");
49764975
}
49774976

4977+
static void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
4978+
GlobalVariable *GV = M.getGlobalVariable("llvm.compiler.used");
4979+
SmallPtrSet<Constant *, 16> InitAsSet;
4980+
SmallVector<Constant *, 16> Init;
4981+
if (GV) {
4982+
if (GV->hasInitializer()) {
4983+
auto *CA = cast<ConstantArray>(GV->getInitializer());
4984+
for (auto &Op : CA->operands()) {
4985+
Constant *C = cast_or_null<Constant>(Op);
4986+
if (InitAsSet.insert(C).second)
4987+
Init.push_back(C);
4988+
}
4989+
}
4990+
GV->eraseFromParent();
4991+
}
4992+
4993+
Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
4994+
for (auto *V : Values) {
4995+
Constant *C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, Int8PtrTy);
4996+
if (InitAsSet.insert(C).second)
4997+
Init.push_back(C);
4998+
}
4999+
5000+
if (Init.empty())
5001+
return;
5002+
5003+
ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
5004+
GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
5005+
ConstantArray::get(ATy, Init),
5006+
"llvm.compiler.used");
5007+
GV->setSection("llvm.metadata");
5008+
}
5009+
49785010
void llvm::EmbedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
49795011
StringRef SectionName) {
49805012
ArrayRef<char> ModuleData =

llvm/lib/Bitcode/Writer/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ add_llvm_component_library(LLVMBitWriter
1111
Analysis
1212
Core
1313
MC
14-
TransformUtils
1514
Object
1615
Support
1716
)

0 commit comments

Comments
 (0)