Skip to content

Commit 2ce59cc

Browse files
author
Garvit Gupta
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 Change-Id: I099a940a46c3d8403207bc5f06fede2010163c34
1 parent 5d849d3 commit 2ce59cc

File tree

18 files changed

+178
-103
lines changed

18 files changed

+178
-103
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
11551155
return;
11561156
}
11571157
MPM.addPass(ThinLTOBitcodeWriterPass(
1158-
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1158+
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr,
1159+
/* ShouldPreserveUseListOrder */ false, TM.get()));
11591160
} else if (Action == Backend_EmitLL) {
11601161
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
11611162
/*EmitLTOSummary=*/true));
@@ -1173,7 +1174,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
11731174
}
11741175
if (Action == Backend_EmitBC) {
11751176
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
1176-
EmitLTOSummary));
1177+
EmitLTOSummary, false, TM.get()));
11771178
} else if (Action == Backend_EmitLL) {
11781179
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
11791180
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: 4 additions & 2 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
///
@@ -37,7 +38,7 @@ class StackSafetyInfo;
3738
LLVM_ABI ModuleSummaryIndex buildModuleSummaryIndex(
3839
const Module &M,
3940
std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
40-
ProfileSummaryInfo *PSI,
41+
ProfileSummaryInfo *PSI, const TargetMachine *TM = nullptr,
4142
std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback =
4243
[](const Function &F) -> const StackSafetyInfo * { return nullptr; });
4344

@@ -51,7 +52,8 @@ class ModuleSummaryIndexAnalysis
5152
public:
5253
using Result = ModuleSummaryIndex;
5354

54-
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &AM);
55+
LLVM_ABI Result run(Module &M, ModuleSummaryIndexAnalysisManager &AM,
56+
const TargetMachine *TM = nullptr);
5557
};
5658

5759
/// Legacy wrapper pass to provide the ModuleSummaryIndex object.

llvm/include/llvm/Bitcode/BitcodeWriter.h

Lines changed: 10 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,13 @@ class BitcodeWriter {
4546

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

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

5357
LLVM_ABI ~BitcodeWriter();
5458

@@ -135,7 +139,8 @@ LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
135139
bool ShouldPreserveUseListOrder = false,
136140
const ModuleSummaryIndex *Index = nullptr,
137141
bool GenerateHash = false,
138-
ModuleHash *ModHash = nullptr);
142+
ModuleHash *ModHash = nullptr,
143+
const TargetMachine *TM = nullptr);
139144

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

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

llvm/include/llvm/Bitcode/BitcodeWriterPass.h

Lines changed: 10 additions & 4 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,13 +60,16 @@ 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.
66-
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
71+
LLVM_ABI PreservedAnalyses run(Module &M,
72+
ModuleSummaryIndexAnalysisManager &AM);
6773

6874
static bool isRequired() { return true; }
6975
};

llvm/include/llvm/IR/PassManager.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace llvm {
5959

6060
class Function;
6161
class Module;
62+
class TargetMachine;
6263

6364
// Forward declare the analysis manager template.
6465
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
@@ -542,6 +543,13 @@ extern template class LLVM_TEMPLATE_ABI AnalysisManager<Module>;
542543
/// Convenience typedef for the Module analysis manager.
543544
using ModuleAnalysisManager = AnalysisManager<Module>;
544545

546+
extern template class LLVM_TEMPLATE_ABI
547+
AnalysisManager<Module, const TargetMachine *>;
548+
549+
/// Separate typedef for ModuleSummaryAnalysisPass to pass TargetMachine to it.
550+
using ModuleSummaryIndexAnalysisManager =
551+
AnalysisManager<Module, const TargetMachine *>;
552+
545553
extern template class LLVM_TEMPLATE_ABI AnalysisManager<Function>;
546554

547555
/// Convenience typedef for the Function analysis manager.

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/EmbedBitcodePass.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class EmbedBitcodePass : public PassInfoMixin<EmbedBitcodePass> {
4545
EmbedBitcodePass(bool IsThinLTO, bool EmitLTOSummary)
4646
: IsThinLTO(IsThinLTO), EmitLTOSummary(EmitLTOSummary) {}
4747

48-
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
48+
LLVM_ABI PreservedAnalyses run(Module &M,
49+
ModuleSummaryIndexAnalysisManager &AM);
4950

5051
static bool isRequired() { return true; }
5152
};

0 commit comments

Comments
 (0)