Skip to content

Pass TargetMachine from from Clang to BitcodeWriterand ThinLTOBitcodeWriter pass for thin and fat LTO respectively. #143692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
return;
}
MPM.addPass(ThinLTOBitcodeWriterPass(
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr, false, TM.get()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else if (Action == Backend_EmitLL) {
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
/*EmitLTOSummary=*/true));
Expand All @@ -1176,7 +1176,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
}
if (Action == Backend_EmitBC) {
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
EmitLTOSummary));
EmitLTOSummary, false, TM.get()));
} else if (Action == Backend_EmitLL) {
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
EmitLTOSummary));
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions clang/test/CodeGen/RISCV/include.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang --target=riscv32-unknown-elf -I %S/../Inputs/ -flto %s -c -o %t.full.bc
// RUN: llvm-dis %t.full.bc -o - | FileCheck %s
// RUN: %clang --target=riscv32-unknown-elf -I %S/../Inputs/ -flto=thin %s -c -o %t.thin.bc
// RUN: llvm-dis %t.thin.bc -o - | FileCheck %s
__asm__(".include \"macros.s\"");

void test() {
}

// CHECK: module asm ".include \22macros.s\22"

3 changes: 2 additions & 1 deletion llvm/include/llvm/Analysis/ModuleSummaryAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Function;
class Module;
class ProfileSummaryInfo;
class StackSafetyInfo;
class TargetMachine;

/// Direct function to compute a \c ModuleSummaryIndex from a given module.
///
Expand All @@ -37,7 +38,7 @@ class StackSafetyInfo;
LLVM_ABI ModuleSummaryIndex buildModuleSummaryIndex(
const Module &M,
std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
ProfileSummaryInfo *PSI,
ProfileSummaryInfo *PSI, const TargetMachine *TM = nullptr,
std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback =
[](const Function &F) -> const StackSafetyInfo * { return nullptr; });

Expand Down
14 changes: 10 additions & 4 deletions llvm/include/llvm/Bitcode/BitcodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace llvm {
class BitstreamWriter;
class Module;
class raw_ostream;
class TargetMachine;

class BitcodeWriter {
std::unique_ptr<BitstreamWriter> Stream;
Expand All @@ -45,10 +46,13 @@ class BitcodeWriter {

std::vector<Module *> Mods;

const TargetMachine *TM;

public:
/// Create a BitcodeWriter that writes to Buffer.
LLVM_ABI BitcodeWriter(SmallVectorImpl<char> &Buffer);
LLVM_ABI BitcodeWriter(raw_ostream &FS);
LLVM_ABI BitcodeWriter(SmallVectorImpl<char> &Buffer,
const TargetMachine *TM = nullptr);
LLVM_ABI BitcodeWriter(raw_ostream &FS, const TargetMachine *TM = nullptr);

LLVM_ABI ~BitcodeWriter();

Expand Down Expand Up @@ -135,7 +139,8 @@ LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
bool ShouldPreserveUseListOrder = false,
const ModuleSummaryIndex *Index = nullptr,
bool GenerateHash = false,
ModuleHash *ModHash = nullptr);
ModuleHash *ModHash = nullptr,
const TargetMachine *TM = nullptr);

/// Write the specified thin link bitcode file (i.e., the minimized bitcode
/// file) to the given raw output stream, where it will be written in a new
Expand All @@ -146,7 +151,8 @@ LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
/// bitcode file writing.
LLVM_ABI void writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
const ModuleSummaryIndex &Index,
const ModuleHash &ModHash);
const ModuleHash &ModHash,
const TargetMachine *TM = nullptr);

/// Write the specified module summary index to the given raw output stream,
/// where it will be written in a new bitcode block. This is used when
Expand Down
11 changes: 8 additions & 3 deletions llvm/include/llvm/Bitcode/BitcodeWriterPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Module;
class ModulePass;
class Pass;
class raw_ostream;
class TargetMachine;

/// Create and return a pass that writes the module to the specified
/// ostream. Note that this pass is designed for use with the legacy pass
Expand All @@ -31,7 +32,8 @@ class raw_ostream;
/// reproduced when deserialized.
LLVM_ABI ModulePass *
createBitcodeWriterPass(raw_ostream &Str,
bool ShouldPreserveUseListOrder = false);
bool ShouldPreserveUseListOrder = false,
const TargetMachine *TM = nullptr);

/// Check whether a pass is a BitcodeWriterPass.
LLVM_ABI bool isBitcodeWriterPass(Pass *P);
Expand All @@ -45,6 +47,7 @@ class BitcodeWriterPass : public PassInfoMixin<BitcodeWriterPass> {
bool ShouldPreserveUseListOrder;
bool EmitSummaryIndex;
bool EmitModuleHash;
const TargetMachine *TM;

public:
/// Construct a bitcode writer pass around a particular output stream.
Expand All @@ -57,9 +60,11 @@ class BitcodeWriterPass : public PassInfoMixin<BitcodeWriterPass> {
explicit BitcodeWriterPass(raw_ostream &OS,
bool ShouldPreserveUseListOrder = false,
bool EmitSummaryIndex = false,
bool EmitModuleHash = false)
bool EmitModuleHash = false,
const TargetMachine *TM = nullptr)
: OS(OS), ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {}
EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash),
TM(TM) {}

/// Run the bitcode writer pass, and output the module to the selected
/// output stream.
Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/Object/IRSymtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace llvm {

struct BitcodeFileContents;
class StringTableBuilder;
class TargetMachine;

namespace irsymtab {

Expand Down Expand Up @@ -164,8 +165,8 @@ struct Header {
/// Fills in Symtab and StrtabBuilder with a valid symbol and string table for
/// Mods.
LLVM_ABI Error build(ArrayRef<Module *> Mods, SmallVector<char, 0> &Symtab,
StringTableBuilder &StrtabBuilder,
BumpPtrAllocator &Alloc);
StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc,
const TargetMachine *TM = nullptr);

/// This represents a symbol that has been read from a storage::Symbol and
/// possibly a storage::Uncommon.
Expand Down
6 changes: 4 additions & 2 deletions llvm/include/llvm/Object/ModuleSymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace llvm {

class GlobalValue;
class Module;
class TargetMachine;

class ModuleSymbolTable {
public:
Expand All @@ -45,7 +46,7 @@ class ModuleSymbolTable {

public:
ArrayRef<Symbol> symbols() const { return SymTab; }
LLVM_ABI void addModule(Module *M);
LLVM_ABI void addModule(Module *M, const TargetMachine *TM = nullptr);

LLVM_ABI void printSymbolName(raw_ostream &OS, Symbol S) const;
LLVM_ABI uint32_t getSymbolFlags(Symbol S) const;
Expand All @@ -57,7 +58,8 @@ class ModuleSymbolTable {
/// and the associated flags.
LLVM_ABI static void CollectAsmSymbols(
const Module &M,
function_ref<void(StringRef, object::BasicSymbolRef::Flags)> AsmSymbol);
function_ref<void(StringRef, object::BasicSymbolRef::Flags)> AsmSymbol,
const TargetMachine *TM = nullptr);

/// Parse inline ASM and collect the symvers directives that are defined in
/// the current module.
Expand Down
7 changes: 5 additions & 2 deletions llvm/include/llvm/Transforms/IPO/ThinLTOBitcodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@
namespace llvm {
class Module;
class raw_ostream;
class TargetMachine;

class ThinLTOBitcodeWriterPass
: public PassInfoMixin<ThinLTOBitcodeWriterPass> {
raw_ostream &OS;
raw_ostream *ThinLinkOS;
const bool ShouldPreserveUseListOrder;
const TargetMachine *TM;

public:
// Writes bitcode to OS. Also write thin link file to ThinLinkOS, if
// it's not nullptr.
ThinLTOBitcodeWriterPass(raw_ostream &OS, raw_ostream *ThinLinkOS,
bool ShouldPreserveUseListOrder = false)
bool ShouldPreserveUseListOrder = false,
const TargetMachine *TM = nullptr)
: OS(OS), ThinLinkOS(ThinLinkOS),
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder), TM(TM) {}

LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);

Expand Down
19 changes: 11 additions & 8 deletions llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
ModuleSummaryIndex llvm::buildModuleSummaryIndex(
const Module &M,
std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
ProfileSummaryInfo *PSI,
ProfileSummaryInfo *PSI, const TargetMachine *TM,
std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
assert(PSI);
bool EnableSplitLTOUnit = false;
Expand Down Expand Up @@ -978,7 +978,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
// be listed on the llvm.used or llvm.compiler.used global and marked as
// referenced from there.
ModuleSymbolTable::CollectAsmSymbols(
M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
M,
[&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
// Symbols not marked as Weak or Global are local definitions.
if (Flags & (object::BasicSymbolRef::SF_Weak |
object::BasicSymbolRef::SF_Global))
Expand All @@ -987,7 +988,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
GlobalValue *GV = M.getNamedValue(Name);
if (!GV)
return;
assert(GV->isDeclaration() && "Def in module asm already has definition");
assert(GV->isDeclaration() &&
"Def in module asm already has definition");
GlobalValueSummary::GVFlags GVFlags(
GlobalValue::InternalLinkage, GlobalValue::DefaultVisibility,
/* NotEligibleToImport = */ true,
Expand Down Expand Up @@ -1031,7 +1033,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
SmallVector<ValueInfo, 0>{});
Index.addGlobalValueSummary(*GV, std::move(Summary));
}
});
},
TM);
}

bool IsThinLTO = true;
Expand Down Expand Up @@ -1144,8 +1147,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(

AnalysisKey ModuleSummaryIndexAnalysis::Key;

ModuleSummaryIndex
ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
ModuleSummaryIndex ModuleSummaryIndexAnalysis::run(Module &M,
ModuleAnalysisManager &AM) {
ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
bool NeedSSI = needsParamAccessSummary(M);
Expand All @@ -1155,7 +1158,7 @@ ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
return &FAM.getResult<BlockFrequencyAnalysis>(
*const_cast<Function *>(&F));
},
&PSI,
&PSI, nullptr,
[&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * {
return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>(
const_cast<Function &>(F))
Expand Down Expand Up @@ -1190,7 +1193,7 @@ bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
*const_cast<Function *>(&F))
.getBFI());
},
PSI,
PSI, nullptr,
[&](const Function &F) -> const StackSafetyInfo * {
return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>(
const_cast<Function &>(F))
Expand Down
21 changes: 12 additions & 9 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5357,13 +5357,14 @@ static void writeBitcodeHeader(BitstreamWriter &Stream) {
Stream.Emit(0xD, 4);
}

BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
: Stream(new BitstreamWriter(Buffer)) {
BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer,
const TargetMachine *TM)
: Stream(new BitstreamWriter(Buffer)), TM(TM) {
writeBitcodeHeader(*Stream);
}

BitcodeWriter::BitcodeWriter(raw_ostream &FS)
: Stream(new BitstreamWriter(FS, FlushThreshold)) {
BitcodeWriter::BitcodeWriter(raw_ostream &FS, const TargetMachine *TM)
: Stream(new BitstreamWriter(FS, FlushThreshold)), TM(TM) {
writeBitcodeHeader(*Stream);
}

Expand Down Expand Up @@ -5405,7 +5406,7 @@ void BitcodeWriter::writeSymtab() {
// module is malformed (e.g. it contains an invalid alias). Writing a symbol
// table is not required for correctness, but we still want to be able to
// write malformed modules to bitcode files, so swallow the error.
if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc, TM)) {
consumeError(std::move(E));
return;
}
Expand Down Expand Up @@ -5465,7 +5466,8 @@ void BitcodeWriter::writeIndex(
void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
bool ShouldPreserveUseListOrder,
const ModuleSummaryIndex *Index,
bool GenerateHash, ModuleHash *ModHash) {
bool GenerateHash, ModuleHash *ModHash,
const TargetMachine *TM) {
auto Write = [&](BitcodeWriter &Writer) {
Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
ModHash);
Expand All @@ -5486,7 +5488,7 @@ void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
emitDarwinBCHeaderAndTrailer(Buffer, TT);
Out.write(Buffer.data(), Buffer.size());
} else {
BitcodeWriter Writer(Out);
BitcodeWriter Writer(Out, TM);
Write(Writer);
}
}
Expand Down Expand Up @@ -5672,11 +5674,12 @@ void BitcodeWriter::writeThinLinkBitcode(const Module &M,
// writing the per-module index file for ThinLTO.
void llvm::writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
const ModuleSummaryIndex &Index,
const ModuleHash &ModHash) {
const ModuleHash &ModHash,
const TargetMachine *TM) {
SmallVector<char, 0> Buffer;
Buffer.reserve(256 * 1024);

BitcodeWriter Writer(Buffer);
BitcodeWriter Writer(Buffer, TM);
Writer.writeThinLinkBitcode(M, Index, ModHash);
Writer.writeSymtab();
Writer.writeStrtab();
Expand Down
Loading