Skip to content

Commit 85dfe19

Browse files
committed
[ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils
D116542 adds EmbedBufferInModule which introduces a layer violation (https://llvm.org/docs/CodingStandards.html#library-layering). See 2d5f857 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
1 parent 4a780aa commit 85dfe19

File tree

6 files changed

+20
-56
lines changed

6 files changed

+20
-56
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
8585
#include "llvm/Transforms/Utils/Debugify.h"
8686
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
87+
#include "llvm/Transforms/Utils/ModuleUtils.h"
8788
#include "llvm/Transforms/Utils/NameAnonGlobals.h"
8889
#include "llvm/Transforms/Utils/SymbolRewriter.h"
8990
#include <memory>
@@ -1775,6 +1776,6 @@ void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
17751776

17761777
SmallString<128> SectionName(
17771778
{".llvm.offloading.", std::get<1>(FilenameAndSection)});
1778-
llvm::EmbedBufferInModule(*M, **ObjectOrErr, SectionName);
1779+
llvm::embedBufferInModule(*M, **ObjectOrErr, SectionName);
17791780
}
17801781
}

llvm/include/llvm/Bitcode/BitcodeWriter.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,6 @@ class raw_ostream;
165165
bool EmbedCmdline,
166166
const std::vector<uint8_t> &CmdArgs);
167167

168-
/// Embeds the memory buffer \p Buf into the module \p M as a global using the
169-
/// section name \p SectionName.
170-
void EmbedBufferInModule(Module &M, MemoryBufferRef Buf,
171-
StringRef SectionName);
172-
173168
} // end namespace llvm
174169

175170
#endif // LLVM_BITCODE_BITCODEWRITER_H

llvm/include/llvm/Transforms/Utils/ModuleUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "llvm/ADT/SmallVector.h"
1717
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/Support/MemoryBuffer.h"
1819
#include <utility> // for std::pair
1920

2021
namespace llvm {
@@ -106,6 +107,10 @@ void filterDeadComdatFunctions(
106107
/// unique identifier for this module, so we return the empty string.
107108
std::string getUniqueModuleId(Module *M);
108109

110+
/// Embed the memory buffer \p Buf into the module \p M as a global using the
111+
/// specified section name.
112+
void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName);
113+
109114
class CallInst;
110115
namespace VFABI {
111116
/// Overwrite the Vector Function ABI variants attribute with the names provide

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4973,52 +4973,3 @@ void llvm::EmbedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
49734973
llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
49744974
NewUsed->setSection("llvm.metadata");
49754975
}
4976-
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-
5010-
void llvm::EmbedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
5011-
StringRef SectionName) {
5012-
ArrayRef<char> ModuleData =
5013-
ArrayRef<char>(Buf.getBufferStart(), Buf.getBufferSize());
5014-
5015-
// Embed the buffer into the module.
5016-
llvm::Constant *ModuleConstant =
5017-
llvm::ConstantDataArray::get(M.getContext(), ModuleData);
5018-
llvm::GlobalVariable *GV = new llvm::GlobalVariable(
5019-
M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
5020-
ModuleConstant, "llvm.embedded.object");
5021-
GV->setSection(SectionName);
5022-
5023-
appendToCompilerUsed(M, GV);
5024-
}

llvm/lib/Transforms/Utils/ModuleUtils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,16 @@ void VFABI::setVectorVariantNames(
264264
CI->addFnAttr(
265265
Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
266266
}
267+
268+
void llvm::embedBufferInModule(Module &M, MemoryBufferRef Buf,
269+
StringRef SectionName) {
270+
// Embed the buffer into the module.
271+
Constant *ModuleConstant = ConstantDataArray::get(
272+
M.getContext(), makeArrayRef(Buf.getBufferStart(), Buf.getBufferSize()));
273+
GlobalVariable *GV = new GlobalVariable(
274+
M, ModuleConstant->getType(), true, GlobalValue::PrivateLinkage,
275+
ModuleConstant, "llvm.embedded.object");
276+
GV->setSection(SectionName);
277+
278+
appendToCompilerUsed(M, GV);
279+
}

utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,6 @@ cc_library(
904904
"include/llvm/Bitcode/BitcodeWriter.h",
905905
"include/llvm/Bitcode/BitcodeWriterPass.h",
906906
"include/llvm/Bitcode/LLVMBitCodes.h",
907-
"include/llvm/Transforms/Utils/ModuleUtils.h",
908907
],
909908
copts = llvm_copts,
910909
deps = [

0 commit comments

Comments
 (0)