Skip to content

Commit 34f8f54

Browse files
committed
Pass TargetMachine from from Clang to BitcodeWriterand
`ThinLTOBitcodeWriter` pass for thin and fat LTO respectively. Currently in the `initializeRecordStreamer` function in the ModuleSymbolTable file, MCTargetOptions are initalized again even though they get populated in BackendUtil.cpp. Because of this, during lto, `IASSearchPaths` field of MCOptions which holds all include paths gets override and thus compiler is not able to locate files included using `.include` asm directive. This patch fixes the above issue. Fixes #112920
1 parent b10d711 commit 34f8f54

File tree

15 files changed

+160
-79
lines changed

15 files changed

+160
-79
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
11581158
return;
11591159
}
11601160
MPM.addPass(ThinLTOBitcodeWriterPass(
1161-
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1161+
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr, false, TM.get()));
11621162
} else if (Action == Backend_EmitLL) {
11631163
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
11641164
/*EmitLTOSummary=*/true));
@@ -1176,7 +1176,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
11761176
}
11771177
if (Action == Backend_EmitBC) {
11781178
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
1179-
EmitLTOSummary));
1179+
EmitLTOSummary, false, TM.get()));
11801180
} else if (Action == Backend_EmitLL) {
11811181
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
11821182
EmitLTOSummary));

clang/test/CodeGen/Inputs/macros.s

Whitespace-only changes.

clang/test/CodeGen/RISCV/include.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang --target=riscv32-unknown-elf -I %S/../Inputs/ -flto %s -c -o %t.full.bc
2+
// RUN: llvm-dis %t.full.bc -o - | FileCheck %s
3+
// RUN: %clang --target=riscv32-unknown-elf -I %S/../Inputs/ -flto=thin %s -c -o %t.thin.bc
4+
// RUN: llvm-dis %t.thin.bc -o - | FileCheck %s
5+
__asm__(".include \"macros.s\"");
6+
7+
void test() {
8+
}
9+
10+
// CHECK: module asm ".include \22macros.s\22"
11+

llvm/include/llvm/Analysis/ModuleSummaryAnalysis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Function;
2727
class Module;
2828
class ProfileSummaryInfo;
2929
class StackSafetyInfo;
30+
class TargetMachine;
3031

3132
/// Direct function to compute a \c ModuleSummaryIndex from a given module.
3233
///
@@ -38,6 +39,7 @@ LLVM_ABI ModuleSummaryIndex buildModuleSummaryIndex(
3839
const Module &M,
3940
std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
4041
ProfileSummaryInfo *PSI,
42+
const TargetMachine *TM = nullptr,
4143
std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback =
4244
[](const Function &F) -> const StackSafetyInfo * { return nullptr; });
4345

llvm/include/llvm/Bitcode/BitcodeWriter.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace llvm {
2929
class BitstreamWriter;
3030
class Module;
3131
class raw_ostream;
32+
class TargetMachine;
3233

3334
class BitcodeWriter {
3435
std::unique_ptr<BitstreamWriter> Stream;
@@ -45,10 +46,12 @@ class BitcodeWriter {
4546

4647
std::vector<Module *> Mods;
4748

49+
const TargetMachine *TM;
4850
public:
4951
/// Create a BitcodeWriter that writes to Buffer.
50-
LLVM_ABI BitcodeWriter(SmallVectorImpl<char> &Buffer);
51-
LLVM_ABI BitcodeWriter(raw_ostream &FS);
52+
LLVM_ABI BitcodeWriter(SmallVectorImpl<char> &Buffer,
53+
const TargetMachine *TM = nullptr);
54+
LLVM_ABI BitcodeWriter(raw_ostream &FS, const TargetMachine *TM = nullptr);
5255

5356
LLVM_ABI ~BitcodeWriter();
5457

@@ -135,7 +138,8 @@ LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
135138
bool ShouldPreserveUseListOrder = false,
136139
const ModuleSummaryIndex *Index = nullptr,
137140
bool GenerateHash = false,
138-
ModuleHash *ModHash = nullptr);
141+
ModuleHash *ModHash = nullptr,
142+
const TargetMachine *TM = nullptr);
139143

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

151156
/// Write the specified module summary index to the given raw output stream,
152157
/// where it will be written in a new bitcode block. This is used when

llvm/include/llvm/Bitcode/BitcodeWriterPass.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Module;
2222
class ModulePass;
2323
class Pass;
2424
class raw_ostream;
25+
class TargetMachine;
2526

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

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

4952
public:
5053
/// Construct a bitcode writer pass around a particular output stream.
@@ -57,9 +60,11 @@ class BitcodeWriterPass : public PassInfoMixin<BitcodeWriterPass> {
5760
explicit BitcodeWriterPass(raw_ostream &OS,
5861
bool ShouldPreserveUseListOrder = false,
5962
bool EmitSummaryIndex = false,
60-
bool EmitModuleHash = false)
63+
bool EmitModuleHash = false,
64+
const TargetMachine *TM = nullptr)
6165
: OS(OS), ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
62-
EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {}
66+
EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash),
67+
TM(TM) {}
6368

6469
/// Run the bitcode writer pass, and output the module to the selected
6570
/// output stream.

llvm/include/llvm/Object/IRSymtab.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace llvm {
4141

4242
struct BitcodeFileContents;
4343
class StringTableBuilder;
44+
class TargetMachine;
4445

4546
namespace irsymtab {
4647

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

170171
/// This represents a symbol that has been read from a storage::Symbol and
171172
/// possibly a storage::Uncommon.

llvm/include/llvm/Object/ModuleSymbolTable.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace llvm {
3030

3131
class GlobalValue;
3232
class Module;
33+
class TargetMachine;
3334

3435
class ModuleSymbolTable {
3536
public:
@@ -45,7 +46,7 @@ class ModuleSymbolTable {
4546

4647
public:
4748
ArrayRef<Symbol> symbols() const { return SymTab; }
48-
LLVM_ABI void addModule(Module *M);
49+
LLVM_ABI void addModule(Module *M, const TargetMachine *TM = nullptr);
4950

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

6264
/// Parse inline ASM and collect the symvers directives that are defined in
6365
/// the current module.

llvm/include/llvm/Transforms/IPO/ThinLTOBitcodeWriter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@
2222
namespace llvm {
2323
class Module;
2424
class raw_ostream;
25+
class TargetMachine;
2526

2627
class ThinLTOBitcodeWriterPass
2728
: public PassInfoMixin<ThinLTOBitcodeWriterPass> {
2829
raw_ostream &OS;
2930
raw_ostream *ThinLinkOS;
3031
const bool ShouldPreserveUseListOrder;
32+
const TargetMachine *TM;
3133

3234
public:
3335
// Writes bitcode to OS. Also write thin link file to ThinLinkOS, if
3436
// it's not nullptr.
3537
ThinLTOBitcodeWriterPass(raw_ostream &OS, raw_ostream *ThinLinkOS,
36-
bool ShouldPreserveUseListOrder = false)
38+
bool ShouldPreserveUseListOrder = false,
39+
const TargetMachine* TM = nullptr)
3740
: OS(OS), ThinLinkOS(ThinLinkOS),
38-
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
41+
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder), TM(TM) {}
3942

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

llvm/lib/Analysis/ModuleSummaryAnalysis.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
934934
ModuleSummaryIndex llvm::buildModuleSummaryIndex(
935935
const Module &M,
936936
std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
937-
ProfileSummaryInfo *PSI,
937+
ProfileSummaryInfo *PSI, const TargetMachine *TM,
938938
std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
939939
assert(PSI);
940940
bool EnableSplitLTOUnit = false;
@@ -1031,7 +1031,7 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
10311031
SmallVector<ValueInfo, 0>{});
10321032
Index.addGlobalValueSummary(*GV, std::move(Summary));
10331033
}
1034-
});
1034+
}, TM);
10351035
}
10361036

10371037
bool IsThinLTO = true;
@@ -1144,8 +1144,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
11441144

11451145
AnalysisKey ModuleSummaryIndexAnalysis::Key;
11461146

1147-
ModuleSummaryIndex
1148-
ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
1147+
ModuleSummaryIndex ModuleSummaryIndexAnalysis::run(Module &M,
1148+
ModuleAnalysisManager &AM) {
11491149
ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
11501150
auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
11511151
bool NeedSSI = needsParamAccessSummary(M);
@@ -1155,7 +1155,7 @@ ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
11551155
return &FAM.getResult<BlockFrequencyAnalysis>(
11561156
*const_cast<Function *>(&F));
11571157
},
1158-
&PSI,
1158+
&PSI, nullptr,
11591159
[&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * {
11601160
return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>(
11611161
const_cast<Function &>(F))
@@ -1190,7 +1190,7 @@ bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
11901190
*const_cast<Function *>(&F))
11911191
.getBFI());
11921192
},
1193-
PSI,
1193+
PSI, nullptr,
11941194
[&](const Function &F) -> const StackSafetyInfo * {
11951195
return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>(
11961196
const_cast<Function &>(F))

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5357,13 +5357,14 @@ static void writeBitcodeHeader(BitstreamWriter &Stream) {
53575357
Stream.Emit(0xD, 4);
53585358
}
53595359

5360-
BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
5361-
: Stream(new BitstreamWriter(Buffer)) {
5360+
BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer,
5361+
const TargetMachine *TM)
5362+
: Stream(new BitstreamWriter(Buffer)), TM(TM) {
53625363
writeBitcodeHeader(*Stream);
53635364
}
53645365

5365-
BitcodeWriter::BitcodeWriter(raw_ostream &FS)
5366-
: Stream(new BitstreamWriter(FS, FlushThreshold)) {
5366+
BitcodeWriter::BitcodeWriter(raw_ostream &FS, const TargetMachine *TM)
5367+
: Stream(new BitstreamWriter(FS, FlushThreshold)), TM(TM) {
53675368
writeBitcodeHeader(*Stream);
53685369
}
53695370

@@ -5405,7 +5406,7 @@ void BitcodeWriter::writeSymtab() {
54055406
// module is malformed (e.g. it contains an invalid alias). Writing a symbol
54065407
// table is not required for correctness, but we still want to be able to
54075408
// write malformed modules to bitcode files, so swallow the error.
5408-
if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
5409+
if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc, TM)) {
54095410
consumeError(std::move(E));
54105411
return;
54115412
}
@@ -5465,7 +5466,8 @@ void BitcodeWriter::writeIndex(
54655466
void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
54665467
bool ShouldPreserveUseListOrder,
54675468
const ModuleSummaryIndex *Index,
5468-
bool GenerateHash, ModuleHash *ModHash) {
5469+
bool GenerateHash, ModuleHash *ModHash,
5470+
const TargetMachine *TM) {
54695471
auto Write = [&](BitcodeWriter &Writer) {
54705472
Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
54715473
ModHash);
@@ -5486,7 +5488,7 @@ void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
54865488
emitDarwinBCHeaderAndTrailer(Buffer, TT);
54875489
Out.write(Buffer.data(), Buffer.size());
54885490
} else {
5489-
BitcodeWriter Writer(Out);
5491+
BitcodeWriter Writer(Out, TM);
54905492
Write(Writer);
54915493
}
54925494
}
@@ -5672,11 +5674,12 @@ void BitcodeWriter::writeThinLinkBitcode(const Module &M,
56725674
// writing the per-module index file for ThinLTO.
56735675
void llvm::writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
56745676
const ModuleSummaryIndex &Index,
5675-
const ModuleHash &ModHash) {
5677+
const ModuleHash &ModHash,
5678+
const TargetMachine* TM) {
56765679
SmallVector<char, 0> Buffer;
56775680
Buffer.reserve(256 * 1024);
56785681

5679-
BitcodeWriter Writer(Buffer);
5682+
BitcodeWriter Writer(Buffer, TM);
56805683
Writer.writeThinLinkBitcode(M, Index, ModHash);
56815684
Writer.writeSymtab();
56825685
Writer.writeStrtab();

0 commit comments

Comments
 (0)